Skip to content

Commit

Permalink
Merge pull request #84 from Artemis-xyz/sm-add-cctp
Browse files Browse the repository at this point in the history
Bridges: adding CCTP
  • Loading branch information
SebMelendez01 authored May 15, 2024
2 parents 03c72fd + 1ce4088 commit c3dea02
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 32 deletions.
59 changes: 44 additions & 15 deletions macros/bridge/cctp_transfers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,46 @@
block_timestamp,
block_id as block_number,
tx_id as tx_hash,
program_id as contract_address,
PC_DBT_DB.PROD.BASE58_TO_HEX(f.value:data) as hex_data,
PC_DBT_DB.PROD.hex_to_base58(SUBSTRING(hex_data,129,64)) as sender, -- Need to figure out which one is the sender
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,33,16)) as nonce,
'0x' || SUBSTRING(hex_data,193,64) as reciepient,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,113,16)) as amount,
PC_DBT_DB.PROD.hex_to_base58(SUBSTRING(hex_data,49,64)) as burn_token,
5 as source_domain_id,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,257,8)) as destination_domain_id
succeeded as status
FROM solana_flipside.core.fact_events,
'{{ contract_address }}' as contract_address,
PC_DBT_DB.PROD.hex_to_base58(SUBSTRING(PC_DBT_DB.PROD.BASE58_TO_HEX(f.value:data),129,64)) as sender,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(PC_DBT_DB.PROD.BASE58_TO_HEX(f.value:data),33,16)) as nonce,
'0x' || SUBSTRING(PC_DBT_DB.PROD.BASE58_TO_HEX(f.value:data),193,64) as reciepient,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(PC_DBT_DB.PROD.BASE58_TO_HEX(f.value:data),113,16)) as amount,
PC_DBT_DB.PROD.hex_to_base58(SUBSTRING(PC_DBT_DB.PROD.BASE58_TO_HEX(f.value:data),49,64)) as burn_token,
{{ source_domain_id }} as source_domain_id,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(PC_DBT_DB.PROD.BASE58_TO_HEX(f.value:data),257,8)) as destination_domain_id,
from solana_flipside.core.fact_events,
lateral flatten(input => get_path(inner_instruction, 'instructions')) AS f
where program_id = 'CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3'
and SUBSTRING(hex_data,17,16) = '90fc9192064aa7eb'
where program_id = '{{ contract_address }}'
and SUBSTRING(PC_DBT_DB.PROD.BASE58_TO_HEX(f.value:data),17,16) = '90fc9192064aa7eb'
and succeeded = 'TRUE'
{% if is_incremental() %}
and block_timestamp > (select dateadd('day', -1, max(block_timestamp)) from {{ this }} where source_domain_id = {{ source_domain_id }})
{% endif %}
{% elif chain == 'noble' %}
select
source:block_timestamp::timestamp as block_timestamp,
source:block_number::number as block_number,
source:tx_hash::string as tx_hash,
'{{ contract_address }}' as contract_address,
source:message:"from"::string as sender,
null as nonce,
source:message:"mint_recipient"::string as reciepient,
source:message:"amount"::number as amount,
source:message:"burn_token"::string as burn_token,
{{ source_domain_id }} as source_domain_id,
source:message:"destination_domain"::number as destination_domain_id
from (
select
max_by(value, extraction_date) as source
from landing_database.prod_landing.raw_noble_cctp_messages,
lateral flatten(input => parse_json(source_json))
group by value
)
where source:message:"burn_token"::string != '' -- bug in noble chain data
{% if is_incremental() %}
and source:block_timestamp::timestamp > (select dateadd('day', -1, max(block_timestamp)) from {{ this }} where source_domain_id = {{ source_domain_id }})
{% endif %}
{% else %}
select
block_timestamp,
Expand All @@ -30,9 +56,12 @@
decoded_log:amount::number as amount,
decoded_log:burnToken::string as burn_token,
{{ source_domain_id }} as source_domain_id,
decoded_log:destinationDomain::number as destination_domain_id ,
tx_status as status
decoded_log:destinationDomain::number as destination_domain_id
from {{ chain }}_flipside.core.ez_decoded_event_logs
where lower(contract_address) = lower('{{ contract_address }}') and event_name in ('DepositForBurn')
and tx_status = 'SUCCESS'
{% if is_incremental() %}
and block_timestamp > (select dateadd('day', -1, max(block_timestamp)) from {{ this }} where source_domain_id = {{ source_domain_id }})
{% endif %}
{% endif %}
{% endmacro %}
29 changes: 29 additions & 0 deletions models/projects/cctp/core/ez_cctp_metrics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{
config(
materialized="table",
snowflake_warehouse="CCTP",
database="cctp",
schema="core",
alias="ez_metrics",
)
}}

with
bridge_volume as (
select date, bridge_volume
from {{ ref("fact_cctp_bridge_volume") }}
where chain is null
),
bridge_dau as (
select date, bridge_dau
from {{ ref("fact_cctp_bridge_dau") }}
)
select
bridge_volume.date as date,
'cctp' as protocol,
'bridge' as category,
bridge_volume.bridge_volume,
bridge_dau.bridge_dau
from bridge_volume
left join bridge_dau on bridge_volume.date = bridge_dau.date
where bridge_volume.date < to_date(sysdate())
25 changes: 25 additions & 0 deletions models/projects/cctp/core/ez_cctp_metrics_by_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{
config(
materialized="table",
snowflake_warehouse="CCTP",
database="cctp",
schema="core",
alias="ez_metrics_by_chain",
)
}}

with
bridge_volume as (
select date, chain, inflow, outflow
from {{ ref("fact_cctp_bridge_volume") }}
where chain is not null
)
select
date,
'cctp' as protocol,
'bridge' as category,
chain,
inflow,
outflow
from bridge_volume
where date < to_date(sysdate())
1 change: 1 addition & 0 deletions models/projects/ton/core/ez_ton_metrics.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ select
market_cap,
fdmc,
tvl,
dex_volumes,
weekly_commits_core_ecosystem,
weekly_commits_sub_ecosystem,
weekly_developers_core_ecosystem,
Expand Down
38 changes: 38 additions & 0 deletions models/staging/cctp/_test_fact_cctp_bridge_dau.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
models:
- name: fact_cctp_bridge_dau
tests:
- "dbt_expectations.expect_table_row_count_to_be_between:":
min_value: 1
max_value: 1000000
columns:
- name: APP
tests:
- not_null
- dbt_expectations.expect_column_to_exist
- name: DATE
tests:
- not_null
- dbt_expectations.expect_column_to_exist
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: day
interval: 3
severity: warn
- name: BRIDGE_DAU
tests:
- not_null
- dbt_expectations.expect_column_to_exist
- dbt_expectations.expect_column_values_to_be_within_n_moving_stdevs:
date_column_name: DATE
period: day
lookback_periods: 1
trend_periods: 14
test_periods: 28
sigma_threshold: 3
take_logs: true
severity: warn
- name: CHAIN
tests:
- dbt_expectations.expect_column_to_exist
- name: CATEGORY
tests:
- dbt_expectations.expect_column_to_exist
38 changes: 38 additions & 0 deletions models/staging/cctp/_test_fact_cctp_bridge_volume.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
models:
- name: fact_cctp_bridge_volume
tests:
- "dbt_expectations.expect_table_row_count_to_be_between:":
min_value: 1
max_value: 1000000
columns:
- name: APP
tests:
- not_null
- dbt_expectations.expect_column_to_exist
- name: DATE
tests:
- not_null
- dbt_expectations.expect_column_to_exist
- dbt_expectations.expect_row_values_to_have_recent_data:
datepart: day
interval: 3
severity: warn
- name: BRIDGE_VOLUME
tests:
- dbt_expectations.expect_column_to_exist
- name: FEES
tests:
- dbt_expectations.expect_column_to_exist
- name: INFLOW
tests:
- dbt_expectations.expect_column_to_exist
- name: OUTFLOW
tests:
- dbt_expectations.expect_column_to_exist
- name: CHAIN
tests:
- dbt_expectations.expect_column_to_exist
- name: CATEGORY
tests:
- not_null
- dbt_expectations.expect_column_to_exist
19 changes: 19 additions & 0 deletions models/staging/cctp/fact_cctp_bridge_dau.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
with
daily_addresses as (
select block_timestamp::date as date, count(distinct user) as bridge_dau
from
(
select block_timestamp, sender as user
from {{ ref("fact_cctp_transfers") }}

union

select block_timestamp, reciepient as user
from {{ ref("fact_cctp_transfers") }}
) t
group by 1
order by 1 asc
)

select date, bridge_dau, 'cctp' as app, null as chain, 'Bridge' as category
from daily_addresses
45 changes: 45 additions & 0 deletions models/staging/cctp/fact_cctp_bridge_volume.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
with
protocol_volume_and_fees as (
select date, sum(amount_usd) as bridge_volume, sum(fee_usd) as fees
from {{ ref("fact_cctp_flows") }}
group by 1
),

outflows as (
select date, source_chain as chain, sum(amount_usd) as outflow
from {{ ref("fact_cctp_flows") }}
where chain is not null
group by 1, 2
),

inflows as (
select date, destination_chain as chain, sum(amount_usd) as inflow
from {{ ref("fact_cctp_flows") }}
where chain is not null
group by 1, 2
)

select
date,
null as chain,
null as inflow,
null as outflow,
bridge_volume,
fees,
'cctp' as app,
'Bridge' as category
from protocol_volume_and_fees

union

select
coalesce(t1.date, t2.date) as date,
coalesce(t1.chain, t2.chain) as chain,
coalesce(t2.inflow, 0) as inflow,
coalesce(t1.outflow, 0) as outflow,
null as bridge_volume,
null as fees,
'cctp' as app,
'Bridge' as category
from outflows t1
full join inflows t2 on t1.date = t2.date and t1.chain = t2.chain
42 changes: 42 additions & 0 deletions models/staging/cctp/fact_cctp_flows.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{ config(
materialized="table",
snowflake_warehouse="CCTP",
)}}
with
chain_id_map as (
select chain_id, chain
from (
values
(0, 'ethereum'),
(1, 'avalanche'),
(2, 'optimism'),
(3, 'arbitrum'),
(4, 'noble'),
(5, 'solana'),
(6, 'base'),
(7, 'polygon')

) as t(chain_id, chain)
),
volume_by_chain_and_symbol as (
select
block_timestamp::date as date,
c1.chain as source_chain,
c2.chain as destination_chain,
'Stablecoin' as category,
amount_usd
from {{ ref("fact_cctp_transfers")}} t
left join chain_id_map c1 on t.source_domain_id = c1.chain_id
left join chain_id_map c2 on t.destination_domain_id = c2.chain_id
)
select
date,
'cctp' as app,
source_chain,
destination_chain,
category,
sum(amount_usd) as amount_usd,
null as fee_usd
from volume_by_chain_and_symbol
where source_chain is not null and destination_chain is not null
group by 1, 2, 3, 4, 5
Loading

0 comments on commit c3dea02

Please sign in to comment.