Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cowprotocol/execution_quality/markout/cow_markout_6031665.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
-- {{max_usd_amount}} - Maximum USD amount of the trade to be considered

select
block_time,
tx_hash,
token_bought_amount,
token_sold_amount,
token_bought_address,
token_sold_address,
token_bought_symbol,
token_sold_symbol,
sell.price as sellprice,
buy.price as buyprice,
amount_usd,
Expand All @@ -33,7 +36,5 @@ where
and token_pair is not null
and block_date >= timestamp '{{start_date}}'
and block_date < timestamp '{{end_date}}'
and token_bought_amount * buy.price >= {{min_usd_amount}}
and token_bought_amount * buy.price <= {{max_usd_amount}}
and token_sold_amount * sell.price >= {{min_usd_amount}}
and token_sold_amount * sell.price <= {{max_usd_amount}}
and token_bought_amount * buy.price between {{min_usd_amount}} and {{max_usd_amount}}
and token_sold_amount * sell.price between {{min_usd_amount}} and {{max_usd_amount}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
-- Markout comparison across multiple DEX aggregators and CoW Protocol over time
-- Returns markout avg and percentiles in basis points (bps) for each project over the specified time period

-- Parameters:
-- {{blockchain}} - The chain on which trades should be counted
-- {{start_date}} - Start date for when trades should be counted
-- {{end_date}} - End date for when trades should be counted
-- {{min_usd_amount}} - Minimum USD amount of the trade to be considered
-- {{max_usd_amount}} - Maximum USD amount of the trade to be considered

select
'CoW' as project
, date_trunc('{{date_granularity}}', block_time) as date
, avg(markout) * 10000 as avg_markout_bps
, approx_percentile(markout, 0.25) * 10000 as p25_markout_bps
, approx_percentile(markout, 0.50) * 10000 as p50_markout_bps
, approx_percentile(markout, 0.75) * 10000 as p75_markout_bps
from "query_6031665(blockchain='{{blockchain}}',start_date='{{start_date}}',end_date='{{end_date}}',min_usd_amount='{{min_usd_amount}}',max_usd_amount='{{max_usd_amount}}')"
group by 1 , 2
union all
select
'1inch (intent based)' as project
, date_trunc('{{date_granularity}}', block_time) as date
, avg(markout) * 10000 as avg_markout_bps
, approx_percentile(markout, 0.25) * 10000 as p25_markout_bps
, approx_percentile(markout, 0.50) * 10000 as p50_markout_bps
, approx_percentile(markout, 0.75) * 10000 as p75_markout_bps
from "query_6031733(blockchain='{{blockchain}}',start_date='{{start_date}}',end_date='{{end_date}}',min_usd_amount='{{min_usd_amount}}',max_usd_amount='{{max_usd_amount}}')"
group by 1 , 2
union all
select
'Uniswap X' as project
, date_trunc('{{date_granularity}}', block_time) as date
, avg(markout) * 10000 as avg_markout_bps
, approx_percentile(markout, 0.25) * 10000 as p25_markout_bps
, approx_percentile(markout, 0.50) * 10000 as p50_markout_bps
, approx_percentile(markout, 0.75) * 10000 as p75_markout_bps
from "query_6033942(blockchain='{{blockchain}}',start_date='{{start_date}}',end_date='{{end_date}}',min_usd_amount='{{min_usd_amount}}',max_usd_amount='{{max_usd_amount}}')"
group by 1 , 2
union all
select
'1inch (non intent based)' as project
, date_trunc('{{date_granularity}}', block_time) as date
, avg(markout) * 10000 as avg_markout_bps
, approx_percentile(markout, 0.25) * 10000 as p25_markout_bps
, approx_percentile(markout, 0.50) * 10000 as p50_markout_bps
, approx_percentile(markout, 0.75) * 10000 as p75_markout_bps
from "query_6031688(blockchain='{{blockchain}}',start_date='{{start_date}}',end_date='{{end_date}}',min_usd_amount='{{min_usd_amount}}',max_usd_amount='{{max_usd_amount}}',project='1inch')"
group by 1 , 2
union all
select
'Kyberswap' as project
, date_trunc('{{date_granularity}}', block_time) as date
, avg(markout) * 10000 as avg_markout_bps
, approx_percentile(markout, 0.25) * 10000 as p25_markout_bps
, approx_percentile(markout, 0.50) * 10000 as p50_markout_bps
, approx_percentile(markout, 0.75) * 10000 as p75_markout_bps
from "query_6031688(blockchain='{{blockchain}}',start_date='{{start_date}}',end_date='{{end_date}}',min_usd_amount='{{min_usd_amount}}',max_usd_amount='{{max_usd_amount}}',project='kyberswap')"
group by 1 , 2
union all
select
'Paraswap' as project
, date_trunc('{{date_granularity}}', block_time) as date
, avg(markout) * 10000 as avg_markout_bps
, approx_percentile(markout, 0.25) * 10000 as p25_markout_bps
, approx_percentile(markout, 0.50) * 10000 as p50_markout_bps
, approx_percentile(markout, 0.75) * 10000 as p75_markout_bps
from "query_6031688(blockchain='{{blockchain}}',start_date='{{start_date}}',end_date='{{end_date}}',min_usd_amount='{{min_usd_amount}}',max_usd_amount='{{max_usd_amount}}',project='paraswap')"
group by 1 , 2
union all
select
'Odos' as project
, date_trunc('{{date_granularity}}', block_time) as date
, avg(markout) * 10000 as avg_markout_bps
, approx_percentile(markout, 0.25) * 10000 as p25_markout_bps
, approx_percentile(markout, 0.50) * 10000 as p50_markout_bps
, approx_percentile(markout, 0.75) * 10000 as p75_markout_bps
from "query_6031688(blockchain='{{blockchain}}',start_date='{{start_date}}',end_date='{{end_date}}',min_usd_amount='{{min_usd_amount}}',max_usd_amount='{{max_usd_amount}}',project='odos')"
group by 1 , 2
union all
select
'0x' as project
, date_trunc('{{date_granularity}}', block_time) as date
, avg(markout) * 10000 as avg_markout_bps
, approx_percentile(markout, 0.25) * 10000 as p25_markout_bps
, approx_percentile(markout, 0.50) * 10000 as p50_markout_bps
, approx_percentile(markout, 0.75) * 10000 as p75_markout_bps
from "query_6031688(blockchain='{{blockchain}}',start_date='{{start_date}}',end_date='{{end_date}}',min_usd_amount='{{min_usd_amount}}',max_usd_amount='{{max_usd_amount}}',project='0x')"
group by 1 , 2
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
--- Markout calculation for non-intent DEX aggregator trades. Compares the price of dex.prices table at the time of the trade to the executed amounts and factors in gas costs.
--- Returns a per transaction hash buy and sell USD value, gas cost in USD and the resulting markout.
-- Markout calculation for non-intent DEX aggregator trades.
-- Compares the price of prices.usd table at the time of the trade to the executed amounts and factors in gas costs.
-- Returns buy and sell USD value, gas cost in USD and the resulting markout, all per tx hash.

-- Parameters:
-- {{project}} - The aggregator to look at
-- {{start_date}} - Start date for when trades should be counted
-- {{end_date}} - End date for when trades should be counted
-- {{project}} - The aggregator to consider
-- {{start_date}} - Start date for when trades should be counted, inclusive
-- {{end_date}} - End date for when trades should be counted, exclusive
-- {{blockchain}} - The chain on which trades should be counted
-- {{native_token}} - The native token of the blockchain (e.g. ETH for Ethereum)
-- {{min_usd_amount}} - Minimum USD amount of the trade to be considered
-- {{max_usd_amount}} - Maximum USD amount of the trade to be considered

with markout_per_trade as (
select
with
markout_per_trade as (
select
block_time,
tx_hash,
token_bought_amount,
token_sold_amount,
Expand All @@ -35,38 +37,56 @@ with markout_per_trade as (
and buy.blockchain = t.blockchain
where
t.blockchain = '{{blockchain}}'
and project = '{{project}}'
and if(project='0x API', '0x', project) = '{{project}}' -- this fixes a weird parameter bug
and t.block_time >= timestamp '{{start_date}}'
and t.block_time < timestamp '{{end_date}}'
and amount_usd >= {{min_usd_amount}}
and amount_usd <= {{max_usd_amount}}
and amount_usd between {{min_usd_amount}} and {{max_usd_amount}}
),

markout_by_tx as (
select
tx_hash,
-- remove 1inch intent based txs
oneinch_intent_based as (
select distinct tx_hash
from oneinch.swaps
where
mode in ('fusion', 'cross-chain')
and block_time >= timestamp '{{start_date}}'
and block_time < timestamp '{{end_date}}'
),
agg_by_tx as (
select
a.block_time,
a.tx_hash,
sum(buy_usd_value) as buy_usd_value,
sum(sell_usd_value) as sell_usd_value
from markout_per_trade
group by 1
from markout_per_trade as a
left join oneinch_intent_based as b
on a.tx_hash = b.tx_hash
where b.tx_hash is null
group by 1,2
)
, native_prices as (
select
minute,
price
from prices.usd
where
contract_address = (select price_address from tokens.native where chain = '{{blockchain}}')
and blockchain = '{{blockchain}}'
and minute >= timestamp '{{start_date}}'
and minute < timestamp '{{end_date}}'
)

select
tx_hash,
buy_usd_value,
sell_usd_value,
(gas_used * gas_price * eth.price) / 1e18 as gas_usd,
(buy_usd_value/(sell_usd_value + (gas_used * gas_price * eth.price) / 1e18 )) - 1.0000 as markout
from markout_by_tx
join {{blockchain}}.transactions
on tx_hash = hash
join prices.usd as eth
on eth.contract_address = {{native_token}}
and eth.minute = date_trunc('minute', block_time)
and eth.blockchain = '{{blockchain}}'
-- filter out extreme native prices
and buy_usd_value >= {{min_usd_amount}}
and buy_usd_value <= {{max_usd_amount}}
and sell_usd_value >= {{min_usd_amount}}
and sell_usd_value <= {{max_usd_amount}}
order by markout asc
aggtx.block_time,
aggtx.tx_hash,
aggtx.buy_usd_value,
aggtx.sell_usd_value,
(tx.gas_used * tx.gas_price * np.price) / 1e18 as gas_usd,
(aggtx.buy_usd_value/(aggtx.sell_usd_value + (tx.gas_used * tx.gas_price * np.price) / 1e18 )) - 1.0000 as markout
from agg_by_tx as aggtx
join {{blockchain}}.transactions as tx
on aggtx.tx_hash = tx.hash
join native_prices as np
on np.minute = date_trunc('minute', tx.block_time)
where
aggtx.buy_usd_value between {{min_usd_amount}} and {{max_usd_amount}}
and aggtx.sell_usd_value between {{min_usd_amount}} and {{max_usd_amount}}
order by markout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- 1inch fusion markout calculation. Compares the price of prices.usd table at the time of the trade to the executed amounts
-- 1inch intent based modes markout calculation. Compares the price of prices.usd table at the time of the trade to the executed amounts

-- Parameters:
-- {{blockchain}} - The chain on which trades should be counted
Expand All @@ -8,30 +8,30 @@
-- {{max_usd_amount}} - Maximum USD amount of the trade to be considered

select
block_time,
tx_hash,
dst_token_amount,
src_token_amount,
cast(dst_executed_amount as double) /pow(10, cast(complement['dst_decimals'] as bigint)) as dst_exec_amount,
cast(src_executed_amount as double) /pow(10, cast(complement['src_decimals'] as bigint)) as src_exec_amount,
dst_token_address,
src_token_address,
sell.price as sellprice,
buy.price as buyprice,
amount_usd,
((dst_token_amount/pow(10,dst_token_decimals))/(src_token_amount/pow(10,src_token_decimals)) / (sell.price / buy.price)) - 1.0000 as markout
(cast(dst_executed_amount as double) /pow(10, cast(complement['dst_decimals'] as bigint))) / (cast(src_executed_amount as double)/pow(10, cast(complement['src_decimals'] as bigint))) / (sell.price / buy.price) - 1.0000 as markout
from oneinch.swaps as t
join prices.usd as sell
on sell.contract_address = src_token_address
and sell.minute = date_trunc('minute', block_time)
and sell.price > 0
and sell.blockchain = t.blockchain
and sell.blockchain = '{{blockchain}}'
join prices.usd as buy
on buy.contract_address = dst_token_address
and buy.minute = date_trunc('minute', block_time)
and buy.price > 0
and buy.blockchain = t.blockchain
and buy.blockchain = '{{blockchain}}'
where
t.blockchain = '{{blockchain}}'
and block_time >= timestamp '{{start_date}}'
and block_time < timestamp '{{end_date}}'
and amount_usd <= {{max_usd_amount}}
and amount_usd >= {{min_usd_amount}}
and flags['fusion']
and amount_usd between {{min_usd_amount}} and {{max_usd_amount}}
and mode in ('fusion', 'cross_chain')
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
-- {{max_usd_amount}} - Maximum USD amount of the trade to be considered

select
block_time,
tx_hash,
token_bought_amount,
token_sold_amount,
token_bought_address,
token_sold_address,
sell_token,
buy_token,
sell_price as sellprice,
buy_price as buyprice,
amount_usd,
Expand All @@ -22,7 +25,5 @@ where
blockchain = '{{blockchain}}'
and block_time >= timestamp '{{start_date}}'
and block_time < timestamp '{{end_date}}'
and token_bought_amount * buy_price >= {{min_usd_amount}}
and token_bought_amount * buy_price <= {{max_usd_amount}}
and token_sold_amount * sell_price >= {{min_usd_amount}}
and token_sold_amount * sell_price <= {{max_usd_amount}}
and token_bought_amount * buy_price between {{min_usd_amount}} and {{max_usd_amount}}
and token_sold_amount * sell_price between {{min_usd_amount}} and {{max_usd_amount}}