aave / protocol-v2

Aave Protocol V2
https://aave.com
Other
668 stars 755 forks source link

duplicated arithmetic multiplication during calculating variable and stable debt? #337

Open bnb-scs opened 1 month ago

bnb-scs commented 1 month ago

Have a question about the stable and variable debt calculating logic in the _mintToTreasury() function.

https://github.com/aave/protocol-v2/blob/release/aave-v2-ethereum/contracts/protocol/libraries/logic/ReserveLogic.sol#L274-L311

it seems the passed in scaledVariableDebt has already multiplied by the reserve.variableBorrowIndex factor in ReserveLogic.sol, why do multiply it by previousVariableBorrowIndex (and newVariableBorrowIndex) again in _mintToTreasury()? the resulting variable debt would be the principal debt supply multiply twice the variableBorrowIndex

    //calculate the last principal variable debt
    vars.previousVariableDebt = scaledVariableDebt.rayMul(previousVariableBorrowIndex);

    //calculate the new total supply after accumulation of the index
    vars.currentVariableDebt = scaledVariableDebt.rayMul(newVariableBorrowIndex);

For stable debt, vars.currentStableDebt and vars.previousStableDebt seem to be the same, where IStableDebtToken(reserve.stableDebtTokenAddress).getSupplyData() calculates the current stable debt by multiply the principleSupply and the compoundedInterest, whereas previousStableDebt is calculated using exactly the same formula with the same avgrate and same stableSupplyUpdatedTimestamp, so these two variables should be always the same?