forked from duneanalytics/spellbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ef2b14
commit 5c36687
Showing
7 changed files
with
423 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
12 changes: 12 additions & 0 deletions
12
models/paraswap/zkevm/paraswap_v6_zkevm_trades_decoded.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
Oops, something went wrong.