John_Galtsuper cool query
Updated 2022-03-28
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
›
⌄
WITH prices as (
select
date_trunc('hour', block_timestamp) as date,
currency,
avg(price_usd) as price
from terra.oracle_prices
group by 1, 2
),
rewards as (
select
date_trunc('hour', t.block_timestamp) as date,
fl.value:denom::string as currency,
SUM(fl.value:amount::float / POW (10, 6)) as total
from terra.transitions t,
lateral flatten(input => event_attributes:amount) fl
where t.event = 'rewards'
group by 1, 2
),
commissions as (
select
date_trunc('hour', t.block_timestamp) as date,
fl.value:denom::string as currency,
SUM(fl.value:amount::float / POW (10, 6)) as event_amount
from terra.transitions t,
lateral flatten(input => event_attributes:amount) fl
where t.event = 'commission'
group by 1, 2
),
daily_rewards as (
select date_trunc('day', r.date) as date,
sum(total * price) as total_rewards
from rewards r
left outer join prices p
on r.date = p.date
and r.currency = p.currency
group by 1