KuramaCosmos - proposal 84
    Updated 2022-12-11
    -- all delegation txs
    with table_0 as (
    select distinct tx_id from cosmos.core.fact_msg_attributes
    where tx_succeeded = 'TRUE'
    and msg_type = 'message'
    and attribute_key = 'action'
    and attribute_value = '/cosmos.staking.v1beta1.MsgDelegate'
    ),
    -- all delegation txs + address
    table_1 as (select distinct tx_id, attribute_value as address from cosmos.core.fact_msg_attributes
    where tx_succeeded = 'TRUE'
    and msg_type = 'transfer'
    and attribute_key = 'sender'
    and tx_id in (select * from table_0)),
    -- all delegation txs + validator
    table_2 as (select distinct tx_id, attribute_value as validator from cosmos.core.fact_msg_attributes
    where tx_succeeded = 'TRUE'
    and msg_type = 'delegate'
    and attribute_key = 'validator'
    and tx_id in (select * from table_0)),

    -- all delegation txs + amount
    table_3 as (select distinct tx_id, attribute_value as shares from cosmos.core.fact_msg_attributes
    where tx_succeeded = 'TRUE'
    and msg_type = 'delegate'
    and attribute_key = 'new_shares'
    and tx_id in (select * from table_0)),

    all_delegations as (select address, b.validator, (sum(c.shares)/pow(10,6)) as amount from table_1 a
    left join table_2 b
    on a.tx_id = b.tx_id
    left join table_3 c
    on a.tx_id = c.tx_id
    group by address, b.validator),

    -- same as before but for undelegations
    Run a query to Download Data