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

Add Uniswap V4 to dex.trades #7439

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
962d0a2
testing uni v4 dex trades on eth sepolia
agaperste Jan 8, 2025
b7fefb9
1/ fix dbt compile error 2/ add optional column to uniswap_v4 trades
agaperste Jan 8, 2025
d9f9532
forgot to delete pasted line, trying again (wish I could run dbt run …
agaperste Jan 8, 2025
a9305c5
fix dbt run errors (again wish i could run dbt run in local)
agaperste Jan 8, 2025
3703356
fix cli issue by hardcoding dex_base_trades to run for l7d
agaperste Jan 8, 2025
93e1f94
remove dupe seed record due to 2 v4 core contracts being decoded (won…
agaperste Jan 9, 2025
2a9799a
1/ temporarily adding row_num to rid of dupes for special scenario on…
agaperste Jan 9, 2025
fd6b5b0
Merge branch 'main' into add-uniswap-v4
agaperste Jan 9, 2025
2eb45c9
putting l7d filter back at the end to make cluster run (fingers crossed)
agaperste Jan 9, 2025
ef4f53c
Merge branch 'add-uniswap-v4' of https://github.com/agaperste/spellbo…
agaperste Jan 9, 2025
e8b235f
Update dbt_subprojects/dex/models/trades/dex_base_trades.sql
agaperste Jan 9, 2025
6cc830a
oops
agaperste Jan 9, 2025
b0138f7
putting back the bottom l7d filter to rid of memory limit error
agaperste Jan 9, 2025
f2b2435
Merge branch 'main' into add-uniswap-v4
jeff-dude Jan 10, 2025
cbc2efc
update schema name to v4
jeff-dude Jan 10, 2025
05ffdcf
remove duplicate flare, cleanup incremental logic
jeff-dude Jan 10, 2025
b24cb51
clean up parameters
jeff-dude Jan 10, 2025
625192a
fix spacing format
jeff-dude Jan 10, 2025
94191b6
quick format fix on dex base
jeff-dude Jan 10, 2025
fed3a93
add back end whitespace
jeff-dude Jan 10, 2025
59ab817
last attempt to improve base trades performance
jeff-dude Jan 10, 2025
110668d
update schema test to match
jeff-dude Jan 10, 2025
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
Expand Up @@ -118,4 +118,80 @@ SELECT
, dexs.evt_index
FROM
dexs
{% endmacro %}


{% macro uniswap_compatible_v4_trades(
blockchain = null
, project = null
, version = null
, Pair_evt_Swap = null
, Factory_evt_PoolCreated = null
, taker_column_name = 'evt_tx_from'
, maker_column_name = null
, optional_columns = ['t.sender', 'f.fee', 'f.hooks']
, pair_column_name = 'id'
)
%}
WITH dexs AS
(
SELECT
t.evt_block_number AS block_number
, t.evt_block_time AS block_time
, t.{{ 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 f.{{ pair_column_name }} = t.id
{% if is_incremental() %}
WHERE
{{ incremental_predicate('t.evt_block_time') }}
{% endif %}
)

SELECT
'{{ blockchain }}' 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 %}
4 changes: 4 additions & 0 deletions dbt_subprojects/dex/models/trades/dex_base_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
, ref('dex_zkevm_base_trades')
, ref('dex_zksync_base_trades')
, ref('dex_zora_base_trades')
, ref('dex_flare_base_trades')
, ref('dex_sepolia_base_trades')
] %}

with base_union as (
Expand Down Expand Up @@ -69,6 +71,7 @@ with base_union as (
token_sold_amount_raw >= 0 and token_bought_amount_raw >= 0
{% if is_incremental() %}
AND {{ incremental_predicate('block_time') }}
AND block_date >= now() - interval '7' day -- TODO remove after testing
agaperste marked this conversation as resolved.
Show resolved Hide resolved
{% endif %}
{% if not loop.last %}
UNION ALL
Expand All @@ -77,6 +80,7 @@ with base_union as (
)
WHERE
duplicates_rank = 1
and block_date >= now() - interval '7' day -- TODO remove after testing
agaperste marked this conversation as resolved.
Show resolved Hide resolved
)
select
*
Expand Down
25 changes: 25 additions & 0 deletions dbt_subprojects/dex/models/trades/sepolia/_schema.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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']
)
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{ config(
schema = 'uniswap_v3_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')
)
}}
14 changes: 14 additions & 0 deletions dbt_subprojects/dex/seeds/trades/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions sources/_sector/dex/trades/sepolia/_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2

sources:
- name: uniswap_v4_sepolia
tables:
- name: PoolManager_evt_Swap
- name: PoolManager_evt_Initialize
Loading