Closed nzhl closed 1 year ago
@nzhl There is no extra compounding interest. Interest get computed based on rates and liquidity rate is calculated from the borrow rate. You can check the implementation at DefaultReserveInterestRateStrategy.
Yes, it's true liquidity rate is calculated from the borrow rate. But when calculating the borrow index based on borrow rate, compounding is involved but that's not the case for liquidity index. (notice the calculateLinearInterest and calculateCompoundInterest )
They should calculated in the same rule IMO. Did I miss something?
They should calculated in the same rule IMO. Did I miss something?
Why? The linear and interest compounding is used just as a way to accumulate the interest. The accumulation depends on the rates, and one rate is based on the other so there is no excess or diff between them.
Considering the following case:
I am sorry maybe i misunderstood the code or ignored some significant details. The above is basically what i am struggling about.
@nzhl some design/architectural tips that maybe will help you to visualize the system:
You can visualize rates and indexes as completely independent components, but consistent between themselves because of some properties:
reserve.accruedToTreasury
instead of aToken balance, but they are factually the same.reserve.accruedToTreasury
happens, the collector/treasury is de-facto just another depositor and this is transparent for the system of rates and indexes: borrow rate keeps growing (covering all claims of depositors + treasury/collector via reserve.accruedToTreasury
coming from rates), supply rate keeps growing but discounting the RF, indexes keep compounding inter-action and linear vs compounded intra-action.So to summarize, apart from the diff caused by intra-action interest dynamics (really small) the claims of aToken holders (+ accruedToTreasury) multiplied by the supply index should be equivalent to the total debt multiplied by the borrowing index. Potentially even a bit higher, whenever the supply index receives extra income flows for example from flash loan fees. If you would like to see better with numbers, I recommend you create a test on the codebase with some sample users, amounts, and time passing, and see how the system reacts
I think the "intra-action interest" is what i am seeking for. The more ppl interact with the contracts with less diff there will be between borrow and supply. Big thx to @eboadom @miguelmtzinf , you guys are very nice š
@nzhl Nice for this issue
Hi everybody, I have a question regarding
Both supply and borrow indexes are actually compoundedĀ inter-actionsĀ (check hereĀ https://github.com/aave/aave-v3-core/blob/master/contracts/protocol/libraries/logic/ReserveLogic.sol#L301), but one is linear (supply) and the other compounded (borrow)Ā intra-action.
https://github.com/aave/aave-v3-core/issues/819#issuecomment-1457716858
Just to clarify, does that mean that both the supply and the borrow liquidity indexes are updated using compounded interests "inter actions" ?
With "actions" I am assuming you mean an action that changes the rate and therefore require a liquidity index update so supply, borrow, withdraw and repay
I am asking because I looked into the code and could not find where this happens: I see the supply liquidity index always updated with linear interests and the borrow liquidity index always updated with compounded interests
Let me share my analysis so you can double-check it
So the Pool.borrow() is defined here https://github.com/aave/aave-v3-core/blob/6070e82d962d9b12835c88e68210d0e63f08d035/contracts/protocol/pool/Pool.sol#L219
It calls BorrowLogic.executeBorrow() here
which internally calls reserve.updateState(reserveCache); here
It is defined in ReserveLogic.updateState() here
https://github.com/aave/aave-v3-core/blob/6070e82d962d9b12835c88e68210d0e63f08d035/contracts/protocol/libraries/logic/ReserveLogic.sol#L93
internally it calls _updateIndexes(reserve, reserveCache); here
https://github.com/aave/aave-v3-core/blob/master/contracts/protocol/libraries/logic/ReserveLogic.sol#L103
It is defined in ReserveLogic._updateIndexes() here
https://github.com/aave/aave-v3-core/blob/6070e82d962d9b12835c88e68210d0e63f08d035/contracts/protocol/libraries/logic/ReserveLogic.sol#L285
This function updates 2 liquidity indexes for the reserve of a specific token
the reserve.variableBorrowIndex that represents the variable borrow rate, representing the accrued interests the borrower has to pay and the implementation is here
https://github.com/aave/aave-v3-core/blob/6070e82d962d9b12835c88e68210d0e63f08d035/contracts/protocol/libraries/logic/ReserveLogic.sol#L303-L317
so as you can see the first index is updated with linear interests while the second index is updated with compound interests which seems quite weird since the documentation says the linear update should not be written in storage as they should both compound
The view methods seem aligned with the doc since the getNormalizedIncome() here
returns the supply side income and the getNormalizedDebt() here
returns the debt side amount using compound interests
The code shows that when calculating the liquidity index calculateLinearInterest is used but in the case of borrow calculateCompoundedInterest is used. After a little googling, i found the closed issue #702
But i am still quite comfused, since "the interest is linear in the supply side and compounded in the debt side", where did the extra compounding interest part go?
I tried to find the answer from aave doc but failed.