77 )
88}}
99
10- with stg_transfer as (
11- select * from {{ ref(' stg_transfer' ) }}
10+ WITH stg_transfer AS (
11+ SELECT * FROM {{ ref(' stg_transfer' ) }}
1212 {% if is_incremental() %}
1313 -- Only process new blocks since last run
14- where block_number > (select coalesce( max (block_number), 0 ) from {{ this }})
14+ WHERE block_number > (SELECT COALESCE( MAX (block_number), 0 ) AS max_block FROM {{ this }})
1515 {% endif %}
1616),
1717
18- dim_stablecoin as (
19- select * from {{ ref(' dim_stablecoin' ) }}
20- where is_current = true -- Only use current stablecoin metadata
18+ dim_stablecoin AS (
19+ SELECT * FROM {{ ref(' dim_stablecoin' ) }}
20+ WHERE is_current = true -- Only use current stablecoin metadata
2121),
2222
23- parsed as (
24- select
23+ parsed AS (
24+ SELECT
2525 -- Parse natural key from id (format: "0xtxhash_logindex")
26- split_part (id, ' _' , 1 ) as transaction_hash ,
27- split_part(id , ' _ ' , 2 )::integer as log_index ,
26+ SPLIT_PART (id, ' _' , 2 ):: INTEGER AS log_index ,
27+ TO_CHAR(block_timestamp , ' YYYYMMDD ' )::INTEGER AS date_key ,
2828
2929 -- Time dimension
30- to_char(block_timestamp, ' YYYYMMDD' )::integer as date_key,
3130 block_number,
3231 block_timestamp,
32+ contract_address,
3333
3434 -- Contract/token dimension
35- contract_address ,
36- ' ethereum ' as chain , -- TODO: get chain from raw data when available
35+ ' ethereum ' AS chain ,
36+ from_address , -- TODO: get chain from raw data when available
3737
3838 -- Address dimensions
39- from_address ,
40- to_address
39+ to_address ,
40+ SPLIT_PART(id, ' _ ' , 1 ) AS transaction_hash
4141
42- from stg_transfer
42+ FROM stg_transfer
4343),
4444
45- enriched as (
46- select
45+ enriched AS (
46+ SELECT
4747 -- Keys
4848 p .transaction_hash ,
4949 p .log_index ,
@@ -64,29 +64,31 @@ enriched as (
6464 -- Join stablecoin metadata
6565 d .symbol ,
6666 d .name ,
67- coalesce (d .decimals , 18 ) as decimals,
67+ COALESCE (d .decimals , 18 ) AS decimals,
6868
69- -- Business enrichment: Determine transaction type
70- case
71- when p .from_address = ' 0x0000000000000000000000000000000000000000' then ' mint'
72- when p .to_address = ' 0x0000000000000000000000000000000000000000' then ' burn'
73- else ' transfer'
74- end as transaction_type,
69+ -- Determine transaction type
70+ CASE
71+ WHEN p .from_address = ' 0x0000000000000000000000000000000000000000' THEN ' mint'
72+ WHEN p .to_address = ' 0x0000000000000000000000000000000000000000' THEN ' burn'
73+ ELSE ' transfer'
74+ END AS transaction_type,
7575
7676 -- Convert to decimal amount using actual decimals from dim_stablecoin
7777 -- For stablecoins, amount ≈ USD value. TODO: have dim_price table
78- {{ convert_token_amount(' s.amount_raw' , ' coalesce(d.decimals, 18)' , 2 ) }} as amount
79-
80- from parsed p
81- left join stg_transfer s
82- on p .transaction_hash = split_part(s .id , ' _' , 1 )
83- and p .log_index = split_part(s .id , ' _' , 2 )::integer
84- left join dim_stablecoin d
85- on lower (p .contract_address ) = lower (d .contract_address )
86- and p .chain = d .chain
78+ {{ convert_token_amount(' s.amount_raw' , ' COALESCE(d.decimals, 18)' , 2 ) }} AS amount
79+
80+ FROM parsed AS p
81+ LEFT JOIN stg_transfer AS s
82+ ON
83+ p .transaction_hash = SPLIT_PART(s .id , ' _' , 1 )
84+ AND p .log_index = SPLIT_PART(s .id , ' _' , 2 )::INTEGER
85+ LEFT JOIN dim_stablecoin AS d
86+ ON
87+ LOWER (p .contract_address ) = LOWER (d .contract_address )
88+ AND p .chain = d .chain
8789)
8890
89- select
91+ SELECT
9092 transaction_hash,
9193 log_index,
9294 date_key,
@@ -101,4 +103,4 @@ select
101103 decimals,
102104 transaction_type,
103105 amount
104- from enriched
106+ FROM enriched
0 commit comments