mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
refactor: further refactored the caching logic
This commit is contained in:
parent
b8fb9e592f
commit
61217d1ee5
|
@ -49,6 +49,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
|
||||||
using WadRayMath for uint256;
|
using WadRayMath for uint256;
|
||||||
using PercentageMath for uint256;
|
using PercentageMath for uint256;
|
||||||
using SafeERC20 for IERC20;
|
using SafeERC20 for IERC20;
|
||||||
|
using ReserveLogic for DataTypes.ReserveCache;
|
||||||
|
|
||||||
uint256 public constant LENDINGPOOL_REVISION = 0x2;
|
uint256 public constant LENDINGPOOL_REVISION = 0x2;
|
||||||
|
|
||||||
|
@ -288,40 +289,26 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
|
||||||
|
|
||||||
if (interestRateMode == DataTypes.InterestRateMode.STABLE) {
|
if (interestRateMode == DataTypes.InterestRateMode.STABLE) {
|
||||||
IStableDebtToken(reserveCache.stableDebtTokenAddress).burn(msg.sender, stableDebt);
|
IStableDebtToken(reserveCache.stableDebtTokenAddress).burn(msg.sender, stableDebt);
|
||||||
|
|
||||||
reserveCache.nextPrincipalStableDebt = reserveCache.nextTotalStableDebt = reserveCache
|
|
||||||
.currTotalStableDebt
|
|
||||||
.sub(stableDebt);
|
|
||||||
|
|
||||||
IVariableDebtToken(reserveCache.variableDebtTokenAddress).mint(
|
IVariableDebtToken(reserveCache.variableDebtTokenAddress).mint(
|
||||||
msg.sender,
|
msg.sender,
|
||||||
msg.sender,
|
msg.sender,
|
||||||
stableDebt,
|
stableDebt,
|
||||||
reserveCache.nextVariableBorrowIndex
|
reserveCache.nextVariableBorrowIndex
|
||||||
);
|
);
|
||||||
reserveCache.nextScaledVariableDebt = reserveCache.currScaledVariableDebt.add(
|
reserveCache.refreshDebt(0, stableDebt, stableDebt, 0);
|
||||||
stableDebt.rayDiv(reserveCache.nextVariableBorrowIndex)
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
IVariableDebtToken(reserveCache.variableDebtTokenAddress).burn(
|
IVariableDebtToken(reserveCache.variableDebtTokenAddress).burn(
|
||||||
msg.sender,
|
msg.sender,
|
||||||
variableDebt,
|
variableDebt,
|
||||||
reserveCache.nextVariableBorrowIndex
|
reserveCache.nextVariableBorrowIndex
|
||||||
);
|
);
|
||||||
reserveCache.nextScaledVariableDebt = reserveCache.currScaledVariableDebt.sub(
|
|
||||||
variableDebt.rayDiv(reserveCache.nextVariableBorrowIndex)
|
|
||||||
);
|
|
||||||
|
|
||||||
IStableDebtToken(reserveCache.stableDebtTokenAddress).mint(
|
IStableDebtToken(reserveCache.stableDebtTokenAddress).mint(
|
||||||
msg.sender,
|
msg.sender,
|
||||||
msg.sender,
|
msg.sender,
|
||||||
variableDebt,
|
variableDebt,
|
||||||
reserve.currentStableBorrowRate
|
reserve.currentStableBorrowRate
|
||||||
);
|
);
|
||||||
|
reserveCache.refreshDebt(variableDebt, 0, 0, variableDebt);
|
||||||
reserveCache.nextPrincipalStableDebt = reserveCache.nextTotalStableDebt = reserveCache
|
|
||||||
.currTotalStableDebt
|
|
||||||
.add(stableDebt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reserve.updateInterestRates(reserveCache, asset, 0, 0);
|
reserve.updateInterestRates(reserveCache, asset, 0, 0);
|
||||||
|
@ -365,6 +352,8 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
|
||||||
reserve.currentStableBorrowRate
|
reserve.currentStableBorrowRate
|
||||||
);
|
);
|
||||||
|
|
||||||
|
reserveCache.refreshDebt(stableDebt, stableDebt, 0, 0);
|
||||||
|
|
||||||
reserve.updateInterestRates(reserveCache, asset, 0, 0);
|
reserve.updateInterestRates(reserveCache, asset, 0, 0);
|
||||||
|
|
||||||
emit RebalanceStableBorrowRate(asset, user);
|
emit RebalanceStableBorrowRate(asset, user);
|
||||||
|
@ -915,8 +904,8 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
|
||||||
);
|
);
|
||||||
|
|
||||||
uint256 currentStableRate = 0;
|
uint256 currentStableRate = 0;
|
||||||
|
|
||||||
bool isFirstBorrowing = false;
|
bool isFirstBorrowing = false;
|
||||||
|
|
||||||
if (DataTypes.InterestRateMode(vars.interestRateMode) == DataTypes.InterestRateMode.STABLE) {
|
if (DataTypes.InterestRateMode(vars.interestRateMode) == DataTypes.InterestRateMode.STABLE) {
|
||||||
currentStableRate = reserve.currentStableBorrowRate;
|
currentStableRate = reserve.currentStableBorrowRate;
|
||||||
|
|
||||||
|
@ -926,11 +915,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
|
||||||
vars.amount,
|
vars.amount,
|
||||||
currentStableRate
|
currentStableRate
|
||||||
);
|
);
|
||||||
|
reserveCache.refreshDebt(vars.amount, 0, 0, 0);
|
||||||
reserveCache.nextPrincipalStableDebt = reserveCache.nextTotalStableDebt = reserveCache
|
|
||||||
.currTotalStableDebt
|
|
||||||
.add(vars.amount);
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
isFirstBorrowing = IVariableDebtToken(reserveCache.variableDebtTokenAddress).mint(
|
isFirstBorrowing = IVariableDebtToken(reserveCache.variableDebtTokenAddress).mint(
|
||||||
vars.user,
|
vars.user,
|
||||||
|
@ -938,10 +923,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
|
||||||
vars.amount,
|
vars.amount,
|
||||||
reserveCache.nextVariableBorrowIndex
|
reserveCache.nextVariableBorrowIndex
|
||||||
);
|
);
|
||||||
|
reserveCache.refreshDebt(0, 0, vars.amount, 0);
|
||||||
reserveCache.nextScaledVariableDebt = reserveCache.nextScaledVariableDebt.add(
|
|
||||||
vars.amount.rayDiv(reserveCache.nextVariableBorrowIndex)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFirstBorrowing) {
|
if (isFirstBorrowing) {
|
||||||
|
@ -1090,18 +1072,14 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
|
||||||
|
|
||||||
if (interestRateMode == DataTypes.InterestRateMode.STABLE) {
|
if (interestRateMode == DataTypes.InterestRateMode.STABLE) {
|
||||||
IStableDebtToken(reserveCache.stableDebtTokenAddress).burn(onBehalfOf, paybackAmount);
|
IStableDebtToken(reserveCache.stableDebtTokenAddress).burn(onBehalfOf, paybackAmount);
|
||||||
reserveCache.nextPrincipalStableDebt = reserveCache.nextTotalStableDebt = reserveCache
|
reserveCache.refreshDebt(0, paybackAmount, 0, 0);
|
||||||
.currTotalStableDebt
|
|
||||||
.sub(paybackAmount);
|
|
||||||
} else {
|
} else {
|
||||||
IVariableDebtToken(reserveCache.variableDebtTokenAddress).burn(
|
IVariableDebtToken(reserveCache.variableDebtTokenAddress).burn(
|
||||||
onBehalfOf,
|
onBehalfOf,
|
||||||
paybackAmount,
|
paybackAmount,
|
||||||
reserveCache.nextVariableBorrowIndex
|
reserveCache.nextVariableBorrowIndex
|
||||||
);
|
);
|
||||||
reserveCache.nextScaledVariableDebt = reserveCache.currScaledVariableDebt.sub(
|
reserveCache.refreshDebt(0, 0, 0, paybackAmount);
|
||||||
paybackAmount.rayDiv(reserveCache.nextVariableBorrowIndex)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reserve.updateInterestRates(reserveCache, asset, paybackAmount, 0);
|
reserve.updateInterestRates(reserveCache, asset, paybackAmount, 0);
|
||||||
|
|
|
@ -10,6 +10,7 @@ import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol';
|
||||||
import {ILendingPoolCollateralManager} from '../../interfaces/ILendingPoolCollateralManager.sol';
|
import {ILendingPoolCollateralManager} from '../../interfaces/ILendingPoolCollateralManager.sol';
|
||||||
import {VersionedInitializable} from '../libraries/aave-upgradeability/VersionedInitializable.sol';
|
import {VersionedInitializable} from '../libraries/aave-upgradeability/VersionedInitializable.sol';
|
||||||
import {GenericLogic} from '../libraries/logic/GenericLogic.sol';
|
import {GenericLogic} from '../libraries/logic/GenericLogic.sol';
|
||||||
|
import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol';
|
||||||
import {Helpers} from '../libraries/helpers/Helpers.sol';
|
import {Helpers} from '../libraries/helpers/Helpers.sol';
|
||||||
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
|
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
|
||||||
import {PercentageMath} from '../libraries/math/PercentageMath.sol';
|
import {PercentageMath} from '../libraries/math/PercentageMath.sol';
|
||||||
|
@ -35,6 +36,7 @@ contract LendingPoolCollateralManager is
|
||||||
using SafeMath for uint256;
|
using SafeMath for uint256;
|
||||||
using WadRayMath for uint256;
|
using WadRayMath for uint256;
|
||||||
using PercentageMath for uint256;
|
using PercentageMath for uint256;
|
||||||
|
using ReserveLogic for DataTypes.ReserveCache;
|
||||||
|
|
||||||
uint256 internal constant LIQUIDATION_CLOSE_FACTOR_PERCENT = 5000;
|
uint256 internal constant LIQUIDATION_CLOSE_FACTOR_PERCENT = 5000;
|
||||||
|
|
||||||
|
@ -173,9 +175,8 @@ contract LendingPoolCollateralManager is
|
||||||
vars.actualDebtToLiquidate,
|
vars.actualDebtToLiquidate,
|
||||||
debtReserveCache.nextVariableBorrowIndex
|
debtReserveCache.nextVariableBorrowIndex
|
||||||
);
|
);
|
||||||
debtReserveCache.nextScaledVariableDebt = debtReserveCache.currScaledVariableDebt.sub(
|
debtReserveCache.refreshDebt(0, 0, 0, vars.actualDebtToLiquidate);
|
||||||
vars.actualDebtToLiquidate.rayDiv(debtReserveCache.nextVariableBorrowIndex)
|
debtReserve.updateInterestRates(debtReserveCache, debtAsset, vars.actualDebtToLiquidate, 0);
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
// If the user doesn't have variable debt, no need to try to burn variable debt tokens
|
// If the user doesn't have variable debt, no need to try to burn variable debt tokens
|
||||||
if (vars.userVariableDebt > 0) {
|
if (vars.userVariableDebt > 0) {
|
||||||
|
@ -184,27 +185,20 @@ contract LendingPoolCollateralManager is
|
||||||
vars.userVariableDebt,
|
vars.userVariableDebt,
|
||||||
debtReserveCache.nextVariableBorrowIndex
|
debtReserveCache.nextVariableBorrowIndex
|
||||||
);
|
);
|
||||||
debtReserveCache.nextScaledVariableDebt = debtReserveCache
|
|
||||||
.currScaledVariableDebt
|
|
||||||
.sub(vars.userVariableDebt.rayDiv(debtReserveCache.nextVariableBorrowIndex));
|
|
||||||
}
|
}
|
||||||
IStableDebtToken(debtReserveCache.stableDebtTokenAddress).burn(
|
IStableDebtToken(debtReserveCache.stableDebtTokenAddress).burn(
|
||||||
user,
|
user,
|
||||||
vars.actualDebtToLiquidate.sub(vars.userVariableDebt)
|
vars.actualDebtToLiquidate.sub(vars.userVariableDebt)
|
||||||
);
|
);
|
||||||
|
debtReserveCache.refreshDebt(
|
||||||
debtReserveCache.nextPrincipalStableDebt = debtReserveCache
|
0,
|
||||||
.nextTotalStableDebt = debtReserveCache.currTotalStableDebt.sub(
|
vars.actualDebtToLiquidate.sub(vars.userVariableDebt),
|
||||||
vars.actualDebtToLiquidate.sub(vars.userVariableDebt)
|
0,
|
||||||
|
vars.userVariableDebt
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
debtReserve.updateInterestRates(
|
debtReserve.updateInterestRates(debtReserveCache, debtAsset, vars.actualDebtToLiquidate, 0);
|
||||||
debtReserveCache,
|
}
|
||||||
debtAsset,
|
|
||||||
vars.actualDebtToLiquidate,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
|
|
||||||
if (receiveAToken) {
|
if (receiveAToken) {
|
||||||
vars.liquidatorPreviousATokenBalance = IERC20(vars.collateralAtoken).balanceOf(msg.sender);
|
vars.liquidatorPreviousATokenBalance = IERC20(vars.collateralAtoken).balanceOf(msg.sender);
|
||||||
|
@ -216,7 +210,7 @@ contract LendingPoolCollateralManager is
|
||||||
emit ReserveUsedAsCollateralEnabled(collateralAsset, msg.sender);
|
emit ReserveUsedAsCollateralEnabled(collateralAsset, msg.sender);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
collateralReserve.updateState(collateralReserveCache);
|
collateralReserve.updateState(collateralReserveCache);
|
||||||
collateralReserve.updateInterestRates(
|
collateralReserve.updateInterestRates(
|
||||||
collateralReserveCache,
|
collateralReserveCache,
|
||||||
collateralAsset,
|
collateralAsset,
|
||||||
|
|
|
@ -186,10 +186,6 @@ library ReserveLogic {
|
||||||
) internal {
|
) internal {
|
||||||
UpdateInterestRatesLocalVars memory vars;
|
UpdateInterestRatesLocalVars memory vars;
|
||||||
|
|
||||||
if (reserveCache.currTotalStableDebt != reserveCache.nextTotalStableDebt) {
|
|
||||||
reserveCache.nextAvgStableBorrowRate = IStableDebtToken(reserveCache.stableDebtTokenAddress)
|
|
||||||
.getAverageStableRate();
|
|
||||||
}
|
|
||||||
|
|
||||||
reserveCache.nextTotalVariableDebt = reserveCache.nextScaledVariableDebt.rayMul(
|
reserveCache.nextTotalVariableDebt = reserveCache.nextScaledVariableDebt.rayMul(
|
||||||
reserveCache.nextVariableBorrowIndex
|
reserveCache.nextVariableBorrowIndex
|
||||||
|
@ -228,11 +224,9 @@ library ReserveLogic {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MintToTreasuryLocalVars {
|
struct MintToTreasuryLocalVars {
|
||||||
uint256 currentStableDebt;
|
uint256 prevTotalStableDebt;
|
||||||
uint256 principalStableDebt;
|
uint256 prevTotalVariableDebt;
|
||||||
uint256 previousStableDebt;
|
uint256 currTotalVariableDebt;
|
||||||
uint256 currentVariableDebt;
|
|
||||||
uint256 previousVariableDebt;
|
|
||||||
uint256 avgStableRate;
|
uint256 avgStableRate;
|
||||||
uint256 cumulatedStableInterest;
|
uint256 cumulatedStableInterest;
|
||||||
uint256 totalDebtAccrued;
|
uint256 totalDebtAccrued;
|
||||||
|
@ -260,12 +254,12 @@ library ReserveLogic {
|
||||||
}
|
}
|
||||||
|
|
||||||
//calculate the last principal variable debt
|
//calculate the last principal variable debt
|
||||||
vars.previousVariableDebt = reserveCache.currScaledVariableDebt.rayMul(
|
vars.prevTotalVariableDebt = reserveCache.currScaledVariableDebt.rayMul(
|
||||||
reserveCache.currVariableBorrowIndex
|
reserveCache.currVariableBorrowIndex
|
||||||
);
|
);
|
||||||
|
|
||||||
//calculate the new total supply after accumulation of the index
|
//calculate the new total supply after accumulation of the index
|
||||||
vars.currentVariableDebt = reserveCache.currScaledVariableDebt.rayMul(
|
vars.currTotalVariableDebt = reserveCache.currScaledVariableDebt.rayMul(
|
||||||
reserveCache.nextVariableBorrowIndex
|
reserveCache.nextVariableBorrowIndex
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -276,16 +270,16 @@ library ReserveLogic {
|
||||||
reserveCache.reserveLastUpdateTimestamp
|
reserveCache.reserveLastUpdateTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
vars.previousStableDebt = reserveCache.currPrincipalStableDebt.rayMul(
|
vars.prevTotalStableDebt = reserveCache.currPrincipalStableDebt.rayMul(
|
||||||
vars.cumulatedStableInterest
|
vars.cumulatedStableInterest
|
||||||
);
|
);
|
||||||
|
|
||||||
//debt accrued is the sum of the current debt minus the sum of the debt at the last update
|
//debt accrued is the sum of the current debt minus the sum of the debt at the last update
|
||||||
vars.totalDebtAccrued = vars
|
vars.totalDebtAccrued = vars
|
||||||
.currentVariableDebt
|
.currTotalVariableDebt
|
||||||
.add(reserveCache.currTotalStableDebt)
|
.add(reserveCache.currTotalStableDebt)
|
||||||
.sub(vars.previousVariableDebt)
|
.sub(vars.prevTotalVariableDebt)
|
||||||
.sub(vars.previousStableDebt);
|
.sub(vars.prevTotalStableDebt);
|
||||||
|
|
||||||
vars.amountToMint = vars.totalDebtAccrued.percentMul(vars.reserveFactor);
|
vars.amountToMint = vars.totalDebtAccrued.percentMul(vars.reserveFactor);
|
||||||
|
|
||||||
|
@ -385,4 +379,32 @@ library ReserveLogic {
|
||||||
|
|
||||||
return reserveCache;
|
return reserveCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function refreshDebt(
|
||||||
|
DataTypes.ReserveCache memory cache,
|
||||||
|
uint256 stableDebtMinted,
|
||||||
|
uint256 stableDebtBurned,
|
||||||
|
uint256 variableDebtMinted,
|
||||||
|
uint256 variableDebtBurned
|
||||||
|
) internal {
|
||||||
|
uint256 scaledVariableDebtMinted = variableDebtMinted.rayDiv(cache.nextVariableBorrowIndex);
|
||||||
|
uint256 scaledVariableDebtBurned = variableDebtBurned.rayDiv(cache.nextVariableBorrowIndex);
|
||||||
|
|
||||||
|
if (cache.currTotalStableDebt.add(stableDebtMinted) > stableDebtBurned) {
|
||||||
|
cache.nextPrincipalStableDebt = cache.nextTotalStableDebt = cache
|
||||||
|
.currTotalStableDebt
|
||||||
|
.add(stableDebtMinted)
|
||||||
|
.sub(stableDebtBurned);
|
||||||
|
if (stableDebtMinted != 0 || stableDebtBurned != 0) {
|
||||||
|
cache.nextAvgStableBorrowRate = IStableDebtToken(cache.stableDebtTokenAddress)
|
||||||
|
.getAverageStableRate();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cache.nextPrincipalStableDebt = cache.nextTotalStableDebt = cache.nextAvgStableBorrowRate = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.nextScaledVariableDebt = cache.currScaledVariableDebt.add(scaledVariableDebtMinted).sub(
|
||||||
|
scaledVariableDebtBurned
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -14793,7 +14793,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ethereumjs-abi": {
|
"ethereumjs-abi": {
|
||||||
"version": "git+https://github.com/ethereumjs/ethereumjs-abi.git#1a27c59c15ab1e95ee8e5c4ed6ad814c49cc439e",
|
"version": "git+https://github.com/ethereumjs/ethereumjs-abi.git#ee3994657fa7a427238e6ba92a84d0b529bbcde0",
|
||||||
"from": "git+https://github.com/ethereumjs/ethereumjs-abi.git",
|
"from": "git+https://github.com/ethereumjs/ethereumjs-abi.git",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user