binhachonFinancial Imagineering: Anchor Protocol - Deposits from customers - Earn
    Updated 2022-01-28
    with Anchor_earn_deposit as (
    select
    block_timestamp,
    event_attributes:to::string as sender,
    event_attributes:amount::float/1e6 as amount,
    event_attributes:deposit_amount::float/1e6 as UST_amount
    from terra.msg_events
    where tx_status = 'SUCCEEDED'
    and event_type = 'from_contract'
    and event_attributes:"0_action"::string = 'deposit_stable'
    and event_attributes:"0_contract_address"::string = 'terra1sepfj7s0aeg5967uxnfk4thzlerrsktkpelm5s'
    ),
    Anchor_earn_withdraw as (
    select
    block_timestamp,
    event_attributes:"0_from"::string as sender,
    event_attributes:"0_amount"::float/1e6 as amount,
    event_attributes:redeem_amount::float/1e6 as UST_amount
    from
    terra.msg_events
    where event_type = 'from_contract'
    and tx_status = 'SUCCEEDED'
    and event_attributes:"1_action"::string = 'redeem_stable'
    and event_attributes:to::string = 'terra1sepfj7s0aeg5967uxnfk4thzlerrsktkpelm5s'
    ),
    Anchor_earn as (
    select block_timestamp, sender as address, amount, UST_amount from Anchor_earn_deposit
    union all
    select block_timestamp, sender as address, -amount as amount, UST_amount from Anchor_earn_withdraw
    ),
    Anchor_earn_quarter as (
    select
    date_trunc('quarter', block_timestamp) as blocktime,
    sum(amount) as amount
    from Anchor_earn
    group by blocktime