AutoCompoundingPodLp `_swapV2()` may lead to leftover tokens in multihops
The AutoCompoundingPodLp contract's two-hop swap function may leave intermediate tokens stuck in the contract when a maxSwap limit is set for the intermediate token. This results in a loss of rewards because those leftover tokens are never used in future swaps.
0xpetern Follow with X77, benjamin_0923 and 2 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2025-01-peapods-finance-judging/issues/470
Found by
0xpetern, X77, benjamin_0923, pkqs90, silver_eth
Summary
AutoCompoundingPodLp _swapV2() may lead to leftover tokens in multihops.
Root Cause
First, let's see how the autocompounding process works: 1) swap rewardTKN -> pairedLpTKN, 2) swap a portion (roughly half) of pairedLpTKN -> pTKN, 3) add pairedLpTKN, pTKN to UniV2 LP, 4) stake LP token to spTKN.
In step 1, if rewardTKN is not PEAS, it would do a UniV2 swap. The swap path is predefined in swapMaps[in][out]. The code supports 2 hops, which is inside the _swapV2 logic.
The bug here is, for 2 hop swaps, the intermediate tokens may be left stuck in the contract, due to the maxSwap limit.
For example, a swap from tokenA -> tokenB -> tokenC. The first swap gets us 10000 tokenB, but we have maxSwap[tokenB] = 1000, so there would be 9000 tokenB leftover in the contract.
For future swaps, unless the tokenB output is 0, which is very unlikely to happen, these 9000 tokenB would be forever stuck in the contract.
function _tokenToPairedLpToken(address _token, uint256 _amountIn) internal returns (uint256 _amountOut) {
...
address _rewardsToken = pod.lpRewardsToken();
if (_token != _rewardsToken) {
@> _amountOut = _swap(_token, _swapOutputTkn, _amountIn, 0);
if (IS_PAIRED_LENDING_PAIR) {
_amountOut = _depositIntoLendingPair(_pairedLpToken, _swapOutputTkn, _amountOut);
}
return _amountOut;
}
...
}
function _swap(address _in, address _out, uint256 _amountIn, uint256 _amountOutMin)
internal
returns (uint256 _amountOut)
{
Pools memory _swapMap = swapMaps[_in][_out];
if (_swapMap.pool1 == address(0)) {
address[] memory _path1 = new address[](2);
_path1[0] = _in;
_path1[1] = _out;
return _swapV2(_path1, _amountIn, _amountOutMin);
}
bool _twoHops = _swapMap.pool2 != address(0);
address _token0 = IUniswapV2Pair(_swapMap.pool1).token0();
address[] memory _path = new address[](_twoHops ? 3 : 2);
_path[0] = _in;
_path[1] = !_twoHops ? _out : _token0 == _in ? IUniswapV2Pair(_swapMap.pool1).token1() : _token0;
if (_twoHops) {
_path[2] = _out;
}
@> _amountOut = _swapV2(_path, _amountIn, _amountOutMin);
}
function _swapV2(address[] memory _path, uint256 _amountIn, uint256 _amountOutMin)
internal
returns (uint256 _amountOut)
{
bool _twoHops = _path.length == 3;
if (maxSwap[_path[0]] > 0 && _amountIn > maxSwap[_path[0]]) {
_amountOutMin = (_amountOutMin * maxSwap[_path[0]]) / _amountIn;
_amountIn = maxSwap[_path[0]];
}
IERC20(_path[0]).safeIncreaseAllowance(address(DEX_ADAPTER), _amountIn);
_amountOut =
DEX_ADAPTER.swapV2Single(_path[0], _path[1], _amountIn, _twoHops ? 0 : _amountOutMin, address(this));
if (_twoHops) {
// @audit-bug: There may be leftovers.
@> uint256 _intermediateBal = _amountOut > 0 ? _amountOut : IERC20(_path[1]).balanceOf(address(this));
@> if (maxSwap[_path[1]] > 0 && _intermediateBal > maxSwap[_path[1]]) {
_intermediateBal = maxSwap[_path[1]];
}
IERC20(_path[1]).safeIncreaseAllowance(address(DEX_ADAPTER), _intermediateBal);
_amountOut = DEX_ADAPTER.swapV2Single(_path[1], _path[2], _intermediateBal, _amountOutMin, address(this));
}
}
Internal pre-conditions
- maxSwap[] is set for the intermediate token
External pre-conditions
N/A
Attack Path
No attack path required. Explained above.
Impact
Intermediate token is stuck in contract, leading to loss of rewards.
PoC
N/A
Mitigation
Always use entire balance of intermediate token for swap.
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 the code caps the intermediate token amount to maxSwap but does not handle the leftover balance, which then remains stuck in the contract.
Impact
Intermediate tokens become stuck in the contract, leading to a loss of rewards.
What the attacker needed
MaxSwap[] is set for the intermediate token
The fix
The contract should always use the entire balance of the intermediate token for the swap.
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 swapping one token for another through an intermediate token, the contract sometimes limits how much of the intermediate token it will swap. If the limit is lower than the amount received, the extra tokens stay in the contract and are never used again, so they are effectively lost.
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.