John_GaltUST - done
    Updated 2022-07-07
    with ltable as (select ---asset deposits and withdrawals
    date_trunc('hour', block_timestamp) as day_hour,
    sum(event_attributes:"0_amount") / pow(10, 6) as deposit_amount,
    null as withdraw_amount
    from terra.msg_events
    where date(block_timestamp) > '2022-02-01'
    and event_attributes:"0_contract_address" = 'terra1pcxwtrxppj9xj7pq3k95wm2zztfr9kwfkcgq0w' ---genesis pool
    and event_attributes:"1_contract_address" = 'terra1wrqm3jzyustdme0vz4t2n8jefz9y6wkl3g39gg' ----eust
    and event_attributes:"0_action" = 'deposit'
    and event_index = 3
    group by day_hour

    union all
    select
    date_trunc('hour', block_timestamp) as day_hour, ----this is messed up. these are withdraws when people are borrowing, not taking off the platform
    null as deposit_amount,
    sum(event_attributes:"0_amount") / pow(10, 6) as withdraw_amount
    from terra.msg_events
    where date(block_timestamp) > '2022-02-01'
    and event_attributes:"0_contract_address" = 'terra1pcxwtrxppj9xj7pq3k95wm2zztfr9kwfkcgq0w' ---genesis pool
    and event_attributes:etoken = 'terra1wrqm3jzyustdme0vz4t2n8jefz9y6wkl3g39gg' ----eust contract
    and event_attributes:"0_action" = 'withdraw'
    and event_index = 3
    group by day_hour
    ),

    dtable as (select distinct ---a complete set of hours as an anchor, as Edge events do not take place every hour
    date_trunc('hour', block_timestamp) as day_hour
    from terra.msg_events
    where day_hour > '2022-02-16 10:00:00.000'
    ),

    lend as (select dtable.day_hour as day_hour, sum(ltable.deposit_amount) as deposit, sum(ltable.withdraw_amount) as withdraw,
    coalesce(deposit, 0) - coalesce(withdraw, 0) as net_hourly_deposit,
    sum(net_hourly_deposit) over (order by dtable.day_hour) as total_hourly_lent
    Run a query to Download Data