From 6e04a6d89b4f5a7716739c9b8710bd1471809277 Mon Sep 17 00:00:00 2001 From: Sam <110511194+sam-xyz@users.noreply.github.com> Date: Tue, 20 May 2025 21:22:36 +0800 Subject: [PATCH] Add bridge volume by chain defillama (#120) --- ...defillama__fact_bridge_volume_by_chain.sql | 29 +++++ ...defillama__fact_bridge_volume_by_chain.yml | 30 +++++ ...lver__defillama_bridge_volume_by_chain.sql | 108 ++++++++++++++++++ ...lver__defillama_bridge_volume_by_chain.yml | 42 +++++++ .../doc_descriptions/general/__overview__.md | 1 + models/sources.yml | 5 + 6 files changed, 215 insertions(+) create mode 100644 models/defillama/gold/defillama__fact_bridge_volume_by_chain.sql create mode 100644 models/defillama/gold/defillama__fact_bridge_volume_by_chain.yml create mode 100644 models/defillama/silver/silver__defillama_bridge_volume_by_chain.sql create mode 100644 models/defillama/silver/silver__defillama_bridge_volume_by_chain.yml diff --git a/models/defillama/gold/defillama__fact_bridge_volume_by_chain.sql b/models/defillama/gold/defillama__fact_bridge_volume_by_chain.sql new file mode 100644 index 0000000..c732214 --- /dev/null +++ b/models/defillama/gold/defillama__fact_bridge_volume_by_chain.sql @@ -0,0 +1,29 @@ +{{ config( + materialized = 'view', + persist_docs ={ "relation": true, + "columns": true }, + tags = ['defillama'], + meta={ + 'database_tags':{ + 'table': { + 'PROTOCOL': 'DEFILLAMA' + } + } + } +) }} + +SELECT + DATE, + chain, + bridge_id, + bridge, + bridge_name, + deposit_txs, + deposit_usd, + withdraw_txs, + withdraw_usd, + defillama_bridge_vol_by_chain_id as defillama_fact_bridge_volume_by_chain_id, + inserted_timestamp, + modified_timestamp +FROM + {{ ref('silver__defillama_bridge_volume_by_chain') }} \ No newline at end of file diff --git a/models/defillama/gold/defillama__fact_bridge_volume_by_chain.yml b/models/defillama/gold/defillama__fact_bridge_volume_by_chain.yml new file mode 100644 index 0000000..175fe78 --- /dev/null +++ b/models/defillama/gold/defillama__fact_bridge_volume_by_chain.yml @@ -0,0 +1,30 @@ +version: 2 +models: + - name: defillama__fact_bridge_volume_by_chain + description: This table contains historical deposit and withdraw transaction volume for the bridges by chain listed on Defillama in `dim_bridges`. Only includes data for chains that are supported in the crosschain table. + + columns: + - name: DATE + description: Date associated with the reported records. + - name: CHAIN + description: Chain associated with the reported records. + - name: BRIDGE_ID + description: Unique identifier of the bridge. + - name: BRIDGE + description: Name of the bridge. + - name: BRIDGE_NAME + description: The more descriptive, official display name of the bridge. + - name: DEPOSIT_TXS + description: Total number of deposit transactions to the bridge. + - name: DEPOSIT_USD + description: Total value of deposits to the bridge, denominated in USD. + - name: WITHDRAW_TXS + description: Total number of withdrawal transactions to the bridge. + - name: WITHDRAW_USD + description: Total value of withdrawals to the bridge, denominated in USD. + - name: DEFILLAMA_FACT_BRIDGE_VOLUME_BY_CHAIN_ID + description: Unique identifier for this record + - name: INSERTED_TIMESTAMP + description: Timestamp when the record was inserted + - name: MODIFIED_TIMESTAMP + description: Timestamp when the record was last modified \ No newline at end of file diff --git a/models/defillama/silver/silver__defillama_bridge_volume_by_chain.sql b/models/defillama/silver/silver__defillama_bridge_volume_by_chain.sql new file mode 100644 index 0000000..43e1ed3 --- /dev/null +++ b/models/defillama/silver/silver__defillama_bridge_volume_by_chain.sql @@ -0,0 +1,108 @@ +{{ config( + materialized = 'incremental', + unique_key = 'defillama_bridge_vol_by_chain_id', + full_refresh = false, + tags = ['defillama'] +) }} + +WITH list_of_bridges AS ( + + SELECT + bridge_id, + bridge, + bridge_name, + LOWER( + VALUE :: STRING + ) AS chain + FROM + {{ ref('bronze__defillama_bridges') }}, + LATERAL FLATTEN ( + input => chains + ) + WHERE + chain IN ( + SELECT + DISTINCT blockchain + FROM + {{ source( + 'crosschain_defi', + 'ez_bridge_activity' + ) }} + ) +), + +build_requests as ( + select + bridge_id, + bridge, + bridge_name, + chain, + CONCAT(bridge_id, '-', chain) AS bridge_id_chain, + ROW_NUMBER() over ( + ORDER BY + bridge_id_chain + ) AS row_number_request +FROM + list_of_bridges + {% if is_incremental() %} +where + bridge_id_chain NOT IN + ( +SELECT + bridge_id_chain +FROM + ( +SELECT + bridge_id_chain, + MAX(DATE) AS max_timestamp +FROM + {{ this }} +GROUP BY + ALL +HAVING + SYSDATE()::date = max_timestamp) + ) +{% endif %}) , + +requests AS ( + {% for item in range(10) %} +SELECT + bridge_id, + bridge, + bridge_name, + chain, + bridge_id_chain, + live.udf_api('GET', CONCAT('https://bridges.llama.fi/bridgevolume/', chain, '?id=', bridge_id),{},{}) AS READ +FROM + build_requests +WHERE + row_number_request BETWEEN {{ item * 40 + 1 }} + AND {{(item + 1) * 40 }} + +{% if not loop.last %} +UNION ALL +{% endif %} +{% endfor %}) +SELECT + bridge_id, + bridge, + bridge_name, + chain, + bridge_id_chain, + TO_TIMESTAMP( + VALUE :date :: INTEGER + )::DATE AS date, + coalesce(VALUE :depositTxs :: INTEGER, 0) AS deposit_txs, + coalesce(VALUE :depositUSD :: INTEGER, 0) AS deposit_usd, + coalesce(VALUE :withdrawTxs :: INTEGER, 0) AS withdraw_txs, + coalesce(VALUE :withdrawUSD :: INTEGER, 0) AS withdraw_usd, + SYSDATE() as inserted_timestamp, + SYSDATE() as modified_timestamp, + {{ dbt_utils.generate_surrogate_key(['bridge_id_chain', 'date']) }} AS defillama_bridge_vol_by_chain_id, + '{{ invocation_id }}' AS _invocation_id +FROM + requests, + LATERAL FLATTEN ( + input => READ :data + ) + WHERE VALUE :depositTxs IS NOT NULL or VALUE :withdrawTxs IS NOT NULL \ No newline at end of file diff --git a/models/defillama/silver/silver__defillama_bridge_volume_by_chain.yml b/models/defillama/silver/silver__defillama_bridge_volume_by_chain.yml new file mode 100644 index 0000000..e864698 --- /dev/null +++ b/models/defillama/silver/silver__defillama_bridge_volume_by_chain.yml @@ -0,0 +1,42 @@ +version: 2 +models: + - name: silver__defillama_bridge_volume_by_chain + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - defillama_bridge_vol_by_chain_id + + columns: + - name: BRIDGE_ID + tests: + - not_null + - name: BRIDGE + tests: + - not_null + - name: CHAIN + tests: + - not_null + - name: DATE + tests: + - not_null + - name: DEPOSIT_TXS + tests: + - not_null + - name: DEPOSIT_USD + tests: + - not_null + - name: WITHDRAW_TXS + tests: + - not_null + - name: WITHDRAW_USD + tests: + - not_null + - name: INSERTED_TIMESTAMP + tests: + - not_null + - dbt_expectations.expect_column_values_to_be_in_type_list: + column_type_list: + - TIMESTAMP_NTZ + - dbt_expectations.expect_row_values_to_have_recent_data: + datepart: day + interval: 2 \ No newline at end of file diff --git a/models/doc_descriptions/general/__overview__.md b/models/doc_descriptions/general/__overview__.md index 0553b16..594e999 100644 --- a/models/doc_descriptions/general/__overview__.md +++ b/models/doc_descriptions/general/__overview__.md @@ -34,6 +34,7 @@ Note: These tables ceased updating on Feburary 4th, 2024. - [defillama__dim_protocols](https://flipsidecrypto.github.io/external-models/#!/model/model.external_models.defillama__dim_protocols) - [defillama__dim_stablecoins](https://flipsidecrypto.github.io/external-models/#!/model/model.external_models.defillama__dim_stablecoins) - [defillama__fact_bridge_volume](https://flipsidecrypto.github.io/external-models/#!/model/model.external_models.defillama__fact_bridge_volume) +- [defillama__fact_bridge_volume_by_chain](https://flipsidecrypto.github.io/external-models/#!/model/model.external_models.defillama__fact_bridge_volume_by_chain) - [defillama__fact_chain_tvl](https://flipsidecrypto.github.io/external-models/#!/model/model.external_models.defillama__fact_chain_tvl) - [defillama__fact_dex_volume](https://flipsidecrypto.github.io/external-models/#!/model/model.external_models.defillama__fact_dex_volume) - [defillama__fact_options_volume](https://flipsidecrypto.github.io/external-models/#!/model/model.external_models.defillama__fact_options_volume) diff --git a/models/sources.yml b/models/sources.yml index 699c52a..37c47f3 100644 --- a/models/sources.yml +++ b/models/sources.yml @@ -34,6 +34,11 @@ sources: schema: core tables: - name: dim_dates + - name: crosschain_defi + database: crosschain + schema: defi + tables: + - name: ez_bridge_activity - name: ethereum_silver database: ethereum schema: silver