Skip to content

Commit

Permalink
Merge pull request #4 from mtitus6/main
Browse files Browse the repository at this point in the history
Added price queries, added trades_peg_ratio, flipped price_peg_ratio over to price
  • Loading branch information
mtitus6 committed May 12, 2024
2 parents 691d41c + 1a33a61 commit c2f3288
Show file tree
Hide file tree
Showing 18 changed files with 772 additions and 72 deletions.
39 changes: 39 additions & 0 deletions coinbase/cbeth_price.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Dune query number - 3661946 */
with weth as (
select
date_trunc('hour', minute) as hr,
avg(price) as price
from
prices.usd
where
contract_address = 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
and minute >= cast(current_timestamp - interval '1' year as timestamp)
group by
date_trunc('hour', minute)
)
,
token as (
select
date_trunc('hour', minute) as hr,
contract_address,
avg(price) as price
from
prices.usd
where
contract_address = 0xbe9895146f7af43049ca1c1ae358b0541ea49704
and minute >= cast(current_timestamp - interval '1' year as timestamp)
group by
date_trunc('hour', minute),
contract_address
)

select
token.hr,
token.contract_address as token_contract_address,
'cbETH' as token_name,
token.price as price_usd,
token.price / weth.price as price_eth
from
token
inner join weth on
token.hr = weth.hr
23 changes: 3 additions & 20 deletions coinbase/cbeth_price_peg_ratio.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Dune query number - 3465286 */
/* Dune query number - 3664524 */
with hours as (
select
timestamp as hr,
Expand Down Expand Up @@ -28,32 +28,17 @@ peg as (
and hours.hr < peg.next_hr
),

trades as (
select
date_trunc('hour', trades.t) as hr,
sum(trades.token_trade_amount_eth) as token_trade_amount_eth,
sum(trades.token_trade_amount) as token_trade_amount,
sum(trades.token_trade_amount_eth) / sum(trades.token_trade_amount) as token_price_eth
from query_3465242 as trades
where trades.t > cast(current_timestamp as timestamp) - interval '1' year
group by 1
),

prices as (
select
hours.hr,
prices.token_price_eth,
prices.token_trade_amount_eth,
prices.token_trade_amount
prices.token_price_eth
from hours
left join
(select
hr,
token_price_eth,
token_trade_amount_eth,
token_trade_amount,
lead(hr) over (order by hr) as next_hr
from trades) as prices on
from query_3661946) as prices on
hours.hr >= prices.hr
and hours.hr < prices.next_hr
)
Expand All @@ -65,8 +50,6 @@ select
avg(prices.token_price_eth)
over (order by hours.hr rows between 5 preceding and current row)
as token_price_eth_6hr_ma,
prices.token_trade_amount_eth,
prices.token_trade_amount,
peg.token_peg_eth,
prices.token_price_eth / peg.token_peg_eth as token_price_peg_ratio,
avg(prices.token_price_eth) over (order by hours.hr rows between 5 preceding and current row)
Expand Down
79 changes: 79 additions & 0 deletions coinbase/cbeth_trades_peg_ratio.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Dune query number - 3465286 */
with hours as (
select
timestamp as hr,
date_trunc('day', timestamp) as d
from
unnest(
sequence(
date_trunc('hour', cast(current_timestamp as timestamp) - interval '1' year),
cast(current_timestamp as timestamp),
interval '60' minute
)
) as tbl (timestamp)
),

peg as (
select
hours.hr,
peg.token_peg_eth
from hours
left join
(select
date_trunc('hour', t) as hr,
token_peg_eth,
lead(date_trunc('hour', t)) over (order by date_trunc('hour', t)) as next_hr
from query_3465256) as peg on
hours.hr >= peg.hr
and hours.hr < peg.next_hr
),

trades as (
select
date_trunc('hour', trades.t) as hr,
sum(trades.token_trade_amount_eth) as token_trade_amount_eth,
sum(trades.token_trade_amount) as token_trade_amount,
sum(trades.token_trade_amount_eth) / sum(trades.token_trade_amount) as token_price_eth
from query_3465242 as trades
where trades.t > cast(current_timestamp as timestamp) - interval '1' year
group by 1
),

prices as (
select
hours.hr,
prices.token_price_eth,
prices.token_trade_amount_eth,
prices.token_trade_amount
from hours
left join
(select
hr,
token_price_eth,
token_trade_amount_eth,
token_trade_amount,
lead(hr) over (order by hr) as next_hr
from trades) as prices on
hours.hr >= prices.hr
and hours.hr < prices.next_hr
)

select
hours.hr,
'cbETH' as token_name,
prices.token_price_eth,
avg(prices.token_price_eth)
over (order by hours.hr rows between 5 preceding and current row)
as token_price_eth_6hr_ma,
prices.token_trade_amount_eth,
prices.token_trade_amount,
peg.token_peg_eth,
prices.token_price_eth / peg.token_peg_eth as token_price_peg_ratio,
avg(prices.token_price_eth) over (order by hours.hr rows between 5 preceding and current row)
/ peg.token_peg_eth as token_price_peg_ratio_6hr_ma,
(prices.token_price_eth / peg.token_peg_eth) - 1 as token_peg_pct_divergence,
avg(prices.token_price_eth) over (order by hours.hr rows between 5 preceding and current row) / peg.token_peg_eth
- 1 as token_peg_pct_divergence_6hr_ma
from hours
left join peg on hours.hr = peg.hr
left join prices on hours.hr = prices.hr
23 changes: 23 additions & 0 deletions lido/steth/steth_peg_no_rebase.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Dune query number - 3621788 */
select
cast(evt_block_number as bigint) as block,
evt_block_time as t,
date_trunc('day', evt_block_time) as d,
posttotalether / cast(posttotalshares as double) as token_peg_eth,
0xae7ab96520de3a18e5e111b5eaab095312d7fe84 as token_contract_address,
'stETH' as token_name
from
lido_ethereum.steth_evt_tokenrebased
union all
select
cast(evt_block_number as bigint) as block,
evt_block_time as t,
date_trunc('day', evt_block_time) as d,
posttotalpooledether / cast(totalshares as double) as token_peg_eth,
0xae7ab96520de3a18e5e111b5eaab095312d7fe84 as token_contract_address,
'stETH' as token_name
from
lido_ethereum.legacyoracle_evt_posttotalshares
where
evt_block_time >= cast('2022-09-01 00:00' as timestamp)
and evt_block_time <= cast('2023-05-16 00:00' as timestamp)
40 changes: 40 additions & 0 deletions lido/steth/steth_price.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Dune query number - 3664583 */
with weth as (
select
date_trunc('hour', minute) as hr,
avg(price) as price
from
prices.usd
where
contract_address = 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
and minute >= cast(current_timestamp - interval '1' year as timestamp)
group by
date_trunc('hour', minute)
)
,
token as (
select
date_trunc('hour', minute) as hr,
contract_address,
avg(price) as price
from
prices.usd
where
contract_address = 0xae7ab96520de3a18e5e111b5eaab095312d7fe84
and minute >= cast(current_timestamp - interval '1' year as timestamp)
group by
date_trunc('hour', minute),
contract_address
)

select
token.hr,
token.contract_address as token_contract_address,
'stETH' as token_name,
token.price as token_price_usd,
weth.price as weth_price_usd,
token.price / weth.price as token_price_eth
from
token
inner join weth on
token.hr = weth.hr
26 changes: 4 additions & 22 deletions lido/steth/steth_price_peg_ratio.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Dune query number - 3480205 */
/* Dune query number - 3668358 */
with hours as (
select
timestamp as hr,
Expand All @@ -16,37 +16,21 @@ with hours as (
peg as (
select
hours.hr,
peg.token_peg_eth
1 as token_peg_eth
from hours
cross join (select distinct token_peg_eth from query_3480196) as peg
),

trades as (
select
date_trunc('hour', trades.t) as hr,
sum(trades.token_trade_amount_eth) as token_trade_amount_eth,
sum(trades.token_trade_amount) as token_trade_amount,
sum(trades.token_trade_amount_eth) / sum(trades.token_trade_amount) as token_price_eth
from query_3480173 as trades
where trades.t > cast(current_timestamp as timestamp) - interval '1' year
group by 1
),

prices as (
select
hours.hr,
prices.token_price_eth,
prices.token_trade_amount_eth,
prices.token_trade_amount
prices.token_price_eth
from hours
left join
(select
hr,
token_price_eth,
token_trade_amount_eth,
token_trade_amount,
lead(hr) over (order by hr) as next_hr
from trades) as prices on
from query_3664583) as prices on
hours.hr >= prices.hr
and hours.hr < prices.next_hr
)
Expand All @@ -58,8 +42,6 @@ select
avg(prices.token_price_eth)
over (order by hours.hr rows between 5 preceding and current row)
as token_price_eth_6hr_ma,
prices.token_trade_amount_eth,
prices.token_trade_amount,
peg.token_peg_eth,
prices.token_price_eth / peg.token_peg_eth as token_price_peg_ratio,
avg(prices.token_price_eth) over (order by hours.hr rows between 5 preceding and current row)
Expand Down
72 changes: 72 additions & 0 deletions lido/steth/steth_trades_peg_ratio.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Dune query number - 3480205 */
with hours as (
select
timestamp as hr,
date_trunc('day', timestamp) as d
from
unnest(
sequence(
date_trunc('hour', cast(current_timestamp as timestamp) - interval '1' year),
cast(current_timestamp as timestamp),
interval '60' minute
)
) as tbl (timestamp)
),

peg as (
select
hours.hr,
peg.token_peg_eth
from hours
cross join (select distinct token_peg_eth from query_3480196) as peg
),

trades as (
select
date_trunc('hour', trades.t) as hr,
sum(trades.token_trade_amount_eth) as token_trade_amount_eth,
sum(trades.token_trade_amount) as token_trade_amount,
sum(trades.token_trade_amount_eth) / sum(trades.token_trade_amount) as token_price_eth
from query_3480173 as trades
where trades.t > cast(current_timestamp as timestamp) - interval '1' year
group by 1
),

prices as (
select
hours.hr,
prices.token_price_eth,
prices.token_trade_amount_eth,
prices.token_trade_amount
from hours
left join
(select
hr,
token_price_eth,
token_trade_amount_eth,
token_trade_amount,
lead(hr) over (order by hr) as next_hr
from trades) as prices on
hours.hr >= prices.hr
and hours.hr < prices.next_hr
)

select
hours.hr,
'stETH' as token_name,
prices.token_price_eth,
avg(prices.token_price_eth)
over (order by hours.hr rows between 5 preceding and current row)
as token_price_eth_6hr_ma,
prices.token_trade_amount_eth,
prices.token_trade_amount,
peg.token_peg_eth,
prices.token_price_eth / peg.token_peg_eth as token_price_peg_ratio,
avg(prices.token_price_eth) over (order by hours.hr rows between 5 preceding and current row)
/ peg.token_peg_eth as token_price_peg_ratio_6hr_ma,
(prices.token_price_eth / peg.token_peg_eth) - 1 as token_peg_pct_divergence,
avg(prices.token_price_eth) over (order by hours.hr rows between 5 preceding and current row) / peg.token_peg_eth
- 1 as token_peg_pct_divergence_6hr_ma
from hours
left join peg on hours.hr = peg.hr
left join prices on hours.hr = prices.hr
Loading

0 comments on commit c2f3288

Please sign in to comment.