keshanUntitled Query
    Updated 2022-06-07
    with hourly_price as (select date_trunc('hour', block_timestamp) as hour,
    (case when to_currency='Osmosis' then from_currency when from_currency='Osmosis' then to_currency else null end) as token,
    avg(price) as price
    from (select block_timestamp,
    (case when from_currency='uosmo' then (from_amount/pow(10, from_decimal)) / (to_amount/pow(10, to_decimal)) when to_currency='uosmo' then (to_amount/pow(10, to_decimal)) / (from_amount/pow(10, from_decimal)) else null end) as price,
    (case when t.label is null then to_currency else t.label end) as to_currency,
    (case when f.label is null then from_currency else f.label end) as from_currency
    from osmosis.core.fact_swaps
    left join osmosis.core.dim_labels t on t.address=to_currency
    left join osmosis.core.dim_labels f on f.address=from_currency)
    where price is not null and token != 'Osmosis'
    group by hour, token)

    select week, label, sum(txs) as txs, sum(amount) as amounts
    from (select date_trunc('week', block_timestamp) as week,
    count(tx_id) as txs,
    sum(case when l.currency='uosmo' then amount/1e6 else price * amount/pow(10, decimal) end) as tokens,
    sum(case when raw_metadata[0]:exponent is null
    then regexp_substr(attribute_value, '^[0-9]+')/pow(10, 18) --1e18 ??? 6E4850CD91A26D0AC59E0D32B887A2BA737FA4568724E34EB89F84F8F817AD08 but 7CC98BE02C5A711C28FA1405ECE6E68B1D1495DF70A835D45BDBCB8C78E90733 1e6
    end) as amount,
    case when label is null then regexp_replace(attribute_value, regexp_substr(attribute_value, '^[0-9]+')) else label end as label
    from osmosis.core.fact_msg_attributes
    left join osmosis.core.dim_labels on split(attribute_value, regexp_substr(attribute_value, '^[0-9]+'))[1] = address
    left join osmosis.core.fact_liquidity_provider_actions l using(tx_id)
    left join hourly_price on hour=date_trunc('hour', block_timestamp) and token=l.currency
    where msg_type='burn' and attribute_key='amount'
    group by week, label, attribute_value)
    where startswith(label, 'gamm')
    group by week, label