Skip to content

fix: use Wrap/Unwrap underlying amounts for Balancer V3 buffer swaps#9511

Open
gustavobftorres wants to merge 3 commits intoduneanalytics:mainfrom
gustavobftorres:fix/#9472/sell-and-buy-amounts-balancerv3
Open

fix: use Wrap/Unwrap underlying amounts for Balancer V3 buffer swaps#9511
gustavobftorres wants to merge 3 commits intoduneanalytics:mainfrom
gustavobftorres:fix/#9472/sell-and-buy-amounts-balancerv3

Conversation

@gustavobftorres
Copy link
Copy Markdown
Contributor

Thank you for contributing to Spellbook 🪄

Please open the PR in draft and mark as ready when you want to request a review.

Description:

Join Vault_evt_Wrap/Unwrap with evt_index tie-break to avoid duplicate rows; COALESCE to Swap amounts when no buffer match.

Fix for #9472


quick links for more information:

Join Vault_evt_Wrap/Unwrap with evt_index tie-break to avoid duplicate
rows; COALESCE to Swap amounts when no buffer match. Fixes duneanalytics#9472.
@github-actions github-actions bot marked this pull request as draft April 1, 2026 12:02
@github-actions github-actions bot added WIP work in progress dbt: dex covers the DEX dbt subproject labels Apr 1, 2026
@gustavobftorres gustavobftorres marked this pull request as ready for review April 1, 2026 19:15
@github-actions github-actions bot added ready-for-review this PR development is complete, please review and removed WIP work in progress labels Apr 1, 2026
@tomfutago
Copy link
Copy Markdown
Contributor

bugbot run

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 8, 2026

Bugbot couldn't run

Bugbot is not enabled for your user on this team.

Ask your team administrator to increase your team's hard limit for Bugbot seats or add you to the allowlist in the Cursor dashboard.

@tomfutago
Copy link
Copy Markdown
Contributor

hey @gustavobftorres - this might be false red flag, but can you address this finding plz:

High: Balancer V3 buffer-event matching assumes a fixed event order that does not hold on-chain

The new logic in dbt_subprojects/dex/macros/models/_project/balancer_compatible_trades.sql only matches:

  • Wrap rows with w.evt_index < s.evt_index
  • Unwrap rows with u.evt_index > s.evt_index

That means it assumes Balancer V3 always emits Wrap -> Swap -> Unwrap.

Live Ethereum data shows this assumption is false. Using the same join keys as the PR (evt_tx_hash, share amount, wrapped token), I found since 2025-01-01:

  • 12,774 swaps with only a matching Wrap after the Swap
  • 1,720 swaps with only a matching Unwrap before the Swap

Concrete counterexamples:

  • 0x62763c7010d03949f8af934fedc9077edc150bce25db3df333412ac25636654c

    • Swap at evt_index = 59
    • Unwrap at evt_index = 60
    • Wrap at evt_index = 63
  • 0x79707734bca5c8fb7e5ca49dd3b742941e9d18d65fc4455a6d02cb286eb6457a

    • Unwrap at evt_index = 124
    • Swap at evt_index = 125
    • Wrap at evt_index = 128

Because the PR falls back to swap.amountIn / swap.amountOut via coalesce(...), these transactions would still keep share-denominated values on one or both sides instead of underlying token amounts.

So the fix is incomplete: it resolves the happy-path ordering covered by the updated seeds, but it still leaves incorrect Balancer V3 trade amounts for real reverse-ordered buffer swaps.

@tomfutago tomfutago added WIP work in progress and removed ready-for-review this PR development is complete, please review labels Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dbt: dex covers the DEX dbt subproject WIP work in progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants