Round protocol fee up in quote computation#4389
Open
anxolin wants to merge 8 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the protocol fee calculation in get_vol_fee_adjusted_quote_data to use ceiling rounding, ensuring fees are rounded up for both sell and buy orders. It also adds an explicit zero-check for the division scale and includes unit tests to verify the new rounding logic. I have no feedback to provide.
jmg-duarte
reviewed
May 8, 2026
Contributor
jmg-duarte
left a comment
There was a problem hiding this comment.
Overall looks good to me and makes sense, just left some notes for small improvements
jmg-duarte
approved these changes
May 19, 2026
squadgazzz
reviewed
May 19, 2026
squadgazzz
reviewed
May 19, 2026
Make the volume-based protocol fee applied during quoting use ceiling division instead of floor, matching the pessimistic rounding already used for the network fee in `crates/shared/src/fee.rs`. The quote now never undercharges users at the wei level: SELL orders deliver a slightly smaller buy_amount, BUY orders quote a slightly larger sell_amount on inexact divisions.
Co-authored-by: José Duarte <15343819+jmg-duarte@users.noreply.github.com>
242c131 to
c5f02bd
Compare
|
Does this interact with how protocol fees are computed in the ranking? Or is it purely about the creation of orders from quotes? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Make fee rounding consistent.
Takes a pessimistic approach, the one who yields a:
buyAmountfor sell orders, which allows solvers to keep all fees and still fulfill the order. If we round it down, we might be promising the user one 1wei more, so when the solver takes the fee, the order might not be fillable.sellAmountfor buy orders should also round fees up, which makes the user to sign 1wei more so we can fill the order after taking the feesContext
I noticed a small divergence between what the quote endpoint returns, and what debug.cow.fi thinks it return based on its own calculations.
See this order:
0xd129e1a85ec79443540264ee8bb37841c9c4d5850c9b1c5ea6e1d764e8aff3e7cfe5660d6c55906ec8c488a466bd4f77f77eec8869fdbaebThe tool calculates the amount at slippage=0, which is basically what the API returns. In this case, returns
63774986, but if you check the logs you see63774987(it overpromises by 1 wei). See https://victorialogs.dev.cow.fi/goto/aflg4t470vz7kc?orgId=1Details
The volume-based protocol fee applied during quote computation in
crates/orderbook/src/quoter.rspreviously used floor division (checked_div), while the network fee incrates/shared/src/fee.rsuses ceiling. This PR aligns both to the same pessimistic policy: always round protocol fee UP.Effect at the wei level on inexact divisions:
buy_amount = quote.buy_amount − ceil(buy * factor / scale)— user receives slightly less buy, never more than the protocol can cover.sell_amount = quote.sell_amount + ceil((sell + network_fee) * factor / scale)— user pays slightly more sell, never less than required.The change touches only quote-time amounts shown to users; settlement-time fee accounting is unchanged.
Test plan
cargo test -p orderbook quoter::tests— 9/9 pass, including two new tests (test_volume_fee_rounds_up_sell_order,test_volume_fee_rounds_up_buy_order) that exercise non-exact divisions where floor and ceil diverge (12345 * 100 / 1_000_000 = 1.2345 → ceil = 2).cargo check -p orderbook