WITH borrowers AS (
SELECT DISTINCT DECODED_LOG:borrower AS address
FROM ethereum.core.ez_decoded_event_logs
WHERE CONTRACT_ADDRESS = LOWER('0xd20508E1E971b80EE172c73517905bfFfcBD87f9')
AND EVENT_NAME = 'BorrowerCreated'
)
SELECT
DATE_TRUNC('month', tt.BLOCK_TIMESTAMP) AS month,
SUM(tt.AMOUNT) AS total_transferred
FROM ethereum.core.ez_token_transfers tt
JOIN borrowers b
ON tt.FROM_ADDRESS = b.address
OR tt.TO_ADDRESS = b.address
WHERE tt.CONTRACT_ADDRESS = LOWER('0xd20508E1E971b80EE172c73517905bfFfcBD87f9')
GROUP BY month
ORDER BY month;