John_GaltWhale LBP Buy Vol, Sell Vol, Price
    Updated 2022-04-13
    with table1 as (select
    date_trunc('hour', block_timestamp) as date,
    event_attributes:ask_asset as ask,
    event_attributes:offer_amount / pow(10, 6) as offer,
    event_attributes:return_amount / pow(10, 6) as returnn,
    case
    when ask = 'uusd' then returnn
    else 0
    end as ust_from_sold_whale,
    case
    when ask = 'terra1php5m8a6qd68z02t3zpw4jv2pj4vgw4wz0t8mz' then offer
    else 0
    end as ust_to_buy_whale,
    case
    when ask = 'terra1php5m8a6qd68z02t3zpw4jv2pj4vgw4wz0t8mz' then returnn
    else 0
    end as bought_whale,
    ust_to_buy_whale / nullif(bought_whale, 0) as whale_price
    from terra.msg_events
    where date(block_timestamp) between '2021-12-01' and '2021-12-30'
    and (event_attributes:ask_asset = 'terra1php5m8a6qd68z02t3zpw4jv2pj4vgw4wz0t8mz' or event_attributes:ask_asset = 'uusd')
    and (event_attributes:"0_contract_address" = 'terra1sz988qp6vma3j0xj5w7fsskqcuc8kjn0mmtcqc'
    or event_attributes:"1_contract_address" = 'terra1sz988qp6vma3j0xj5w7fsskqcuc8kjn0mmtcqc')
    and event_index = 3
    ),

    table2 as (select date, sum(ust_from_sold_whale) as sell, sum(ust_to_buy_whale) as buy, avg(whale_price) as whale_price
    from table1
    group by date
    order by date
    )

    select * from table2