KARTODBend Total Debt and Collateral Value
    Updated 2023-04-13
    with collateral as (
    select
    date_trunc('day', BLOCK_TIMESTAMP) as day,
    sum(RAW_AMOUNT/1e18) as amt
    from ethereum.core.fact_token_transfers
    where TO_ADDRESS = lower('0xeD1840223484483C0cb050E6fC344d1eBF0778a9')
    and CONTRACT_ADDRESS = lower('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2')
    group by 1
    ),

    borrow as (
    select
    date_trunc('day', BLOCK_TIMESTAMP) as day,
    sum(RAW_AMOUNT/1e18) as amt
    from ethereum.core.fact_token_transfers
    where FROM_ADDRESS = lower('0xeD1840223484483C0cb050E6fC344d1eBF0778a9')
    and CONTRACT_ADDRESS = lower('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2')
    group by 1
    )


    SELECT
    c.day,
    c.amt AS "ETH Deposit",
    b.amt AS "ETH Borrow",
    SUM(c.amt) OVER (ORDER BY c.day) AS "Total Bend Collateral Value",
    SUM(b.amt) OVER (ORDER BY b.day) AS "Bend Total Debt"
    FROM collateral c
    LEFT JOIN borrow b ON c.day = b.day
    ORDER BY c.day DESC
    Run a query to Download Data