From 9a1e530e1c11a449700bf375f83600b70a0336de Mon Sep 17 00:00:00 2001 From: SebMelendez01 <78228475+SebMelendez01@users.noreply.github.com> Date: Tue, 20 Aug 2024 11:00:53 -0400 Subject: [PATCH] Stablecoin V2: Celo Stablecoin Supply (#420) --- .../agg_chain_stablecoin_transfers.sql | 60 ++++++++++++++----- macros/stablecoins/stablecoin_metrics.sql | 2 +- ...fact_celo_stablecoin_premint_addresses.sql | 12 ++++ 3 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 models/metrics/stablecoins/contracts/fact_celo_stablecoin_premint_addresses.sql diff --git a/macros/stablecoins/agg_chain_stablecoin_transfers.sql b/macros/stablecoins/agg_chain_stablecoin_transfers.sql index 0bc132b7..ef37e343 100644 --- a/macros/stablecoins/agg_chain_stablecoin_transfers.sql +++ b/macros/stablecoins/agg_chain_stablecoin_transfers.sql @@ -198,17 +198,53 @@ from fact_{{ chain }}_stablecoin_contracts ) and transfer_type = 'nep141' + + {% elif chain in ("celo") %} + select + block_timestamp, + trunc(block_timestamp, 'day') as date, + block_number, + event_index as index, + transaction_hash as tx_hash, + from_address, + to_address, + from_address = '0x0000000000000000000000000000000000000000' + or lower(from_address) in ( + select distinct (lower(premint_address)) + from {{ ref("fact_"~chain~"_stablecoin_premint_addresses") }} + ) + as is_mint, + to_address = '0x0000000000000000000000000000000000000000' + or lower(to_address) in ( + select distinct (lower(premint_address)) + from {{ ref("fact_"~chain~"_stablecoin_premint_addresses") }} + ) + as is_burn, + amount, + case + when is_mint then amount when is_burn then -1 * amount else 0 + end as inflow, + case + when not is_mint and not is_burn then amount else 0 + end as transfer_volume, + t1.contract_address, + contracts.symbol + from {{ref("fact_" ~ chain ~ "_token_transfers")}} t1 + join {{ref("fact_" ~chain~ "_stablecoin_contracts")}} contracts + on lower(t1.contract_address) = lower(contracts.contract_address) + where lower(t1.contract_address) in ( + select lower(contract_address) + from {{ref("fact_" ~chain~ "_stablecoin_contracts")}} + ) + -- DO NOT include mint / burn events here - they will be duped + and tx_status = 1 {% else %} select block_timestamp, trunc(block_timestamp, 'day') as date, block_number, event_index as index, - {% if chain in ("celo") %} - transaction_hash as tx_hash, - {% else %} - tx_hash, - {% endif %} + tx_hash, -- Notably, we do NOT use the Mint / Burn events here because the -- basic IERC20 interface does not require them to be implemented coalesce( @@ -222,7 +258,7 @@ {% if chain in ("ethereum") %} or lower(from_address) in ( select distinct (lower(premint_address)) - from {{ ref("fact_ethereum_stablecoin_premint_addresses") }} + from {{ ref("fact_"~chain~"_stablecoin_premint_addresses") }} ) {% endif %} as is_mint, @@ -231,7 +267,7 @@ {% if chain in ("ethereum") %} or lower(to_address) in ( select distinct (lower(premint_address)) - from {{ ref("fact_ethereum_stablecoin_premint_addresses") }} + from {{ ref("fact_"~chain~"_stablecoin_premint_addresses") }} ) {% endif %} as is_burn, @@ -264,11 +300,7 @@ end as transfer_volume, t1.contract_address, fact_{{ chain }}_stablecoin_contracts.symbol - {% if chain in ("celo")%} - from {{ref("fact_" ~ chain ~ "_decoded_events")}} t1 - {% else %} - from {{ chain }}_flipside.core.ez_decoded_event_logs t1 - {% endif %} + from {{ chain }}_flipside.core.ez_decoded_event_logs t1 join fact_{{ chain }}_stablecoin_contracts on lower(t1.contract_address) @@ -280,11 +312,7 @@ ) -- DO NOT include mint / burn events here - they will be duped and event_name in ('Transfer', 'Issue', 'Redeem') - {% if chain in ("celo") %} - and tx_status = 1 - {% else %} and tx_status = 'SUCCESS' - {% endif %} {% endif %} {% if is_incremental() %} and block_timestamp >= ( diff --git a/macros/stablecoins/stablecoin_metrics.sql b/macros/stablecoins/stablecoin_metrics.sql index e479488c..261135d8 100644 --- a/macros/stablecoins/stablecoin_metrics.sql +++ b/macros/stablecoins/stablecoin_metrics.sql @@ -12,7 +12,7 @@ when lower(address) in (select lower(premint_address) from {{ref("fact_"~chain~"_stablecoin_bridge_addresses")}}) then 0 else stablecoin_supply end as stablecoin_supply - {% elif chain in ('solana') %} + {% elif chain in ('solana', 'celo') %} , case when lower(address) in (select lower(premint_address) from {{ref("fact_"~chain~"_stablecoin_premint_addresses")}}) then 0 diff --git a/models/metrics/stablecoins/contracts/fact_celo_stablecoin_premint_addresses.sql b/models/metrics/stablecoins/contracts/fact_celo_stablecoin_premint_addresses.sql new file mode 100644 index 00000000..8672c5ee --- /dev/null +++ b/models/metrics/stablecoins/contracts/fact_celo_stablecoin_premint_addresses.sql @@ -0,0 +1,12 @@ +{{ config(materialized="table") }} +-- Premint addresses for ethereum are representative of USDT that has been bridged to other L2s +select contract_address, premint_address +from + ( + values + -- Un-released Supply + ( + '0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e', + '0x5754284f345afc66a98fbb0a0afe71e0f007b949' + ) + ) as results(contract_address, premint_address) \ No newline at end of file