diff --git a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/arbitrum/cow_protocol_arbitrum_app_data.sql b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/arbitrum/cow_protocol_arbitrum_app_data.sql new file mode 100644 index 00000000000..d06a630a131 --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/arbitrum/cow_protocol_arbitrum_app_data.sql @@ -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 diff --git a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/arbitrum/cow_protocol_arbitrum_schema.yml b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/arbitrum/cow_protocol_arbitrum_schema.yml index 01bc985c01a..6f0f8854bc4 100644 --- a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/arbitrum/cow_protocol_arbitrum_schema.yml +++ b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/arbitrum/cow_protocol_arbitrum_schema.yml @@ -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 diff --git a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/arbitrum/cow_protocol_arbitrum_trade_slippage.sql b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/arbitrum/cow_protocol_arbitrum_trade_slippage.sql new file mode 100644 index 00000000000..68923227b9c --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/arbitrum/cow_protocol_arbitrum_trade_slippage.sql @@ -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 diff --git a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/base/cow_protocol_base_app_data.sql b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/base/cow_protocol_base_app_data.sql new file mode 100644 index 00000000000..0b1a6b37f12 --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/base/cow_protocol_base_app_data.sql @@ -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 diff --git a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/base/cow_protocol_base_schema.yml b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/base/cow_protocol_base_schema.yml index ecc9d21b4f1..d4fafb4330c 100644 --- a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/base/cow_protocol_base_schema.yml +++ b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/base/cow_protocol_base_schema.yml @@ -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 diff --git a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/base/cow_protocol_base_trade_slippage.sql b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/base/cow_protocol_base_trade_slippage.sql new file mode 100644 index 00000000000..54254510b55 --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/base/cow_protocol_base_trade_slippage.sql @@ -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 diff --git a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/gnosis/cow_protocol_gnosis_app_data.sql b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/gnosis/cow_protocol_gnosis_app_data.sql new file mode 100644 index 00000000000..a178ee08242 --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/gnosis/cow_protocol_gnosis_app_data.sql @@ -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 diff --git a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/gnosis/cow_protocol_gnosis_schema.yml b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/gnosis/cow_protocol_gnosis_schema.yml index 80d39a5c579..2cd523cd3a7 100644 --- a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/gnosis/cow_protocol_gnosis_schema.yml +++ b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/gnosis/cow_protocol_gnosis_schema.yml @@ -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 diff --git a/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/gnosis/cow_protocol_gnosis_trade_slippage.sql b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/gnosis/cow_protocol_gnosis_trade_slippage.sql new file mode 100644 index 00000000000..591debbe43e --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_project/cow_protocol/gnosis/cow_protocol_gnosis_trade_slippage.sql @@ -0,0 +1,63 @@ +{{ config( + alias='trade_slippage', + + post_hook='{{ expose_spells(\'["gnosis"]\', + "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_gnosis_app_data')}} as ad +inner join {{ source('cow_protocol_gnosis', '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