Skip to content

feat(PFW): compute_probe_amount and compute_average_price_impact#230

Merged
tamaralipows merged 4 commits into
mainfrom
split-routing/tnl/ENG-5854-probe-amount-and-impact
Jun 5, 2026
Merged

feat(PFW): compute_probe_amount and compute_average_price_impact#230
tamaralipows merged 4 commits into
mainfrom
split-routing/tnl/ENG-5854-probe-amount-and-impact

Conversation

@tamaralipows
Copy link
Copy Markdown
Contributor

@tamaralipows tamaralipows commented Jun 2, 2026

Task

Context: In the PFW algorithm, after we route the full trade through the best single path (via BF), we want to know: is there a second path worth splitting to? We can't just re-run BF at the full trade amount - BF would find the same path again because nothing changed. Instead, we:

  1. "Degrade" the pools on the first path (simulate their post-swap state, as if the trade already went through)
  2. Run BF again with a small probe amount on all pools, including those degraded pools, in oder to discover a candidate second path.

Note: This part of the task is not relevant yet, but we should keep in mind that when calling these methods:

gas_cost_output_tokens is obtained from ctx.gas_price_wei and the output token's price from ctx.token_prices. If token price data is unavailable, fall back to total_amount * 0.10 (10% of trade) and always return Some.

@tamaralipows tamaralipows marked this pull request as draft June 2, 2026 21:05
@tamaralipows tamaralipows force-pushed the split-routing/tnl/ENG-5854-probe-amount-and-impact branch 5 times, most recently from e2d93f4 to 4ca80b9 Compare June 2, 2026 22:37
Comment thread fynd-core/src/algorithm/path_frank_wolfe.rs Outdated
@tamaralipows tamaralipows marked this pull request as ready for review June 2, 2026 22:45
Copy link
Copy Markdown
Collaborator

@dianacarvalho1 dianacarvalho1 left a comment

Choose a reason for hiding this comment

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

Thank you @tamaralipows ! 🙏🏼 it looks good to me! only have 1 question for my understanding

Comment on lines +125 to +127
if price_impact <= 0.0 {
return None;
}
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.

in which case is this possible? 🤔

Copy link
Copy Markdown
Contributor Author

@tamaralipows tamaralipows Jun 3, 2026

Choose a reason for hiding this comment

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

The pool has a flat pricing curve (e.g. stable-swap pools within their peg range). A verrryyy real example: WETH-ETH unwrapping.

Comment thread fynd-core/src/algorithm/path_frank_wolfe.rs
/// Returns `None` when the probe exceeds `config.max_probe × total_amount`,
/// signalling that splitting is not worthwhile.
#[allow(dead_code)]
fn compute_probe_amount(
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.

why isn't this a method of the Algorithm struct? same for the compute_average_price_impact

Copy link
Copy Markdown
Contributor Author

@tamaralipows tamaralipows Jun 3, 2026

Choose a reason for hiding this comment

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

I've left them free because neither function needs &self - they're pure computations with explicit inputs. Keeping them as free functions also makes tests simpler: you can call them directly without constructing a full PathFrankWolfeAlgorithm (which in turn requires setting up BellmanFordAlgorithm).

I can also see the organization benefit of moving them into the algorithm though - so please see commit c8308f7 and let me know if you like that better!

Copy link
Copy Markdown
Contributor

@Troshchk Troshchk left a comment

Choose a reason for hiding this comment

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

Thank you!
I have a general question: why do we degrade the pools on the first path, as if the full trade already went through? Why would not we use max_probe, for example?

Comment thread fynd-core/src/algorithm/path_frank_wolfe.rs Outdated
Comment thread fynd-core/src/algorithm/path_frank_wolfe.rs Outdated
@tamaralipows tamaralipows force-pushed the split-routing/tnl/ENG-5853-pfw-structs branch from 63984f1 to f78e247 Compare June 3, 2026 18:01
@tamaralipows tamaralipows force-pushed the split-routing/tnl/ENG-5854-probe-amount-and-impact branch 2 times, most recently from b7756e3 to 4f20708 Compare June 3, 2026 18:30
@tamaralipows
Copy link
Copy Markdown
Contributor Author

I have a general question: why do we degrade the pools on the first path, as if the full trade already went through? Why would not we use max_probe, for example?

@Troshchk There is more context about this in the story. This is actually a core property of the Frank-Wolfe method. In particular this line:

Ask: "If I were already routing everything through this path, where would BF send a small probe trade?" BF sees the pool states after the big trade, so it finds a path that possibly avoids the now-depleted pools. This is the candidate second path.

Keep in mind this step doesn't determine the split amount - it only finds candidate paths. We degrade by the full amount because that's what Frank-Wolfe requires: evaluate the descent direction at the current allocation (initially 100% on path 1). If we degraded by less, BF would see a less-stressed pool and might miss better alternatives or pick the wrong one.

The actual split fraction is decided afterwards by golden-section line search (step 2f), which finds the optimal balance between the existing allocation and the new candidate. So this full degradation is just "where should I look next?", not "how much should I send there?"

Copy link
Copy Markdown
Collaborator

@louise-poole louise-poole left a comment

Choose a reason for hiding this comment

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

Small error message feedback, otherwise approved! Looks good 🚀

Comment thread fynd-core/src/algorithm/path_frank_wolfe.rs
@tamaralipows tamaralipows force-pushed the split-routing/tnl/ENG-5853-pfw-structs branch 2 times, most recently from c6b06ac to 7ff2d81 Compare June 4, 2026 14:51
@tamaralipows tamaralipows self-assigned this Jun 4, 2026
Copy link
Copy Markdown
Collaborator

@louise-poole louise-poole left a comment

Choose a reason for hiding this comment

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

Nice! Happy with this 👍

Copy link
Copy Markdown
Collaborator

@dianacarvalho1 dianacarvalho1 left a comment

Choose a reason for hiding this comment

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

✨ ✨ ✨

@louise-poole louise-poole force-pushed the split-routing/tnl/ENG-5853-pfw-structs branch from 7ff2d81 to 6e651ca Compare June 5, 2026 12:50
Base automatically changed from split-routing/tnl/ENG-5853-pfw-structs to main June 5, 2026 12:53
@tamaralipows tamaralipows force-pushed the split-routing/tnl/ENG-5854-probe-amount-and-impact branch 2 times, most recently from 9057f63 to 02f0623 Compare June 5, 2026 13:13
tamaralipows and others added 4 commits June 5, 2026 09:16
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…eAlgorithm

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ount

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@tamaralipows tamaralipows force-pushed the split-routing/tnl/ENG-5854-probe-amount-and-impact branch from 02f0623 to 4ac233d Compare June 5, 2026 13:16
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 5, 2026

No API Breaking Changes Detected

The PR title signals breaking changes, but cargo-semver-checks found none.
If the breaking change is behavioral, CLI, or config-level (not public Rust API), this is expected.
Otherwise, consider using fix: instead of feat: in the PR title.

@tamaralipows tamaralipows merged commit 39fff3a into main Jun 5, 2026
15 checks passed
@tamaralipows tamaralipows deleted the split-routing/tnl/ENG-5854-probe-amount-and-impact branch June 5, 2026 13:18
@propellerci
Copy link
Copy Markdown

propellerci Bot commented Jun 5, 2026

This PR is included in version 0.75.0 🎉

@propellerci propellerci Bot added the true label Jun 5, 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