Skip to content

Rust fix 4 - #168

Closed
johngrantuk wants to merge 4 commits into
mainfrom
rust-fix-4
Closed

Rust fix 4#168
johngrantuk wants to merge 4 commits into
mainfrom
rust-fix-4

Conversation

@johngrantuk

Copy link
Copy Markdown
Member

Report issue:

4. compute_and_charge_aggregate_swap_fees returns raw units but reads as scaled-18

Impact: the crate is internally safe, but the API shape misleads consumers. It cost us a real
bug that a wei-exact amountOut parity suite could not catch.

The helper mirrors the Vault and returns the fee in the token's raw units:

let total_swap_fee_amount_raw = to_raw_undo_rate_round_down(swap_fee_amount_scaled18, sf, rate)?;
Ok(mul_down_fixed(&total_swap_fee_amount_raw, aggregate_swap_fee_percentage)?)

vault::swap::swap binds it to a scaled-18 name and subtracts it from scaled-18 balances:

let aggregate_swap_fee_amount_scaled_18 = compute_and_charge_aggregate_swap_fees(...)?;
// ...
updated_balances[input_index] += amount_given_scaled_18 - aggregate_swap_fee_amount_scaled_18;

Inside the crate this is harmless — updated_balances only feeds AfterSwapParams, and swap()
returns Result<U256, _>, so for hookless pools the value is computed and discarded.

It is a trap for anyone reproducing that balance update, which a quoter must do to report post-swap
state for multi-hop routing. We copied the pattern and under-deducted the protocol fee by the
token's scaling factor — 1e12 for USDC, and exactly zero for 18-decimal tokens at a rate of one,
which is why it survived our parity suite for so long. Measured with a 6-decimal input token, 1%
swap fee, 25% aggregate share:

expected deduction: 250000000000000000   (scaled-18)
actual   deduction:            250000    (raw)

Suggested fix: rename the function to ..._raw, or return a newtype carrying the unit. Renaming
the local in vault::swap::swap alone would remove most of the hazard.

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.

1 participant