Ffunding fee being calculated with the wrong sign, potentially leading to funds flowing in the wrong direction.
The unary
function typically returns the negative of its input. So this line is effectively calculating -(lastFundingRate + fundingRate) / 2
, which is the negative of the average, not the average itself.
This will result in the funding fee being calculated with the wrong sign, potentially leading to funds flowing in the wrong direction (e.g., longs paying shorts when they should be receiving, or vice versa).
POC Below - forgive the formatting.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@prb/math/contracts/SD59x18.sol";
import "@prb/math/contracts/UD60x18.sol";
contract PerpMarket {
using SD59x18 for SD59x18.SD59x18;
using UD60x18 for UD60x18.UD60x18;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import "../src/PerpMarket.sol";
import "@prb/math/contracts/SD59x18.sol";
import "@prb/math/contracts/UD60x18.sol";
contract PerpMarketTest is Test {
using SD59x18 for SD59x18.SD59x18;
using UD60x18 for UD60x18.UD60x18;
}
Run forge test -vv
in the terminal
The getPendingFundingFeePerUnit
function returns a negative value when it should be positive. The absolute value of the incorrect fee equals the correct fee, showing that the only error is the sign flip caused by the unary
function. This will result in the funding fee being calculated with the wrong sign, potentially leading to funds flowing in the wrong direction
Manual Review
SD59x18 avgFundingRate = sd59x18(self.lastFundingRate).add(fundingRate).div(sd59x18Convert(2));
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.