Skip to content

Commit

Permalink
add multiple price feeds option
Browse files Browse the repository at this point in the history
  • Loading branch information
harisang committed Nov 13, 2024
1 parent 9621aff commit 5bd104f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ select --noqa: ST06
concat(
'<a href="https://dune.com/queries/4059683',
'?blockchain=ethereum',
'&price_feed=dune_price_feed'
'&start_time={{start_time}}',
'&end_time={{end_time}}',
'&slippage_table_name=raw_slippage_breakdown',
Expand Down
1 change: 1 addition & 0 deletions cowprotocol/accounting/slippage/.sqlfluff
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ end_time='2024-08-02 12:00'
blockchain='ethereum'
slippage_table_name=slippage_per_solver,slippage_per_transaction
raw_slippage_table_name=raw_slippage_breakdown,raw_slippage_per_transaction
price_feed=dune_price_feed,multiple_price_feeds
2 changes: 1 addition & 1 deletion cowprotocol/accounting/slippage/raw_slippage_4059683.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fees as (
-amount as amount,
fee_type as slippage_type,
date_trunc('hour', block_time) as hour --noqa: RF04
from "query_4058574(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')"
from "query_4058574(blockchain='{{blockchain}}',price_feed='{{price_feed}}',start_time='{{start_time}}',end_time='{{end_time}}')"
),

imbalances as (
Expand Down
2 changes: 1 addition & 1 deletion cowprotocol/accounting/slippage/slippage_4070065.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ slippage_per_transaction as (
solver_address,
sum(slippage_usd) as slippage_usd,
sum(slippage_wei) as slippage_wei
from "query_4059683(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}',raw_slippage_table_name='raw_slippage_breakdown')" as rs
from "query_4059683(blockchain='{{blockchain}}',price_feed='{{price_feed}}',start_time='{{start_time}}',end_time='{{end_time}}',raw_slippage_table_name='raw_slippage_breakdown')" as rs
inner join cow_protocol_{{blockchain}}.batches as b
on rs.tx_hash = b.tx_hash
where rs.tx_hash not in (select tx_hash from excluded_batches)
Expand Down
70 changes: 69 additions & 1 deletion cowprotocol/accounting/slippage/slippage_prices_4064601.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,47 @@ with token_times as (
group by 1, 2
),

imported_price_feed as (
select *
from "query_4252674"
where price_unit < 10000000 -- here we filter all tokens with price more than $1M, as these are probably bogus prices
),

coingecko_price_feed as (
select
ipf.hour,
ipf.token_address,
ipf.decimals,
ipf.price_unit,
ipf.price_atom
from imported_price_feed as ipf inner join token_times as tt on ipf.hour = tt.hour and ipf.token_address = tt.token_address
where source = 'coingecko'
),

moralis_price_feed as (
select
ipf.hour,
ipf.token_address,
ipf.decimals,
ipf.price_unit,
ipf.price_atom
from imported_price_feed as ipf inner join token_times as tt on ipf.hour = tt.hour and ipf.token_address = tt.token_address
where source = 'moralis'
),

auction_price_feed as (
select
ipf.hour,
ipf.token_address,
ipf.decimals,
ipf.price_unit,
ipf.price_atom
from imported_price_feed as ipf inner join token_times as tt on ipf.hour = tt.hour and ipf.token_address = tt.token_address
where source = 'native'
),

-- Precise prices are prices from the Dune price feed.
precise_prices as (
dune_price_feed as (
select -- noqa: ST06
date_trunc('hour', minute) as hour, --noqa: RF04
token_address,
Expand All @@ -43,6 +82,35 @@ precise_prices as (
group by 1, 2, 3
),

multiple_price_feeds_pre as (
select *
from dune_price_feed
union all
select *
from coingecko_price_feed
union all
select *
from moralis_price_feed
union all
select *
from auction_price_feed
),

multiple_price_feeds as (
select
hour,
token_address,
decimals,
approx_percentile(price_unit, 0.5) as price_unit,
approx_percentile(price_atom, 0.5) as price_atom
from multiple_price_feeds_pre group by 1, 2, 3
),

precise_prices as (
select *
from {{price_feed}}
),

-- Intrinsic prices are prices reconstructed from exchange rates from within the auction
-- A price can be reconstructed if there was a trade with another token which did have a Dune price.
-- If there a multiple prices reconstructed in this way, an average is taken.
Expand Down

0 comments on commit 5bd104f

Please sign in to comment.