diff --git a/models/odos/optimism/odos_optimism_schema.yml b/models/odos/optimism/odos_optimism_schema.yml index dcb4eb95292..1f2c01d374f 100644 --- a/models/odos/optimism/odos_optimism_schema.yml +++ b/models/odos/optimism/odos_optimism_schema.yml @@ -1,7 +1,7 @@ version: 2 models: - - name: odos_optimism_trades + - name: odos_v1_optimism_trades meta: blockchain: optimism sector: dex @@ -10,7 +10,7 @@ models: config: tags: ['optimism','trades', 'odos','dex'] description: > - odos dex trades on optimism + A table containing all trades of odos v1 on optimism tests: - dbt_utils.unique_combination_of_columns: combination_of_columns: @@ -20,6 +20,7 @@ models: - version - tx_hash - evt_index + - trace_address - check_dex_aggregator_seed: blockchain: optimism project: odos @@ -96,4 +97,90 @@ models: description: "" - &trace_address name: trace_address - description: "" \ No newline at end of file + description: "" + + - name: odos_v2_optimism_trades + meta: + blockchain: optimism + sector: dex + project: odos + contributors: ARDev097 + config: + tags: ['optimism','trades', 'odos','dex'] + description: > + A table containing all trades of odos v2 on optimism + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - block_date + - blockchain + - project + - version + - tx_hash + - evt_index + - trace_address + - check_dex_aggregator_seed: + blockchain: optimism + project: odos + version: 2 + columns: + - *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 + - *evt_index + - *trace_address + + - name: odos_optimism_trades + meta: + blockchain: optimism + sector: dex + project: odos + contributors: ARDev097 + config: + tags: ['optimism','trades', 'odos','dex'] + description: > + A table containing all trades of odos on optimism. + columns: + - *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 + - *evt_index + - *trace_address \ No newline at end of file diff --git a/models/odos/optimism/odos_optimism_trades.sql b/models/odos/optimism/odos_optimism_trades.sql index f64cb568368..be802ef47a6 100644 --- a/models/odos/optimism/odos_optimism_trades.sql +++ b/models/odos/optimism/odos_optimism_trades.sql @@ -1,103 +1,46 @@ {{ config( - schema = 'odos_optimism' - ,alias = 'trades' - ,materialized = 'incremental' - ,file_format = 'delta' - ,incremental_strategy = 'merge' - ,unique_key = ['tx_hash', 'evt_index'] - ,incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] - ) + schema = 'odos_optimism' + , alias = 'trades' + ) }} -{% set project_start_date = '2022-10-06' %} +{% set odos_models = [ +ref('odos_v1_optimism_trades') +, ref('odos_v2_optimism_trades') +] %} -WITH -dexs as ( - SELECT - evt_block_time as block_time, - evt_block_number as block_number, - json_extract_scalar(outputs[1], '$.receiver') AS taker, - CAST(NULL as VARBINARY) as maker, - amountsIn[1] as token_sold_amount_raw, - amountsOut[1] as token_bought_amount_raw, - CAST(NULL as double) as amount_usd, - tokensIn[1] as token_sold_address, - json_extract_scalar(outputs[1], '$.tokenAddress') AS token_bought_address, - contract_address as project_contract_address, - evt_tx_hash as tx_hash, - evt_index, - array[-1] as trace_address - FROM - {{ source('odos_optimism', 'OdosRouter_evt_Swapped') }} - {% if is_incremental() %} - WHERE {{incremental_predicate('evt_block_time')}} - {% else %} - WHERE evt_block_time >= TIMESTAMP '{{project_start_date}}' - {% endif %} -) -SELECT - 'optimism' AS blockchain, - 'odos' AS project, - '1' AS version, - TRY_CAST(date_trunc('DAY', dexs.block_time) AS date) AS block_date, - TRY_CAST(date_trunc('MONTH', dexs.block_time) AS date) AS block_month, - dexs.block_time, - erc20a.symbol AS token_bought_symbol, - erc20b.symbol AS token_sold_symbol, - CASE - WHEN lower(erc20a.symbol) > lower(erc20b.symbol) - THEN concat(erc20b.symbol, '-', erc20a.symbol) - ELSE concat(erc20a.symbol, '-', erc20b.symbol) - END AS token_pair, - dexs.token_bought_amount_raw / power(10, erc20a.decimals) AS token_bought_amount, - dexs.token_sold_amount_raw / power(10, erc20b.decimals) AS token_sold_amount, - dexs.token_bought_amount_raw AS token_bought_amount_raw, - dexs.token_sold_amount_raw AS token_sold_amount_raw, - COALESCE( - dexs.amount_usd, - (dexs.token_bought_amount_raw / power(10, erc20a.decimals)) * p_bought.price, - (dexs.token_sold_amount_raw / power(10, erc20b.decimals)) * p_sold.price - ) AS amount_usd, - CAST(dexs.token_bought_address AS varbinary) AS token_bought_address, - dexs.token_sold_address, - CAST(dexs.taker AS varbinary) AS taker, - dexs.maker, - dexs.project_contract_address, - dexs.tx_hash, - tx."from" AS tx_from, - tx.to AS tx_to, - dexs.evt_index, - dexs.trace_address -FROM dexs -INNER JOIN {{ source('optimism', 'transactions') }} tx - ON dexs.tx_hash = tx.hash - {% if not is_incremental() %} - AND tx.block_time >= DATE '{{project_start_date}}' - {% else %} - AND {{ incremental_predicate('tx.block_time') }} - {% endif %} -LEFT JOIN {{ source('tokens', 'erc20') }} erc20a - ON CAST(erc20a.contract_address AS varchar) = dexs.token_bought_address - AND erc20a.blockchain = 'optimism' -LEFT JOIN {{ source('tokens', 'erc20') }} erc20b - ON erc20b.contract_address = dexs.token_sold_address - AND erc20b.blockchain = 'optimism' -LEFT JOIN {{ source('prices', 'usd') }} p_bought - ON p_bought.minute = date_trunc('minute', dexs.block_time) - AND CAST(p_bought.contract_address AS varchar) = dexs.token_bought_address - AND p_bought.blockchain = 'optimism' - {% if not is_incremental() %} - AND p_bought.minute >= DATE '{{project_start_date}}' - {% else %} - AND {{ incremental_predicate('p_bought.minute') }} - {% endif %} -LEFT JOIN {{ source('prices', 'usd') }} p_sold - ON p_sold.minute = date_trunc('minute', dexs.block_time) - AND p_sold.contract_address = dexs.token_sold_address - AND p_sold.blockchain = 'optimism' - {% if not is_incremental() %} - AND p_sold.minute >= DATE '{{project_start_date}}' - {% else %} - AND {{ incremental_predicate('p_sold.minute') }} - {% endif %} +SELECT * +FROM ( + {% for dex_model in odos_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, + evt_index, + trace_address + FROM {{ dex_model }} + {% if not loop.last %} + UNION ALL + {% endif %} + {% endfor %} +) \ No newline at end of file diff --git a/models/odos/optimism/odos_v1_optimism_trades.sql b/models/odos/optimism/odos_v1_optimism_trades.sql new file mode 100644 index 00000000000..f541672ab79 --- /dev/null +++ b/models/odos/optimism/odos_v1_optimism_trades.sql @@ -0,0 +1,103 @@ +{{ config( + schema = 'odos_v1_optimism' + ,alias = 'trades' + ,materialized = 'incremental' + ,file_format = 'delta' + ,incremental_strategy = 'merge' + ,unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index', 'trace_address'] + ,incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +{% set project_start_date = '2022-10-06' %} + +WITH +dexs as ( + SELECT + evt_block_time as block_time, + evt_block_number as block_number, + json_extract_scalar(outputs[1], '$.receiver') AS taker, + CAST(NULL as VARBINARY) as maker, + amountsIn[1] as token_sold_amount_raw, + amountsOut[1] as token_bought_amount_raw, + CAST(NULL as double) as amount_usd, + tokensIn[1] as token_sold_address, + json_extract_scalar(outputs[1], '$.tokenAddress') AS token_bought_address, + contract_address as project_contract_address, + evt_tx_hash as tx_hash, + evt_index, + array[-1] as trace_address + FROM + {{ source('odos_optimism', 'OdosRouter_evt_Swapped') }} + {% if is_incremental() %} + WHERE {{incremental_predicate('evt_block_time')}} + {% else %} + WHERE evt_block_time >= TIMESTAMP '{{project_start_date}}' + {% endif %} +) + +SELECT + 'optimism' AS blockchain, + 'odos' AS project, + '1' AS version, + TRY_CAST(date_trunc('DAY', dexs.block_time) AS date) AS block_date, + TRY_CAST(date_trunc('MONTH', dexs.block_time) AS date) AS block_month, + dexs.block_time, + erc20a.symbol AS token_bought_symbol, + erc20b.symbol AS token_sold_symbol, + CASE + WHEN lower(erc20a.symbol) > lower(erc20b.symbol) + THEN concat(erc20b.symbol, '-', erc20a.symbol) + ELSE concat(erc20a.symbol, '-', erc20b.symbol) + END AS token_pair, + dexs.token_bought_amount_raw / power(10, erc20a.decimals) AS token_bought_amount, + dexs.token_sold_amount_raw / power(10, erc20b.decimals) AS token_sold_amount, + dexs.token_bought_amount_raw AS token_bought_amount_raw, + dexs.token_sold_amount_raw AS token_sold_amount_raw, + COALESCE( + dexs.amount_usd, + (dexs.token_bought_amount_raw / power(10, erc20a.decimals)) * p_bought.price, + (dexs.token_sold_amount_raw / power(10, erc20b.decimals)) * p_sold.price + ) AS amount_usd, + CAST(dexs.token_bought_address AS varbinary) AS token_bought_address, + dexs.token_sold_address, + CAST(dexs.taker AS varbinary) AS taker, + dexs.maker, + dexs.project_contract_address, + dexs.tx_hash, + tx."from" AS tx_from, + tx.to AS tx_to, + dexs.evt_index, + dexs.trace_address +FROM dexs +INNER JOIN {{ source('optimism', 'transactions') }} tx + ON dexs.tx_hash = tx.hash + {% if not is_incremental() %} + AND tx.block_time >= DATE '{{project_start_date}}' + {% else %} + AND {{ incremental_predicate('tx.block_time') }} + {% endif %} +LEFT JOIN {{ source('tokens', 'erc20') }} erc20a + ON CAST(erc20a.contract_address AS varchar) = dexs.token_bought_address + AND erc20a.blockchain = 'optimism' +LEFT JOIN {{ source('tokens', 'erc20') }} erc20b + ON erc20b.contract_address = dexs.token_sold_address + AND erc20b.blockchain = 'optimism' +LEFT JOIN {{ source('prices', 'usd') }} p_bought + ON p_bought.minute = date_trunc('minute', dexs.block_time) + AND CAST(p_bought.contract_address AS varchar) = dexs.token_bought_address + AND p_bought.blockchain = 'optimism' + {% if not is_incremental() %} + AND p_bought.minute >= DATE '{{project_start_date}}' + {% else %} + AND {{ incremental_predicate('p_bought.minute') }} + {% endif %} +LEFT JOIN {{ source('prices', 'usd') }} p_sold + ON p_sold.minute = date_trunc('minute', dexs.block_time) + AND p_sold.contract_address = dexs.token_sold_address + AND p_sold.blockchain = 'optimism' + {% if not is_incremental() %} + AND p_sold.minute >= DATE '{{project_start_date}}' + {% else %} + AND {{ incremental_predicate('p_sold.minute') }} + {% endif %} diff --git a/models/odos/optimism/odos_v2_optimism_trades.sql b/models/odos/optimism/odos_v2_optimism_trades.sql new file mode 100644 index 00000000000..028a23c55d9 --- /dev/null +++ b/models/odos/optimism/odos_v2_optimism_trades.sql @@ -0,0 +1,102 @@ +{{ config( + alias = 'trades' + ,schema = 'odos_v2_optimism' + ,materialized = 'incremental' + ,file_format = 'delta' + ,incremental_strategy = 'merge' + ,unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index', 'trace_address'] + ,incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +{% set project_start_date = '2023-07-28' %} + +with dexs AS ( + SELECT + evt_block_number AS block_number, + evt_block_time AS block_time, + sender AS taker, + contract_address AS maker, + CASE WHEN inputAmount < INT256 '0' THEN abs(inputAmount) ELSE abs(amountOut) END AS token_bought_amount_raw, + CASE WHEN inputAmount < INT256 '0' THEN abs(amountOut) ELSE abs(inputAmount) END AS token_sold_amount_raw, + CAST(NULL as double) as amount_usd, + CASE WHEN inputAmount < INT256 '0' THEN inputToken ELSE outputToken END AS token_bought_address, + CASE WHEN inputAmount < INT256 '0' THEN outputToken ELSE inputToken END AS token_sold_address, + contract_address AS project_contract_address, + evt_tx_hash AS tx_hash, + evt_index, + array[-1] as trace_address + FROM + {{ source('odos_v2_optimism', 'OdosRouterV2_evt_Swap') }} + {% if is_incremental() %} + WHERE {{incremental_predicate('evt_block_time')}} + {% else %} + WHERE evt_block_time >= TIMESTAMP '{{project_start_date}}' + {% endif %} +) + +SELECT + 'optimism' AS blockchain, + 'odos' AS project, + '2' AS version, + TRY_CAST(date_trunc('DAY', dexs.block_time) AS date) AS block_date, + TRY_CAST(date_trunc('MONTH', dexs.block_time) AS date) AS block_month, + dexs.block_time, + erc20a.symbol AS token_bought_symbol, + erc20b.symbol AS token_sold_symbol, + CASE + WHEN lower(erc20a.symbol) > lower(erc20b.symbol) + THEN concat(erc20b.symbol, '-', erc20a.symbol) + ELSE concat(erc20a.symbol, '-', erc20b.symbol) + END AS token_pair, + dexs.token_bought_amount_raw / power(10, erc20a.decimals) AS token_bought_amount, + dexs.token_sold_amount_raw / power(10, erc20b.decimals) AS token_sold_amount, + dexs.token_bought_amount_raw AS token_bought_amount_raw, + dexs.token_sold_amount_raw AS token_sold_amount_raw, + COALESCE( + dexs.amount_usd, + (dexs.token_bought_amount_raw / power(10, erc20a.decimals)) * p_bought.price, + (dexs.token_sold_amount_raw / power(10, erc20b.decimals)) * p_sold.price + ) AS amount_usd, + CAST(dexs.token_bought_address AS varbinary) AS token_bought_address, + dexs.token_sold_address, + CAST(dexs.taker AS varbinary) AS taker, + dexs.maker, + dexs.project_contract_address, + dexs.tx_hash, + tx."from" AS tx_from, + tx.to AS tx_to, + dexs.evt_index, + dexs.trace_address +FROM dexs +INNER JOIN {{ source('optimism', 'transactions') }} tx + ON dexs.tx_hash = tx.hash + {% if not is_incremental() %} + AND tx.block_time >= DATE '{{project_start_date}}' + {% else %} + AND {{ incremental_predicate('tx.block_time') }} + {% endif %} +LEFT JOIN {{ source('tokens', 'erc20') }} erc20a + ON erc20a.contract_address = dexs.token_bought_address + AND erc20a.blockchain = 'optimism' +LEFT JOIN {{ source('tokens', 'erc20') }} erc20b + ON erc20b.contract_address = dexs.token_sold_address + AND erc20b.blockchain = 'optimism' +LEFT JOIN {{ source('prices', 'usd') }} p_bought + ON p_bought.minute = date_trunc('minute', dexs.block_time) + AND p_bought.contract_address = dexs.token_bought_address + AND p_bought.blockchain = 'optimism' + {% if not is_incremental() %} + AND p_bought.minute >= DATE '{{project_start_date}}' + {% else %} + AND {{ incremental_predicate('p_bought.minute') }} + {% endif %} +LEFT JOIN {{ source('prices', 'usd') }} p_sold + ON p_sold.minute = date_trunc('minute', dexs.block_time) + AND p_sold.contract_address = dexs.token_sold_address + AND p_sold.blockchain = 'optimism' + {% if not is_incremental() %} + AND p_sold.minute >= DATE '{{project_start_date}}' + {% else %} + AND {{ incremental_predicate('p_sold.minute') }} + {% endif %} \ No newline at end of file diff --git a/seeds/dex/aggregator/dex_aggregator_seed.csv b/seeds/dex/aggregator/dex_aggregator_seed.csv index 374b79820ee..5f08f53a3fa 100644 --- a/seeds/dex/aggregator/dex_aggregator_seed.csv +++ b/seeds/dex/aggregator/dex_aggregator_seed.csv @@ -129,4 +129,6 @@ polygon,bebop,jam,2023-12-26,0x2d557f42bc297c4a80953ab411b0215332a479f9c8965a94c arbitrum,bebop,jam,2023-12-22,0x9af150d67bf4a45f431eaea2b31c45b545caf142b4e407e66f84893bd644e5f7,15,"0,0",0x11cdb42b0eb46d95f990bedd4695a6e3fa034978,369.4936753177027,0xda10009cbd5d07dd0cecc66161fc93d7c9000da1,224.0498860609381 ethereum,bebop,jam,2023-12-11,0x8487dba2850c0dc4e9760eaf96d1912e13a0c1dc5632dc82d1a8fa362e853cce,646,"0,0",0x6b175474e89094c44da98b954eedeac495271d0f,42.825602116243985,0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48,82.019954 optimism,odos,1,2023-01-06,0x34bdb3f700f86d466b89ee75af6b59ca793f9710be1f17a7209d39a73edebc68,21,-1,0x307837663563373634636263313466393636396238383833376361313439306363613137633331363037,203.102916,0x4200000000000000000000000000000000000006,0.16 -optimism,odos,1,2023-01-06,0xc9ef2bd31fb060d19ac98860fbff8d8a8916222db4ab91d96410ceff44c3d5b7,9,-1,0x307864613130303039636264356430376464306365636336363136316663393364376339303030646131,11.51720714468193,0x7f5c764cbc14f9669b88837ca1490cca17c31607,11.54 \ No newline at end of file +optimism,odos,1,2023-01-06,0xc9ef2bd31fb060d19ac98860fbff8d8a8916222db4ab91d96410ceff44c3d5b7,9,-1,0x307864613130303039636264356430376464306365636336363136316663393364376339303030646131,11.51720714468193,0x7f5c764cbc14f9669b88837ca1490cca17c31607,11.54 +optimism,odos,2,2023-09-12,0x0499c7b804af5cdc6abaf6f87231c2845aa9952efee194c8e352f0458ff93c45,52,-1,0x7f5c764cbc14f9669b88837ca1490cca17c31607,0.800767,0x0b2c639c533813f4aa9d7837caf62653d097ff85,0.801448 +optimism,odos,2,2024-01-03,0xdc2104faf071a2cd7200bec603cdd26623da73d7d44aea84b9cf088a3417ff2c,133,-1,0x8c6f28f2f1a3c87f0f938b96d27520d9751ec8d9,28320.33619113824,0x94b008aa00579c1307b0ef2c499ad98a8ce58e58,27998.94 \ No newline at end of file diff --git a/sources/odos/optimism/odos_optimism_sources.yml b/sources/odos/optimism/odos_optimism_sources.yml index 81fd935df6c..35e6182e569 100644 --- a/sources/odos/optimism/odos_optimism_sources.yml +++ b/sources/odos/optimism/odos_optimism_sources.yml @@ -8,4 +8,12 @@ sources: Decoded event table for swaps on odos tables: - name: OdosRouter_evt_Swapped - loaded_at_field: evt_block_time \ No newline at end of file + loaded_at_field: evt_block_time + + - name: odos_v2_optimism + freshness: + warn_after: { count: 12, period: hour } + description: > + Decoded event table for swaps on odos_v2 + tables: + - name: OdosRouterV2_evt_Swap \ No newline at end of file