Rounding error when call function `dodoMultiswap()` can lead to revert of transaction or fund of user
The split-swap calculation in `_multiSwap` ignores integer rounding, so the sum of transferred amounts can be less than the total input. This can cause a transaction to revert if a strict minimum return is set, or leave a small amount of tokens stranded for the sender.
Source: https://github.com/sherlock-audit/2022-11-dodo-judging/issues/45
Found by
TrungOre
Summary
The calculation of the proportion when do the split swap in function _multiSwap doesn't care about the rounding error
Vulnerability Detail
The amount of midToken will be transfered to the each adapter can be calculated by formula curAmount = curTotalAmount * weight / totalWeight
if (assetFrom[i - 1] == address(this)) {
uint256 curAmount = curTotalAmount * curPoolInfo.weight / curTotalWeight;
if (curPoolInfo.poolEdition == 1) {
//For using transferFrom pool (like dodoV1, Curve), pool call transferFrom function to get tokens from adapter
IERC20(midToken[i]).transfer(curPoolInfo.adapter, curAmount);
} else {
//For using transfer pool (like dodoV2), pool determine swapAmount through balanceOf(Token) - reserve
IERC20(midToken[i]).transfer(curPoolInfo.pool, curAmount);
}
}
It will lead to some scenarios when curTotalAmount * curPoolInfo.weight is not divisible by curTotalWeight, there will be some token left after the swap.
For some tx, if user set a minReturnAmount strictly, it may incur the reversion.
For some token with small decimal and high value, it can make a big loss for the sender.
Impact
- Revert the transaction because not enough amount of
toToken - Sender can lose a small amount of tokens
Code Snippet
Tool used
Manual review
Recommendation
Add a accumulation variable to maintain the total amount is transfered after each split swap. In the last split swap, instead of calculating the curAmount by formula above, just take the remaining amount to swap.
Discussion
Attens1423
https://github.com/DODOEX/dodo-route-contract/pull/3
aktech297
Fixes are verified.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely integer division truncation in the per-pool amount calculation, with no accumulation of the remainder for the final split.
Impact
The transaction may revert because the output is below the user's minimum, and the sender could lose a small amount of tokens, especially for tokens with few decimals and high value.
What the attacker needed
Swap split across multiple pools; weights do not divide evenly; strict minimum return set
The fix
The recommendation is to track the total transferred amount and, on the last split, transfer the remaining amount instead of recalculating it; the linked fix was verified.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When a smart contract splits a swap across several pools, it calculates each share with a division. Division in Solidity rounds down, so the shares can add up to slightly less than the original amount. That leftover can make the swap fail if the user demanded a minimum output, or it can be lost.