Missing totalFunds update in LiquidityPool’s OpenShort() , causing LiquidityPool token holder to lose a portion of their token value
The openShort() function in LiquidityPool.sol fails to increase totalFunds by the net fees collected, so availableFunds is understated and the token price is lower than it should be. This causes liquidity pool token holders to lose part of their token value.
peakbolt Follow with auditor0517, 0xbepresent and 2 others
Originally disclosed on Code4rena
Submitted by peakbolt, also found by auditor0517, 0xbepresent, kaden, and 0xRobocop
The function openShort() in LiquidityPool.sol is missing an update to totalFunds, to increase LiquidityPool funds by the collected net fees.
Impact
As a result of the missing increment to totalFunds, the availableFunds in the LiquidityPool will be lower. This will impact the token price, causing a lower token price on openShort() trades. This will result in LiquidityPool token holders to lose part of their token value.
Detailed Explanation
The function openShort() is supposed to increase the totalFunds by (feesCollected - externalFee) as the trading fees is paid by the trader, via a deduction of the tradeCost.
totalCost = tradeCost - fees;
SUSD.safeTransfer(user, totalCost);
Proof of Concept
Add the following imports and test case to test/LiquidityPool.Trades.t.sol
import {wadMul} from "solmate/utils/SignedWadMath.sol";
import {IPerpsV2Market} from "../src/interfaces/synthetix/IPerpsV2Market.sol";
function testLiquidityPoolOpenShort() public {
uint256 amount = 1e18;
(uint256 markPrice, bool isInvalid) = pool.getMarkPrice();
uint256 tradeCost = amount.mulWadDown(markPrice);
uint256 fees = pool.orderFee(int256(amount));
uint256 delta = pool.getDelta();
int256 hedgingSize = wadMul(int256(amount), int256(delta));
IPerpsV2Market perp = pool.perpMarket();
(uint256 hedgingFees, ) = perp.orderFee(hedgingSize, IPerpsV2MarketBaseTypes.OrderType.Delayed);
uint256 feesCollected = fees - hedgingFees;
uint256 externalFee = feesCollected.mulWadDown(pool.devFee());
uint256 totalFundsBefore = pool.totalFunds();
int256 usedFundsBefore = pool.usedFunds();
// Open a Short trade
openShort(amount, amount * 1000, user_1);
// Calculated expected totalFunds and usedFunds
uint256 expectedTotalFunds = totalFundsBefore + feesCollected - externalFee;
uint256 marginRequired = tradeCost + hedgingFees;
int256 expectedUsedFunds = usedFundsBefore + int256(tradeCost) - int256(hedgingFees) + int256(marginRequired);
// This is incorrect as LiquidityPool's totalFunds is supposed to increase by net fee (feesCollected - externalFee)
assertLt(pool.totalFunds(), expectedTotalFunds);
// LiquidityPool's UsedFunds is also wrong and is higher than expected as it included hedgingFees.
assertGt(pool.usedFunds(), expectedUsedFunds);
uint256 poolAvailableFunds = pool.totalFunds() - uint256(pool.usedFunds());
uint256 expectedAvailableFunds = expectedTotalFunds - uint256(expectedUsedFunds);
// LiquidityPool's available funds is wrong and is less than expected, as totalFunds is not increased correctly
assertLt(poolAvailableFunds, expectedAvailableFunds);
assertEq(poolAvailableFunds, expectedAvailableFunds - hedgingFees);
// LiquidityPool's available funds is wrong and is also less than SUSD balance
assertLt(poolAvailableFunds, susd.balanceOf(address(pool)));
// LiquidityPool's available fund is expected to be the same as Pool's SUSD balance
assertEq(expectedAvailableFunds, susd.balanceOf(address(pool)));
// LiquidityPool Token price is less than expected.
assertLt(pool.getTokenPrice(), getExpectedTokenPrice(expectedTotalFunds, expectedUsedFunds, perp));
}
function getExpectedTokenPrice(uint256 expectedTotalFunds, int256 expectedUsedFunds, IPerpsV2Market perp) public returns (uint256 expectedTokenPrice) {
(uint256 markPrice,) = pool.getMarkPrice();
uint256 totalValue = expectedTotalFunds;
uint256 totalSupply = lqToken.totalSupply() + pool.totalQueuedWithdrawals();
uint256 amountOwed = markPrice.mulWadDown(powerPerp.totalSupply());
uint256 amountToCollect = markPrice.mulWadDown(shortToken.totalShorts());
//uint256 totalMargin = _getTotalMargin();
(uint256 totalMargin,) = perp.remainingMargin(address(pool));
totalValue += totalMargin + amountToCollect;
totalValue -= uint256((int256(amountOwed) + expectedUsedFunds));
expectedTokenPrice = totalValue.divWadDown(totalSupply);
}
Recommended Mitigation Steps
Add the following to update totalFunds with the net fee collection.
totalFunds += feesCollected - externalFee;
rivalq (Polynomial) disputed and commented:
Fee part is included in usedFunds, At any time pool’s total funds is not just included in
totalFunds, but some part of it is inusedFunds, note that usedFunds can be negative too.
As this issue was raised by several wardens, I’m willing to give this the benefit of the doubt and would like to ask the sponsor @rivalq to view it a second time. Perhaps looking through duplicated issues? They are mostly low quality and badly explained but might be on to something. https://github.com/code-423n4/2023-03-polynomial-findings/issues/46 is the duplicate with the most arguments.
I’d like to use this issue to bring attention to another issue, https://github.com/code-423n4/2023-03-polynomial-findings/issues/117, as it actually says the opposite (that
openShortis done right butcloseLonghas an extra update that shouldn’t exist). So, this one, which I repeat isn’t a duplicate but actually an opposite finding, might be right.
mubaris (Polynomial) confirmed and commented:
I’m confirming this from our side, although I think it’s a Medium risk similar to
#117.
As this is still considered a loss of funds for users, I’ll keep it as High.
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 openShort() function does not update totalFunds to include the net fees collected from the trade.
Impact
LiquidityPool token holders could lose part of their token value because the token price is lower than it should be.
What the attacker needed
A short trade is opened
The fix
The openShort() function should be updated to add the net fees to totalFunds.
How it was found
- Add the provided test case to the test file.
- Record totalFunds and usedFunds before the trade.
- Call openShort with a given amount.
- Calculate expected totalFunds and usedFunds.
- Assert that actual totalFunds is less than expected and usedFunds is greater than expected.
- Assert that available funds and token price are lower than expected.
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.
A liquidity pool holds money that users deposit, and each user gets tokens representing their share. The pool's token price is based on how much money it holds. When someone opens a short trade, they pay a fee, but the code forgets to add that fee to the pool's total funds, so the pool looks poorer than it is and each token is worth less.
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.