From 4712530d359c948fbd85ce1362427aea2ee19329 Mon Sep 17 00:00:00 2001
From: Sebastian Melendez <sebm@seas.upenn.edu>
Date: Wed, 11 Dec 2024 22:02:07 -0500
Subject: [PATCH] updated fact celo token transfers

---
 .../get_coingecko_price_with_latest.sql       | 18 +++++++++++++++
 .../celo/raw/ez_celo_transactions.sql         | 13 +++++++++++
 .../celo/fact_celo_token_transfers.sql        | 22 ++++++++++++++++++-
 .../coingecko/dim_coingecko_token_map.sql     | 21 ++++++++++++++++++
 4 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 models/projects/celo/raw/ez_celo_transactions.sql
 create mode 100644 models/staging/coingecko/dim_coingecko_token_map.sql

diff --git a/macros/coingecko/get_coingecko_price_with_latest.sql b/macros/coingecko/get_coingecko_price_with_latest.sql
index 36f6ed28..8335e4a9 100644
--- a/macros/coingecko/get_coingecko_price_with_latest.sql
+++ b/macros/coingecko/get_coingecko_price_with_latest.sql
@@ -9,3 +9,21 @@
     from {{ ref("fact_coingecko_token_realtime_data") }}
     where token_id = '{{ coingecko_id }}'
 {% endmacro %}
+
+
+
+{% macro get_multiple_coingecko_price_with_latest(chain) %}
+    select date as date, contract_address, decimals, symbol, shifted_token_price_usd as price
+    from {{ ref("fact_coingecko_token_date_adjusted_gold") }}
+    inner join {{ ref('dim_coingecko_token_map')}}
+        on coingecko_id = coingecko_token_id
+    where
+        chain = '{{ chain }}'
+        and date < dateadd(day, -1, to_date(sysdate()))
+    union
+    select dateadd('day', -1, to_date(sysdate())) as date, contract_address, decimals, symbol, token_current_price as price
+    from {{ ref("fact_coingecko_token_realtime_data") }}
+    inner join {{ ref('dim_coingecko_token_map')}}
+        on token_id = coingecko_token_id
+    where chain = '{{ chain }}'
+{% endmacro %}
diff --git a/models/projects/celo/raw/ez_celo_transactions.sql b/models/projects/celo/raw/ez_celo_transactions.sql
new file mode 100644
index 00000000..4709cd22
--- /dev/null
+++ b/models/projects/celo/raw/ez_celo_transactions.sql
@@ -0,0 +1,13 @@
+{{
+    config(
+        materialized="view",
+        snowflake_warehouse="CELO",
+        database="celo",
+        schema="raw",
+        alias="ez_transactions",
+    )
+}}
+
+select
+    *
+from {{ref("fact_celo_transactions")}}
\ No newline at end of file
diff --git a/models/staging/celo/fact_celo_token_transfers.sql b/models/staging/celo/fact_celo_token_transfers.sql
index e70bc87e..67139ec0 100644
--- a/models/staging/celo/fact_celo_token_transfers.sql
+++ b/models/staging/celo/fact_celo_token_transfers.sql
@@ -1,3 +1,23 @@
 {{ config(materialized="incremental", unique_key=["transaction_hash", "event_index"]) }}
+with
+prices as ({{get_multiple_coingecko_price_with_latest('celo')}})
+, celo_token_transfers as ({{ token_transfer_events("celo") }})
+select
+    block_timestamp,
+    block_number,
+    transaction_hash,
+    event_index,
+    origin_from_address,
+    origin_to_address,
+    celo_token_transfers.contract_address,
+    from_address,
+    to_address,
+    amount,
+    amount / pow(10, decimals) as amount_adjusted,
+    amount_adjusted * price as amount_usd,
+    tx_status
+from celo_token_transfers
+left join prices
+    on block_timestamp::date = prices.date
+    and lower(celo_token_transfers.contract_address) = lower(prices.contract_address)
 
-{{ token_transfer_events("celo") }}
\ No newline at end of file
diff --git a/models/staging/coingecko/dim_coingecko_token_map.sql b/models/staging/coingecko/dim_coingecko_token_map.sql
new file mode 100644
index 00000000..cac92254
--- /dev/null
+++ b/models/staging/coingecko/dim_coingecko_token_map.sql
@@ -0,0 +1,21 @@
+{{config(materialized="view")}}
+with 
+    max_extraction as (
+        select max(extraction_date) as max_date
+        from {{ source("PROD_LANDING", "raw_coingecko_token_metadata") }}
+    )
+select
+    parse_json(source_json) as json,
+    json:id::string AS coingecko_token_id,
+    json:symbol::string AS symbol,
+    --TODO: Right now chains are coingecko chains, we need to map them to our chains
+    f.key::string AS chain,
+    f.value:contract_address::string AS contract_address,
+    f.value:decimal_place::number AS decimals
+from {{ source("PROD_LANDING", "raw_coingecko_token_metadata") }},
+    lateral flatten(input => parse_json(source_json):detail_platforms) f
+where extraction_date = (select max_date from max_extraction) and (
+    chain <> '' and chain is not null
+    and contract_address <> '' and chain is not null
+    and decimals is not null
+)
\ No newline at end of file