Submitted by WinSec, also found by Tigerfrake, 0xhere2learn, TECHFUND-inc (1, 2), SpicyMeatball, and zhaojohnson
https://github.com/code-423n4/2024-05-predy/blob/a9246db5f874a91fb71c296aac6a66902289306a/src/libraries/logic/AddPairLogic.sol#L128https://github.com/code-423n4/2024-05-predy/blob/a9246db5f874a91fb71c296aac6a66902289306a/src/libraries/logic/AddPairLogic.sol#L96
Incorrect interest rate value will be calculated for future trades as the update to irmParams does not update the lastUpdatedAt or calculate the interest rate before updating the value of irmParams. This also results in the incorrect calculation of totalProtocolFees
Consider the function updateIRMParams. This is called from the PredyPool contract which internally calls this function in the AddPairLogic contract:
It validates the IRMParams and updates it. But, there is an issue in doing so. Before the IRMParams is updated, applyInterestForToken in ApplyInterestLib should be called first. Its not being called.
applyInterestForToken first calls the function applyInterestForPoolStatus twice (for quotepool and basepool) and then applyInterestForToken also updates the lastUpdateTimestamp to block.timestamp.
In applyInterestForPoolStatus:
Note this line:
The irmParams params are used to calculate the interestRate, which is then further used to calculate the protocolFee.
So, the period between block.timestamp - lastUpdateTimestamp is supposed to use the current poolStatus.params to calculate the interest rate. But, when updateIRMParams are directly updated, without first calling applyInterestForToken, any time a new trade is opened, it will be using the updated values of irmParams to calculate the interest rate for the current time period instead of using the previous values. This is problematic.
For example, if the previous values of slope1 and slope2 were smaller in the previous values of irmParams, then the interestRate calculated would be smaller. But, if the new updated values of irmParams (slope1 and slope2) were bigger, then the interest rate would become bigger. But, for the current time period (block.timestamp - lastUpdateTimestamp), this indicates a step-wise jump in the calculation of interestRate. A higher interest rate is being calculated than intended for the current period. This directly affects the calculation of the totalProtocolFees as well.
This can be seen in the graph below:
Note: to view the provided image, please see the original submission here.
Consider that the interest rate is the area under the graph. The first one represents the current case, where applyInterestForToken is not called before the params are updated. In the second case, applyInterestForToken is called first before updating. Its clear from the graphs that the first case is considering a higher value of interest rate because of a larger area, which is incorrect.
The same argument can be made for the updateFeeRatio function as well. The current time period must use the previous feeRatio to account for accurate fee calculation. So, before feeRatio is updated, it should also call the applyInterestForToken function.
While updating the params, the applyInterestForToken function should be called first for the current period to calculate the interestRate from block.timestamp to lastUpdateTimestamp, and then the updated value of irmParams can be used for durations after this block.timestamp.
The same should be done for the updateFeeRatio function.
syuhei176 (Predy) confirmed via duplicate Issue #12
0xsomeone (judge) commented:
