usedFunds is wrong in LiquidityPool, and usedFunds tracks spent quote tokens. usedFunds is an important state in LiquidityPool, so the impact will be high.
Proof of Concept
Liquidity.closeLong and openShort don't update the state usedFunds correctly.
In the implementation of closeLong, tradeCost is added to usedFunds.
usedFunds += int256(tradeCost);
But tradeCost already contains hedgingFees and hedgingFees are added to usedFunds in _hedge method before.
So hedgingFees are added to usedFunds twice, and usedFunds will be wrong. There are similar things in openShort method, too. In the implementation of openShort, hedgingFees are added to usedFunds twice from direct addition and _hedge method similarly.
Lines of code
https://github.com/code-423n4/2023-03-polynomial/blob/aeecafc8aaceab1ebeb94117459946032ccdff1e/src/LiquidityPool.sol#L472-L484 https://github.com/code-423n4/2023-03-polynomial/blob/aeecafc8aaceab1ebeb94117459946032ccdff1e/src/LiquidityPool.sol#L807-L808 https://github.com/code-423n4/2023-03-polynomial/blob/aeecafc8aaceab1ebeb94117459946032ccdff1e/src/LiquidityPool.sol#L549 https://github.com/code-423n4/2023-03-polynomial/blob/aeecafc8aaceab1ebeb94117459946032ccdff1e/src/LiquidityPool.sol#L516
Vulnerability details
Impact
usedFunds
is wrong inLiquidityPool
, andusedFunds
tracks spent quote tokens.usedFunds
is an important state inLiquidityPool
, so the impact will be high.Proof of Concept
Liquidity.closeLong
andopenShort
don't update the stateusedFunds
correctly.In the implementation of
closeLong
,tradeCost
is added tousedFunds
.But
tradeCost
already containshedgingFees
andhedgingFees
are added tousedFunds
in_hedge
method before.So
hedgingFees
are added tousedFunds
twice, andusedFunds
will be wrong. There are similar things inopenShort
method, too. In the implementation ofopenShort
,hedgingFees
are added tousedFunds
twice from direct addition and_hedge
method similarly.Tools Used
Manual Review
Recommended Mitigation Steps
we can use
totalCost
instead oftradeCost
to updateusedFunds
as follows forcloseLong
. And same thing foropenShort
.And this is for
closeShort
: