Incorrect funding-rate calculation due to rounding
Description
The protocol implements a velocity-based funding-rate model where the velocity is defined as follows:
And the actual funding rate is defined as follows:
This funding rate is calculated as the area under the curve using the below formula:
accumulated_funding_rate_change = int256(
(absLastFundingRate + (numBlocksToLimit / 2) * absLastVelocity)
* numBlocksToLimit
+ ((uint256(numBlocksToCharge) - numBlocksToLimit)
* f.maxFundingFeePerBlock)
);
Here, numBlocksToLimit
are the number of blocks for which the area under the curve is required. In cases where numBlocksToLimit
is an odd number, there will be a rounding error, due to which the funding rate calculate would not be accurate. For example, when numBlocksToLimit
is 1, the value of numBlocksToLimit / 2
would be 0
, leading to a funding-rate value slightly less than expected.
Impact
The value of the funding rate would be slightly inaccurate in cases where numBlocksToLimit
is an odd integer.
Recommendations
Add 1 to the numBlocksToLimit
before dividing it by 2 to fix the rounding error.
Remediation
This issue has been acknowledged by Ostium Labs, and a fix was implemented in commit 74f6cda0↗.