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

Upgrade transfers_enrich macro and benchmark prices.hour #7077

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 17 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
18 changes: 9 additions & 9 deletions dbt_subprojects/tokens/macros/transfers/transfers_enrich.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ END
, prices_model = null
, evms_info_model = null
, transfers_start_date = '2000-01-01'
, transfers_end_date = '9999-12-31'
, blockchain = null
, usd_amount_threshold = 25000000000
)
Expand All @@ -23,10 +24,11 @@ WITH base_transfers as (
*
FROM
{{ base_transfers }}
{% if is_incremental() %}
WHERE
{{ incremental_predicate('block_date') }}
{% endif %}
block_date BETWEEN DATE '{{ transfers_start_date }}' AND DATE '{{ transfers_end_date }}'
{% if is_incremental() %}
AND {{ incremental_predicate('block_date') }}
{% endif %}
)
, prices AS (
SELECT
Expand All @@ -38,13 +40,11 @@ WITH base_transfers as (
, price
FROM
{{ prices_model }}
{% if is_incremental() %}
WHERE
{{ incremental_predicate('minute') }}
{% else %}
WHERE
minute >= TIMESTAMP '{{ transfers_start_date }}'
{% endif %}
minute BETWEEN TIMESTAMP '{{ transfers_start_date }}' AND TIMESTAMP '{{ transfers_end_date }}'
{% if is_incremental() %}
AND {{ incremental_predicate('minute') }}
{% endif %}
)
, transfers as (
SELECT
Expand Down
117 changes: 117 additions & 0 deletions dbt_subprojects/tokens/macros/transfers/transfers_enrich_beta.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{% macro transfers_enrich_beta(
base_transfers = null
, tokens_erc20_model = null
, prices_model = null
, evms_info_model = null
, transfers_start_date = '2000-01-01'
, transfers_end_date = '9999-12-31'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding transfers_end_date param for benchmark and testing.

, blockchain = null
, usd_amount_threshold = 25000000000
)
%}

WITH base_transfers as (
SELECT
*
FROM
{{ base_transfers }}
WHERE
block_date BETWEEN DATE '{{ transfers_start_date }}' AND DATE '{{ transfers_end_date }}'
{% if is_incremental() %}
AND {{ incremental_predicate('block_date') }}
{% endif %}
)
, prices AS (
SELECT
timestamp
, blockchain
, contract_address
, decimals
, symbol
, price
FROM
{{ prices_model }}
WHERE
timestamp BETWEEN TIMESTAMP '{{ transfers_start_date }}' AND TIMESTAMP '{{ transfers_end_date }}'
{% if is_incremental() %}
AND {{ incremental_predicate('timestamp') }}
{% endif %}
)
, transfers as (
SELECT
t.unique_key
, t.blockchain
, t.block_month
, t.block_date
, t.block_time
, date_trunc('hour', t.block_time) AS block_hour
, t.block_number
, t.tx_hash
, t.evt_index
, t.trace_address
, t.token_standard
, t.tx_from
, t.tx_to
, t.tx_index
, t."from"
, t.to
, t.contract_address
, {{case_when_token_standard('evms_info.native_token_symbol', 'tokens_erc20.symbol', 'NULL')}} AS symbol
, t.amount_raw
, {{case_when_token_standard('t.amount_raw / power(10, 18)', 't.amount_raw / power(10, tokens_erc20.decimals)', 'cast(t.amount_raw as double)')}} AS amount
, prices.price AS price_usd
, {{case_when_token_standard('(t.amount_raw / power(10, 18)) * prices.price',
'(t.amount_raw / power(10, tokens_erc20.decimals)) * prices.price',
'NULL')}} AS amount_usd
FROM
base_transfers as t
INNER JOIN
{{ evms_info_model }} as evms_info
ON evms_info.blockchain = t.blockchain
LEFT JOIN
{{ tokens_erc20_model }} as tokens_erc20
ON tokens_erc20.blockchain = t.blockchain
AND tokens_erc20.contract_address = t.contract_address
LEFT JOIN
prices
ON date_trunc('hour', t.block_time) = prices.timestamp
AND CASE
WHEN t.token_standard = 'native'
THEN
prices.blockchain IS NULL
AND prices.contract_address IS NULL
AND evms_info.native_token_symbol = prices.symbol
ELSE
prices.blockchain = '{{ blockchain }}'
AND t.contract_address = prices.contract_address
END
)
SELECT
unique_key
, blockchain
, block_month
, block_date
, block_time
, block_number
, tx_hash
, evt_index
, trace_address
, token_standard
, tx_from
, tx_to
, tx_index
, "from"
, to
, contract_address
, symbol
, amount_raw
, amount
, price_usd
, CASE
WHEN amount_usd >= {{ usd_amount_threshold }}
THEN CAST(NULL as double)
ELSE amount_usd -- Select only transfers where USD amount is less than the threshold
END AS amount_usd
FROM
transfers
{%- endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,62 @@ models:
combination_of_columns:
- block_date
- unique_key
columns:
- name: unique_key
description: "Surrogate key to identify unique row"
- name: blockchain
description: "The blockchain of the transfers"
- name: block_date
description: "The date of the block"
- name: block_time
description: "The time of the block"
- name: block_number
description: "The block number"
- name: tx_hash
description: "The transaction hash"
- name: evt_index
description: "The log event index of the transfer if any"
- name: trace_address
description: "The trace address of the transfer if any"
- name: token_standard
description: "The token standard of the transfer"
- name: tx_from
description: "The transaction sender"
- name: tx_to
description: "The transaction receiver"
- name: tx_index
description: "The transaction index"
- name: from
description: "The sender of the transfer"
- name: to
description: "The receiver of the transfer"
- name: contract_address
description: "The contract address of the transfer"
- name: symbol
description: "The token symbol transferred"
- name: amount_raw
description: "The raw amount of the transfer"
- name: amount
description: "The formatted amount of the transfer"
- name: price_usd
description: "The USD price used to calculate the amount_usd"
- name: amount_usd
description: "The USD amount of the transfer"

- name: tokens_blast_transfers_beta
meta:
blockchain: blast
sector: tokens
contributors: aalan3, jeff-dude, hildobby
config:
tags: [ 'tokens','transfers', 'blast' ]
description: >
Token transfers
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_date
- unique_key
columns:
- name: unique_key
description: "Surrogate key to identify unique row"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
, tokens_erc20_model = source('tokens', 'erc20')
, prices_model = source('prices', 'usd')
, evms_info_model = source('evms','info')
, transfers_start_date = '2020-04-22'
, transfers_start_date = '2024-10-01'
, transfers_end_date = '2024-10-10'
, blockchain = 'blast'
)
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{config(
schema = 'tokens_blast',
alias = 'transfers_beta',
partition_by = ['block_month'],
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['block_date','unique_key'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_date')],
post_hook='{{ expose_spells(\'["blast"]\',
"sector",
"tokens",
\'["aalan3", "jeff-dude", "hildobby"]\') }}'
)
}}

{{
transfers_enrich_beta(
base_transfers = ref('tokens_blast_base_transfers')
, tokens_erc20_model = source('tokens', 'erc20')
, prices_model = ref('prices_hour')
, evms_info_model = source('evms','info')
, transfers_start_date = '2024-10-01'
, transfers_end_date = '2024-10-10'
, blockchain = 'blast'
)
}}
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,59 @@ models:
- name: symbol
- name: decimals
- name: collection_name

- name: tokens_ethereum_transfers_beta
meta:
blockchain: ethereum
sector: tokens
contributors: aalan3, jeff-dude
config:
tags: [ 'tokens','transfers', 'ethereum' ]
description: >
Token transfers
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_date
- unique_key
columns:
- name: unique_key
description: "Surrogate key to identify unique row"
- name: blockchain
description: "The blockchain of the transfers"
- name: block_date
description: "The date of the block"
- name: block_time
description: "The time of the block"
- name: block_number
description: "The block number"
- name: tx_hash
description: "The transaction hash"
- name: evt_index
description: "The log event index of the transfer if any"
- name: trace_address
description: "The trace address of the transfer if any"
- name: token_standard
description: "The token standard of the transfer"
- name: tx_from
description: "The transaction sender"
- name: tx_to
description: "The transaction receiver"
- name: tx_index
description: "The transaction index"
- name: from
description: "The sender of the transfer"
- name: to
description: "The receiver of the transfer"
- name: contract_address
description: "The contract address of the transfer"
- name: symbol
description: "The token symbol transferred"
- name: amount_raw
description: "The raw amount of the transfer"
- name: amount
description: "The formatted amount of the transfer"
- name: price_usd
description: "The USD price used to calculate the amount_usd"
- name: amount_usd
description: "The USD amount of the transfer"
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
, tokens_erc20_model = source('tokens', 'erc20')
, prices_model = source('prices', 'usd')
, evms_info_model = source('evms','info')
, transfers_start_date = '2015-08-07'
, transfers_start_date = '2024-10-01'
, transfers_end_date = '2024-10-02'
, blockchain = 'ethereum'
)
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{config(
schema = 'tokens_ethereum',
alias = 'transfers_beta',
partition_by = ['block_month'],
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['block_date','unique_key'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_date')],
post_hook='{{ expose_spells(\'["ethereum"]\',
"sector",
"tokens",
\'["aalan3", "jeff-dude"]\') }}'
)
}}

{{
transfers_enrich_beta(
base_transfers = ref('tokens_ethereum_base_transfers')
, tokens_erc20_model = source('tokens', 'erc20')
, prices_model = ref('prices_hour')
, evms_info_model = source('evms','info')
, transfers_start_date = '2024-10-01'
, transfers_end_date = '2024-10-02'
, blockchain = 'ethereum'
)
}}
Loading