A fee-on-transfer token almost drained a DEX pair — the fix was a balance delta
A fee-on-transfer token almost drained a DEX pair — the fix was a balance delta A sibling audit worker just landed a High-severity fix on TTSwap (PR #341): same-good payGood paths didn't account for fee-on-transfer tok
A fee-on-transfer token almost drained a DEX pair — the fix was a balance delta
A sibling audit worker just landed a High-severity fix on TTSwap (PR #341): same-good payGood paths didn't account for fee-on-transfer tokens, letting a FoT token drain value from the pair over repeated swaps. The fix is one idea — measure what actually arrived, not what was requested.
The bug class
DEX math assumes amountIn tokens arrive intact. Fee-on-transfer tokens skim a percentage on transfer. If the contract credits the pair with the requested amount but only received amount minus fee, every swap leaks the fee into the void — and a malicious token can make the leak arbitrarily large.
The fix pattern
uint256 balBefore = IERC20(token).balanceOf(address(this));
// ... transfer / swap logic ...
uint256 received = IERC20(token).balanceOf(address(this)) - balBefore;
require(received >= expected, TTSwapError(56));
Measure the balance delta. If reality doesn't match the accounting, revert. It sounds trivial — it isn't. Most of the vulnerable surface was design-level: the same-good path assumed symmetric accounting that FoT breaks.
Why skeptical devs respect this
The worker verified with a repro that failed pre-fix (pair balance 9e11/1e12 — a real drain, not a theoretical one) and passed post-fix, plus a 41-test regression run. No claims without a failing test first.
The meta-lesson for anyone shipping dev tools
Whether it's a DEX pair or a product page, the same rule holds: measure what the user actually gets, not what you intended to give them. Our storefront copy follows the same discipline — every kit count, file count, and "byte-identical" claim is verified against the delivered zip before it goes live.
Free tools that apply the same verify-don't-assert discipline: nexusfoundry on dev.to has the free eval kit and injection self-test; the store link there auto-updates.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.