From 4481541309c644113a343ae116a9cdaf66f84e65 Mon Sep 17 00:00:00 2001 From: Karim Hassan Date: Mon, 15 Jun 2026 17:04:11 +0200 Subject: [PATCH] fix(dex): guard 0x settler volume_usd against mismatched maker leg The 0x settler decoder (zeroex_v2 macro) matches the maker (bought) leg heuristically. In bundled / multi-path settler txs (e.g. Relay-routed orders) it can latch onto an unrelated side-swap transfer, so volume_usd collapses to a tiny amount (e.g. tx 0xf61374...bf57 reported $0.75 for a ~$499 RLUSD->cbBTC trade, mirroring a parallel RLUSD->USDC fee hop). Add a leg-divergence guard: when both priced legs diverge by >= 5x, anchor volume_usd on the deterministic sold (taker) leg -- the user's actual settler input -- instead of the mismatch-prone maker leg. This fixes both the understatement (tiny mismatched maker) and a smaller set of overstatement cases (large intermediate-hop maker), and leaves all non-divergent rows byte-identical. Regression (ethereum, 60d): ~948k consistent rows unchanged to the cent; ~2.6k understated rows corrected up (+$95.8M); ~1.1k overstated rows corrected down (-$4.8M). Does not re-derive the wrong maker_token columns; that is a tracked follow-up. Co-authored-by: Cursor --- .../models/_project/zeroex/zeroex_v2.sql | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/dbt_subprojects/dex/macros/models/_project/zeroex/zeroex_v2.sql b/dbt_subprojects/dex/macros/models/_project/zeroex/zeroex_v2.sql index 66fa4b8b599..13e89b52aa7 100644 --- a/dbt_subprojects/dex/macros/models/_project/zeroex/zeroex_v2.sql +++ b/dbt_subprojects/dex/macros/models/_project/zeroex/zeroex_v2.sql @@ -358,6 +358,15 @@ select * from tbl_trades {%- if target.name == 'ci' -%} {%- set start_date = (modules.datetime.date.today() - modules.datetime.timedelta(days=14)).strftime('%Y-%m-%d') -%} {%- endif -%} +{# Leg-divergence guard: a settler trade's two priced legs (sold vs bought) should be + close in USD. In bundled / multi-path settler txs (e.g. Relay-routed orders) the + heuristically-matched maker (bought/output) leg can latch onto an unrelated side-swap + transfer, distorting volume_usd. The taker (sold) leg is the deterministic settler + input (the user's actual deposit, matched first), so it is the trustworthy anchor. + When both legs are priced and diverge by at least this factor, fall back to the + sold-leg value rather than the mismatch-prone maker leg (which can be a tiny side-swap + amount -> understatement, or a large intermediate hop -> overstatement). #} +{%- set leg_divergence_tolerance = 5 -%} WITH token_metadata AS ( SELECT blockchain, @@ -450,7 +459,17 @@ SELECT maker_token_amount, taker_token_amount_raw, maker_token_amount_raw, - amount_usd as volume_usd, + -- Guard against the maker leg being mismatched to an unrelated side-swap transfer + -- (see leg_divergence_tolerance above): when both legs are priced and diverge + -- implausibly, anchor on the deterministic sold (taker) leg rather than the + -- mismatch-prone maker leg that produced the collapsed amount_usd. + CASE + WHEN taker_amount > 0 + AND maker_amount > 0 + AND greatest(taker_amount, maker_amount) / least(taker_amount, maker_amount) >= {{ leg_divergence_tolerance }} + THEN taker_amount + ELSE amount_usd + END AS volume_usd, taker_token, maker_token, taker,