Router’s complexPath percentagePaths don’t work as expected
The complexPath function recalculates the token balance after each swap iteration, so percentages are applied to the remaining balance rather than the original total. This can cause users to lose funds if they specify percentages that sum to 100%.
Submitted by cmichel
The TridentRouter.complexPath function allows splitting a trade result into several buckets and trade them in a different pool each. The distribution is defined by the params.percentagePath[i].balancePercentage values:
for (uint256 i; i < params.percentagePath.length; i++) {
uint256 balanceShares = bento.balanceOf(params.percentagePath[i].tokenIn, address(this));
uint256 transferShares = (balanceShares * params.percentagePath[i].balancePercentage) / uint256(10)**8;
bento.transfer(params.percentagePath[i].tokenIn, address(this), params.percentagePath[i].pool, transferShares);
isWhiteListed(params.percentagePath[i].pool);
IPool(params.percentagePath[i].pool).swap(params.percentagePath[i].data);
}
However, the base value bento.balanceOf(params.percentagePath[i].tokenIn, address(this)); is recomputed after each iteration instead of caching it before the loop.
This leads to not all tokens being used even though the percentages add up to 100%.
POC
Assume I want to trade 50% DAI to WETH and the other 50% DAI to WBTC. In the first iteration, balanceShares is computed and then 50% of it is swapped in the first pool.
However, in the second iteration, balanceShares is updated again, and only 50% of the remaining (instead of the total) balance, i.e. 25%, is traded.
The final 25% are lost and can be skimmed by anyone afterwards.
Impact
Users can lose their funds using complexPath.
Recommended Mitigation Steps
Cache the balanceShares value once before the second for loop starts.
sarangparikh22 (Sushi) disputed:
This is not the correct way to calculate the complexPath swap parameters. For instance, if we need to swap 50% DAI to WETH and the other 50% DAI to WBTC, we would keep percentages as 50 and 100, instead of 50-50 as described above. If the user enters wrong percentages, they would loose funds.
The format for entering the percentages is not documented. Sustained as Sev 2 as the lack of documentation on this parameter could lead to loss of funds.
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
Impact
Users can lose funds when using complexPath if they specify percentages that sum to 100%, as the leftover tokens remain in the contract and can be taken by anyone.
What the attacker needed
User calls complexPath with percentages summing to 100%
The fix
The balance should be cached before the loop so that each percentage is applied to the initial total, and the parameter format should be documented.
How it was found
- Call complexPath with percentagePath entries that sum to 100% (e.g., 50% and 50%).
- The first iteration swaps 50% of the initial balance.
- The second iteration swaps 50% of the remaining balance (i.e., 25% of the initial).
- The remaining 25% stays in the contract and can be taken by anyone.
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 you split a trade into parts, you expect each part to be a percentage of your original amount. But here, after the first trade, the code looks at what's left and takes a percentage of that, so you end up trading less than you intended. The leftover tokens stay in the contract and anyone can take them.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.