Skip to content
Audit finding Intermediate

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.

TrungOre Follow

Originally disclosed on Sherlock

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

https://github.com/sherlock-audit/2022-11-dodo/blob/main/contracts/SmartRoute/DODORouteProxy.sol#L415-L425

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.

The original report as published on Sherlock, by TrungOre. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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.

More by TrungOre

View all

7 more disclosed reports, best first.

More from DODO V3 update

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.