diff --git a/dbt_subprojects/dex/macros/models/_project/uniswap_compatible_trades.sql b/dbt_subprojects/dex/macros/models/_project/uniswap_compatible_trades.sql index 9cf02fee495..f6b5b92ff67 100644 --- a/dbt_subprojects/dex/macros/models/_project/uniswap_compatible_trades.sql +++ b/dbt_subprojects/dex/macros/models/_project/uniswap_compatible_trades.sql @@ -118,4 +118,74 @@ SELECT , dexs.evt_index FROM dexs +{% endmacro %} + + +{% macro uniswap_compatible_v4_trades( + blockchain = null + , project = 'uniswap' + , version = '4' + , Pair_evt_Swap = source('uniswap_v4_sepolia', 'PoolManager_evt_Swap') + , Factory_evt_PoolCreated = source('uniswap_v4_sepolia', 'PoolManager_evt_Initialize') + , taker_column_name = 't.evt_tx_from' + , maker_column_name = null + , optional_columns = [] + , pair_column_name = 'f.id' + ) +%} +WITH dexs AS +( + SELECT + t.evt_block_number AS block_number + , t.evt_block_time AS block_time + , {{ taker_column_name }} as taker + , {% if maker_column_name -%} {{ maker_column_name }} {% else -%} cast(null as varbinary) {% endif -%} as maker + -- in v4, when amount is negative, then user are selling the token (so things are done from the perspective of the user instead of the pool) + , CASE WHEN amount0 < INT256 '0' THEN abs(amount1) ELSE abs(amount0) END AS token_bought_amount_raw + , CASE WHEN amount0 < INT256 '0' THEN abs(amount0) ELSE abs(amount1) END AS token_sold_amount_raw + , CASE WHEN amount0 < INT256 '0' THEN f.currency1 ELSE f.currency0 END AS token_bought_address + , CASE WHEN amount0 < INT256 '0' THEN f.currency0 ELSE f.currency1 END AS token_sold_address + , t.contract_address as project_contract_address + {%- if optional_columns %} + {%- for optional_column in optional_columns %} + , {{ optional_column }} + {%- endfor %} + {%- endif %} + , t.evt_tx_hash AS tx_hash + , t.evt_index + , row_number() over (partition by t.evt_tx_hash, t.evt_index order by t.evt_block_time desc) as duplicates_rank -- TODO remove after testing + FROM + {{ Pair_evt_Swap }} t + INNER JOIN + {{ Factory_evt_PoolCreated }} f + ON {{ pair_column_name }} = t.id + {%- if is_incremental() %} + WHERE + {{ incremental_predicate('t.evt_block_time') }} + {%- endif %} +) + +SELECT + {% if blockchain -%} '{{ blockchain }}' {% else -%} 'Unassigned' {% endif -%} as blockchain + , '{{ project }}' AS project + , '{{ version }}' AS version + , CAST(date_trunc('month', dexs.block_time) AS date) AS block_month + , CAST(date_trunc('day', dexs.block_time) AS date) AS block_date + , dexs.block_time + , dexs.block_number + , CAST(dexs.token_bought_amount_raw AS UINT256) AS token_bought_amount_raw + , CAST(dexs.token_sold_amount_raw AS UINT256) AS token_sold_amount_raw + , dexs.token_bought_address + , dexs.token_sold_address + , dexs.sender as router + , dexs.fee + , dexs.hooks -- if 0x00 null address then no hook, else yes hook + , dexs.taker + , dexs.maker + , dexs.project_contract_address + , dexs.tx_hash + , dexs.evt_index +FROM + dexs +WHERE duplicates_rank = 1 -- TODO remove after testing {% endmacro %} \ No newline at end of file diff --git a/dbt_subprojects/dex/models/trades/_schema.yml b/dbt_subprojects/dex/models/trades/_schema.yml index 19c6fe6ba67..37918e56dc3 100644 --- a/dbt_subprojects/dex/models/trades/_schema.yml +++ b/dbt_subprojects/dex/models/trades/_schema.yml @@ -110,6 +110,9 @@ models: data_tests: - dbt_utils.unique_combination_of_columns: combination_of_columns: + - block_month + - block_date + - block_number - blockchain - project - version diff --git a/dbt_subprojects/dex/models/trades/dex_base_trades.sql b/dbt_subprojects/dex/models/trades/dex_base_trades.sql index 92c86f287c0..b52768d8ec8 100644 --- a/dbt_subprojects/dex/models/trades/dex_base_trades.sql +++ b/dbt_subprojects/dex/models/trades/dex_base_trades.sql @@ -5,7 +5,7 @@ , materialized = 'incremental' , file_format = 'delta' , incremental_strategy = 'merge' - , unique_key = ['blockchain', 'project', 'version', 'tx_hash', 'evt_index'] + , unique_key = ['block_month', 'block_date', 'block_number', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index'] , incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] ) }} @@ -31,6 +31,7 @@ , ref('dex_ronin_base_trades') , ref('dex_scroll_base_trades') , ref('dex_sei_base_trades') + , ref('dex_sepolia_base_trades') , ref('dex_sonic_base_trades') , ref('dex_worldchain_base_trades') , ref('dex_zkevm_base_trades') @@ -66,14 +67,17 @@ with base_union as ( FROM {{ model }} WHERE - token_sold_amount_raw >= 0 and token_bought_amount_raw >= 0 - {% if is_incremental() %} + token_sold_amount_raw >= 0 + AND token_bought_amount_raw >= 0 + {%- if is_incremental() %} AND {{ incremental_predicate('block_time') }} - {% endif %} - {% if not loop.last %} + {%- else %} + AND block_time >= timestamp '2025-01-01' -- TODO remove after testing + {%- endif %} + {%- if not loop.last %} UNION ALL - {% endif %} - {% endfor %} + {%- endif %} + {%- endfor %} ) WHERE duplicates_rank = 1 @@ -81,4 +85,4 @@ with base_union as ( select * from - base_union + base_union \ No newline at end of file diff --git a/dbt_subprojects/dex/models/trades/sepolia/_schema.yml b/dbt_subprojects/dex/models/trades/sepolia/_schema.yml new file mode 100644 index 00000000000..852670f592a --- /dev/null +++ b/dbt_subprojects/dex/models/trades/sepolia/_schema.yml @@ -0,0 +1,25 @@ +version: 2 + +models: + - name: dex_sepolia_base_trades + data_tests: + - check_dex_info_relationship + + - name: uniswap_v4_sepolia_base_trades + meta: + blockchain: sepolia + sector: dex + project: uniswap + contributors: agaperste + config: + tags: [ 'sepolia', 'dex', 'trades', 'uniswap', 'v4' ] + description: "uniswap sepolia v4 base trades" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - tx_hash + - evt_index + - check_dex_base_trades_seed: + seed_file: ref('uniswap_sepolia_base_trades_seed') + filter: + version: 4 diff --git a/dbt_subprojects/dex/models/trades/sepolia/dex_sepolia_base_trades.sql b/dbt_subprojects/dex/models/trades/sepolia/dex_sepolia_base_trades.sql new file mode 100644 index 00000000000..ab1e83cb495 --- /dev/null +++ b/dbt_subprojects/dex/models/trades/sepolia/dex_sepolia_base_trades.sql @@ -0,0 +1,48 @@ +{{ config( + schema = 'dex_sepolia' + , alias = 'base_trades' + , materialized = 'view' + ) +}} + +{% set base_models = [ + ref('uniswap_v4_sepolia_base_trades') +] %} + +WITH base_union AS ( + SELECT * + FROM ( + {% for base_model in base_models %} + SELECT + blockchain + , project + , version + , block_month + , block_date + , block_time + , block_number + , token_bought_amount_raw + , token_sold_amount_raw + , token_bought_address + , token_sold_address + , taker + , maker + , project_contract_address + , tx_hash + , evt_index + FROM + {{ base_model }} + {% if not loop.last %} + UNION ALL + {% endif %} + {% endfor %} + ) +) + +{{ + add_tx_columns( + model_cte = 'base_union' + , blockchain = 'sepolia' + , columns = ['from', 'to', 'index'] + ) +}} diff --git a/dbt_subprojects/dex/models/trades/sepolia/platforms/uniswap_v4_sepolia_base_trades.sql b/dbt_subprojects/dex/models/trades/sepolia/platforms/uniswap_v4_sepolia_base_trades.sql new file mode 100644 index 00000000000..1c19168d061 --- /dev/null +++ b/dbt_subprojects/dex/models/trades/sepolia/platforms/uniswap_v4_sepolia_base_trades.sql @@ -0,0 +1,21 @@ +{{ config( + schema = 'uniswap_v4_sepolia' + , alias = 'base_trades' + , materialized = 'incremental' + , file_format = 'delta' + , incremental_strategy = 'merge' + , unique_key = ['tx_hash', 'evt_index'] + , incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +{{ + uniswap_compatible_v4_trades( + blockchain = 'sepolia' + , project = 'uniswap' + , version = '4' + , Pair_evt_Swap = source('uniswap_v4_sepolia', 'PoolManager_evt_Swap') + , Factory_evt_PoolCreated = source('uniswap_v4_sepolia', 'PoolManager_evt_Initialize') + , optional_columns = ['t.sender', 'f.fee', 'f.hooks'] + ) +}} diff --git a/dbt_subprojects/dex/seeds/trades/_schema.yml b/dbt_subprojects/dex/seeds/trades/_schema.yml index 0c6a2f32b67..cf4ca00dbb9 100644 --- a/dbt_subprojects/dex/seeds/trades/_schema.yml +++ b/dbt_subprojects/dex/seeds/trades/_schema.yml @@ -4184,6 +4184,20 @@ seeds: token_bought_amount_raw: uint256 token_sold_amount_raw: uint256 block_date: timestamp + - name: uniswap_sepolia_base_trades_seed # TODO: remove after testing uni v4 sepolia + config: + column_types: + blockchain: varchar + project: varchar + version: varchar + tx_hash: varbinary + evt_index: uint256 + block_number: uint256 + token_bought_address: varbinary + token_sold_address: varbinary + token_bought_amount_raw: uint256 + token_sold_amount_raw: uint256 + block_date: timestamp - name: sushiswap_zkevm_base_trades_seed config: diff --git a/dbt_subprojects/dex/seeds/trades/uniswap_sepolia_base_trades_seed.csv b/dbt_subprojects/dex/seeds/trades/uniswap_sepolia_base_trades_seed.csv new file mode 100644 index 00000000000..1c785a8b592 --- /dev/null +++ b/dbt_subprojects/dex/seeds/trades/uniswap_sepolia_base_trades_seed.csv @@ -0,0 +1,10 @@ +blockchain,project,version,block_date,tx_hash,evt_index,token_bought_address,token_sold_address,block_number,token_bought_amount_raw,token_sold_amount_raw,router,fee,hooks +sepolia,uniswap,4,2025-01-07 00:00:00.000 UTC,0x86366ac3242478fab08e194cd43b8ef28614eb9e3f4d503419d5d837d5aec8c7,237,0xfff9976782d46cc05630d1f6ebab18b2324d6b14,0x1c7d4b196cb0c7b01d743fbc6116a902379c7238,7440597,1072128371,5,0x9b6b46e2c869aa39918db7f52f5557fe577b6eee,1000,0x0000000000000000000000000000000000000000 +sepolia,uniswap,4,2025-01-07 00:00:00.000 UTC,0xcf4c6284ac412b68851a0327500ee87e70b27eb2216f18d450a0e67a04eba05e,20,0x89434ec9fbc25c9ef419e4457a9b846d68ec580c,0x0000000000000000000000000000000000000000,7442216,166655944935531162481,50000000000000000,0x4d73a4411ca1c660035e4aecc8270e5dddec8c17,100000,0x0000000000000000000000000000000000000000 +sepolia,uniswap,4,2025-01-06 00:00:00.000 UTC,0x03caeff5a9677bb0b0ca211966fd4eec508366e3c08b1d7695dbf71a2ec2b0e0,190,0x42dd4a197f443a830538023faa4ff6bddd1b4cff,0x19352a81131b6857cb23168c1e61df49e7e3751d,7433233,5543660545321377,5555555555552000,0x2785f45c36d043626df16174d6b9beb89e2ca880,3000,0x2785f45c36d043626df16174d6b9beb89e2ca880 +sepolia,uniswap,4,2025-01-06 00:00:00.000 UTC,0xf9405d2e681fa8d43457871df1f96441fbdb15b1efd5ea4c21a0f4ebb4b79ced,130,0xc979f27d03685e92618fca1d0ae46ba1b6cdfcec,0x432702d8007dea73344d8d685ca46c92d90446fe,7433899,99999899,100300803,0x56b9b1287cdfd0e85e5f631c284055b3e2286880,3000,0x56b9b1287cdfd0e85e5f631c284055b3e2286880 +sepolia,uniswap,4,2025-01-06 00:00:00.000 UTC,0x6e3ae1b5441fa53077033dd64c60938f818986b574dcd1056f38b088ad3810f8,303,0x42dd4a197f443a830538023faa4ff6bddd1b4cff,0x19352a81131b6857cb23168c1e61df49e7e3751d,7433268,5810696613028011,5833333333332800,0x2785f45c36d043626df16174d6b9beb89e2ca880,3000,0x2785f45c36d043626df16174d6b9beb89e2ca880 +sepolia,uniswap,4,2025-01-06 00:00:00.000 UTC,0x5d277242306669264ab9930104361f21a05865b9c6db5a89e161f94967093636,61,0x0000000000000000000000000000000000000000,0xdfa556856b77da71f70d56c9fd1ef4dd891ff131,7431128,0,0,0xf9577ab14fd416c3c9b59c660408dbfffa87527d,3000,0x41e1c0cd59d538893df9960373330585dc3e8888 +sepolia,uniswap,4,2025-01-06 00:00:00.000 UTC,0xa7d157b6d2b7e5cbb7afd1134c4c6f64ce8b755457c3fa84c45be372bac70cf1,131,0x19352a81131b6857cb23168c1e61df49e7e3751d,0x42dd4a197f443a830538023faa4ff6bddd1b4cff,7432917,7073692745115717,7099999999999200,0x2785f45c36d043626df16174d6b9beb89e2ca880,3000,0x2785f45c36d043626df16174d6b9beb89e2ca880 +sepolia,uniswap,4,2025-01-06 00:00:00.000 UTC,0xa4e4f2be672049dc71244af27ceb35e7a2ff4676d41ea5cc6c5d3988a3fab278,179,0xc979f27d03685e92618fca1d0ae46ba1b6cdfcec,0x432702d8007dea73344d8d685ca46c92d90446fe,7433915,1384530901776389,1388889288888700,0x56b9b1287cdfd0e85e5f631c284055b3e2286880,3000,0x56b9b1287cdfd0e85e5f631c284055b3e2286880 +sepolia,uniswap,4,2025-01-06 00:00:00.000 UTC,0xb95e5a82ca98ae76c3206c4d6c1777f35119b8876cd2e2d37e03ce1acc113549,115,0xc979f27d03685e92618fca1d0ae46ba1b6cdfcec,0x432702d8007dea73344d8d685ca46c92d90446fe,7433872,332333299,333333300,0x56b9b1287cdfd0e85e5f631c284055b3e2286880,3000,0x56b9b1287cdfd0e85e5f631c284055b3e2286880 diff --git a/sources/_sector/dex/trades/sepolia/_sources.yml b/sources/_sector/dex/trades/sepolia/_sources.yml new file mode 100644 index 00000000000..4079838185c --- /dev/null +++ b/sources/_sector/dex/trades/sepolia/_sources.yml @@ -0,0 +1,7 @@ +version: 2 + +sources: + - name: uniswap_v4_sepolia + tables: + - name: PoolManager_evt_Swap + - name: PoolManager_evt_Initialize \ No newline at end of file