The getPendingFundingFeePerUnit
function in PerpMarket.sol incorrectly calculates funding rates by applying the unary
operation to rate averages. This issue impacts critical in-scope functions in LiquidationBranch and SettlementBranch by causing incorrect margin calculations and potentially preventing necessary liquidations.
Both LiquidationBranch::liquidateAccounts
and SettlementBranch::_fillOrder
rely on tradingAccount.getAccountMarginRequirementUsdAndUnrealizedPnlUsd()
, which uses the vulnerable funding rate calculation:
The issue manifests in margin calculations through this chain:
LiquidationBranch::liquidateAccounts
or SettlementBranch::_fillOrder
-> tradingAccount.getAccountMarginRequirementUsdAndUnrealizedPnlUsd()
-> position.getAccruedFunding(fundingFeePerUnit)
-> PerpMarket::getPendingFundingFeePerUnit
(where the bug occurs)
For example, during a market transition:
This causes incorrect accrued funding calculation in:
This impacts the liquidation and settlement logic:
Incorrect Liquidation Decisions
When LiquidationBranch::liquidateAccounts
checks account health via getAccountMarginRequirementUsdAndUnrealizedPnlUsd
, the inflated PnL from incorrect funding calculations may prevent necessary liquidations
For example, if a position should pay -5 in funding but receives +5 instead, their PnL is inflated by 10 units
Settlement Issues
SettlementBranch::_fillOrder
uses the same margin calculations for validation
Incorrect funding calculations could allow trades that should be rejected based on true margin requirements
Manual Analysis, Foundry
Remove the unary
operation and calculate average directly:
This ensures correct funding rate signs flow through to liquidation and settlement margin checks.
Direct impact on in-scope functions: LiquidationBranch::liquidateAccounts
and SettlementBranch::_fillOrder
Affects core account health calculations through margin requirement checks
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.