diff --git a/dbt_subprojects/daily_spellbook/macros/project/lifi/lifi_extract_bridge_data_macro.sql b/dbt_subprojects/daily_spellbook/macros/project/lifi/lifi_extract_bridge_data_macro.sql new file mode 100644 index 00000000000..d10528b0eab --- /dev/null +++ b/dbt_subprojects/daily_spellbook/macros/project/lifi/lifi_extract_bridge_data_macro.sql @@ -0,0 +1,31 @@ +{% macro lifi_extract_bridge_data(blockchain) %} + +{% set bridge_data_fields = [ + 'transactionId', + 'bridge', + 'integrator', + 'referrer', + 'sendingAssetId', + 'receiver', + 'minAmount', + 'destinationChainId' +] %} + +select + contract_address, + evt_tx_hash as tx_hash, + evt_index, + evt_block_time as block_time, + evt_block_number as block_number, + cast(date_trunc('day', evt_block_time) as date) as block_date, + {% for field in bridge_data_fields %} + json_extract_scalar(bridgeData, '$.{{ field }}') as {{ field }}, + {% endfor %} + '{{ blockchain }}' as source_chain, + {{ dbt_utils.generate_surrogate_key(['evt_tx_hash', 'evt_index']) }} as transfer_id +from {{ source('lifi_' ~ blockchain, 'LiFiDiamond_v2_evt_LiFiTransferStarted') }} +{% if is_incremental() %} +where {{ incremental_predicate('evt_block_time') }} +{% endif %} + +{% endmacro %} diff --git a/dbt_subprojects/daily_spellbook/models/_projects/lifi/arbitrum/lifi_arbitrum_transfers.sql b/dbt_subprojects/daily_spellbook/models/_projects/lifi/arbitrum/lifi_arbitrum_transfers.sql new file mode 100644 index 00000000000..094fc5fc347 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/lifi/arbitrum/lifi_arbitrum_transfers.sql @@ -0,0 +1,33 @@ +{{ config( + schema = 'lifi_arbitrum', + alias = 'transfers', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['transfer_id'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +with source_data as ( + {{ lifi_extract_bridge_data('arbitrum') }} +), + +price_data as ( + select + source_data.*, + p.price * cast(source_data.minAmount as double) / power(10, p.decimals) as amount_usd + from source_data + left join {{ source('prices', 'usd') }} p + on cast(p.contract_address as varchar) = source_data.sendingAssetId + and p.blockchain = 'arbitrum' + and p.minute = date_trunc('minute', source_data.block_time) +) + +{{ + add_tx_columns( + model_cte = 'price_data' + , blockchain = 'arbitrum' + , columns = ['from'] + ) +}} diff --git a/dbt_subprojects/daily_spellbook/models/_projects/lifi/avalanche_c/lifi_avalanche_c_transfers.sql b/dbt_subprojects/daily_spellbook/models/_projects/lifi/avalanche_c/lifi_avalanche_c_transfers.sql new file mode 100644 index 00000000000..dc0e6e617f8 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/lifi/avalanche_c/lifi_avalanche_c_transfers.sql @@ -0,0 +1,33 @@ +{{ config( + schema = 'lifi_avalanche_c', + alias = 'transfers', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['transfer_id'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +with source_data as ( + {{ lifi_extract_bridge_data('avalanche_c') }} +), + +price_data as ( + select + source_data.*, + p.price * cast(source_data.minAmount as double) / power(10, p.decimals) as amount_usd + from source_data + left join {{ source('prices', 'usd') }} p + on cast(p.contract_address as varchar) = source_data.sendingAssetId + and p.blockchain = 'avalanche_c' + and p.minute = date_trunc('minute', source_data.block_time) +) + +{{ + add_tx_columns( + model_cte = 'price_data' + , blockchain = 'avalanche_c' + , columns = ['from'] + ) +}} diff --git a/dbt_subprojects/daily_spellbook/models/_projects/lifi/bnb/lifi_bnb_transfers.sql b/dbt_subprojects/daily_spellbook/models/_projects/lifi/bnb/lifi_bnb_transfers.sql new file mode 100644 index 00000000000..d06981c2535 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/lifi/bnb/lifi_bnb_transfers.sql @@ -0,0 +1,33 @@ +{{ config( + schema = 'lifi_bnb', + alias = 'transfers', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['transfer_id'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +with source_data as ( + {{ lifi_extract_bridge_data('bnb') }} +), + +price_data as ( + select + source_data.*, + p.price * cast(source_data.minAmount as double) / power(10, p.decimals) as amount_usd + from source_data + left join {{ source('prices', 'usd') }} p + on cast(p.contract_address as varchar) = source_data.sendingAssetId + and p.blockchain = 'bnb' + and p.minute = date_trunc('minute', source_data.block_time) +) + +{{ + add_tx_columns( + model_cte = 'price_data' + , blockchain = 'bnb' + , columns = ['from'] + ) +}} diff --git a/dbt_subprojects/daily_spellbook/models/_projects/lifi/ethereum/lifi_ethereum_transfers.sql b/dbt_subprojects/daily_spellbook/models/_projects/lifi/ethereum/lifi_ethereum_transfers.sql new file mode 100644 index 00000000000..c92d1ef0920 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/lifi/ethereum/lifi_ethereum_transfers.sql @@ -0,0 +1,33 @@ +{{ config( + schema = 'lifi_ethereum', + alias = 'transfers', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['transfer_id'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +with source_data as ( + {{ lifi_extract_bridge_data('ethereum') }} +), + +price_data as ( + select + source_data.*, + p.price * cast(source_data.minAmount as double) / power(10, p.decimals) as amount_usd + from source_data + left join {{ source('prices', 'usd') }} p + on cast(p.contract_address as varchar) = source_data.sendingAssetId + and p.blockchain = 'ethereum' + and p.minute = date_trunc('minute', source_data.block_time) +) + +{{ + add_tx_columns( + model_cte = 'price_data' + , blockchain = 'ethereum' + , columns = ['from'] + ) +}} \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/lifi/fantom/lifi_fantom_transfers.sql b/dbt_subprojects/daily_spellbook/models/_projects/lifi/fantom/lifi_fantom_transfers.sql new file mode 100644 index 00000000000..2190a631cbd --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/lifi/fantom/lifi_fantom_transfers.sql @@ -0,0 +1,33 @@ +{{ config( + schema = 'lifi_fantom', + alias = 'transfers', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['transfer_id'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +with source_data as ( + {{ lifi_extract_bridge_data('fantom') }} +), + +price_data as ( + select + source_data.*, + p.price * cast(source_data.minAmount as double) / power(10, p.decimals) as amount_usd + from source_data + left join {{ source('prices', 'usd') }} p + on cast(p.contract_address as varchar) = source_data.sendingAssetId + and p.blockchain = 'fantom' + and p.minute = date_trunc('minute', source_data.block_time) +) + +{{ + add_tx_columns( + model_cte = 'price_data' + , blockchain = 'fantom' + , columns = ['from'] + ) +}} diff --git a/dbt_subprojects/daily_spellbook/models/_projects/lifi/gnosis/lifi_gnosis_transfers.sql b/dbt_subprojects/daily_spellbook/models/_projects/lifi/gnosis/lifi_gnosis_transfers.sql new file mode 100644 index 00000000000..793f4a4a573 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/lifi/gnosis/lifi_gnosis_transfers.sql @@ -0,0 +1,33 @@ +{{ config( + schema = 'lifi_gnosis', + alias = 'transfers', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['transfer_id'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +with source_data as ( + {{ lifi_extract_bridge_data('gnosis') }} +), + +price_data as ( + select + source_data.*, + p.price * cast(source_data.minAmount as double) / power(10, p.decimals) as amount_usd + from source_data + left join {{ source('prices', 'usd') }} p + on cast(p.contract_address as varchar) = source_data.sendingAssetId + and p.blockchain = 'gnosis' + and p.minute = date_trunc('minute', source_data.block_time) +) + +{{ + add_tx_columns( + model_cte = 'price_data' + , blockchain = 'gnosis' + , columns = ['from'] + ) +}} diff --git a/dbt_subprojects/daily_spellbook/models/_projects/lifi/lifi_transfers.sql b/dbt_subprojects/daily_spellbook/models/_projects/lifi/lifi_transfers.sql new file mode 100644 index 00000000000..84619a2e62e --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/lifi/lifi_transfers.sql @@ -0,0 +1,49 @@ +{{ + config( + schema = 'lifi', + alias = 'transfers', + materialized = 'view', + contributor = 'lequangphu' + ) +}} + +{% set chains = [ + 'ethereum', + 'arbitrum', + 'avalanche_c', + 'bnb', + 'fantom', + 'gnosis', + 'zksync' +] %} + +with chain_transfers as ( + {% for chain in chains %} + select + contract_address, + tx_hash, + evt_index, + block_time, + block_number, + block_date, + transactionId, + bridge, + integrator, + referrer, + sendingAssetId, + receiver, + minAmount, + destinationChainId, + source_chain, + transfer_id, + amount_usd, + tx_from + from {{ ref('lifi_' ~ chain ~ '_transfers') }} + {% if not loop.last %} + union all + {% endif %} + {% endfor %} +) + +select * +from chain_transfers \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/lifi/schema.yml b/dbt_subprojects/daily_spellbook/models/_projects/lifi/schema.yml new file mode 100644 index 00000000000..4b659fb5a9b --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/lifi/schema.yml @@ -0,0 +1,71 @@ +version: 2 + +models: + - name: lifi_ethereum_transfers + description: "Ethereum LiFi transfer events" + columns: &common_columns + - name: contract_address + description: "Contract address of LiFi Diamond" + - name: tx_hash + description: "Transaction hash" + - name: evt_index + description: "Event index" + - name: block_time + description: "Timestamp of the block" + - name: block_number + description: "Block number" + - name: transactionId + description: "Unique transaction ID from LiFi" + - name: bridge + description: "Bridge used for the transfer" + - name: integrator + description: "Integrator of the transfer" + - name: referrer + description: "Referrer of the transfer" + - name: sendingAssetId + description: "Asset being transferred" + - name: receiver + description: "Address receiving the transfer" + - name: minAmount + description: "Minimum amount to be received" + - name: destinationChainId + description: "Target chain ID for the transfer" + - name: source_chain + description: "Source blockchain" + - name: tx_from + description: "Address that initiated the transaction" + - name: transfer_id + description: "Unique identifier for each transfer" + data_tests: + - unique + - not_null + - name: amount_usd + description: "USD value of the transfer amount at the time of transfer" + + - name: lifi_arbitrum_transfers + description: "Arbitrum LiFi transfer events" + columns: *common_columns + + - name: lifi_avalanche_c_transfers + description: "Avalanche C-Chain LiFi transfer events" + columns: *common_columns + + - name: lifi_bnb_transfers + description: "BNB LiFi transfer events" + columns: *common_columns + + - name: lifi_fantom_transfers + description: "Fantom LiFi transfer events" + columns: *common_columns + + - name: lifi_gnosis_transfers + description: "Gnosis LiFi transfer events" + columns: *common_columns + + - name: lifi_zksync_transfers + description: "zkSync LiFi transfer events" + columns: *common_columns + + - name: lifi_transfers + description: "Combined LiFi transfer events across all supported chains" + columns: *common_columns \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/lifi/zksync/lifi_zksync_transfers.sql b/dbt_subprojects/daily_spellbook/models/_projects/lifi/zksync/lifi_zksync_transfers.sql new file mode 100644 index 00000000000..f09bd3251f6 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/lifi/zksync/lifi_zksync_transfers.sql @@ -0,0 +1,33 @@ +{{ config( + schema = 'lifi_zksync', + alias = 'transfers', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['transfer_id'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +with source_data as ( + {{ lifi_extract_bridge_data('zksync') }} +), + +price_data as ( + select + source_data.*, + p.price * cast(source_data.minAmount as double) / power(10, p.decimals) as amount_usd + from source_data + left join {{ source('prices', 'usd') }} p + on cast(p.contract_address as varchar) = source_data.sendingAssetId + and p.blockchain = 'zksync' + and p.minute = date_trunc('minute', source_data.block_time) +) + +{{ + add_tx_columns( + model_cte = 'price_data' + , blockchain = 'zksync' + , columns = ['from'] + ) +}} diff --git a/sources/lifi/sources.yml b/sources/lifi/sources.yml new file mode 100644 index 00000000000..87486c607d9 --- /dev/null +++ b/sources/lifi/sources.yml @@ -0,0 +1,56 @@ +version: 2 + +sources: + - name: lifi_ethereum + description: "Ethereum LiFi events" + tables: + - name: LiFiDiamond_v2_evt_LiFiTransferStarted + columns: &common_columns + - name: contract_address + description: "Contract address of the LiFi Diamond" + - name: evt_tx_hash + description: "Transaction hash of the event" + - name: evt_index + description: "Event index in the transaction" + - name: evt_block_time + description: "Timestamp of the block when the event was emitted" + - name: evt_block_number + description: "Block number when the event was emitted" + - name: bridgeData + description: "JSON data containing bridge transfer information including transactionId, bridge, integrator, referrer, sendingAssetId, receiver, minAmount, and destinationChainId" + + - name: lifi_arbitrum + description: "Arbitrum LiFi events" + tables: + - name: LiFiDiamond_v2_evt_LiFiTransferStarted + columns: *common_columns + + - name: lifi_avalanche_c + description: "Avalanche C-Chain LiFi events" + tables: + - name: LiFiDiamond_v2_evt_LiFiTransferStarted + columns: *common_columns + + - name: lifi_bnb + description: "BNB Chain LiFi events" + tables: + - name: LiFiDiamond_v2_evt_LiFiTransferStarted + columns: *common_columns + + - name: lifi_fantom + description: "Fantom LiFi events" + tables: + - name: LiFiDiamond_v2_evt_LiFiTransferStarted + columns: *common_columns + + - name: lifi_gnosis + description: "Gnosis Chain LiFi events" + tables: + - name: LiFiDiamond_v2_evt_LiFiTransferStarted + columns: *common_columns + + - name: lifi_zksync + description: "zkSync LiFi events" + tables: + - name: LiFiDiamond_v2_evt_LiFiTransferStarted + columns: *common_columns