nitsAnchor Post Liquidation 1
    Updated 2022-04-18
    with l as (SELECT BLOCK_TIMESTAMP, borrower::string as addr, 'liquidated' AS ACTION from anchor.liquidations ),
    swaps as (SELECT BLOCK_TIMESTAMP, msg_value:sender::string as addr, 'swap' AS ACTION from terra.msgs
    where msg_value:contract = 'terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp' and contains(msg_value:execute_msg:send:msg,'swap')),
    d as (SELECT BLOCK_TIMESTAMP, msg_value:sender::string as addr, 'deposit' AS ACTION from terra.msgs
    where msg_value:contract = 'terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp' and contains(msg_value:execute_msg:send:msg, 'deposit')),
    details as


    (SELECT *, row_number() over (partition by addr order by block_timestamp) as rn from
    (SELECT * from swaps k
    UNION ALL
    SELECT * from d
    UNION ALL
    SELECT * from l ) ),
    details1 as
    (SELECT block_timestamp as bt, addr as address,rn as rn1, action as act from details
    where action = 'liquidated')

    SELECT distribution, count(*) as total_incidents from
    ( SELECT *, case when action is NULL then 'hold' else action end as distribution from
    (SELECT * from details
    right join details1
    on bt< block_timestamp and addr= address and rn = rn1+1 )
    where action != 'liquidated')
    gr
    LIMIT 100
    Run a query to Download Data