updateIRMParams
does not call applyInterestForToken
before updating irmParams
which leads to incorrect calculation of interest rate for subsequent trades.
#134
Labels
2 (Med Risk)
Assets not at direct risk, but function/availability of the protocol could be impacted or leak value
bug
Something isn't working
edited-by-warden
M-02
primary issue
Highest quality submission among a set of duplicates
🤖_112_group
AI based duplicate group recommendation
satisfactory
satisfies C4 submission criteria; eligible for awards
selected for report
This submission will be included/highlighted in the audit report
sufficient quality report
This report is of sufficient quality
Lines of code
https://github.com/code-423n4/2024-05-predy/blob/a9246db5f874a91fb71c296aac6a66902289306a/src/libraries/logic/AddPairLogic.sol#L128
https://github.com/code-423n4/2024-05-predy/blob/a9246db5f874a91fb71c296aac6a66902289306a/src/libraries/logic/AddPairLogic.sol#L96
Vulnerability details
Impact
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 ofirmParams
. This also results in the incorrect calculation oftotalProtocolFees
Proof of Concept
Consider the function
updateIRMParams
. This is called from the PredyPool contract which internally calls this function in theAddPairLogic
contract -It validates the
IRMParams
and updates it. But, there is an issue in doing so.Before the
IRMParams
is updated,applyInterestForToken
inApplyInterestLib
should be called first. It's not being called.applyInterestForToken
first calls the functionapplyInterestForPoolStatus
twice (forquotepool
andbasepool
) and thenapplyInterestForToken
also updates thelastUpdateTimestamp
toblock.timestamp
.In
applyInterestForPoolStatus
-Note this line -
The
irmParams
params are used to calculate theinterestRate
, which is then further used to calculate theprotocolFee
.So, the period between ->
block.timestamp - lastUpdateTimestamp
is supposed to use the currentpoolStatus.params
to calculate the interest rate. But, whenupdateIRMParams
are directly updated, without first callingapplyInterestForToken
, any time a new trade is opened, it will be using the updated values ofirmParams
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
andslope2
were smaller in the previous values ofirmParams
, then theinterestRate
calculated would be smaller. But, if the new updated values ofirmParams
(slope1
andslope2
) 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 ofinterestRate
. A higher interest rate is being calculated than intended for the current period. This directly affects the calculation of thetotalProtocolFees
as well.This can be seen in the graph 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. It's 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 previousfeeRatio
to account for accurate fee calculation. So, beforefeeRatio
is updated, it should also call theapplyInterestForToken
function.Tools Used
Manual review
Recommended Mitigation Steps
While updating the params, the
applyInterestForToken
function should be called first for the current period to calculate theinterestRate
fromblock.timestamp
tolastUpdateTimestamp
, and then the updated value ofirmParams
can be used for durations after thisblock.timestamp
.The same should be done for the
updateFeeRatio
function.Assessed type
Other
The text was updated successfully, but these errors were encountered: