@@ -17,15 +17,17 @@ WITH send_calls AS (
1717 )
1818
1919, distinct_calls AS (
20- SELECT block_number
20+ SELECT block_time
21+ , block_number
2122 , tx_hash
2223 , MAX (call_send_index) AS max_call_send_index
2324 FROM send_calls
24- GROUP BY 1 , 2
25+ GROUP BY 1 , 2 , 3
2526 )
2627
2728, transfers AS (
28- SELECT block_number
29+ SELECT block_time
30+ , block_number
2931 , tx_hash
3032 , sender
3133 , recipient
@@ -36,7 +38,8 @@ WITH send_calls AS (
3638 , unique_key
3739 , rn
3840 FROM (
39- SELECT t .block_number
41+ SELECT t .block_time
42+ , t .block_number
4043 , t .tx_hash
4144 , t." from" AS sender
4245 , t .to AS recipient
@@ -45,11 +48,15 @@ WITH send_calls AS (
4548 , t .contract_address AS deposit_token_address
4649 , t .evt_index
4750 , t .unique_key
48- , ROW_NUMBER() OVER (PARTITION BY t .tx_hash ORDER BY COALESCE(t .trace_address , ARRAY[t .evt_index ])) AS rn
51+ -- block_time in the window partition keys is redundant for correctness (tx_hash
52+ -- determines the block) but lets the planner propagate the incremental
53+ -- block_time predicate below the window instead of scanning all history
54+ , ROW_NUMBER() OVER (PARTITION BY t .block_time , t .block_number , t .tx_hash ORDER BY COALESCE(t .trace_address , ARRAY[t .evt_index ])) AS rn
4955 , sc .max_call_send_index
5056 FROM {{ source(' tokens_' + blockchain, ' transfers' ) }} t
5157 INNER JOIN {{ ref(' bridges_layerzero_chain_indexes' ) }} i ON i .blockchain = ' {{blockchain}}'
52- INNER JOIN distinct_calls sc ON t .block_number = sc .block_number
58+ INNER JOIN distinct_calls sc ON t .block_time = sc .block_time
59+ AND t .block_number = sc .block_number
5360 AND t .tx_hash = sc .tx_hash
5461 AND t .to = i .endpoint_address
5562 )
@@ -75,7 +82,8 @@ SELECT distinct '{{blockchain}}' AS deposit_chain
7582, sc .contract_address
7683, {{ dbt_utils .generate_surrogate_key ([' sc.tx_hash' , ' t.evt_index' , ' sc.call_send_index' ]) }} as bridge_transfer_id
7784FROM send_calls sc
78- LEFT JOIN transfers t ON t .block_number = sc .block_number
85+ LEFT JOIN transfers t ON t .block_time = sc .block_time
86+ AND t .block_number = sc .block_number
7987 AND t .tx_hash = sc .tx_hash
8088 AND t .rn = sc .call_send_index
8189LEFT JOIN {{ ref(' bridges_layerzero_chain_indexes' ) }} ci ON sc .withdrawal_chain_id = ci .id
0 commit comments