Open sherlock-admin4 opened 5 months ago
Not a issue: Mechanistically, it is neutral in the long term, and the mechanism balances the impact of funding fee imbalances.
@0xELFi02 What exactly is the design choice here that makes it neutral in the long term to balance funding fee imbalance? Since it was not noted in the READ.ME, I believe this issue could be valid
Same comments applies for issue #33, #102, #258
For the funding fee, we will use the pool as an intermediary for receiving and paying. The pool will bear the risk of timing differences in funding fee settlements. During a certain period, the pool may either profit or incur losses. Over a longer period, we believe that these fluctuations will remain within a certain range.
mstpr-brainbot
High
Pool value does not consider the open funding fees
Summary
The pool's value is not considering a vital component: the open funding fees. The pool value is used when calculating staking token mint/redeem shares, and since the funding fees are not accounted for, minting/redeeming of shares will not be accurate. Additionally, someone can exploit this by sandwiching a closing position, knowing that the funding fees will be realized when the position is closed, and take advantage of the previous pool value.
Vulnerability Detail
First, let's see how the pools value is calculated:
Simply, considering there are no stable tokens in a pool the total value is: baseTokenBalance.amount + baseTokenBalance.unsettledAmount + marketPnL
Little bit more detail on the
unsettledAmount
:unsettledAmount
is only accounted when a position is updated. For example when closing a position or increasing a positions margin. Also, it will change via funding fees. Since the previous actions changes the funding fee theunsettledAmount
will also change.When a position is closed the funding fees will accounted in
unsettledAmount
which previously it wasn't accounted as follows:So if there are some funding fees accrued in the life time of the position they are now added to the pools
unsettledAmount
which this amount is directly affecting the pools value.If the closed position is "cross"
unsettledAmount
is not resetted as we can see here:Hence, the
unsettledAmount
is increased and pools value changed without any changes in stake token supply creating a discrepancy in the share calculation.Share calculations for minting and redeeming is like ERC4626 just for a reference let's see how minting new shares are calculated:
As we can observe, the increase on
unsettledAmount
will spike the pools value and share calculations will not be correct.Coded PoC:
Test Logs: Pool value very beginning 2497000000000000000000000n Pool value with next oracle 2897900000000000000010000n Pool value with next oracle after funding fees 2897900000000000000010000n Pool value final 2910696155073654825000000n
Impact
Pools value will spike when positions are updated. This will create unfair minting/redeeming for shares.
Code Snippet
https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/process/LpPoolQueryProcess.sol#L110-L144
https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/process/DecreasePositionProcess.sol#L150-L156
https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/process/MarketProcess.sol#L104-L127
https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/process/FeeProcess.sol#L102-L137
https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/process/DecreasePositionProcess.sol#L64-L65
Tool used
Manual Review
Recommendation
Account the net funding fee market will have considering all users positions and add it to the pools value calculation.