Open fee is overcharged in `_addLeveragePostCallback` function
The contract charges an open fee based on the user's desired paired token amount rather than the amount actually used in the liquidity provision, causing users to overpay fees on leftover tokens. An attacker could exploit this by setting a high desired amount to increase the overpayment, though the primary impact is accidental overcharging.
Source: https://github.com/sherlock-audit/2025-01-peapods-finance-judging/issues/185
Found by
ZoA
Summary
In the _addLeveragePostCallback function, the user pays an open fee proportional to pairedLpDesired, which is the amount of pairedLPToken that the user has decided to use for lpAndStakeInPod.
In fact, there are pairedLPToken that are used for lpAndStakeInPod and leftover. It is inccorect to pay fees for these leftover tokens.
In short, fees are overpaid by first calculating the fees without any guarantee that all pairedLPTokens are used for lpAndStakeInPod..
Root Cause
In the _addLeveragePostCallback function, the user actually pays by the amount of pairedLPToken that he has desired, not by the amount of pairedLPToken used for lpAndStakeInPod.
pairedLPToken reduced by _openFeeAmt from the user desired amount , is really used in lpAndStakeInPod, and after calling _lpAndStakeInPod, pairedLPToken remains as much as _pairedLeftover.
The amount of pairedLPToken used in lpAndStakeInPod is returned from _lpAndStakeInPod function as the second return value.
function _addLeveragePostCallback(bytes memory _data) internal returns (uint256 _ptknRefundAmt) {
IFlashLoanSource.FlashData memory _d = abi.decode(_data, (IFlashLoanSource.FlashData));
(LeverageFlashProps memory _props,) = abi.decode(_d.data, (LeverageFlashProps, bytes));
(uint256 _overrideBorrowAmt,,) = abi.decode(_props.config, (uint256, uint256, uint256));
address _pod = positionProps[_props.positionId].pod;
uint256 _borrowTknAmtToLp = _props.pairedLpDesired;
// if there's an open fee send debt/borrow token to protocol
if (openFeePerc > 0) {
uint256 _openFeeAmt = (_borrowTknAmtToLp * openFeePerc) / 1000;
IERC20(_d.token).safeTransfer(feeReceiver, _openFeeAmt);
_borrowTknAmtToLp -= _openFeeAmt;
}
@> (uint256 _pTknAmtUsed,, uint256 _pairedLeftover) = _lpAndStakeInPod(_d.token, _borrowTknAmtToLp, _props);
_ptknRefundAmt = _props.pTknAmt - _pTknAmtUsed;
...
}
The user overpays the fee by paying the fee before executing lpAndStakeInPod, even though it is not known exactly how much pairedLPToken will be used in lpAndStakeInPod.
If the user does not accurately calculate the amount of Pod tokens and pairedLPToken required for lpAndStakeInPod and sets pairedLpDesired to a value larger than the amount of Pod tokens, the consequence of overpaying fees is more serious.
Attack Path
- User sets the pod token and
pairedLPTokenamount like this:pTknAmt = 100,pairedLpDesired = 140,openFeePerc=5% - Amount of
pairedLPTokenis decreased byopenFeeAmt._openFeeAmt = 7,_borrowTknAmtToLp = pairedLpDesired - _openFeeAmt = 133 - After
_lpAndStakeInPod, remainingpairedLPTokenamount in contract is20. This means real used amount of the pod token andpairedLPTokenfor_lpAndStakeInPodis 100 and 113. - User also paid the open fee for the
pairedLPTokenof 20 that was not used for_lpAndStakeInPod.
Impact
Users will pay more open fees than the amount of pairedLPToken actually used for lpAndStakeInPod.
Mitigation
- First decrease
_borrowTknAmtToLpbyopenFeePercand execute_lpAndStakeInPod, and then calculate the open fee again.
function _addLeveragePostCallback(bytes memory _data) internal returns (uint256 _ptknRefundAmt) {
...
if (openFeePerc > 0) {
uint256 _openFeeAmt = (_borrowTknAmtToLp * openFeePerc) / 1000;
-- IERC20(_d.token).safeTransfer(feeReceiver, _openFeeAmt);
_borrowTknAmtToLp -= _openFeeAmt;
}
-- (uint256 _pTknAmtUsed,, uint256 _pairedLeftover) = _lpAndStakeInPod(_d.token, _borrowTknAmtToLp, _props);
++ (uint256 _pTknAmtUsed, uint256 _pairedLpUsed, uint256 _pairedLeftover) = _lpAndStakeInPod(_d.token, _borrowTknAmtToLp, _props);
++ if (openFeePerc > 0) {
++ _openFeeAmt = (_pairedLpUsed * openFeePerc) / 1000;
++ IERC20(_d.token).safeTransfer(feeReceiver, _openFeeAmt);
++ }
...
}
- Or calculate the open fee based on the amount really used for
_lpAndStakeInPod.
function _addLeveragePostCallback(bytes memory _data) internal returns (uint256 _ptknRefundAmt) {
...
-- if (openFeePerc > 0) {
-- uint256 _openFeeAmt = (_borrowTknAmtToLp * openFeePerc) / 1000;
-- IERC20(_d.token).safeTransfer(feeReceiver, _openFeeAmt);
-- _borrowTknAmtToLp -= _openFeeAmt;
-- }
-- (uint256 _pTknAmtUsed,, uint256 _pairedLeftover) = _lpAndStakeInPod(_d.token, _borrowTknAmtToLp, _props);
++ (uint256 _pTknAmtUsed, uint256 _pairedLpUsed, uint256 _pairedLeftover) = _lpAndStakeInPod(_d.token, _borrowTknAmtToLp, _props);
uint256 _aspTknCollateralBal = _spTknToAspTkn(IDecentralizedIndex(_pod).lpStakingPool(), _pairedLeftover, _props);
uint256 _flashPaybackAmt = _d.amount + _d.fee;
uint256 _borrowAmt = _overrideBorrowAmt > _flashPaybackAmt ? _overrideBorrowAmt : _flashPaybackAmt;
++ _borrowAmt += (_pairedLpUsed * openFeePerc) / 1000
address _aspTkn = _getAspTkn(_props.positionId);
IERC20(_aspTkn).safeTransfer(positionProps[_props.positionId].custodian, _aspTknCollateralBal);
LeveragePositionCustodian(positionProps[_props.positionId].custodian).borrowAsset(
positionProps[_props.positionId].lendingPair, _borrowAmt, _aspTknCollateralBal, address(this)
);
++ if (openFeePerc > 0) {
++ uint256 _openFeeAmt = (_borrowTknAmtToLp * openFeePerc) / 1000;
++ IERC20(_d.token).safeTransfer(feeReceiver, _openFeeAmt);
++ }
...
}
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
The open fee is calculated and transferred before the actual amount of paired tokens used in liquidity provision is known, using the desired amount instead of the used amount.
What the attacker needed
User initiates leverage with pairedLpDesired > used amount; openFeePerc > 0
The fix
The open fee should be calculated based on the amount of pairedLPToken actually used in lpAndStakeInPod, not the desired amount.
How it was found
- User sets the pod token and pairedLPToken amount like this: pTknAmt = 100, pairedLpDesired = 140, openFeePerc=5%
- Amount of pairedLPToken is decreased by openFeeAmt. _openFeeAmt = 7, _borrowTknAmtToLp = pairedLpDesired - _openFeeAmt = 133
- After _lpAndStakeInPod, remaining pairedLPToken amount in contract is 20. This means real used amount of the pod token and pairedLPToken for _lpAndStakeInPod is 100 and 113.
- User also paid the open fee for the pairedLPToken of 20 that was not used for _lpAndStakeInPod.
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 add money to a liquidity pool, the system takes a small fee. This bug calculates that fee based on how much you said you wanted to add, not how much was actually used. So if some of your money is left over, you still pay a fee on it, meaning you pay more than you should.
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.