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 poly, arb, and bnb xchange dex trades models and include xchange trades in dex.trades #3576

Merged
merged 3 commits into from
Jun 20, 2023
Merged
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
12 changes: 6 additions & 6 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,16 @@ models:

xchange:
+schema: xchange
#arbitrum:
# +schema: xchange_arbitrum
arbitrum:
+schema: xchange_arbitrum
ethereum:
+schema: xchange_ethereum
#optimism:
# +schema: xchange_optimism
#polygon:
# +schema: xchange_polygon
#bnb:
# +schema: xchange_bnb
polygon:
+schema: xchange_polygon
bnb:
+schema: xchange_bnb

uniswap:
+schema: uniswap
Expand Down
3 changes: 2 additions & 1 deletion models/dex/dex_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
post_hook='{{ expose_spells(\'["ethereum", "bnb", "avalanche_c", "gnosis", "optimism", "arbitrum", "fantom", "polygon"]\',
"sector",
"dex",
\'["jeff-dude", "hosuke", "0xRob", "pandajackson42", "Henrystats", "scoffie", "zhongyiio", "justabi", "umer_h_adil", "mtitus6", "dbustos20", "tian7", "bh2smith", "rantum"]\') }}'
\'["jeff-dude", "hosuke", "0xRob", "pandajackson42", "Henrystats", "scoffie", "zhongyiio", "justabi", "umer_h_adil", "mtitus6", "dbustos20", "tian7", "bh2smith", "rantum", "mike-x7f"]\') }}'
)
}}

Expand Down Expand Up @@ -64,6 +64,7 @@
,ref('thena_trades')
,ref('camelot_trades')
,ref('zeroex_native_trades')
,ref('xchange_trades')
] %}


Expand Down
1 change: 1 addition & 0 deletions models/tokens/arbitrum/tokens_arbitrum_erc20.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,5 @@ FROM (VALUES
,('0x088cd8f5eF3652623c22D48b1605DCfE860Cd704', 'VELA', 18)
,('0xd77b108d4f6cefaa0cae9506a934e825becca46e', 'WINR', 18)
,('0xd4d026322c88c2d49942a75dff920fcfbc5614c1', 'OLE', 18)
,('0xdd69db25f6d620a7bad3023c5d32761d353d3de9', 'GETH', 18)
) AS temp_table (contract_address, symbol, decimals)
1 change: 1 addition & 0 deletions models/tokens/bnb/tokens_bnb_bep20.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5534,4 +5534,5 @@ SELECT LOWER(contract_address) AS contract_address, symbol, decimals
,('0xbdeae1ca48894a1759a8374d63925f21f2ee2639', 'EDU', 18)
,('0xa2e3356610840701bdf5611a53974510ae27e2e1', 'wBETH', 18)
,('0xf7de7e8a6bd59ed41a4b5fe50278b3b7f31384df', 'RDNT', 18)
,('0xc7d31a6d7aa357066c37bf383e4a7028083a46d4', 'XPEPE', 18)
) AS temp_table (contract_address, symbol, decimals)
1 change: 1 addition & 0 deletions models/tokens/polygon/tokens_polygon_erc20.sql
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,5 @@ SELECT LOWER(contract_address) AS contract_address, symbol, decimals
,('0x4d8be0203962716f7e92484ddbee3af2b503972f' ,'test' ,9)
,('0xe2995c68a67202d4b5aa01b01c6b4bf319b49d43' ,'SS' ,9)
,('0x30e1cd47b5cbc7568c3339c7b288098be467f662' ,'SBx' ,18)
,('0xa8318cb82cd01ed1b552ce03ea7099900178355f' ,'XHELLOWORLD' ,18)
) AS temp_table (contract_address, symbol, decimals)
97 changes: 97 additions & 0 deletions models/xchange/arbitrum/xchange_arbitrum_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
version: 2

models:
- name: xchange_arbitrum_trades
meta:
blockchain: arbitrum
sector: dex
project: xchange
contributors: mike-x7f
config:
tags: ['arbitrum','xchange','x7finance','trades','dex']
description: >
Xchange contract trades on arbitrum
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_date
- blockchain
- project
- version
- tx_hash
- evt_index
- trace_address
- check_dex_seed:
blockchain: arbitrum
project: xchange
version: 1
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: ""
58 changes: 58 additions & 0 deletions models/xchange/arbitrum/xchange_arbitrum_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
version: 2

sources:
- name: xchange_arbitrum
description: "Arbitrum decoded tables related to xchange contract"
freshness:
warn_after: { count: 12, period: hour }
tables:
- name: XchangePair_evt_Swap
loaded_at_field: evt_block_time
description: "" # to-do
columns:
- &amount0In
name: amount0In
- &amount0Out
name: amount0Out
- &amount1In
name: amount1In
- &amount1Out
name: amount1Out
- &contract_address
name: contract_address
description: "Arbitrum address for the liquidity pool used in transaction"
- &evt_block_time
name: evt_block_time
description: "Timestamp for block event time in UTC"
- &evt_index
name: evt_index
description: "Index value of the transaction"
- &evt_tx_hash
name: evt_tx_hash
description: "Primary key of the transaction"
tests:
- not_null
- &evt_block_number
name: evt_block_number
description: "Block number which processed the unique transaction hash"
- &sender
name: sender
- &to
name: to
- name: XchangeFactory_evt_PairCreated
loaded_at_field: evt_block_time
description: "" # to-do
columns:
- &_0
name: _0
- *contract_address
- *evt_block_number
- *evt_block_time
- *evt_index
- *evt_tx_hash
- &pair
name: pair
- &token0
name: token0
- &token1
name: token1
109 changes: 109 additions & 0 deletions models/xchange/arbitrum/xchange_arbitrum_trades.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{{ config(
schema = 'xchange_arbitrum'
,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='{{ expose_spells(\'["arbitrum"]\',
"project",
"xchange",
\'["mike-x7f"]\') }}'
)
}}

{% set project_start_date = '2023-05-31' %}

with dexs as (
-- Xchange
SELECT
t.evt_block_time as block_time,
t.to as taker,
'' as maker,
case when amount0Out = 0 then amount1Out else amount0Out end as token_bought_amount_raw,
case when amount0In = 0 then amount1In else amount0In end as token_sold_amount_raw,
null as amount_usd,
case when amount0Out = 0 then f.token1 else f.token0 end as token_bought_address,
case when amount0In = 0 then f.token1 else f.token0 end as token_sold_address,
t.contract_address as project_contract_address,
t.evt_tx_hash as tx_hash,
'' as trace_address,
t.evt_index
FROM
{{ source('xchange_arbitrum', 'XchangePair_evt_Swap') }} t
inner join {{ source('xchange_arbitrum', 'XchangeFactory_evt_PairCreated') }} f
on f.pair = t.contract_address
{% if is_incremental() %}
WHERE t.evt_block_time >= date_trunc("day", now() - interval '1 week')
{% else %}
WHERE t.evt_block_time >= '{{ project_start_date }}'
{% endif %}
)
select
'arbitrum' as blockchain,
'xchange' 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,
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('arbitrum', 'transactions') }} tx
on dexs.tx_hash = tx.hash
{% if not is_incremental() %}
and tx.block_time >= '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
and tx.block_time >= date_trunc("day", now() - interval '1 week')
{% endif %}
left join {{ ref('tokens_erc20') }} erc20a
on erc20a.contract_address = dexs.token_bought_address
and erc20a.blockchain = 'arbitrum'
left join {{ ref('tokens_erc20') }} erc20b
on erc20b.contract_address = dexs.token_sold_address
and erc20b.blockchain = 'arbitrum'
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 = 'arbitrum'
{% 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 = 'arbitrum'
{% 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 %}

Loading