Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
104 changes: 95 additions & 9 deletions dbt_subprojects/dex/models/_projects/odos/avalanche_c/_schema.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
version: 2

models:
- name: odos_avalanche_c_trades
- name: odos_v2_avalanche_c_trades
meta:
blockchain: avalanche_c
sector: dex
project: odos
contributors: Henrystats
config:
tags: ['avalanche_c','trades', 'dfx','dex']
tags: ['avalanche_c','trades', 'odos','dex']
description: >
odos aggregator trades on avalanche_c
A table containing all trades of odos v2 on avalanche_c
data_tests:
- dbt_utils.unique_combination_of_columns:
arguments:
Expand All @@ -26,17 +26,20 @@ models:
arguments:
blockchain: avalanche_c
project: odos
version: 1
version: 2
columns:
- &blockchain
name: blockchain
description: "Blockchain which the DEX is deployed"
- &project
- &project
name: project
description: "Project name of the DEX"
description: "Project name of the DEX"
- &version
name: version
description: "Version of the contract built and deployed by the DEX project"
- &block_month
name: block_month
description: "UTC event block month of each DEX trade"
- &block_date
name: block_date
description: "UTC event block date of each DEX trade"
Expand Down Expand Up @@ -91,9 +94,92 @@ models:
- &tx_to
name: tx_to
description: "Address which received the transaction"
- &trace_address
name: trace_address
description: ""
- &evt_index
name: evt_index
description: ""
- &trace_address
name: trace_address
description: ""

- name: odos_v1_avalanche_c_trades
meta:
blockchain: avalanche_c
sector: dex
project: odos
contributors: Henrystats
config:
tags: ['avalanche_c','trades', 'odos','dex']
description: >
A table containing all trades of odos v1 on avalanche_c
data_tests:
- dbt_utils.unique_combination_of_columns:
arguments:
combination_of_columns:
- block_date
- blockchain
- project
- version
- tx_hash
- evt_index
- trace_address
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_avalanche_c_trades
meta:
blockchain: avalanche_c
sector: dex
project: odos
contributors: Henrystats
config:
tags: ['avalanche_c','trades', 'odos','dex']
description: >
A table containing all trades of odos on avalanche_c.
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
Original file line number Diff line number Diff line change
@@ -1,127 +1,46 @@
{{ config(
tags=['prod_exclude'],
schema = 'odos_avalanche_c',
alias = 'trades',
partition_by = ['block_date'],
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index', 'trace_address']
, post_hook='{{ hide_spells() }}'
)
schema = 'odos_avalanche_c',
alias = 'trades'
, post_hook='{{ hide_spells() }}'
)
}}

/*
note: this spell has not been migrated to dunesql, as there are duplicates issues and issue is not resolved
please migrate to dunesql & fix duplicates to ensure up-to-date logic & data
*/
{% set odos_models = [
ref('odos_v1_avalanche_c_trades'),
ref('odos_v2_avalanche_c_trades')
] %}


{% set project_start_date = '2022-11-29' %}

WITH

dexs_raw as (
SELECT
evt_block_time as block_time,
explode(outputs) as data_value,
'' as maker,
CAST(amountsIn[0] as double) as token_sold_amount_raw,
CAST(amountsOut[0] as double) as token_bought_amount_raw,
CAST(NULL as double) as amount_usd,
CASE
WHEN CAST(tokensIn[0] as string) IN ('0', 'O', '0x0000000000000000000000000000000000000000')
THEN '0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7' -- WAVAX
ELSE CAST(tokensIn[0] as string)
END as token_sold_address,
contract_address as project_contract_address,
evt_tx_hash as tx_hash,
CAST(ARRAY() as array<bigint>) AS trace_address,
evt_index
FROM
{{ source('odos_avalanche_c', 'OdosRouter_evt_Swapped') }}
{% if is_incremental() %}
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
{% endif %}
),

dexs as (
SELECT
*,
CAST(data_value:receiver as string) as taker,
CASE
WHEN CAST(data_value:tokenAddress as string) IN ('0', 'O', '0x0000000000000000000000000000000000000000')
THEN '0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7' -- WAVAX
ELSE CAST(data_value:tokenAddress as string)
END as token_bought_address
FROM
dexs_raw
)

SELECT
'avalanche_c' as blockchain,
'odos' as project,
'1' as version,
TRY_CAST(date_trunc('DAY', dexs.block_time) as date) as block_date,
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,
CAST(dexs.token_bought_amount_raw AS DECIMAL(38,0)) AS token_bought_amount_raw,
CAST(dexs.token_sold_amount_raw AS DECIMAL(38,0)) AS token_sold_amount_raw,
COALESCE(
dexs.amount_usd,
(dexs.token_bought_amount_raw / power(10, p_bought.decimals)) * p_bought.price,
(dexs.token_sold_amount_raw / power(10, p_sold.decimals)) * p_sold.price
) as amount_usd,
dexs.token_bought_address,
dexs.token_sold_address,
COALESCE(dexs.taker, tx.from) as taker, -- subqueries rely on this COALESCE to avoid redundant joins with the transactions table
dexs.maker,
dexs.project_contract_address,
dexs.tx_hash,
tx.from as tx_from,
tx.to AS tx_to,
dexs.trace_address,
dexs.evt_index
FROM dexs
INNER JOIN {{ source('avalanche_c', 'transactions') }} tx
ON tx.hash = dexs.tx_hash
{% if not is_incremental() %}
AND tx.block_time >= '{{project_start_date}}'
select *
from (
{% for aggregator_model in odos_models %}
select
blockchain
, project
, version
, block_date
, block_month
, 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 {{ aggregator_model }}
{% if not loop.last %}
union all
{% endif %}
{% if is_incremental() %}
AND tx.block_time >= date_trunc("day", now() - interval '1 week')
{% endif %}
LEFT JOIN {{ source('tokens', 'erc20') }} erc20a
ON erc20a.contract_address = dexs.token_bought_address
AND erc20a.blockchain = 'avalanche_c'
LEFT JOIN {{ source('tokens', 'erc20') }} erc20b
ON erc20b.contract_address = dexs.token_sold_address
AND erc20b.blockchain = 'avalanche_c'
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 = 'avalanche_c'
{% if not is_incremental() %}
AND p_bought.minute >= '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
AND p_bought.minute >= date_trunc("day", now() - interval '1 week')
{% 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 = 'avalanche_c'
{% if not is_incremental() %}
AND p_sold.minute >= '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
AND p_sold.minute >= date_trunc("day", now() - interval '1 week')
{% endif %}
;
{% endfor %}
)
Loading
Loading