Skip to content

fix(split routing): PathFrankWolfe bugs#249

Merged
tamaralipows merged 4 commits into
mainfrom
split-routing/tnl/pfw-fixes
Jun 12, 2026
Merged

fix(split routing): PathFrankWolfe bugs#249
tamaralipows merged 4 commits into
mainfrom
split-routing/tnl/pfw-fixes

Conversation

@tamaralipows

@tamaralipows tamaralipows commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

I'll fix the base branch to be main before merging.

Bug 1: Price impact was not considering token decimals.

  • compute_marginal_price_product used spot_price (which returns decimal-normalized human units, e.g. 2000.0 for WETH/USDC) directly against raw token amounts. For cross-decimal pairs like WETH (18) / USDC (6), this produced price impact values of -10^12 instead of ~0.01, causing the PI gate to skip splitting on virtually every trade.

The logs I was seeing:

2026-06-11T19:36:33.008667Z DEBUG fynd_core::algorithm::path_frank_wolfe: price impact too low to justify splitting pi=-999594367938.8019 gas_cost=6.978733999167245e16
2026-06-11T19:36:33.822045Z DEBUG fynd_core::algorithm::path_frank_wolfe: price impact too low to justify splitting pi=-999594267632.0906 gas_cost=6.978733999167245e16
2026-06-11T19:36:33.822140Z DEBUG fynd_core::algorithm::path_frank_wolfe: price impact too low to justify splitting pi=-999594267638.7703 gas_cost=6.978733999167245e16
2026-06-11T19:36:33.822266Z DEBUG fynd_core::algorithm::path_frank_wolfe: price impact too low to justify splitting pi=-999594366339.0143 gas_cost=6.978733999167245e16
2026-06-11T19:36:33.966675Z DEBUG fynd_core::algorithm::path_frank_wolfe: price impact too low to justify splitting pi=-999594269039.1056 gas_cost=6.978733999167245e16
2026-06-11T19:36:33.967420Z DEBUG fynd_core::algorithm::path_frank_wolfe: price impact too low to justify splitting pi=-999594152364.2762 gas_cost=6.978733999167245e16
2026-06-11T19:36:34.667360Z DEBUG fynd_core::algorithm::path_frank_wolfe: price impact too low to justify splitting pi=-999594152307.2026 gas_cost=6.978733999167245e16
2026-06-11T19:36:34.667364Z DEBUG fynd_core::algorithm::path_frank_wolfe: price impact too low to justify splitting pi=-999594153127.4119 gas_cost=6.978733999167245e16

Bug 2: Split route results panicked the server

When a split route was chosen, amount_out was computed from only the last swap (one leg of the split), but amount_out_net_gas was correctly summed across all legs. Downstream gas refinement subtracted the larger total from the smaller partial, causing a BigUint underflow panic.

@tamaralipows tamaralipows force-pushed the split-routing/tnl/pfw-fixes branch from 59b2608 to 52dbbe7 Compare June 11, 2026 20:16
tamaralipows and others added 2 commits June 11, 2026 16:49
compute_marginal_price_product used spot_price (decimal-normalized,
human units) directly against raw token amounts, producing nonsensical
price impact values (~-1e12) for cross-decimal pairs like WETH/USDC.
This caused the PI gate to skip splitting on virtually every real
trade.

Apply 10^(decimals_out - decimals_in) correction per hop so that
amount_in_raw * product ≈ amount_out_raw. Invisible in existing
tests because all test tokens use 18 decimals; two new mixed-decimal
tests added.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
For single-path routes, taking the last swap's amount_out is correct.
For split routes, the last swap is only one leg of the split. The
downstream gas refinement in WorkerPoolRouter subtracts net_gas
(correctly totalled) from amount_out (partial), causing a BigUint
underflow panic.

Sum all swaps matching the output token, mirroring the logic already
used in compute_split_net_amount_out.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@tamaralipows tamaralipows force-pushed the split-routing/tnl/pfw-fixes branch 2 times, most recently from 84a8adc to 8471f7a Compare June 12, 2026 03:01
Keep compute_marginal_price_product as a clean float ratio in human
units. Instead, normalize raw amounts to human units in
compute_average_price_impact before comparing against the price
product. Mathematically equivalent, but preserves the semantic that
spot_price products are floating-point ratios, not decimal-scaled.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@tamaralipows tamaralipows force-pushed the split-routing/tnl/pfw-fixes branch from 8471f7a to d1ea2e1 Compare June 12, 2026 03:02

@louise-poole louise-poole left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good finds!

@dianacarvalho1 dianacarvalho1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @tamaralipows 🙏🏼 looks good!

Comment thread fynd-core/src/algorithm/path_frank_wolfe.rs Outdated

@Troshchk Troshchk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @tamaralipows!
Just a small comment, otherwise looks good 💫

Comment thread fynd-core/src/algorithm/path_frank_wolfe.rs
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@tamaralipows tamaralipows changed the base branch from feat/benchmark-audit to main June 12, 2026 14:41
@tamaralipows tamaralipows merged commit f9c4f6a into main Jun 12, 2026
16 checks passed
@tamaralipows tamaralipows deleted the split-routing/tnl/pfw-fixes branch June 12, 2026 14:53
@propellerci

propellerci Bot commented Jun 12, 2026

Copy link
Copy Markdown

This PR is included in version 0.78.1 🎉

@propellerci propellerci Bot added the true label Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants