Maker/taker will be paying slightly less funding fee to taker/maker respectively due to precision loss
Summary
Precision loss while calculating linear interpolation of points on curve due to division before multiplication.
Vulnerability Detail
The funding rate is used to determine the periodic funding payments between long and short positions. If there is a precision loss in the calculation, it can result in incorrect funding payments.
For calculating the jump rate / funding rate for the utilization rate inside JumpRateUtlizationCurve.sol file, its using the linear interpolation formula to find corresponding jump rate for the utilization rate. Below is the correct formula to calculate targetY (funding rate):
$$targetY = startY + \frac{(targetX - startX)(endY- startY)}{(endX - startX)}$$
However, the formula used in the code to find it, is shown below, where division is occurring before multiplication which will results in precision loss.
$$targetY = startY + \frac{(targetX - startX)}{(endX - startX)} * (endY- startY)$$
Due to the precision loss, maker/taker will be sending slightly less funding fee than they should be to taker/maker respectively as there is a precision loss while calculating funding rate. This means that the payments may not accurately reflect the intended redistribution of funds between maker/taker, leading to unfair outcomes or misaligned incentives.
As can be seen in output, the one written in actual code is producing slightly less funding rate than the correct one.
Impact
Precision loss while calculating linear interpolation of points on curve due to division before multiplication, which will results in wrong calculation of jump rate / funding rate. As a result will make taker/maker to pay slightly less funding fee to maker/taker respectively. This means that the payments may not accurately reflect the intended redistribution of funds between maker/taker, leading to unfair outcomes or misaligned incentives.
While the precision loss is there, as per the submission it is in the magnitude of ~0.0000007 * fundingRate...
I consider this a valid low, as debatably it seems quite negligible.
BLACK-PANDA-REACH
medium
Maker/taker will be paying slightly less funding fee to taker/maker respectively due to precision loss
Summary
Precision loss while calculating linear interpolation of points on curve due to division before multiplication.
Vulnerability Detail
The funding rate is used to determine the periodic funding payments between long and short positions. If there is a precision loss in the calculation, it can result in incorrect funding payments.
For calculating the jump rate / funding rate for the utilization rate inside
JumpRateUtlizationCurve.sol
file, its using the linear interpolation formula to find corresponding jump rate for the utilization rate. Below is the correct formula to calculatetargetY (funding rate)
: $$targetY = startY + \frac{(targetX - startX)(endY- startY)}{(endX - startX)}$$However, the formula used in the code to find it, is shown below, where division is occurring before multiplication which will results in precision loss. $$targetY = startY + \frac{(targetX - startX)}{(endX - startX)} * (endY- startY)$$
Due to the precision loss, maker/taker will be sending slightly less funding fee than they should be to taker/maker respectively as there is a precision loss while calculating funding rate. This means that the payments may not accurately reflect the intended redistribution of funds between maker/taker, leading to unfair outcomes or misaligned incentives.
Below is the vulnerable code: https://github.com/sherlock-audit/2023-05-perennial/blob/0f73469508a4cd3d90b382eac2112f012a5a9852/root/contracts/curve/CurveMath6.sol#L23-L36
Here,
xRatio
which is having a division, gets multiplied withyRange
, which will results in precision loss while calculating jump rate.Proof Of Concept
We wrote a PoC in foundry to demonstrate issue.
As can be seen in output, the one written in actual code is producing slightly less funding rate than the correct one.
Impact
Precision loss while calculating linear interpolation of points on curve due to division before multiplication, which will results in wrong calculation of jump rate / funding rate. As a result will make taker/maker to pay slightly less funding fee to maker/taker respectively. This means that the payments may not accurately reflect the intended redistribution of funds between maker/taker, leading to unfair outcomes or misaligned incentives.
Code Snippet
https://github.com/sherlock-audit/2023-05-perennial/blob/0f73469508a4cd3d90b382eac2112f012a5a9852/root/contracts/curve/CurveMath6.sol#L23-L36 https://github.com/sherlock-audit/2023-05-perennial/blob/0f73469508a4cd3d90b382eac2112f012a5a9852/root/contracts/curve/CurveMath18.sol#L23-L36
Tool used
Manual Review, Foundry
Recommendation
We recommend to perform multiplication before division to avoid precision loss as shown in first formula above.