boomer77beth collateral nett
    Updated 2022-01-31
    with prov as (select date_trunc('day', block_timestamp) as dt, currency, case
    when currency = 'terra1dzhzukyezv0etz22ud940z7adyv7xgcjkahuun' then 'bETH'
    when currency = 'terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp' then 'bLUNA'
    else null end as bAssets,
    sum(amount) as basset_amount,
    sum(amount_usd) as usd_amount,
    count(distinct tx_id) as tx_count,
    count(distinct sender) as sender_count
    from anchor.collateral
    where event_type = 'provide' and block_timestamp >= CURRENT_DATE - 90 and currency = 'terra1dzhzukyezv0etz22ud940z7adyv7xgcjkahuun'
    group by 1,2),

    wd as (select date_trunc('day', block_timestamp) as dt, currency, case
    when currency = 'terra1dzhzukyezv0etz22ud940z7adyv7xgcjkahuun' then 'bETH'
    when currency = 'terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp' then 'bLUNA'
    else null end as bAssets,
    sum(amount) as basset_amount,
    sum(amount_usd) as usd_amount,
    count(distinct tx_id) as tx_count,
    count(distinct sender) as sender_count
    from anchor.collateral
    where event_type = 'withdraw' and block_timestamp >= CURRENT_DATE - 90 and currency = 'terra1dzhzukyezv0etz22ud940z7adyv7xgcjkahuun'
    group by 1,2)

    select a.dt, a.bassets, a.usd_amount as provide, (b.usd_amount * -1) as withdraw, (provide + withdraw) as net_change
    from prov a
    left outer join wd b on a.dt = b.dt and a.bassets = b.bassets
    Run a query to Download Data