mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
Updated code to fix deposit and withdraw tests
This commit is contained in:
parent
e4890a14c2
commit
bfe0657b1a
|
@ -27,6 +27,7 @@ import {LendingPoolCollateralManager} from './LendingPoolCollateralManager.sol';
|
||||||
import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol';
|
import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol';
|
||||||
import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol';
|
import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol';
|
||||||
import {ILendingPool} from '../interfaces/ILendingPool.sol';
|
import {ILendingPool} from '../interfaces/ILendingPool.sol';
|
||||||
|
import "@nomiclabs/buidler/console.sol";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @title LendingPool contract
|
* @title LendingPool contract
|
||||||
|
@ -702,6 +703,7 @@ contract LendingPool is VersionedInitializable, ILendingPool {
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
ReserveLogic.ReserveData memory reserve = _reserves[asset];
|
ReserveLogic.ReserveData memory reserve = _reserves[asset];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
IERC20(asset).balanceOf(reserve.aTokenAddress),
|
IERC20(asset).balanceOf(reserve.aTokenAddress),
|
||||||
IERC20(reserve.stableDebtTokenAddress).totalSupply(),
|
IERC20(reserve.stableDebtTokenAddress).totalSupply(),
|
||||||
|
|
|
@ -484,7 +484,7 @@ contract LendingPoolConfigurator is VersionedInitializable {
|
||||||
* @param asset the address of the reserve
|
* @param asset the address of the reserve
|
||||||
* @param reserveFactor the new reserve factor 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);
|
ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset);
|
||||||
|
|
||||||
currentConfig.setReserveFactor(reserveFactor);
|
currentConfig.setReserveFactor(reserveFactor);
|
||||||
|
|
|
@ -42,8 +42,7 @@ library ReserveConfiguration {
|
||||||
* @param self the reserve configuration
|
* @param self the reserve configuration
|
||||||
* @param reserveFactor the reserve factor
|
* @param reserveFactor the reserve factor
|
||||||
**/
|
**/
|
||||||
function setReserveFactor(ReserveConfiguration.Map memory self, uint256 reserveFactor) internal view {
|
function setReserveFactor(ReserveConfiguration.Map memory self, uint256 reserveFactor) internal pure {
|
||||||
console.log("Setting reserve factor to %s", reserveFactor);
|
|
||||||
|
|
||||||
self.data = (self.data & RESERVE_FACTOR_MASK) | reserveFactor << 64;
|
self.data = (self.data & RESERVE_FACTOR_MASK) | reserveFactor << 64;
|
||||||
}
|
}
|
||||||
|
|
|
@ -147,7 +147,7 @@ library ReserveLogic {
|
||||||
* a formal specification.
|
* a formal specification.
|
||||||
* @param reserve the reserve object
|
* @param reserve the reserve object
|
||||||
**/
|
**/
|
||||||
function updateState(ReserveData storage reserve) internal {
|
function updateState(ReserveData storage reserve) external {
|
||||||
address stableDebtToken = reserve.stableDebtTokenAddress;
|
address stableDebtToken = reserve.stableDebtTokenAddress;
|
||||||
address variableDebtToken = reserve.variableDebtTokenAddress;
|
address variableDebtToken = reserve.variableDebtTokenAddress;
|
||||||
uint256 previousVariableBorrowIndex = reserve.variableBorrowIndex;
|
uint256 previousVariableBorrowIndex = reserve.variableBorrowIndex;
|
||||||
|
@ -166,7 +166,6 @@ library ReserveLogic {
|
||||||
reserve,
|
reserve,
|
||||||
stableDebtToken,
|
stableDebtToken,
|
||||||
variableDebtToken,
|
variableDebtToken,
|
||||||
previousLiquidityIndex,
|
|
||||||
previousVariableBorrowIndex,
|
previousVariableBorrowIndex,
|
||||||
newLiquidityIndex,
|
newLiquidityIndex,
|
||||||
newVariableBorrowIndex,
|
newVariableBorrowIndex,
|
||||||
|
@ -306,7 +305,6 @@ library ReserveLogic {
|
||||||
ReserveData storage reserve,
|
ReserveData storage reserve,
|
||||||
address stableDebtToken,
|
address stableDebtToken,
|
||||||
address variableDebtToken,
|
address variableDebtToken,
|
||||||
uint256 previousLiquidityIndex,
|
|
||||||
uint256 previousVariableBorrowIndex,
|
uint256 previousVariableBorrowIndex,
|
||||||
uint256 newLiquidityIndex,
|
uint256 newLiquidityIndex,
|
||||||
uint256 newVariableBorrowIndex,
|
uint256 newVariableBorrowIndex,
|
||||||
|
|
|
@ -8,6 +8,7 @@ import {DebtTokenBase} from './base/DebtTokenBase.sol';
|
||||||
import {MathUtils} from '../libraries/math/MathUtils.sol';
|
import {MathUtils} from '../libraries/math/MathUtils.sol';
|
||||||
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
|
import {WadRayMath} from '../libraries/math/WadRayMath.sol';
|
||||||
import {IStableDebtToken} from './interfaces/IStableDebtToken.sol';
|
import {IStableDebtToken} from './interfaces/IStableDebtToken.sol';
|
||||||
|
import "@nomiclabs/buidler/console.sol";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @title contract StableDebtToken
|
* @title contract StableDebtToken
|
||||||
|
@ -82,7 +83,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MintLocalVars {
|
struct MintLocalVars {
|
||||||
uint256 currentSupply;
|
uint256 previousSupply;
|
||||||
uint256 nextSupply;
|
uint256 nextSupply;
|
||||||
uint256 amountInRay;
|
uint256 amountInRay;
|
||||||
uint256 newStableRate;
|
uint256 newStableRate;
|
||||||
|
@ -111,9 +112,9 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
|
||||||
) = _calculateBalanceIncrease(user);
|
) = _calculateBalanceIncrease(user);
|
||||||
|
|
||||||
//accrueing the interest accumulation to the stored total supply and caching it
|
//accrueing the interest accumulation to the stored total supply and caching it
|
||||||
vars.currentSupply = totalSupply();
|
vars.previousSupply = totalSupply();
|
||||||
vars.currentAvgStableRate = _avgStableRate;
|
vars.currentAvgStableRate = _avgStableRate;
|
||||||
vars.nextSupply = _totalSupply = vars.currentSupply.add(amount);
|
vars.nextSupply = _totalSupply = vars.previousSupply.add(amount);
|
||||||
|
|
||||||
vars.amountInRay = amount.wadToRay();
|
vars.amountInRay = amount.wadToRay();
|
||||||
|
|
||||||
|
@ -132,11 +133,11 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
|
||||||
|
|
||||||
//calculates the updated average stable rate
|
//calculates the updated average stable rate
|
||||||
_avgStableRate = vars.currentAvgStableRate
|
_avgStableRate = vars.currentAvgStableRate
|
||||||
.rayMul(vars.currentSupply.wadToRay())
|
.rayMul(vars.previousSupply.wadToRay())
|
||||||
.add(rate.rayMul(vars.amountInRay))
|
.add(rate.rayMul(vars.amountInRay))
|
||||||
.rayDiv(vars.nextSupply.wadToRay());
|
.rayDiv(vars.nextSupply.wadToRay());
|
||||||
|
|
||||||
_mint(user, amount.add(balanceIncrease));
|
_mint(user, amount.add(balanceIncrease), vars.previousSupply);
|
||||||
|
|
||||||
// transfer event to track balances
|
// transfer event to track balances
|
||||||
emit Transfer(address(0), user, amount);
|
emit Transfer(address(0), user, amount);
|
||||||
|
@ -164,17 +165,15 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
|
||||||
) = _calculateBalanceIncrease(user);
|
) = _calculateBalanceIncrease(user);
|
||||||
|
|
||||||
|
|
||||||
uint256 currentSupply = totalSupply();
|
uint256 previousSupply = totalSupply();
|
||||||
uint256 currentAvgStableRate = _avgStableRate;
|
|
||||||
|
|
||||||
|
|
||||||
if (currentSupply <= amount) {
|
if (previousSupply <= amount) {
|
||||||
_avgStableRate = 0;
|
_avgStableRate = 0;
|
||||||
_totalSupply = 0;
|
_totalSupply = 0;
|
||||||
} else {
|
} else {
|
||||||
uint256 nextSupply = _totalSupply = currentSupply.sub(amount);
|
uint256 nextSupply = _totalSupply = previousSupply.sub(amount);
|
||||||
_avgStableRate = _avgStableRate
|
_avgStableRate = _avgStableRate
|
||||||
.rayMul(currentSupply.wadToRay())
|
.rayMul(previousSupply.wadToRay())
|
||||||
.sub(_usersData[user].rayMul(amount.wadToRay()))
|
.sub(_usersData[user].rayMul(amount.wadToRay()))
|
||||||
.rayDiv(nextSupply.wadToRay());
|
.rayDiv(nextSupply.wadToRay());
|
||||||
}
|
}
|
||||||
|
@ -191,9 +190,9 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
|
||||||
_totalSupplyTimestamp = uint40(block.timestamp);
|
_totalSupplyTimestamp = uint40(block.timestamp);
|
||||||
|
|
||||||
if (balanceIncrease > amount) {
|
if (balanceIncrease > amount) {
|
||||||
_mint(user, balanceIncrease.sub(amount));
|
_mint(user, balanceIncrease.sub(amount), previousSupply);
|
||||||
} else {
|
} else {
|
||||||
_burn(user, amount.sub(balanceIncrease));
|
_burn(user, amount.sub(balanceIncrease), previousSupply);
|
||||||
}
|
}
|
||||||
|
|
||||||
// transfer event to track balances
|
// transfer event to track balances
|
||||||
|
@ -244,7 +243,7 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
function totalSupply() public override view returns (uint256) {
|
function totalSupply() public override view returns (uint256) {
|
||||||
_calcTotalSupply(_avgStableRate);
|
return _calcTotalSupply(_avgStableRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTotalSupplyLastUpdated() public override view returns(uint40) {
|
function getTotalSupplyLastUpdated() public override view returns(uint40) {
|
||||||
|
@ -261,13 +260,37 @@ contract StableDebtToken is IStableDebtToken, DebtTokenBase {
|
||||||
|
|
||||||
function _calcTotalSupply(uint256 avgRate) internal view returns(uint256) {
|
function _calcTotalSupply(uint256 avgRate) internal view returns(uint256) {
|
||||||
uint256 principalSupply = super.totalSupply();
|
uint256 principalSupply = super.totalSupply();
|
||||||
|
|
||||||
if (principalSupply == 0) {
|
if (principalSupply == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint256 cumulatedInterest = MathUtils.calculateCompoundedInterest(
|
uint256 cumulatedInterest = MathUtils.calculateCompoundedInterest(
|
||||||
avgRate,
|
avgRate,
|
||||||
_totalSupplyTimestamp
|
_totalSupplyTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
return principalSupply.rayMul(cumulatedInterest);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -192,8 +192,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
|
||||||
const {configurator, users, weth} = testEnv;
|
const {configurator, users, weth} = testEnv;
|
||||||
await expect(
|
await expect(
|
||||||
configurator.connect(users[2].signer).setReserveFactor(weth.address, '2000'),
|
configurator.connect(users[2].signer).setReserveFactor(weth.address, '2000'),
|
||||||
CALLER_NOT_LENDING_POOL_MANAGER
|
CALLER_NOT_AAVE_ADMIN
|
||||||
).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER);
|
).to.be.revertedWith(CALLER_NOT_AAVE_ADMIN);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -375,6 +375,19 @@ export const borrow = async (
|
||||||
txCost
|
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(reserveDataAfter, expectedReserveData);
|
||||||
expectEqual(userDataAfter, expectedUserData);
|
expectEqual(userDataAfter, expectedUserData);
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,9 @@ export const calcExpectedUserDataAfterDeposit = (
|
||||||
const expectedUserData = <UserReserveData>{};
|
const expectedUserData = <UserReserveData>{};
|
||||||
|
|
||||||
expectedUserData.currentStableDebt = calcExpectedStableDebtTokenBalance(
|
expectedUserData.currentStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction.principalStableDebt,
|
||||||
|
userDataBeforeAction.stableBorrowRate,
|
||||||
|
userDataBeforeAction.stableRateLastUpdated,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -71,7 +73,9 @@ export const calcExpectedUserDataAfterDeposit = (
|
||||||
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(amountDeposited);
|
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.minus(amountDeposited);
|
||||||
|
|
||||||
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
|
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction.principalStableDebt,
|
||||||
|
userDataBeforeAction.stableBorrowRate,
|
||||||
|
userDataBeforeAction.stableRateLastUpdated,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -118,7 +122,9 @@ export const calcExpectedUserDataAfterWithdraw = (
|
||||||
expectedUserData.scaledVariableDebt = userDataBeforeAction.scaledVariableDebt;
|
expectedUserData.scaledVariableDebt = userDataBeforeAction.scaledVariableDebt;
|
||||||
|
|
||||||
expectedUserData.currentStableDebt = calcExpectedStableDebtTokenBalance(
|
expectedUserData.currentStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction.principalStableDebt,
|
||||||
|
userDataBeforeAction.stableBorrowRate,
|
||||||
|
userDataBeforeAction.stableRateLastUpdated,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -294,13 +300,21 @@ export const calcExpectedReserveDataAfterBorrow = (
|
||||||
reserveDataBeforeAction,
|
reserveDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedReserveData.variableBorrowIndex = calcExpectedVariableBorrowIndex(
|
expectedReserveData.variableBorrowIndex = calcExpectedVariableBorrowIndex(
|
||||||
reserveDataBeforeAction,
|
reserveDataBeforeAction,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity.minus(
|
||||||
|
amountBorrowedBN
|
||||||
|
);
|
||||||
|
|
||||||
|
expectedReserveData.lastUpdateTimestamp = txTimestamp;
|
||||||
|
|
||||||
if (borrowRateMode == RateMode.Stable) {
|
if (borrowRateMode == RateMode.Stable) {
|
||||||
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt;
|
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt;
|
||||||
|
|
||||||
expectedReserveData.totalVariableDebt = reserveDataBeforeAction.scaledVariableDebt.rayMul(
|
expectedReserveData.totalVariableDebt = reserveDataBeforeAction.scaledVariableDebt.rayMul(
|
||||||
expectedReserveData.variableBorrowIndex
|
expectedReserveData.variableBorrowIndex
|
||||||
);
|
);
|
||||||
|
@ -317,32 +331,68 @@ export const calcExpectedReserveDataAfterBorrow = (
|
||||||
reserveDataBeforeAction.stableBorrowRate
|
reserveDataBeforeAction.stableBorrowRate
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expectedReserveData.principalStableDebt = totalStableDebtUntilTx.plus(amountBorrowedBN);
|
||||||
|
|
||||||
expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(
|
expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(
|
||||||
{
|
{
|
||||||
...reserveDataBeforeAction,
|
...reserveDataBeforeAction,
|
||||||
principalStableDebt: totalStableDebtUntilTx.plus(amountBorrowedBN),
|
principalStableDebt: expectedReserveData.principalStableDebt,
|
||||||
averageStableBorrowRate: expectedReserveData.averageStableBorrowRate,
|
averageStableBorrowRate: expectedReserveData.averageStableBorrowRate,
|
||||||
|
lastUpdateTimestamp: txTimestamp,
|
||||||
},
|
},
|
||||||
currentTimestamp
|
currentTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedReserveData.totalLiquidity = reserveDataBeforeAction.availableLiquidity
|
expectedReserveData.totalLiquidity = expectedReserveData.availableLiquidity
|
||||||
.minus(amountBorrowedBN)
|
|
||||||
.plus(expectedReserveData.totalVariableDebt)
|
.plus(expectedReserveData.totalVariableDebt)
|
||||||
.plus(expectedReserveData.totalStableDebt);
|
.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 {
|
} else {
|
||||||
expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt;
|
expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt;
|
||||||
|
|
||||||
expectedReserveData.totalStableDebt = calcExpectedStableDebtTokenBalance(
|
expectedReserveData.totalStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction.principalStableDebt,
|
||||||
|
userDataBeforeAction.stableBorrowRate,
|
||||||
|
userDataBeforeAction.stableRateLastUpdated,
|
||||||
currentTimestamp
|
currentTimestamp
|
||||||
);
|
);
|
||||||
|
expectedReserveData.averageStableBorrowRate = reserveDataBeforeAction.averageStableBorrowRate;
|
||||||
|
|
||||||
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.plus(
|
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.plus(
|
||||||
amountBorrowedBN.rayDiv(expectedReserveData.variableBorrowIndex)
|
amountBorrowedBN.rayDiv(expectedReserveData.variableBorrowIndex)
|
||||||
);
|
);
|
||||||
const totalVariableDebtAfterTx = reserveDataBeforeAction.scaledVariableDebt.rayMul(
|
const totalVariableDebtAfterTx = expectedReserveData.scaledVariableDebt.rayMul(
|
||||||
expectedReserveData.variableBorrowIndex
|
expectedReserveData.variableBorrowIndex
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expectedReserveData.utilizationRate = calcExpectedUtilizationRate(
|
||||||
|
expectedReserveData.totalStableDebt,
|
||||||
|
totalVariableDebtAfterTx,
|
||||||
|
expectedReserveData.availableLiquidity
|
||||||
|
.plus(expectedReserveData.totalStableDebt)
|
||||||
|
.plus(totalVariableDebtAfterTx)
|
||||||
|
);
|
||||||
|
|
||||||
const rates = calcExpectedInterestRates(
|
const rates = calcExpectedInterestRates(
|
||||||
reserveDataBeforeAction.symbol,
|
reserveDataBeforeAction.symbol,
|
||||||
reserveDataBeforeAction.marketStableRate,
|
reserveDataBeforeAction.marketStableRate,
|
||||||
|
@ -358,27 +408,15 @@ export const calcExpectedReserveDataAfterBorrow = (
|
||||||
|
|
||||||
expectedReserveData.variableBorrowRate = rates[2];
|
expectedReserveData.variableBorrowRate = rates[2];
|
||||||
|
|
||||||
expectedReserveData.lastUpdateTimestamp = txTimestamp;
|
|
||||||
|
|
||||||
expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul(
|
expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul(
|
||||||
calcExpectedReserveNormalizedDebt(expectedReserveData, currentTimestamp)
|
calcExpectedReserveNormalizedDebt(expectedReserveData, currentTimestamp)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity.minus(
|
|
||||||
amountBorrowedBN
|
|
||||||
);
|
|
||||||
|
|
||||||
expectedReserveData.totalLiquidity = expectedReserveData.availableLiquidity
|
expectedReserveData.totalLiquidity = expectedReserveData.availableLiquidity
|
||||||
.plus(expectedReserveData.totalStableDebt)
|
.plus(expectedReserveData.totalStableDebt)
|
||||||
.plus(expectedReserveData.totalVariableDebt);
|
.plus(expectedReserveData.totalVariableDebt);
|
||||||
|
|
||||||
expectedReserveData.utilizationRate = calcExpectedUtilizationRate(
|
|
||||||
expectedReserveData.totalStableDebt,
|
|
||||||
expectedReserveData.totalVariableDebt,
|
|
||||||
expectedReserveData.totalLiquidity
|
|
||||||
);
|
|
||||||
|
|
||||||
return expectedReserveData;
|
return expectedReserveData;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -396,7 +434,12 @@ export const calcExpectedReserveDataAfterRepay = (
|
||||||
|
|
||||||
let amountRepaidBN = new BigNumber(amountRepaid);
|
let amountRepaidBN = new BigNumber(amountRepaid);
|
||||||
|
|
||||||
const userStableDebt = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp);
|
const userStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
|
userDataBeforeAction.principalStableDebt,
|
||||||
|
userDataBeforeAction.stableBorrowRate,
|
||||||
|
userDataBeforeAction.stableRateLastUpdated,
|
||||||
|
txTimestamp
|
||||||
|
);
|
||||||
|
|
||||||
const userVariableDebt = calcExpectedVariableDebtTokenBalance(
|
const userVariableDebt = calcExpectedVariableDebtTokenBalance(
|
||||||
reserveDataBeforeAction,
|
reserveDataBeforeAction,
|
||||||
|
@ -500,29 +543,50 @@ export const calcExpectedUserDataAfterBorrow = (
|
||||||
): UserReserveData => {
|
): UserReserveData => {
|
||||||
const expectedUserData = <UserReserveData>{};
|
const expectedUserData = <UserReserveData>{};
|
||||||
|
|
||||||
const currentStableDebt = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp);
|
const amountBorrowedBN = new BigNumber(amountBorrowed);
|
||||||
|
|
||||||
const currentVariableDebt = calcExpectedVariableDebtTokenBalance(
|
|
||||||
reserveDataBeforeAction,
|
|
||||||
userDataBeforeAction,
|
|
||||||
txTimestamp
|
|
||||||
);
|
|
||||||
|
|
||||||
if (interestRateMode == RateMode.Stable) {
|
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.principalStableDebt = stableDebtUntilTx.plus(amountBorrowed);
|
||||||
expectedUserData.scaledVariableDebt = userDataBeforeAction.scaledVariableDebt;
|
expectedUserData.stableRateLastUpdated = txTimestamp;
|
||||||
|
|
||||||
expectedUserData.stableBorrowRate = calcExpectedUserStableRate(
|
expectedUserData.stableBorrowRate = calcExpectedUserStableRate(
|
||||||
userDataBeforeAction.principalStableDebt.plus(debtAccrued),
|
stableDebtUntilTx,
|
||||||
userDataBeforeAction.stableBorrowRate,
|
userDataBeforeAction.stableBorrowRate,
|
||||||
new BigNumber(amountBorrowed),
|
amountBorrowedBN,
|
||||||
reserveDataBeforeAction.stableBorrowRate
|
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 {
|
} else {
|
||||||
expectedUserData.principalVariableDebt = currentVariableDebt.plus(amountBorrowed);
|
expectedUserData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.plus(
|
||||||
|
amountBorrowedBN.rayDiv(expectedDataAfterAction.variableBorrowIndex)
|
||||||
|
);
|
||||||
|
|
||||||
expectedUserData.principalStableDebt = userDataBeforeAction.principalStableDebt;
|
expectedUserData.principalStableDebt = userDataBeforeAction.principalStableDebt;
|
||||||
|
|
||||||
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
|
expectedUserData.stableBorrowRate = userDataBeforeAction.stableBorrowRate;
|
||||||
|
@ -530,42 +594,6 @@ export const calcExpectedUserDataAfterBorrow = (
|
||||||
expectedUserData.stableRateLastUpdated = userDataBeforeAction.stableRateLastUpdated;
|
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.liquidityRate = expectedDataAfterAction.liquidityRate;
|
||||||
|
|
||||||
expectedUserData.usageAsCollateralEnabled = userDataBeforeAction.usageAsCollateralEnabled;
|
expectedUserData.usageAsCollateralEnabled = userDataBeforeAction.usageAsCollateralEnabled;
|
||||||
|
@ -575,6 +603,7 @@ export const calcExpectedUserDataAfterBorrow = (
|
||||||
userDataBeforeAction,
|
userDataBeforeAction,
|
||||||
currentTimestamp
|
currentTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
expectedUserData.scaledATokenBalance = userDataBeforeAction.scaledATokenBalance;
|
expectedUserData.scaledATokenBalance = userDataBeforeAction.scaledATokenBalance;
|
||||||
|
|
||||||
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.plus(amountBorrowed);
|
expectedUserData.walletBalance = userDataBeforeAction.walletBalance.plus(amountBorrowed);
|
||||||
|
@ -603,7 +632,9 @@ export const calcExpectedUserDataAfterRepay = (
|
||||||
);
|
);
|
||||||
|
|
||||||
const stableBorrowBalance = calcExpectedStableDebtTokenBalance(
|
const stableBorrowBalance = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction.principalStableDebt,
|
||||||
|
userDataBeforeAction.stableBorrowRate,
|
||||||
|
userDataBeforeAction.stableRateLastUpdated,
|
||||||
currentTimestamp
|
currentTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -700,7 +731,12 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
const stableDebt = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp);
|
const stableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
|
userDataBeforeAction.principalStableDebt,
|
||||||
|
userDataBeforeAction.stableBorrowRate,
|
||||||
|
userDataBeforeAction.stableRateLastUpdated,
|
||||||
|
txTimestamp
|
||||||
|
);
|
||||||
|
|
||||||
expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity;
|
expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity;
|
||||||
|
|
||||||
|
@ -793,7 +829,12 @@ export const calcExpectedUserDataAfterSwapRateMode = (
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
const stableBorrowBalance = calcExpectedStableDebtTokenBalance(userDataBeforeAction, txTimestamp);
|
const stableBorrowBalance = calcExpectedStableDebtTokenBalance(
|
||||||
|
userDataBeforeAction.principalStableDebt,
|
||||||
|
userDataBeforeAction.stableBorrowRate,
|
||||||
|
userDataBeforeAction.stableRateLastUpdated,
|
||||||
|
txTimestamp
|
||||||
|
);
|
||||||
|
|
||||||
expectedUserData.currentATokenBalance = calcExpectedATokenBalance(
|
expectedUserData.currentATokenBalance = calcExpectedATokenBalance(
|
||||||
reserveDataBeforeAction,
|
reserveDataBeforeAction,
|
||||||
|
@ -848,7 +889,12 @@ export const calcExpectedReserveDataAfterStableRateRebalance = (
|
||||||
|
|
||||||
expectedReserveData.address = reserveDataBeforeAction.address;
|
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);
|
const debtAccrued = stableBorrowBalance.minus(userDataBeforeAction.principalStableDebt);
|
||||||
|
|
||||||
|
@ -924,7 +970,9 @@ export const calcExpectedUserDataAfterStableRateRebalance = (
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
|
expectedUserData.currentStableDebt = expectedUserData.principalStableDebt = calcExpectedStableDebtTokenBalance(
|
||||||
userDataBeforeAction,
|
userDataBeforeAction.principalStableDebt,
|
||||||
|
userDataBeforeAction.stableBorrowRate,
|
||||||
|
userDataBeforeAction.stableRateLastUpdated,
|
||||||
txTimestamp
|
txTimestamp
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -957,13 +1005,13 @@ const calcExpectedScaledATokenBalance = (
|
||||||
};
|
};
|
||||||
|
|
||||||
export const calcExpectedATokenBalance = (
|
export const calcExpectedATokenBalance = (
|
||||||
reserveDataBeforeAction: ReserveData,
|
reserveData: ReserveData,
|
||||||
userDataBeforeAction: UserReserveData,
|
userData: UserReserveData,
|
||||||
currentTimestamp: BigNumber
|
currentTimestamp: BigNumber
|
||||||
) => {
|
) => {
|
||||||
const index = calcExpectedReserveNormalizedIncome(reserveDataBeforeAction, currentTimestamp);
|
const index = calcExpectedReserveNormalizedIncome(reserveData, currentTimestamp);
|
||||||
|
|
||||||
const {scaledATokenBalance: scaledBalanceBeforeAction} = userDataBeforeAction;
|
const {scaledATokenBalance: scaledBalanceBeforeAction} = userData;
|
||||||
|
|
||||||
return scaledBalanceBeforeAction.rayMul(index);
|
return scaledBalanceBeforeAction.rayMul(index);
|
||||||
};
|
};
|
||||||
|
@ -998,23 +1046,23 @@ const calcExpectedVariableDebtUserIndex = (
|
||||||
};
|
};
|
||||||
|
|
||||||
export const calcExpectedVariableDebtTokenBalance = (
|
export const calcExpectedVariableDebtTokenBalance = (
|
||||||
reserveDataBeforeAction: ReserveData,
|
reserveData: ReserveData,
|
||||||
userDataBeforeAction: UserReserveData,
|
userData: UserReserveData,
|
||||||
currentTimestamp: BigNumber
|
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 = (
|
export const calcExpectedStableDebtTokenBalance = (
|
||||||
userDataBeforeAction: UserReserveData,
|
principalStableDebt: BigNumber,
|
||||||
|
stableBorrowRate: BigNumber,
|
||||||
|
stableRateLastUpdated: BigNumber,
|
||||||
currentTimestamp: BigNumber
|
currentTimestamp: BigNumber
|
||||||
) => {
|
) => {
|
||||||
const {principalStableDebt, stableBorrowRate, stableRateLastUpdated} = userDataBeforeAction;
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
stableBorrowRate.eq(0) ||
|
stableBorrowRate.eq(0) ||
|
||||||
currentTimestamp.eq(stableRateLastUpdated) ||
|
currentTimestamp.eq(stableRateLastUpdated) ||
|
||||||
|
@ -1029,7 +1077,7 @@ export const calcExpectedStableDebtTokenBalance = (
|
||||||
stableRateLastUpdated
|
stableRateLastUpdated
|
||||||
);
|
);
|
||||||
|
|
||||||
return principalStableDebt.wadToRay().rayMul(cumulatedInterest).rayToWad();
|
return principalStableDebt.rayMul(cumulatedInterest);
|
||||||
};
|
};
|
||||||
|
|
||||||
const calcLinearInterest = (
|
const calcLinearInterest = (
|
||||||
|
|
|
@ -24,7 +24,7 @@ export const getReserveData = async (
|
||||||
const stableDebtToken = await getStableDebtToken(tokenAddresses.stableDebtTokenAddress);
|
const stableDebtToken = await getStableDebtToken(tokenAddresses.stableDebtTokenAddress);
|
||||||
const variableDebtToken = await getVariableDebtToken(tokenAddresses.variableDebtTokenAddress);
|
const variableDebtToken = await getVariableDebtToken(tokenAddresses.variableDebtTokenAddress);
|
||||||
|
|
||||||
const [principalStableDebt] = await stableDebtToken.getPrincipalSupplyAndAvgRate();
|
const [principalStableDebt] = await stableDebtToken.getSupplyData();
|
||||||
|
|
||||||
const scaledVariableDebt = await variableDebtToken.scaledTotalSupply();
|
const scaledVariableDebt = await variableDebtToken.scaledTotalSupply();
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ import {executeStory} from './helpers/scenario-engine';
|
||||||
|
|
||||||
const scenarioFolder = './test/helpers/scenarios/';
|
const scenarioFolder = './test/helpers/scenarios/';
|
||||||
|
|
||||||
const selectedScenarios: string[] = ['deposit.json'];
|
const selectedScenarios: string[] = ['withdraw.json'];
|
||||||
|
|
||||||
fs.readdirSync(scenarioFolder).forEach((file) => {
|
fs.readdirSync(scenarioFolder).forEach((file) => {
|
||||||
if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;
|
if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user