Skip to content

fix(token-analyzer): avoid underflow in transfer fee calculation#1066

Open
mev15 wants to merge 1 commit into
propeller-heads:mainfrom
mev15:fix/token-fee-calc-underflow
Open

fix(token-analyzer): avoid underflow in transfer fee calculation#1066
mev15 wants to merge 1 commit into
propeller-heads:mainfrom
mev15:fix/token-fee-calc-underflow

Conversation

@mev15

@mev15 mev15 commented Jun 4, 2026

Copy link
Copy Markdown

calculate_fee computed the inbound fee as balance_before_in + (amount - balance_after_in). The inner amount - balance_after_in underflows when the settlement contract already holds more of a fee-on-transfer token than the per-transfer fee, so the function returned Err and the token was misclassified as Bad — the source of the frequent "Failed to calculate transfer fee: overflow" logs. Tokens the settlement holds none of (e.g. sDAI) were unaffected.

Computing (balance_before_in + amount) - balance_after_in restores the original left-associative order from before the checked-arithmetic rewrite. The recipient leg gets the same change.

Adds two unit tests in common.rs: one reproduces the underflow (settlement holds a prior balance above the fee), one covers the zero-prior-balance case.

calculate_fee evaluated the inbound fee as
`balance_before_in + (amount - balance_after_in)`. The inner subtraction
underflows when the settlement already holds more of a fee-on-transfer token
than the per-transfer fee, so it returned Err and the token was misclassified
as Bad. Evaluate `(balance_before_in + amount) - balance_after_in` instead,
restoring the original left-associative order. Apply the same fix to the
recipient leg and add unit tests for the prior-balance and zero-balance cases.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant