Skip to content

Commit fd7d76a

Browse files
thevaizmanclaude
andcommitted
fix(dex): dedup 0x Settler RFQ rows on (tx_hash, evt_index)
The 0xd92aadfb selector can recur inside an RFQ action's ABI body, so the all-occurrences scan decodes the SAME action twice (identical maker/asset/token) at two byte offsets, both matching the one maker-leg transfer -> duplicate (tx_hash, evt_index). Keep one row per (tx_hash, maker_evt_index) via row_number; genuine distinct fills always have distinct maker-leg logs, so only the spurious re-decode is dropped (verified: the lone ethereum 14d collision was an identical-tuple recurrence at offsets 1093/1125 of tx 0x49da06f1...). Uses a subquery rather than QUALIFY (no precedent for QUALIFY in this codebase). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 4c82b3f commit fd7d76a

1 file changed

Lines changed: 35 additions & 21 deletions

File tree

dbt_subprojects/dex/macros/models/_project/zeroex/zeroex_settler_rfq.sql

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,42 @@ legs AS (
163163
OR (t.token = a.taker_token AND t.transfer_to = a.maker)
164164
)
165165
GROUP BY a.tx_hash, a.p, a.block_time, a.block_number, a.settler_address, a.taker, a.maker_asset, a.taker_token, a.maker
166-
)
166+
),
167167

168+
-- A false-positive 0xd92aadfb recurrence inside an RFQ action's ABI body can decode the SAME action twice
169+
-- (identical maker/asset/token) at two byte offsets, both matching the one maker-leg transfer -> same evt_index.
170+
-- Keep one row per (tx_hash, evt_index): genuine distinct fills always have distinct maker-leg logs, so this
171+
-- only drops the spurious re-decode (verified: the lone ethereum collision was an identical-tuple recurrence).
172+
-- (row_number() in a subquery rather than QUALIFY, which has no precedent in this codebase.)
173+
deduped AS (
174+
SELECT
175+
'{{ blockchain }}' AS blockchain,
176+
'{{ project }}' AS project,
177+
'{{ version }}' AS version,
178+
CAST(date_trunc('month', block_time) AS date) AS block_month,
179+
CAST(date_trunc('day', block_time) AS date) AS block_date,
180+
block_time,
181+
block_number,
182+
maker_amount AS token_bought_amount_raw, -- taker receives the maker's asset
183+
taker_amount AS token_sold_amount_raw, -- taker gives the taker token
184+
maker_asset AS token_bought_address,
185+
taker_token AS token_sold_address,
186+
taker,
187+
CAST(NULL AS varbinary) AS maker, -- PMM venue: settler-side maker not emitted (matches native/clipper)
188+
settler_address AS project_contract_address,
189+
tx_hash,
190+
maker_evt_index AS evt_index,
191+
row_number() OVER (PARTITION BY tx_hash, maker_evt_index ORDER BY p) AS rn
192+
FROM legs
193+
WHERE maker_n = 1
194+
AND taker_n = 1
195+
AND maker_asset <> taker_token
196+
)
168197
SELECT
169-
'{{ blockchain }}' AS blockchain,
170-
'{{ project }}' AS project,
171-
'{{ version }}' AS version,
172-
CAST(date_trunc('month', block_time) AS date) AS block_month,
173-
CAST(date_trunc('day', block_time) AS date) AS block_date,
174-
block_time,
175-
block_number,
176-
maker_amount AS token_bought_amount_raw, -- taker receives the maker's asset
177-
taker_amount AS token_sold_amount_raw, -- taker gives the taker token
178-
maker_asset AS token_bought_address,
179-
taker_token AS token_sold_address,
180-
taker,
181-
CAST(NULL AS varbinary) AS maker, -- PMM venue: settler-side maker not emitted (matches native/clipper)
182-
settler_address AS project_contract_address,
183-
tx_hash,
184-
maker_evt_index AS evt_index
185-
FROM legs
186-
WHERE maker_n = 1
187-
AND taker_n = 1
188-
AND maker_asset <> taker_token
198+
blockchain, project, version, block_month, block_date, block_time, block_number,
199+
token_bought_amount_raw, token_sold_amount_raw, token_bought_address, token_sold_address,
200+
taker, maker, project_contract_address, tx_hash, evt_index
201+
FROM deduped
202+
WHERE rn = 1
189203

190204
{% endmacro %}

0 commit comments

Comments
 (0)