diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 275843a7..9fd5ce71 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -27,6 +27,7 @@ import {LendingPoolCollateralManager} from './LendingPoolCollateralManager.sol'; import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import {ILendingPool} from '../interfaces/ILendingPool.sol'; +import "@nomiclabs/buidler/console.sol"; /** * @title LendingPool contract @@ -702,6 +703,7 @@ contract LendingPool is VersionedInitializable, ILendingPool { ) { ReserveLogic.ReserveData memory reserve = _reserves[asset]; + return ( IERC20(asset).balanceOf(reserve.aTokenAddress), IERC20(reserve.stableDebtTokenAddress).totalSupply(), diff --git a/contracts/lendingpool/LendingPoolConfigurator.sol b/contracts/lendingpool/LendingPoolConfigurator.sol index 0b5b472f..6bedff76 100644 --- a/contracts/lendingpool/LendingPoolConfigurator.sol +++ b/contracts/lendingpool/LendingPoolConfigurator.sol @@ -484,7 +484,7 @@ contract LendingPoolConfigurator is VersionedInitializable { * @param asset the address of the reserve * @param reserveFactor the new reserve factor of the reserve **/ - function setReserveFactor(address asset, uint256 reserveFactor) external onlyLendingPoolManager { + function setReserveFactor(address asset, uint256 reserveFactor) external onlyAaveAdmin { ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); currentConfig.setReserveFactor(reserveFactor); diff --git a/contracts/libraries/configuration/ReserveConfiguration.sol b/contracts/libraries/configuration/ReserveConfiguration.sol index 10471f6e..ed4df31e 100644 --- a/contracts/libraries/configuration/ReserveConfiguration.sol +++ b/contracts/libraries/configuration/ReserveConfiguration.sol @@ -42,8 +42,7 @@ library ReserveConfiguration { * @param self the reserve configuration * @param reserveFactor the reserve factor **/ - function setReserveFactor(ReserveConfiguration.Map memory self, uint256 reserveFactor) internal view { - console.log("Setting reserve factor to %s", reserveFactor); + function setReserveFactor(ReserveConfiguration.Map memory self, uint256 reserveFactor) internal pure { self.data = (self.data & RESERVE_FACTOR_MASK) | reserveFactor << 64; } diff --git a/contracts/libraries/logic/ReserveLogic.sol b/contracts/libraries/logic/ReserveLogic.sol index ecd96954..e5eac57a 100644 --- a/contracts/libraries/logic/ReserveLogic.sol +++ b/contracts/libraries/logic/ReserveLogic.sol @@ -147,7 +147,7 @@ library ReserveLogic { * a formal specification. * @param reserve the reserve object **/ - function updateState(ReserveData storage reserve) internal { + function updateState(ReserveData storage reserve) external { address stableDebtToken = reserve.stableDebtTokenAddress; address variableDebtToken = reserve.variableDebtTokenAddress; uint256 previousVariableBorrowIndex = reserve.variableBorrowIndex; @@ -166,7 +166,6 @@ library ReserveLogic { reserve, stableDebtToken, variableDebtToken, - previousLiquidityIndex, previousVariableBorrowIndex, newLiquidityIndex, newVariableBorrowIndex, @@ -306,7 +305,6 @@ library ReserveLogic { ReserveData storage reserve, address stableDebtToken, address variableDebtToken, - uint256 previousLiquidityIndex, uint256 previousVariableBorrowIndex, uint256 newLiquidityIndex, uint256 newVariableBorrowIndex, diff --git a/contracts/tokenization/StableDebtToken.sol b/contracts/tokenization/StableDebtToken.sol index 5c31e0cf..6401bfc9 100644 --- a/contracts/tokenization/StableDebtToken.sol +++ b/contracts/tokenization/StableDebtToken.sol @@ -8,6 +8,7 @@ import {DebtTokenBase} from './base/DebtTokenBase.sol'; import {MathUtils} from '../libraries/math/MathUtils.sol'; import {WadRayMath} from '../libraries/math/WadRayMath.sol'; import {IStableDebtToken} from './interfaces/IStableDebtToken.sol'; +import "@nomiclabs/buidler/console.sol"; /** * @title contract StableDebtToken @@ -82,7 +83,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { } struct MintLocalVars { - uint256 currentSupply; + uint256 previousSupply; uint256 nextSupply; uint256 amountInRay; uint256 newStableRate; @@ -111,9 +112,9 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { ) = _calculateBalanceIncrease(user); //accrueing the interest accumulation to the stored total supply and caching it - vars.currentSupply = totalSupply(); + vars.previousSupply = totalSupply(); vars.currentAvgStableRate = _avgStableRate; - vars.nextSupply = _totalSupply = vars.currentSupply.add(amount); + vars.nextSupply = _totalSupply = vars.previousSupply.add(amount); vars.amountInRay = amount.wadToRay(); @@ -132,11 +133,11 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { //calculates the updated average stable rate _avgStableRate = vars.currentAvgStableRate - .rayMul(vars.currentSupply.wadToRay()) + .rayMul(vars.previousSupply.wadToRay()) .add(rate.rayMul(vars.amountInRay)) .rayDiv(vars.nextSupply.wadToRay()); - _mint(user, amount.add(balanceIncrease)); + _mint(user, amount.add(balanceIncrease), vars.previousSupply); // transfer event to track balances emit Transfer(address(0), user, amount); @@ -164,17 +165,15 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { ) = _calculateBalanceIncrease(user); - uint256 currentSupply = totalSupply(); - uint256 currentAvgStableRate = _avgStableRate; - + uint256 previousSupply = totalSupply(); - if (currentSupply <= amount) { + if (previousSupply <= amount) { _avgStableRate = 0; _totalSupply = 0; } else { - uint256 nextSupply = _totalSupply = currentSupply.sub(amount); + uint256 nextSupply = _totalSupply = previousSupply.sub(amount); _avgStableRate = _avgStableRate - .rayMul(currentSupply.wadToRay()) + .rayMul(previousSupply.wadToRay()) .sub(_usersData[user].rayMul(amount.wadToRay())) .rayDiv(nextSupply.wadToRay()); } @@ -191,9 +190,9 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { _totalSupplyTimestamp = uint40(block.timestamp); if (balanceIncrease > amount) { - _mint(user, balanceIncrease.sub(amount)); + _mint(user, balanceIncrease.sub(amount), previousSupply); } else { - _burn(user, amount.sub(balanceIncrease)); + _burn(user, amount.sub(balanceIncrease), previousSupply); } // transfer event to track balances @@ -244,7 +243,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { } function totalSupply() public override view returns (uint256) { - _calcTotalSupply(_avgStableRate); + return _calcTotalSupply(_avgStableRate); } function getTotalSupplyLastUpdated() public override view returns(uint40) { @@ -261,13 +260,37 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase { function _calcTotalSupply(uint256 avgRate) internal view returns(uint256) { uint256 principalSupply = super.totalSupply(); + if (principalSupply == 0) { return 0; } + uint256 cumulatedInterest = MathUtils.calculateCompoundedInterest( avgRate, _totalSupplyTimestamp ); + return principalSupply.rayMul(cumulatedInterest); } + + function _mint(address account, uint256 amount, uint256 oldTotalSupply) internal { + + uint256 oldAccountBalance = _balances[account]; + _balances[account] = oldAccountBalance.add(amount); + + if (address(_incentivesController) != address(0)) { + _incentivesController.handleAction(account, oldTotalSupply, oldAccountBalance); + } + } + + function _burn(address account, uint256 amount, uint256 oldTotalSupply) internal { + + + uint256 oldAccountBalance = _balances[account]; + _balances[account] = oldAccountBalance.sub(amount, 'ERC20: burn amount exceeds balance'); + + if (address(_incentivesController) != address(0)) { + _incentivesController.handleAction(account, oldTotalSupply, oldAccountBalance); + } + } } diff --git a/test/configurator.spec.ts b/test/configurator.spec.ts index 66f7ef82..0fa3289c 100644 --- a/test/configurator.spec.ts +++ b/test/configurator.spec.ts @@ -192,8 +192,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).setReserveFactor(weth.address, '2000'), - CALLER_NOT_LENDING_POOL_MANAGER - ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); + CALLER_NOT_AAVE_ADMIN + ).to.be.revertedWith(CALLER_NOT_AAVE_ADMIN); }); diff --git a/test/helpers/actions.ts b/test/helpers/actions.ts index 3e60955d..0d1d2775 100644 --- a/test/helpers/actions.ts +++ b/test/helpers/actions.ts @@ -375,6 +375,19 @@ export const borrow = async ( txCost ); + console.log("Expected available liquidity: ", expectedReserveData.availableLiquidity.toFixed()); + console.log("Expected total liquidity: ", expectedReserveData.totalLiquidity.toFixed()); + console.log("Expected total debt stable: ", expectedReserveData.totalStableDebt.toFixed()); + console.log("Expected total debt variable: ", expectedReserveData.totalVariableDebt.toFixed()); + console.log("Expected utilization rate: ", expectedReserveData.utilizationRate.toFixed()); + + console.log("actual available liquidity: ", reserveDataAfter.availableLiquidity.toFixed()); + console.log("actual total liquidity: ", reserveDataAfter.totalLiquidity.toFixed()); + console.log("actual total debt stable: ", reserveDataAfter.totalStableDebt.toFixed()); + console.log("actual total debt variable: ", reserveDataAfter.totalVariableDebt.toFixed()); + console.log("actual utilization rate: ", reserveDataAfter.utilizationRate.toFixed()); + console.log("User debt: ", userDataAfter.currentStableDebt.toFixed(0)); + expectEqual(reserveDataAfter, expectedReserveData); expectEqual(userDataAfter, expectedUserData); diff --git a/test/helpers/utils/calculations.ts b/test/helpers/utils/calculations.ts index f8ccc4ce..d394a8ff 100644 --- a/test/helpers/utils/calculations.ts +++ b/test/helpers/utils/calculations.ts @@ -31,7 +31,9 @@ export const calcExpectedUserDataAfterDeposit = ( const expectedUserData = {}; expectedUserData.currentStableDebt = calcExpectedStableDebtTokenBalance( - userDataBeforeAction, + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, txTimestamp ); @@ -71,7 +73,9 @@ export const calcExpectedUserDataAfterDeposit = ( expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(amountDeposited); expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance( - userDataBeforeAction, + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, txTimestamp ); @@ -118,7 +122,9 @@ export const calcExpectedUserDataAfterWithdraw = ( expectedUserData.scaledVariableDebt = userDataBeforeAction.scaledVariableDebt; expectedUserData.currentStableDebt = calcExpectedStableDebtTokenBalance( - userDataBeforeAction, + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, txTimestamp ); @@ -294,13 +300,21 @@ export const calcExpectedReserveDataAfterBorrow = ( reserveDataBeforeAction, txTimestamp ); + expectedReserveData.variableBorrowIndex = calcExpectedVariableBorrowIndex( reserveDataBeforeAction, txTimestamp ); + expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity.minus( + amountBorrowedBN + ); + + expectedReserveData.lastUpdateTimestamp = txTimestamp; + if (borrowRateMode == RateMode.Stable) { expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt; + expectedReserveData.totalVariableDebt = reserveDataBeforeAction.scaledVariableDebt.rayMul( expectedReserveData.variableBorrowIndex ); @@ -317,32 +331,68 @@ export const calcExpectedReserveDataAfterBorrow = ( reserveDataBeforeAction.stableBorrowRate ); + expectedReserveData.principalStableDebt = totalStableDebtUntilTx.plus(amountBorrowedBN); + expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt( { ...reserveDataBeforeAction, - principalStableDebt: totalStableDebtUntilTx.plus(amountBorrowedBN), + principalStableDebt: expectedReserveData.principalStableDebt, averageStableBorrowRate: expectedReserveData.averageStableBorrowRate, + lastUpdateTimestamp: txTimestamp, }, currentTimestamp ); - expectedReserveData.totalLiquidity = reserveDataBeforeAction.availableLiquidity - .minus(amountBorrowedBN) + expectedReserveData.totalLiquidity = expectedReserveData.availableLiquidity .plus(expectedReserveData.totalVariableDebt) .plus(expectedReserveData.totalStableDebt); + + expectedReserveData.utilizationRate = calcExpectedUtilizationRate( + expectedReserveData.totalStableDebt, + expectedReserveData.totalVariableDebt, + expectedReserveData.totalLiquidity + ); + + const rates = calcExpectedInterestRates( + reserveDataBeforeAction.symbol, + reserveDataBeforeAction.marketStableRate, + expectedReserveData.utilizationRate, + expectedReserveData.totalStableDebt, + expectedReserveData.totalVariableDebt, + expectedReserveData.averageStableBorrowRate + ); + + expectedReserveData.liquidityRate = rates[0]; + + expectedReserveData.stableBorrowRate = rates[1]; + + expectedReserveData.variableBorrowRate = rates[2]; } else { expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt; + expectedReserveData.totalStableDebt = calcExpectedStableDebtTokenBalance( - userDataBeforeAction, + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, currentTimestamp ); + expectedReserveData.averageStableBorrowRate = reserveDataBeforeAction.averageStableBorrowRate; + expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.plus( amountBorrowedBN.rayDiv(expectedReserveData.variableBorrowIndex) ); - const totalVariableDebtAfterTx = reserveDataBeforeAction.scaledVariableDebt.rayMul( + const totalVariableDebtAfterTx = expectedReserveData.scaledVariableDebt.rayMul( expectedReserveData.variableBorrowIndex ); + expectedReserveData.utilizationRate = calcExpectedUtilizationRate( + expectedReserveData.totalStableDebt, + totalVariableDebtAfterTx, + expectedReserveData.availableLiquidity + .plus(expectedReserveData.totalStableDebt) + .plus(totalVariableDebtAfterTx) + ); + const rates = calcExpectedInterestRates( reserveDataBeforeAction.symbol, reserveDataBeforeAction.marketStableRate, @@ -358,27 +408,15 @@ export const calcExpectedReserveDataAfterBorrow = ( expectedReserveData.variableBorrowRate = rates[2]; - expectedReserveData.lastUpdateTimestamp = txTimestamp; - expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul( calcExpectedReserveNormalizedDebt(expectedReserveData, currentTimestamp) ); } - expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity.minus( - amountBorrowedBN - ); - expectedReserveData.totalLiquidity = expectedReserveData.availableLiquidity .plus(expectedReserveData.totalStableDebt) .plus(expectedReserveData.totalVariableDebt); - expectedReserveData.utilizationRate = calcExpectedUtilizationRate( - expectedReserveData.totalStableDebt, - expectedReserveData.totalVariableDebt, - expectedReserveData.totalLiquidity - ); - return expectedReserveData; }; @@ -396,7 +434,12 @@ export const calcExpectedReserveDataAfterRepay = ( let amountRepaidBN = new BigNumber(amountRepaid); - const userStableDebt = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp); + const userStableDebt = calcExpectedStableDebtTokenBalance( + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, + txTimestamp + ); const userVariableDebt = calcExpectedVariableDebtTokenBalance( reserveDataBeforeAction, @@ -500,29 +543,50 @@ export const calcExpectedUserDataAfterBorrow = ( ): UserReserveData => { const expectedUserData = {}; - const currentStableDebt = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp); - - const currentVariableDebt = calcExpectedVariableDebtTokenBalance( - reserveDataBeforeAction, - userDataBeforeAction, - txTimestamp - ); + const amountBorrowedBN = new BigNumber(amountBorrowed); if (interestRateMode == RateMode.Stable) { - const debtAccrued = currentStableDebt.minus(userDataBeforeAction.principalStableDebt); + const stableDebtUntilTx = calcExpectedStableDebtTokenBalance( + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, + txTimestamp + ); - expectedUserData.principalStableDebt = currentStableDebt.plus(amountBorrowed); - expectedUserData.scaledVariableDebt = userDataBeforeAction.scaledVariableDebt; + expectedUserData.principalStableDebt = stableDebtUntilTx.plus(amountBorrowed); + expectedUserData.stableRateLastUpdated = txTimestamp; expectedUserData.stableBorrowRate = calcExpectedUserStableRate( - userDataBeforeAction.principalStableDebt.plus(debtAccrued), + stableDebtUntilTx, userDataBeforeAction.stableBorrowRate, - new BigNumber(amountBorrowed), + amountBorrowedBN, reserveDataBeforeAction.stableBorrowRate ); - expectedUserData.stableRateLastUpdated = txTimestamp; + + console.log("Principal stable debt: ",expectedUserData.principalStableDebt.toFixed() ); + console.log("stable borrow rate: ",expectedUserData.stableBorrowRate.toFixed() ); + + expectedUserData.currentStableDebt = calcExpectedStableDebtTokenBalance( + expectedUserData.principalStableDebt, + expectedUserData.stableBorrowRate, + txTimestamp, + currentTimestamp + ); + + console.log("expected stable debt: ", expectedUserData.currentStableDebt); + + expectedUserData.scaledVariableDebt = userDataBeforeAction.scaledVariableDebt; + + expectedUserData.currentVariableDebt = calcExpectedVariableDebtTokenBalance( + expectedDataAfterAction, + expectedUserData, + currentTimestamp + ); } else { - expectedUserData.principalVariableDebt = currentVariableDebt.plus(amountBorrowed); + expectedUserData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.plus( + amountBorrowedBN.rayDiv(expectedDataAfterAction.variableBorrowIndex) + ); + expectedUserData.principalStableDebt = userDataBeforeAction.principalStableDebt; expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate; @@ -530,42 +594,6 @@ export const calcExpectedUserDataAfterBorrow = ( expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated; } - expectedUserData.currentStableDebt = calcExpectedStableDebtTokenBalance( - { - ...userDataBeforeAction, - currentStableDebt: expectedUserData.principalStableDebt, - principalStableDebt: expectedUserData.principalStableDebt, - stableBorrowRate: expectedUserData.stableBorrowRate, - stableRateLastUpdated: expectedUserData.stableRateLastUpdated, - }, - currentTimestamp - ); - - expectedUserData.currentVariableDebt = calcExpectedVariableDebtTokenBalance( - expectedDataAfterAction, - { - ...userDataBeforeAction, - currentVariableDebt: expectedUserData.scaledVariableDebt.rayMul( - reserveDataBeforeAction.variableBorrowIndex - ), - scaledVariableDebt: expectedUserData.scaledVariableDebt, - variableBorrowIndex: - interestRateMode == RateMode.Variable - ? expectedDataAfterAction.variableBorrowIndex - : userDataBeforeAction.variableBorrowIndex, - }, - currentTimestamp - ); - - if (expectedUserData.scaledVariableDebt.eq(0)) { - expectedUserData.variableBorrowIndex = new BigNumber(0); - } else { - expectedUserData.variableBorrowIndex = - interestRateMode == RateMode.Variable - ? expectedDataAfterAction.variableBorrowIndex - : userDataBeforeAction.variableBorrowIndex; - } - expectedUserData.liquidityRate = expectedDataAfterAction.liquidityRate; expectedUserData.usageAsCollateralEnabled = userDataBeforeAction.usageAsCollateralEnabled; @@ -575,6 +603,7 @@ export const calcExpectedUserDataAfterBorrow = ( userDataBeforeAction, currentTimestamp ); + expectedUserData.scaledATokenBalance = userDataBeforeAction.scaledATokenBalance; expectedUserData.walletBalance = userDataBeforeAction.walletBalance.plus(amountBorrowed); @@ -603,7 +632,9 @@ export const calcExpectedUserDataAfterRepay = ( ); const stableBorrowBalance = calcExpectedStableDebtTokenBalance( - userDataBeforeAction, + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, currentTimestamp ); @@ -700,7 +731,12 @@ export const calcExpectedReserveDataAfterSwapRateMode = ( txTimestamp ); - const stableDebt = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp); + const stableDebt = calcExpectedStableDebtTokenBalance( + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, + txTimestamp + ); expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity; @@ -793,7 +829,12 @@ export const calcExpectedUserDataAfterSwapRateMode = ( txTimestamp ); - const stableBorrowBalance = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp); + const stableBorrowBalance = calcExpectedStableDebtTokenBalance( + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, + txTimestamp + ); expectedUserData.currentATokenBalance = calcExpectedATokenBalance( reserveDataBeforeAction, @@ -848,7 +889,12 @@ export const calcExpectedReserveDataAfterStableRateRebalance = ( expectedReserveData.address = reserveDataBeforeAction.address; - const stableBorrowBalance = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp); + const stableBorrowBalance = calcExpectedStableDebtTokenBalance( + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, + txTimestamp + ); const debtAccrued = stableBorrowBalance.minus(userDataBeforeAction.principalStableDebt); @@ -924,7 +970,9 @@ export const calcExpectedUserDataAfterStableRateRebalance = ( txTimestamp ); expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance( - userDataBeforeAction, + userDataBeforeAction.principalStableDebt, + userDataBeforeAction.stableBorrowRate, + userDataBeforeAction.stableRateLastUpdated, txTimestamp ); @@ -957,13 +1005,13 @@ const calcExpectedScaledATokenBalance = ( }; export const calcExpectedATokenBalance = ( - reserveDataBeforeAction: ReserveData, - userDataBeforeAction: UserReserveData, + reserveData: ReserveData, + userData: UserReserveData, currentTimestamp: BigNumber ) => { - const index = calcExpectedReserveNormalizedIncome(reserveDataBeforeAction, currentTimestamp); + const index = calcExpectedReserveNormalizedIncome(reserveData, currentTimestamp); - const {scaledATokenBalance: scaledBalanceBeforeAction} = userDataBeforeAction; + const {scaledATokenBalance: scaledBalanceBeforeAction} = userData; return scaledBalanceBeforeAction.rayMul(index); }; @@ -998,23 +1046,23 @@ const calcExpectedVariableDebtUserIndex = ( }; export const calcExpectedVariableDebtTokenBalance = ( - reserveDataBeforeAction: ReserveData, - userDataBeforeAction: UserReserveData, + reserveData: ReserveData, + userData: UserReserveData, currentTimestamp: BigNumber ) => { - const debt = calcExpectedReserveNormalizedDebt(reserveDataBeforeAction, currentTimestamp); + const normalizedDebt = calcExpectedReserveNormalizedDebt(reserveData, currentTimestamp); - const {scaledVariableDebt} = userDataBeforeAction; + const {scaledVariableDebt} = userData; - return scaledVariableDebt.rayMul(debt); + return scaledVariableDebt.rayMul(normalizedDebt); }; export const calcExpectedStableDebtTokenBalance = ( - userDataBeforeAction: UserReserveData, + principalStableDebt: BigNumber, + stableBorrowRate: BigNumber, + stableRateLastUpdated: BigNumber, currentTimestamp: BigNumber ) => { - const {principalStableDebt, stableBorrowRate, stableRateLastUpdated} = userDataBeforeAction; - if ( stableBorrowRate.eq(0) || currentTimestamp.eq(stableRateLastUpdated) || @@ -1029,7 +1077,7 @@ export const calcExpectedStableDebtTokenBalance = ( stableRateLastUpdated ); - return principalStableDebt.wadToRay().rayMul(cumulatedInterest).rayToWad(); + return principalStableDebt.rayMul(cumulatedInterest); }; const calcLinearInterest = ( diff --git a/test/helpers/utils/helpers.ts b/test/helpers/utils/helpers.ts index 3f3a2ec8..b98ba688 100644 --- a/test/helpers/utils/helpers.ts +++ b/test/helpers/utils/helpers.ts @@ -24,7 +24,7 @@ export const getReserveData = async ( const stableDebtToken = await getStableDebtToken(tokenAddresses.stableDebtTokenAddress); const variableDebtToken = await getVariableDebtToken(tokenAddresses.variableDebtTokenAddress); - const [principalStableDebt] = await stableDebtToken.getPrincipalSupplyAndAvgRate(); + const [principalStableDebt] = await stableDebtToken.getSupplyData(); const scaledVariableDebt = await variableDebtToken.scaledTotalSupply(); diff --git a/test/scenario.spec.ts b/test/scenario.spec.ts index 17830d1c..ee45a08f 100644 --- a/test/scenario.spec.ts +++ b/test/scenario.spec.ts @@ -10,7 +10,7 @@ import {executeStory} from './helpers/scenario-engine'; const scenarioFolder = './test/helpers/scenarios/'; -const selectedScenarios: string[] = ['deposit.json']; +const selectedScenarios: string[] = ['withdraw.json']; fs.readdirSync(scenarioFolder).forEach((file) => { if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;