Skip to content

Commit

Permalink
adding uni v4 to dex.trades (#7543)
Browse files Browse the repository at this point in the history
* adding uni v4 to base dex.trades

* oops typo

* addressing PR comments

* fix dbt run error

* fix for dbt run

* clean up params, fix optional column logic

* cleanup taker param, move column alias

* adding rest of the wave1 v4 deployment to dex.trades (fingers crossed dbt run works!)

* workaround to bypass evt_tx_from being null for certain decoded tables

* fix bnb crosschain

---------

Co-authored-by: jeff-dude <[email protected]>
Co-authored-by: jeff-dude <[email protected]>
  • Loading branch information
3 people authored Jan 24, 2025
1 parent 8b3fdb2 commit fddbb5d
Show file tree
Hide file tree
Showing 50 changed files with 551 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,86 @@ SELECT
, dexs.evt_index
FROM
dexs
{% endmacro %}

{% macro uniswap_compatible_v4_trades(
blockchain = null
, project = 'uniswap'
, version = '4'
, PoolManager_evt_Swap = null
, PoolManager_evt_Initialize = null
, taker_column_name = null
, maker_column_name = null
, swap_optional_columns = ['fee']
, initialize_optional_columns = ['hooks']
, pair_column_name = 'id'
)
%}
WITH dexs AS
(
SELECT
t.evt_block_number AS block_number
, t.evt_block_time AS block_time
, {% if taker_column_name -%} t.{{ taker_column_name }} {% else -%} cast(null as varbinary) {% endif -%} as taker
, {% if maker_column_name -%} t.{{ 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 t.amount0 < INT256 '0' THEN abs(t.amount1) ELSE abs(t.amount0) END AS token_bought_amount_raw
, CASE WHEN t.amount0 < INT256 '0' THEN abs(t.amount0) ELSE abs(t.amount1) END AS token_sold_amount_raw
, CASE WHEN t.amount0 < INT256 '0' THEN f.currency1 ELSE f.currency0 END AS token_bought_address
, CASE WHEN t.amount0 < INT256 '0' THEN f.currency0 ELSE f.currency1 END AS token_sold_address
, t.contract_address as project_contract_address
, t.sender
, t.evt_tx_hash AS tx_hash
, t.evt_index
{%- if swap_optional_columns %}
{%- for optional_column in swap_optional_columns %}
, t.{{ optional_column }}
{%- endfor %}
{%- endif %}
{%- if initialize_optional_columns %}
{%- for optional_column in initialize_optional_columns %}
, f.{{ optional_column }}
{%- endfor %}
{%- endif %}
FROM
{{ PoolManager_evt_Swap }} t
INNER JOIN
{{ PoolManager_evt_Initialize }} f
ON f.{{ 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.taker
, dexs.maker
, dexs.project_contract_address
, dexs.tx_hash
, dexs.evt_index
{%- if swap_optional_columns %}
{%- for optional_column in swap_optional_columns %}
, dexs.{{ optional_column }}
{%- endfor %}
{%- endif %}
{%- if initialize_optional_columns %}
{%- for optional_column in initialize_optional_columns %}
, dexs.{{ optional_column }}
{%- endfor %}
{%- endif %}
FROM
dexs
{% endmacro %}
19 changes: 19 additions & 0 deletions dbt_subprojects/dex/models/trades/arbitrum/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ models:
column_name: amount_usd
max_value: 1000000000

- name: uniswap_v4_arbitrum_base_trades
meta:
blockchain: arbitrum
sector: dex
project: uniswap
contributors: agaperste
config:
tags: [ 'arbitrum', 'dex', 'trades', 'uniswap', 'v4' ]
description: "uniswap arbitrum 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_arbitrum_base_trades_seed')
filter:
version: 4

- name: uniswap_v3_arbitrum_base_trades
meta:
blockchain: arbitrum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

{% set base_models = [
ref('uniswap_v3_arbitrum_base_trades')
, ref('uniswap_v4_arbitrum_base_trades')
, ref('camelot_v2_arbitrum_base_trades')
, ref('camelot_v3_arbitrum_base_trades')
, ref('airswap_arbitrum_base_trades')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{ config(
schema = 'uniswap_v4_arbitrum'
, 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 = 'arbitrum'
, project = 'uniswap'
, version = '4'
, PoolManager_evt_Swap = source('uniswap_v4_arbitrum', 'PoolManager_evt_Swap')
, PoolManager_evt_Initialize = source('uniswap_v4_arbitrum', 'PoolManager_evt_Initialize')
)
}}
19 changes: 19 additions & 0 deletions dbt_subprojects/dex/models/trades/avalanche_c/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ models:
column_name: amount_usd
max_value: 1000000000

- name: uniswap_v4_avalanche_c_base_trades
meta:
blockchain: avalanche_c
sector: dex
project: uniswap
contributors: agaperste
config:
tags: [ 'avalanche_c', 'dex', 'trades', 'uniswap', 'v4' ]
description: "uniswap avalanche_c 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_avalanche_c_base_trades_seed')
filter:
version: 4

- name: uniswap_v3_avalanche_c_base_trades
meta:
blockchain: avalanche_c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

{% set base_models = [
ref('uniswap_v3_avalanche_c_base_trades')
, ref('uniswap_v4_avalanche_c_base_trades')
, ref('airswap_avalanche_c_base_trades')
, ref('sushiswap_v1_avalanche_c_base_trades')
, ref('sushiswap_v2_avalanche_c_base_trades')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{ config(
schema = 'uniswap_v4_avalanche_c'
, 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 = 'avalanche_c'
, project = 'uniswap'
, version = '4'
, PoolManager_evt_Swap = source('uniswap_v4_avalanche_c', 'PoolManager_evt_Swap')
, PoolManager_evt_Initialize = source('uniswap_v4_avalanche_c', 'PoolManager_evt_Initialize')
)
}}
19 changes: 19 additions & 0 deletions dbt_subprojects/dex/models/trades/base/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ models:
column_name: amount_usd
max_value: 1000000000

- name: uniswap_v4_base_base_trades
meta:
blockchain: base
sector: dex
project: uniswap
contributors: agaperste
config:
tags: [ 'base', 'dex', 'trades', 'uniswap', 'v4' ]
description: "uniswap base 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_base_base_trades_seed')
filter:
version: 4

- name: uniswap_v3_base_base_trades
meta:
blockchain: base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

{% set base_models = [
ref('uniswap_v3_base_base_trades')
, ref('uniswap_v4_base_base_trades')
, ref('sushiswap_v1_base_base_trades')
, ref('sushiswap_v2_base_base_trades')
, ref('stablebase_base_base_trades')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ config(
schema = 'uniswap_v4_base'
, 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 = 'base'
, project = 'uniswap'
, version = '4'
, PoolManager_evt_Swap = source('uniswap_v4_base', 'PoolManager_evt_Swap')
, PoolManager_evt_Initialize = source('uniswap_v4_base', 'PoolManager_evt_Initialize')
, taker_column_name = 'evt_tx_from'
)
}}
19 changes: 19 additions & 0 deletions dbt_subprojects/dex/models/trades/blast/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ models:
column_name: amount_usd
max_value: 1000000000

- name: uniswap_v4_blast_base_trades
meta:
blockchain: blast
sector: dex
project: uniswap
contributors: agaperste
config:
tags: [ 'blast', 'dex', 'trades', 'uniswap', 'v4' ]
description: "uniswap blast 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_blast_base_trades_seed')
filter:
version: 4

- name: uniswap_v3_blast_base_trades
meta:
blockchain: blast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

{% set base_models = [
ref('uniswap_v3_blast_base_trades')
, ref('uniswap_v4_blast_base_trades')
, ref('uniswap_v2_blast_base_trades')
, ref('thruster_blast_base_trades')
, ref('blasterswap_blast_base_trades')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ config(
schema = 'uniswap_v4_blast'
, 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 = 'blast'
, project = 'uniswap'
, version = '4'
, PoolManager_evt_Swap = source('uniswap_v4_blast', 'PoolManager_evt_Swap')
, PoolManager_evt_Initialize = source('uniswap_v4_blast', 'PoolManager_evt_Initialize')
, taker_column_name = 'evt_tx_from'
)
}}
19 changes: 19 additions & 0 deletions dbt_subprojects/dex/models/trades/bnb/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ models:
column_name: amount_usd
max_value: 1000000000

- name: uniswap_v4_bnb_base_trades
meta:
blockchain: bnb
sector: dex
project: uniswap
contributors: agaperste
config:
tags: [ 'bnb', 'dex', 'trades', 'uniswap', 'v4' ]
description: "uniswap bnb 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_bnb_base_trades_seed')
filter:
version: 4

- name: uniswap_v3_bnb_base_trades
meta:
blockchain: bnb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
-- (blockchain, project, project_version, model)
{% set base_models = [
ref('uniswap_v3_bnb_base_trades')
, ref('uniswap_v4_bnb_base_trades')
, ref('apeswap_bnb_base_trades')
, ref('airswap_bnb_base_trades')
, ref('sushiswap_v1_bnb_base_trades')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{ config(
schema = 'uniswap_v4_bnb'
, 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 = 'bnb'
, project = 'uniswap'
, version = '4'
, PoolManager_evt_Swap = source('uniswap_v4_bnb', 'PoolManager_evt_Swap')
, PoolManager_evt_Initialize = source('uniswap_v4_bnb', 'PoolManager_evt_Initialize')
)
}}
19 changes: 19 additions & 0 deletions dbt_subprojects/dex/models/trades/ethereum/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ models:
filter:
version: 3

- name: uniswap_v4_ethereum_base_trades
meta:
blockchain: ethereum
sector: dex
project: uniswap
contributors: agaperste
config:
tags: [ 'ethereum', 'dex', 'trades', 'uniswap', 'v4' ]
description: "uniswap ethereum 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_ethereum_base_trades_seed')
filter:
version: 4

- name: defiswap_ethereum_base_trades
meta:
blockchain: ethereum
Expand Down
Loading

0 comments on commit fddbb5d

Please sign in to comment.