From 6a6c128fc0e3f398c490eb26cf9ccf7670d90089 Mon Sep 17 00:00:00 2001 From: Mohamed Hassen Mami Date: Thu, 6 Jun 2024 19:31:37 +0700 Subject: [PATCH] paraswap: add zkevm v6.1 --- models/paraswap/paraswap_trades.sql | 3 +- models/paraswap/paraswap_trades_schema.yml | 4 +- .../zkevm/paraswap_v6_zkevm_trades.sql | 135 ++++++++++++ .../paraswap_v6_zkevm_trades_decoded.sql | 12 ++ .../paraswap/zkevm/paraswap_zkevm_schema.yml | 195 ++++++++++++++++++ .../paraswap/zkevm/paraswap_zkevm_trades.sql | 47 +++++ .../paraswap/zkevm/paraswap_zkevm_sources.yml | 30 +++ 7 files changed, 423 insertions(+), 3 deletions(-) create mode 100644 models/paraswap/zkevm/paraswap_v6_zkevm_trades.sql create mode 100644 models/paraswap/zkevm/paraswap_v6_zkevm_trades_decoded.sql create mode 100644 models/paraswap/zkevm/paraswap_zkevm_schema.yml create mode 100644 models/paraswap/zkevm/paraswap_zkevm_trades.sql create mode 100644 sources/paraswap/zkevm/paraswap_zkevm_sources.yml diff --git a/models/paraswap/paraswap_trades.sql b/models/paraswap/paraswap_trades.sql index 6c5d6cddccf..ebf43d066cb 100644 --- a/models/paraswap/paraswap_trades.sql +++ b/models/paraswap/paraswap_trades.sql @@ -1,7 +1,7 @@ {{ config( schema='paraswap', alias = 'trades', - post_hook='{{ expose_spells(\'["avalanche_c","fantom","arbitrum","bnb","ethereum","optimism","polygon","base"]\', + post_hook='{{ expose_spells(\'["avalanche_c","fantom","arbitrum","bnb","ethereum","optimism","polygon","base", "zkevm"]\', "project", "paraswap", \'["Henrystats","springzh"]\') }}' @@ -17,6 +17,7 @@ ref('paraswap_avalanche_c_trades') ,ref('paraswap_optimism_trades') ,ref('paraswap_polygon_trades') ,ref('paraswap_base_trades') +,ref('paraswap_zkevm_trades') ] %} diff --git a/models/paraswap/paraswap_trades_schema.yml b/models/paraswap/paraswap_trades_schema.yml index ce9241d035d..94cf8aad2c3 100644 --- a/models/paraswap/paraswap_trades_schema.yml +++ b/models/paraswap/paraswap_trades_schema.yml @@ -3,12 +3,12 @@ version: 2 models: - name: paraswap_trades meta: - blockchain: avalanche_c, fantom, arbitrum, bnb, ethereum, optimism, polygon + blockchain: avalanche_c, fantom, arbitrum, bnb, ethereum, optimism, polygon, zkevm sector: dex project: paraswap contributors: Henrystats, springzh config: - tags: ['avalanche_c', 'fantom', 'paraswap','trades', 'dex', 'aggregator', 'Henrystats','cross-chain', 'arbitrum', 'bnb', 'ethereum', 'optimism', 'polygon'] + tags: ['avalanche_c', 'fantom', 'paraswap','trades', 'dex', 'aggregator', 'Henrystats','cross-chain', 'arbitrum', 'bnb', 'ethereum', 'optimism', 'polygon', 'zkevm'] description: > Paraswap aggregator trades on all chains across all contracts and versions. This table will load dex trades downstream. columns: diff --git a/models/paraswap/zkevm/paraswap_v6_zkevm_trades.sql b/models/paraswap/zkevm/paraswap_v6_zkevm_trades.sql new file mode 100644 index 00000000000..84ad80f920f --- /dev/null +++ b/models/paraswap/zkevm/paraswap_v6_zkevm_trades.sql @@ -0,0 +1,135 @@ +{{ config( + schema = 'paraswap_v6_zkevm', + alias = 'trades', + partition_by = ['block_month'], + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'method', 'trace_address'], + post_hook='{{ expose_spells(\'["zkevm"]\', + "project", + "paraswap_v6", + \'["eptighte", "mwamedacen"]\') }}' + ) +}} + +{% set project_start_date = '2024-03-01' %} + +with dexs AS ( + SELECT + blockTime AS block_time, + blockNumber AS block_number, + from_hex(beneficiary) AS taker, + null AS maker, -- TODO: can parse from traces + receivedAmount AS token_bought_amount_raw, + fromAmount AS token_sold_amount_raw, + CAST(NULL AS double) AS amount_usd, + method, + CASE + WHEN from_hex(destToken) = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee + THEN 0x4f9a0e7fd2bf6067db6994cf12e4495df938e6e9 -- WETH + ELSE from_hex(destToken) + END AS token_bought_address, + CASE + WHEN from_hex(srcToken) = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee + THEN 0x4f9a0e7fd2bf6067db6994cf12e4495df938e6e9 -- WETH + ELSE from_hex(srcToken) + END AS token_sold_address, + projectContractAddress as project_contract_address, + txHash AS tx_hash, + callTraceAddress AS trace_address, + CAST(-1 as integer) AS evt_index + FROM {{ ref('paraswap_v6_zkevm_trades_decoded') }} + {% if is_incremental() %} + WHERE blockTime >= date_trunc('day', now() - interval '7' day) + {% endif %} +), + +price_missed_previous AS ( + SELECT minute, contract_address, decimals, symbol, price + FROM {{ source('prices', 'usd') }} + WHERE contract_address = 0x4f9a0e7fd2bf6067db6994cf12e4495df938e6e9 -- WETH + ORDER BY minute + LIMIT 1 +), + +-- WETH price may be missed for latest swaps +price_missed_next AS ( + SELECT minute, contract_address, decimals, symbol, price + FROM {{ source('prices', 'usd') }} + WHERE contract_address = 0x4f9a0e7fd2bf6067db6994cf12e4495df938e6e9 -- WETH + ORDER BY minute desc + LIMIT 1 +) + +SELECT 'zkevm' AS blockchain, + 'paraswap' AS project, + '6' AS version, + cast(date_trunc('day', d.block_time) as date) as block_date, + cast(date_trunc('month', d.block_time) as date) as block_month, + d.block_time, + method, + e1.symbol AS token_bought_symbol, + e2.symbol AS token_sold_symbol, + CASE + WHEN lower(e1.symbol) > lower(e2.symbol) THEN concat(e2.symbol, '-', e1.symbol) + ELSE concat(e1.symbol, '-', e2.symbol) + END AS token_pair, + d.token_bought_amount_raw / power(10, e1.decimals) AS token_bought_amount, + d.token_sold_amount_raw / power(10, e2.decimals) AS token_sold_amount, + d.token_bought_amount_raw, + d.token_sold_amount_raw, + coalesce( + d.amount_usd + ,(d.token_bought_amount_raw / power(10, e1.decimals)) * coalesce(p1.price, p_prev1.price, p_next1.price) + ,(d.token_sold_amount_raw / power(10, e2.decimals)) * coalesce(p2.price, p_prev2.price, p_next2.price) + ) AS amount_usd, + d.token_bought_address, + d.token_sold_address, + coalesce(d.taker, tx."from") AS taker, + coalesce(d.maker, tx."from") as maker, + d.project_contract_address, + d.tx_hash, + tx."from" AS tx_from, + tx.to AS tx_to, + d.trace_address, + d.evt_index +FROM dexs d +INNER JOIN {{ source('zkevm', 'transactions') }} tx ON d.tx_hash = tx.hash + AND d.block_number = tx.block_number + {% if not is_incremental() %} + AND tx.block_time >= TIMESTAMP '{{project_start_date}}' + {% endif %} + {% if is_incremental() %} + AND tx.block_time >= date_trunc('day', now() - interval '7' day) + {% endif %} +LEFT JOIN {{ source('tokens', 'erc20') }} e1 ON e1.contract_address = d.token_bought_address + AND e1.blockchain = 'zkevm' +LEFT JOIN {{ source('tokens', 'erc20') }} e2 on e2.contract_address = d.token_sold_address + AND e2.blockchain = 'zkevm' +LEFT JOIN {{ source('prices', 'usd') }} p1 ON p1.minute = date_trunc('minute', d.block_time) + AND p1.contract_address = d.token_bought_address + AND p1.blockchain = 'zkevm' + {% if not is_incremental() %} + AND p1.minute >= TIMESTAMP '{{project_start_date}}' + {% endif %} + {% if is_incremental() %} + AND p1.minute >= date_trunc('day', now() - interval '7' day) + {% endif %} +LEFT JOIN price_missed_previous p_prev1 ON d.token_bought_address = p_prev1.contract_address + AND d.block_time < p_prev1.minute -- Swap before first price record time +LEFT JOIN price_missed_next p_next1 ON d.token_bought_address = p_next1.contract_address + AND d.block_time > p_next1.minute -- Swap after last price record time +LEFT JOIN {{ source('prices', 'usd') }} p2 ON p2.minute = date_trunc('minute', d.block_time) + AND p2.contract_address = d.token_sold_address + AND p2.blockchain = 'zkevm' + {% if not is_incremental() %} + AND p2.minute >= TIMESTAMP '{{project_start_date}}' + {% endif %} + {% if is_incremental() %} + AND p2.minute >= date_trunc('day', now() - interval '7' day) + {% endif %} +LEFT JOIN price_missed_previous p_prev2 ON d.token_sold_address = p_prev2.contract_address + AND d.block_time < p_prev2.minute -- Swap before first price record time +LEFT JOIN price_missed_next p_next2 ON d.token_sold_address = p_next2.contract_address + AND d.block_time > p_next2.minute -- Swap after last price record time diff --git a/models/paraswap/zkevm/paraswap_v6_zkevm_trades_decoded.sql b/models/paraswap/zkevm/paraswap_v6_zkevm_trades_decoded.sql new file mode 100644 index 00000000000..3e334bde558 --- /dev/null +++ b/models/paraswap/zkevm/paraswap_v6_zkevm_trades_decoded.sql @@ -0,0 +1,12 @@ +{{ config( + schema = 'paraswap_v6_zkevm', + alias = 'trades_decoded', + partition_by = ['block_month'], + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['block_date', 'blockchain', 'project', 'version', 'txHash', 'method', 'callTraceAddress'] + ) +}} + +{{ paraswap_v6_trades_master('zkevm', 'paraswap', 'AugustusV6_1') }} \ No newline at end of file diff --git a/models/paraswap/zkevm/paraswap_zkevm_schema.yml b/models/paraswap/zkevm/paraswap_zkevm_schema.yml new file mode 100644 index 00000000000..d862ea2db3e --- /dev/null +++ b/models/paraswap/zkevm/paraswap_zkevm_schema.yml @@ -0,0 +1,195 @@ +version: 2 + +models: + - name: paraswap_v6_zkevm_trades + meta: + blockchain: zkevm + sector: dex + project: paraswap_v6 + contributors: eptighte, mwamedacen + config: + tags: ['zkevm','paraswap_v6','trades', 'paraswap','dex'] + description: > + Paraswap V6 contract aggregator trades on zkevm + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - block_date + - blockchain + - project + - version + - tx_hash + - method + - trace_address + - check_dex_aggregator_seed: + blockchain: zkevm + project: paraswap + version: 6 + columns: + - &blockchain + name: blockchain + description: "Blockchain which the DEX is deployed" + - &project + name: project + description: "Project name of the DEX" + - &version + name: version + description: "Version of the contract built and deployed by the DEX project" + - &block_date + name: block_date + description: "UTC event block date of each DEX trade" + - &block_time + name: block_time + description: "UTC event block time of each DEX trade" + - &token_bought_symbol + name: token_bought_symbol + description: "Token symbol for token bought in the transaction" + - &token_sold_symbol + name: token_sold_symbol + description: "Token symbol for token sold in the transaction" + - &token_pair + name: token_pair + description: "Token symbol pair for each token involved in the transaction" + - &token_bought_amount + name: token_bought_amount + description: "Value of the token bought at time of execution in the original currency" + - &token_sold_amount + name: token_sold_amount + description: "Value of the token sold at time of execution in the original currency" + - &token_bought_amount_raw + name: token_bought_amount_raw + description: "Raw value of the token bought at time of execution in the original currency" + - &token_sold_amount_raw + name: token_sold_amount_raw + description: "Raw value of the token sold at time of execution in the original currency" + - &amount_usd + name: amount_usd + description: "USD value of the trade at time of execution" + - &token_bought_address + name: token_bought_address + description: "Contract address of the token bought" + - &token_sold_address + name: token_sold_address + description: "Contract address of the token sold" + - &taker + name: taker + description: "Address of trader who purchased a token" + - &maker + name: maker + description: "Address of trader who sold a token" + - &project_contract_address + name: project_contract_address + description: "Project contract address which executed the trade on the blockchain" + - &tx_hash + name: tx_hash + description: "Unique transaction hash value tied to each transaction on the DEX" + - &tx_from + name: tx_from + description: "Address which initiated the transaction" + - &tx_to + name: tx_to + description: "Address which received the transaction" + - &trace_address + name: trace_address + description: "" + - &evt_index + name: evt_index + description: "" + - &method + name: method + description: "Method" + + - name: paraswap_zkevm_trades + meta: + blockchain: zkevm + sector: dex + project: paraswap + contributors: springzh + config: + tags: ['zkevm','dex','trades', 'paraswap'] + description: > + paraswap aggregator trades on zkevm across all contracts and versions. This table will load dex trades downstream. + columns: + - *blockchain + - *project + - *version + - *block_date + - *block_time + - *token_bought_symbol + - *token_sold_symbol + - *token_pair + - *token_bought_amount + - *token_sold_amount + - *token_bought_amount_raw + - *token_sold_amount_raw + - *amount_usd + - *token_bought_address + - *token_sold_address + - *taker + - *maker + - *project_contract_address + - *tx_hash + - *tx_from + - *tx_to + - *trace_address + - *evt_index + + - name: paraswap_v6_zkevm_trades_decoded + description: "Paraswap V6 trades decoded" + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - block_date + - blockchain + - project + - version + - txHash + - method + - callTraceAddress + columns: + - name: blockTime + description: "Block time" + - name: blockNumber + description: "Block number" + - name: txHash + description: "Transaction hash" + - name: projectContractAddress + description: "Project contract address" + - name: callTraceAddress + description: "Call trace address" + - name: srcToken + description: "Source token" + - name: destToken + description: "Destination token" + - name: fromAmount + description: "From amount" + - name: spentAmount + description: "Spent amount" + - name: toAmount + description: "To amount" + - name: quotedAmount + description: "Quoted amount" + - name: receivedAmount + description: "Received amount" + - name: metadata + description: "Metadata" + - name: beneficiary + description: "Beneficiary" + - name: method + description: "Method" + - name: side + description: "Side" + - name: feeCode + description: "Fee code" + - name: partnerShare + description: "Partner share" + - name: paraswapShare + description: "Paraswap share" + - name: partnerAddress + description: "Partner address" + - name: feeBps + description: "Fee in basis points" + - name: isReferral + description: "Is referral" + - name: isTakeSurplus + description: "Is take surplus" \ No newline at end of file diff --git a/models/paraswap/zkevm/paraswap_zkevm_trades.sql b/models/paraswap/zkevm/paraswap_zkevm_trades.sql new file mode 100644 index 00000000000..42189e688c4 --- /dev/null +++ b/models/paraswap/zkevm/paraswap_zkevm_trades.sql @@ -0,0 +1,47 @@ +{{ config( + schema='paraswap_zkevm', + alias = 'trades', + + ) +}} + +{% set paraswap_models = [ + ref('paraswap_v6_zkevm_trades') +] %} + + +SELECT * +FROM ( + {% for dex_model in paraswap_models %} + SELECT + blockchain, + project, + version, + block_month, + block_date, + block_time, + token_bought_symbol, + token_sold_symbol, + token_pair, + token_bought_amount, + token_sold_amount, + token_bought_amount_raw, + token_sold_amount_raw, + amount_usd, + token_bought_address, + token_sold_address, + taker, + maker, + project_contract_address, + tx_hash, + tx_from, + tx_to, + trace_address, + evt_index + FROM {{ dex_model }} + {% if not loop.last %} + UNION ALL + {% endif %} + {% endfor %} +) +; \ No newline at end of file diff --git a/sources/paraswap/zkevm/paraswap_zkevm_sources.yml b/sources/paraswap/zkevm/paraswap_zkevm_sources.yml new file mode 100644 index 00000000000..14b14523478 --- /dev/null +++ b/sources/paraswap/zkevm/paraswap_zkevm_sources.yml @@ -0,0 +1,30 @@ +version: 2 + +sources: + - name: paraswap_zkevm + freshness: + warn_after: { count: 12, period: hour } + description: > + Decoded event table for swaps on paraswap + tables: + # v6 + - name: AugustusV6_1_call_swapExactAmountIn + loaded_at_field: call_block_time + - name: AugustusV6_1_call_swapExactAmountInOnUniswapV2 + loaded_at_field: call_block_time + - name: AugustusV6_1_call_swapExactAmountInOnUniswapV3 + loaded_at_field: call_block_time + - name: AugustusV6_1_call_swapExactAmountInOnCurveV1 + loaded_at_field: call_block_time + - name: AugustusV6_1_call_swapExactAmountInOnCurveV2 + loaded_at_field: call_block_time + - name: AugustusV6_1_call_swapExactAmountInOnBalancerV2 + loaded_at_field: call_block_time + - name: AugustusV6_1_call_swapExactAmountOut + loaded_at_field: call_block_time + - name: AugustusV6_1_call_swapExactAmountOutOnUniswapV2 + loaded_at_field: call_block_time + - name: AugustusV6_1_call_swapExactAmountOutOnUniswapV3 + loaded_at_field: call_block_time + - name: AugustusV6_1_call_swapExactAmountOutOnBalancerV2 + loaded_at_field: call_block_time \ No newline at end of file