Open sherlock-admin3 opened 6 months ago
2 comment(s) were left on this issue during the judging contest.
santipu_ commented:
Medium
takarez commented:
valid; medium(8)
Confirmed, this is valid. Thanks for finding this bug!
We're still figuring out a fix, or we might probably just disable fundingFee entirely if we cannot solve it before launch.
Escalate
I think this is a high
risk issue.
This has a high likelihood
of occurrence, as it can happen easily.
The impact is also high
due to the absence of available funding sources
in case of bad debt
.
Consequently, users' margins
and borrowing fees
will be reduced.
And the sponsor has confirmed their intention to disable this feature if a suitable solution is not found. This shows the severity of the impact.
Anyway, this is my first contest in Sherlock and am not familiar with the rules. I hope for a kind review. Thanks in advance.
Escalate
I think this is a
high
risk issue. This has ahigh likelihood
of occurrence, as it can happen easily. The impact is alsohigh
due to the absence ofavailable funding sources
in case ofbad debt
. Consequently, users'margins
andborrowing fees
will be reduced.And the sponsor has confirmed their intention to disable this feature if a suitable solution is not found. This shows the severity of the impact.
Anyway, this is my first contest in Sherlock and am not familiar with the rules. I hope for a kind review. Thanks in advance.
The escalation could not be created because you are not exceeding the escalation threshold.
You can view the required number of additional valid issues/judging contest payouts in your Profile page, in the Sherlock webapp.
Hi @nevillehuang , thanks for your judging.
I need assistance with escalation, as I couldn't create it. What do I need to do for this?
Thanks.
Escalate
I think this is a high risk issue. This has a high likelihood of occurrence, as it can happen easily. The impact is also high due to the absence of available funding sources in case of bad debt. Consequently, users' margins and borrowing fees will be reduced.
And the sponsor has confirmed their intention to disable this feature if a suitable solution is not found. This shows the severity of the impact.
Escalate
I think this is a high risk issue. This has a high likelihood of occurrence, as it can happen easily. The impact is also high due to the absence of available funding sources in case of bad debt. Consequently, users' margins and borrowing fees will be reduced.
And the sponsor has confirmed their intention to disable this feature if a suitable solution is not found. This shows the severity of the impact.
You've created a valid escalation!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
There may be
meaning it won't necessarily happen (i.e. Medium). The funding fee does not cause bad debt, it just temporarily causes there to be fewer funds in the pool than expected. As soon as the funding flips to the other side, it's likely to be remedied without any intervention at all (as is indicated by the fact that the title begins with There may be excess funds
). The readme says PnL Pool currently starts empty, so a trader with realized gains may experience temporary illiquidity when he tries to withdraw those gains. This inconvenience will be mitigated in the future with a buffer balance
so the pool is expected to have temporary liquidity problems for traders, which are expected to be mitigated. There is no bad debt being created here.
Let's change There may be
to There should be
.
In cases where the opposite open notional is larger, there should be bad debt
.
The PnL pool
involves the user's margin
and borrowing fees
.
When users withdraw their funding fees
, it is deducted from the PnL pool
, which involves the user's margin
and borrowing fees
.
And As soon as the funding flips to the other side,
: this is just assumption.
If we evaluate things in this manner, the likelihood of most issues will be very low.
@etherSky111 @IllIllI000 How often can this situation of excess funds occur?
This situation can easily happen (The likelihood
is really high).
There are 2
possible states.
1
. The opposite open notional
is larger.
2
. vice versa
In case 1, there will be bad debts
due to there are more receivers
than payers
.
Users' PnL
, borrowing fees
and funding fees
are settled through PnL pool
.
If there are X bad debts
in funding fees
, these will be deducted from PnL pool
which includes users' PnL
and borrowing fees
.
It's important to note that there's no guarantee that the market will swiftly transition from the first case to the second.
It will disrupt the equilibrium of the market
.
Thanks.
There is no bad debt created. Bad debt is when a specific user has losses that are greater than the margin collateral backing the position, which means it will never be paid back and the system as a whole will have a loss forever. In this case, it's a temporary liquidity deficit that will be resolved when the opposite side becomes the larger open notional. As I've pointed out here, according to the readme, it's expected that there are temporary liquidity deficits, so that is not a security risk. The only thing broken here is that the sponsor though that only trader PnL could cause the temporary deficit, but with this issue, the funding fee can cause it too.
Are you sure 100%?
when the opposite side becomes the larger open notional.
As I said earlier, this is just an assumption.
There has never been a perpetuals market, where the funding fee stayed only on one side. The whole point of the fee is to incentivize people to open positions on the other side of the market in order to make the futures price equal the spot price, at which point the fee rate will become zero again, and then there will be a random walk of orders coming in, which will randomly choose the next side with the larger open notional.
Ensuring that the sum of funding fees
aligns with 0
is not guaranteed.
And this is not a temporary liquidity deficits
.
This deficit is based on the fact that the sum of borrowing fees
and users' PnL
equal 0
.
As a newcomer to Sherlock
, I'm unfamiliar with the rules, so I'll leave it to the judge to decide, and I'll respect his decision.
Thank you.
Based on discussions, I believe medium severity is appropriate for this issue.
Agree with the Lead Judge, Medium is indeed appropriate here due to requirement of specific state. Hence, planning to reject the escalation and leave the issue as it is.
Result: Medium Unique
ether_sky
medium
There may be excess funds in the PnL pool or bad debt due to the funding fee.
Summary
There are two types of
makers
:OracleMaker
andSpotHedgeBaseMaker
, whereLPs
can deposit funds.Traders
can then execute theirorders
against thesemakers
. To incentivizeLPs
, several mechanisms exist for thesemakers
to profit. One is theborrowing fee
, which bothmakers
can benefit from. Another is thefunding fee
, which specifically benefitsOracleMaker
. Thefunding fee
incentivizes users to maintainpositions
with the same direction ofOracleMaker
. However, due to thefunding fee
, there may be excess funds in thePnL pool
or occurrences ofbad debt
.Vulnerability Detail
Typically, in most
protocols
, the generatedyields
are totally distributed to users and theprotocol
itself. In thePerpetual
protocol, allborrowing fees
frompayers
are solely distributed toreceivers
, which are whitelisted by theprotocol
. However, not allfunding fees
are distributed, or there may be a lack offunding fees
available for distribution. The currentfunding rate
is determined based on the currentposition
of thebase pool
(OracleMaker
).Holders of
positions
with the same direction of theposition
of theOracleMaker
receivefunding fees
, while those withpositions
in the opposite direction are required to payfunding fees
. The amount offunding fees
generated per second is calculated as the product of thefunding rate
and the sum ofopenNotionals
ofpositions
with the opposite direction. Conversely, the amount offunding fees
distributed per second is calculated as the product of thefunding rate
and the sum ofopenNotionls
ofpositions
with the same direction of theposition
of theOracleMaker
.All
orders
are settled againstmakers
, meaning for everylong position
, there should be an equivalentshort position
. While we might expect the sum ofopenNotionals
oflong positions
to be equal to theopenNotionals
ofshort positions
, in reality, they may differ.Suppose there are two
long positions
withopenNotional
values ofS
andS/2
. Then there should be twoshort positions
withopenNotianal
values of-S
and-S/2
. If the holder of the firstlong position
cancels hisorder
against the secondshort position
with-S/2
, theopenNotional
of thelong position
becomes0
, and the secondshort position
becomes along position
. However, we can not be certain that theopenNotional
of the newlong position
is exactlyS/2
.Indeed, the
openNotional
of the newlong position
is determined by the currentprice
. Consequently, while theposition size
of this newlong position
will be the same with the old secondlong position
with anopenNotional
value ofS/2
, theopenNotional
of the newlong position
can indeed vary fromS/2
. As a result, the sum ofopenNotionals
ofshort positions
can differ from the sum oflong positions
. There are numerous other scenarios where the sums ofopenNotionals
may vary.I believe that the developers also thought that the
funding fees
are totally used between it'spayers
andreceivers
from the below code.They even took
rounding
into serious consideration to prevent any shortfall offunding fees
for distribution.Impact
Excess
funding fees
in thePnL pool
can arise when the sum ofopenNotionals
of thepayers
exceeds that of thereceivers
. Conversely,bad debt
may occur in other cases, leading to a situation where users are unable to receive theirfunding fees
due to an insufficientPnL pool
. It is worth to note that otheryields
, such as theborrowing fee
, are entirely utilized between it'spayers
andreceivers
. Therefore, there are no additionalfunding sources
available to address any shortages offunding fees
.Code Snippet
https://github.com/sherlock-audit/2024-02-perpetual/blob/main/perp-contract-v3/src/fundingFee/FundingFee.sol#L133-L139 https://github.com/sherlock-audit/2024-02-perpetual/blob/main/perp-contract-v3/src/fundingFee/FundingFee.sol#L89-L102 https://github.com/sherlock-audit/2024-02-perpetual/blob/main/perp-contract-v3/src/vault/LibPosition.sol#L45-L48 https://github.com/sherlock-audit/2024-02-perpetual/blob/main/perp-contract-v3/src/fundingFee/FundingFee.sol#L183-L191
Tool used
Manual Review
Recommendation
We can calculate
funding fees
based on theposition size
because the sum of theposition sizes
oflong positions
will always be equal to the sum ofshort positions
in all cases.