Skip to content

fix: apply max_slip_fee cap in buy-side inversion - #1413

Closed
F3Joule wants to merge 21 commits into
masterfrom
f3/fix-slip-fee-cap
Closed

fix: apply max_slip_fee cap in buy-side inversion#1413
F3Joule wants to merge 21 commits into
masterfrom
f3/fix-slip-fee-cap

Conversation

@F3Joule

@F3Joule F3Joule commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes a bug where max_slip_fee cap was not applied during buy-side trade inversion in the
Omnipool math, breaking the invariant delta_hub_reserve_in − total_protocol_fee = D_net.

Changes:

  • calculate_buy_state_changes — after invert_buy_side_slip, check if cap fired and override
    d_gross with the capped linear formula; same check after invert_sell_side_fees for the
    sell-side leg of buy inversion
  • Add regression tests buy_slip_fee_cap_invariant_holds and sell_slip_fee_cap_invariant_holds

Related Issue

#1412

Motivation and Context

The forward path (calculate_slip_fee_amount) correctly caps the slip fee; the inverse path did
not - so the two paths produced inconsistent results when the slip rate exceeded max_slip_fee.
Sell trades are unaffected (no inversion involved).

How Has This Been Tested?

Two regression tests, one per inversion step, each verified to fail before the fix and pass after.

Checklist:

  • I have updated the documentation if necessary.
  • I have added tests to cover my changes, regression test if fixing an issue.
  • This is a breaking change.

@F3Joule F3Joule self-assigned this Apr 7, 2026
@github-actions

github-actions Bot commented Apr 7, 2026

Copy link
Copy Markdown

Crate versions that have been updated:

  • hydra-dx-math: v13.2.1 -> v13.2.2

Runtime version has not been increased.

@F3Joule
F3Joule marked this pull request as ready for review April 8, 2026 12:09
@F3Joule
F3Joule requested a review from enthusiastmartin April 8, 2026 12:12
*r_fresh.asset.delta_hub_reserve
);
}

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.

i would add a test for the case when both cap is fired, so the trade size is relative big compared to both asset in and asset out.

Comment thread math/src/omnipool/tests.rs
Comment thread math/src/omnipool/math.rs
Comment thread math/src/omnipool/math.rs
// Step 2: Invert buy-side slip to find D_gross from D_net
let d_gross = if let Some(slip) = slip {
invert_buy_side_slip(d_net, slip.asset_out_hub_reserve, slip.asset_out_delta)?
let d_gross_uncapped = invert_buy_side_slip(d_net, slip.asset_out_hub_reserve, slip.asset_out_delta)?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why we suddenly add bunch of code here ? same for the next change?

i havent thought it but i. believe it should be just possible to pass the max fee into the function and handle it there ?

@F3Joule F3Joule May 7, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I intentionally kept the cap logic in calculate_buy_state_changes to keep invert_* functions as pure math without business constraints. Passing max_slip_fee into them would work but couples the inversion logic to a policy decision.

Happy to go either way though - from my side it was just a matter of preference

@F3Joule
F3Joule requested a review from dmoka May 8, 2026 11:17
@github-actions

github-actions Bot commented May 8, 2026

Copy link
Copy Markdown

Quick benchmark at commit b5df01d has been executed successfully.
View results

@F3Joule
F3Joule requested a review from enthusiastmartin May 11, 2026 11:50
@enthusiastmartin

Copy link
Copy Markdown
Member

in favor of #1456

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants