Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trade slippage & app Data #7477

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{{ config(alias='app_data',

post_hook='{{ expose_spells(\'["arbitrum"]\',
"project",
"cow_protocol",
\'["bh2smith"]\') }}'
)}}

-- Find the PoC Query here: https://dune.com/queries/1751965
with
partially_unpacked_app_content as (
select
distinct from_hex(app_hash) as app_hash,
content.appCode as app_code,
content.environment,
content.metadata.orderClass.orderClass as order_class,
content.metadata.partnerFee as partner_fee,
content.metadata.quote,
content.metadata.referrer,
content.metadata.utm,
content.metadata.widget
from {{ source('cowswap', 'raw_app_data') }}
),
unpacked_referrer_app_data as (
select
app_hash,
app_code,
environment,
order_class,
partner_fee,
quote,
-- different app data versions put referrer in two possible places.
from_hex(coalesce(referrer.address, referrer.referrer)) as referrer,
utm,
widget
from partially_unpacked_app_content
),
results as (
select
app_hash,
app_code,
environment,
order_class,
referrer,
partner_fee.bps as partner_fee_bps,
partner_fee.recipient as partner_recipient,
cast(quote.slippageBips as double) as slippage_bips,
utm,
utm.utmSource as utm_source,
utm.utmMedium as utm_medium,
utm.utmContent as utm_content,
utm.utmCampaign as utm_campaign,
utm.utmTerm as utm_term,
widget.appCode as widget_app_code,
widget.environment as widget_environment
from unpacked_referrer_app_data
)
select * from results
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,30 @@ models:
data_tests:
- unique:
column_name: tx_hash

- name: cow_protocol_arbitrum_app_data
meta:
blockchain: arbitrum
project: cow_protocol
contributors: bh2smith
config:
tags: ['arbitrum','cow_protocol','app_data', "metadata"]
description: >
CoW Protocol App Data is JSON content stored on IPFS corresponding to order AppHash (cf. CoW Documentation https://docs.cow.fi/front-end/creating-app-ids).

- name: cow_protocol_arbitrum_trade_slippage
meta:
blockchain: arbitrum
project: cow_protocol
contributors: bh2smith, gentrexha, josojo
config:
tags: [ 'arbitrum','cow_protocol','trade_slippage' ]
description: >
The query contains several measurements between the quoted price on cowswap and
the executed price. The price difference between the quote and the actual price
is called price improvement and is measured as percentage or in absolute dollars.
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- order_uid
- block_number
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{{ config(
alias='trade_slippage',

post_hook='{{ expose_spells(\'["arbitrum"]\',
"project",
"cow_protocol",
\'["bh2smith", "gentrexha", "josojo"]\') }}'
)}}

-- PoC Query: https://dune.com/queries/2279196
with
raw_data as (
select
order_uid,
block_number,
block_time,
order_type,
CASE
when order_type = 'SELL'
then cast(atoms_sold as double) / cast(limit_sell_amount as double)
else cast(atoms_bought as double) / cast(limit_buy_amount as double)
end as fill_proportion,
CASE when order_type = 'SELL'
THEN (cast(limit_buy_amount as double) / (1.0 - (cast(slippage_bips as double) / 10000.0)))
else cast(limit_buy_amount as double) end as buy_quote,
atoms_bought,
CASE when order_type = 'BUY'
THEN (cast(limit_sell_amount as double) / (1.0 + (cast(slippage_bips as double) / 10000.0)))
else cast(limit_sell_amount as double) end as sell_quote,
atoms_sold,
usd_value as trade_usd_value,
slippage_bips as tolerance_bips
from {{ref('cow_protocol_arbitrum_app_data')}} as ad
inner join {{ source('cow_protocol_arbitrum', 'trades') }} as t on t.app_data = ad.app_hash
where slippage_bips is not null
),

results as (
select order_uid,
block_number,
block_time,
buy_quote,
sell_quote,
tolerance_bips,
trade_usd_value,
fill_proportion,
CASE
WHEN order_type = 'SELL' THEN (atoms_bought - (buy_quote * fill_proportion))
ELSE ((sell_quote * fill_proportion) - atoms_sold)
END AS amount_atoms,
100.0 * (CASE
WHEN order_type = 'SELL'
THEN (atoms_bought - (buy_quote * fill_proportion)) / (buy_quote * fill_proportion)
ELSE ((sell_quote * fill_proportion) - atoms_sold) / (sell_quote * fill_proportion)
END) AS amount_percentage,
CASE
WHEN order_type = 'SELL'
THEN (atoms_bought - (buy_quote * fill_proportion)) * (trade_usd_value / atoms_bought)
ELSE ((sell_quote * fill_proportion) - atoms_sold) * (trade_usd_value / atoms_sold)
END AS amount_usd
from raw_data
)
select * from results
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{{ config(alias='app_data',

post_hook='{{ expose_spells(\'["base"]\',
"project",
"cow_protocol",
\'["bh2smith"]\') }}'
)}}

-- Find the PoC Query here: https://dune.com/queries/1751965
with
partially_unpacked_app_content as (
select
distinct from_hex(app_hash) as app_hash,
content.appCode as app_code,
content.environment,
content.metadata.orderClass.orderClass as order_class,
content.metadata.partnerFee as partner_fee,
content.metadata.quote,
content.metadata.referrer,
content.metadata.utm,
content.metadata.widget
from {{ source('cowswap', 'raw_app_data') }}
),
unpacked_referrer_app_data as (
select
app_hash,
app_code,
environment,
order_class,
partner_fee,
quote,
-- different app data versions put referrer in two possible places.
from_hex(coalesce(referrer.address, referrer.referrer)) as referrer,
utm,
widget
from partially_unpacked_app_content
),
results as (
select
app_hash,
app_code,
environment,
order_class,
referrer,
partner_fee.bps as partner_fee_bps,
partner_fee.recipient as partner_recipient,
cast(quote.slippageBips as double) as slippage_bips,
utm,
utm.utmSource as utm_source,
utm.utmMedium as utm_medium,
utm.utmContent as utm_content,
utm.utmCampaign as utm_campaign,
utm.utmTerm as utm_term,
widget.appCode as widget_app_code,
widget.environment as widget_environment
from unpacked_referrer_app_data
)
select * from results
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,30 @@ models:
data_tests:
- unique:
column_name: tx_hash

- name: cow_protocol_base_app_data
meta:
blockchain: base
project: cow_protocol
contributors: bh2smith
config:
tags: ['base','cow_protocol','app_data', "metadata"]
description: >
CoW Protocol App Data is JSON content stored on IPFS corresponding to order AppHash (cf. CoW Documentation https://docs.cow.fi/front-end/creating-app-ids).

- name: cow_protocol_base_trade_slippage
meta:
blockchain: base
project: cow_protocol
contributors: bh2smith, gentrexha, josojo
config:
tags: [ 'base','cow_protocol','trade_slippage' ]
description: >
The query contains several measurements between the quoted price on cowswap and
the executed price. The price difference between the quote and the actual price
is called price improvement and is measured as percentage or in absolute dollars.
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- order_uid
- block_number
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{{ config(
alias='trade_slippage',

post_hook='{{ expose_spells(\'["base"]\',
"project",
"cow_protocol",
\'["bh2smith", "gentrexha", "josojo"]\') }}'
)}}

-- PoC Query: https://dune.com/queries/2279196
with
raw_data as (
select
order_uid,
block_number,
block_time,
order_type,
CASE
when order_type = 'SELL'
then cast(atoms_sold as double) / cast(limit_sell_amount as double)
else cast(atoms_bought as double) / cast(limit_buy_amount as double)
end as fill_proportion,
CASE when order_type = 'SELL'
THEN (cast(limit_buy_amount as double) / (1.0 - (cast(slippage_bips as double) / 10000.0)))
else cast(limit_buy_amount as double) end as buy_quote,
atoms_bought,
CASE when order_type = 'BUY'
THEN (cast(limit_sell_amount as double) / (1.0 + (cast(slippage_bips as double) / 10000.0)))
else cast(limit_sell_amount as double) end as sell_quote,
atoms_sold,
usd_value as trade_usd_value,
slippage_bips as tolerance_bips
from {{ref('cow_protocol_base_app_data')}} as ad
inner join {{ source('cow_protocol_base', 'trades') }} as t on t.app_data = ad.app_hash
where slippage_bips is not null
),

results as (
select order_uid,
block_number,
block_time,
buy_quote,
sell_quote,
tolerance_bips,
trade_usd_value,
fill_proportion,
CASE
WHEN order_type = 'SELL' THEN (atoms_bought - (buy_quote * fill_proportion))
ELSE ((sell_quote * fill_proportion) - atoms_sold)
END AS amount_atoms,
100.0 * (CASE
WHEN order_type = 'SELL'
THEN (atoms_bought - (buy_quote * fill_proportion)) / (buy_quote * fill_proportion)
ELSE ((sell_quote * fill_proportion) - atoms_sold) / (sell_quote * fill_proportion)
END) AS amount_percentage,
CASE
WHEN order_type = 'SELL'
THEN (atoms_bought - (buy_quote * fill_proportion)) * (trade_usd_value / atoms_bought)
ELSE ((sell_quote * fill_proportion) - atoms_sold) * (trade_usd_value / atoms_sold)
END AS amount_usd
from raw_data
)
select * from results
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{{ config(alias='app_data',

post_hook='{{ expose_spells(\'["gnosis"]\',
"project",
"cow_protocol",
\'["bh2smith"]\') }}'
)}}

-- Find the PoC Query here: https://dune.com/queries/1751965
with
partially_unpacked_app_content as (
select
distinct from_hex(app_hash) as app_hash,
content.appCode as app_code,
content.environment,
content.metadata.orderClass.orderClass as order_class,
content.metadata.partnerFee as partner_fee,
content.metadata.quote,
content.metadata.referrer,
content.metadata.utm,
content.metadata.widget
from {{ source('cowswap', 'raw_app_data') }}
),
unpacked_referrer_app_data as (
select
app_hash,
app_code,
environment,
order_class,
partner_fee,
quote,
-- different app data versions put referrer in two possible places.
from_hex(coalesce(referrer.address, referrer.referrer)) as referrer,
utm,
widget
from partially_unpacked_app_content
),
results as (
select
app_hash,
app_code,
environment,
order_class,
referrer,
partner_fee.bps as partner_fee_bps,
partner_fee.recipient as partner_recipient,
cast(quote.slippageBips as double) as slippage_bips,
utm,
utm.utmSource as utm_source,
utm.utmMedium as utm_medium,
utm.utmContent as utm_content,
utm.utmCampaign as utm_campaign,
utm.utmTerm as utm_term,
widget.appCode as widget_app_code,
widget.environment as widget_environment
from unpacked_referrer_app_data
)
select * from results
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,30 @@ models:
tags: ['gnosis','cow_protocol','trades', 'dex', 'aggregator', 'auction']
description: >
CoW Protocol enriched batches table on Gnosis Chain

- name: cow_protocol_gnosis_app_data
meta:
blockchain: gnosis
project: cow_protocol
contributors: bh2smith
config:
tags: ['gnosis','cow_protocol','app_data', "metadata"]
description: >
CoW Protocol App Data is JSON content stored on IPFS corresponding to order AppHash (cf. CoW Documentation https://docs.cow.fi/front-end/creating-app-ids).

- name: cow_protocol_gnosis_trade_slippage
meta:
blockchain: gnosis
project: cow_protocol
contributors: bh2smith, gentrexha, josojo
config:
tags: [ 'gnosis','cow_protocol','trade_slippage' ]
description: >
The query contains several measurements between the quoted price on cowswap and
the executed price. The price difference between the quote and the actual price
is called price improvement and is measured as percentage or in absolute dollars.
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- order_uid
- block_number
Loading
Loading