Updating rebalance rate tests

This commit is contained in:
The3D 2020-09-21 15:27:53 +02:00
parent 8792515f5b
commit a1a45d392a
5 changed files with 74 additions and 32 deletions

View File

@ -49,7 +49,8 @@ const almostEqualOrEqual = function (
key === 'marketStableRate' ||
key === 'symbol' ||
key === 'aTokenAddress' ||
key === 'decimals'
key === 'decimals' ||
key === 'totalStableDebtLastUpdated'
) {
// skipping consistency check on accessory data
return;
@ -369,9 +370,23 @@ export const borrow = async (
userDataBefore,
txTimestamp,
timestamp,
txCost
onBehalfOf,
user.address
);
console.log("total debt stable exp ", expectedReserveData.totalStableDebt.toFixed());
console.log("total debt stable act ", reserveDataAfter.totalStableDebt.toFixed());
console.log("total debt variable exp ", expectedReserveData.totalVariableDebt.toFixed());
console.log("total debt variable act ", reserveDataAfter.totalVariableDebt.toFixed());
console.log("avl liquidity exp ", expectedReserveData.availableLiquidity.toFixed());
console.log("avl liquidity exp ", reserveDataAfter.availableLiquidity.toFixed());
console.log("avg borrow rate exp ", expectedReserveData.averageStableBorrowRate.toFixed());
console.log("avl borrow rate exp ", reserveDataAfter.averageStableBorrowRate.toFixed());
expectEqual(reserveDataAfter, expectedReserveData);
expectEqual(userDataAfter, expectedUserData);
@ -664,6 +679,15 @@ export const rebalanceStableBorrowRate = async (
txTimestamp
);
console.log("total debt stable exp ", expectedReserveData.totalStableDebt.toFixed());
console.log("total debt stable act ", reserveDataAfter.totalStableDebt.toFixed());
console.log("total debt variable exp ", expectedReserveData.totalVariableDebt.toFixed());
console.log("total debt variable act ", reserveDataAfter.totalVariableDebt.toFixed());
console.log("avl liquidity exp ", expectedReserveData.availableLiquidity.toFixed());
console.log("avl liquidity exp ", reserveDataAfter.availableLiquidity.toFixed());
expectEqual(reserveDataAfter, expectedReserveData);
expectEqual(userDataAfter, expectedUserData);

View File

@ -7,7 +7,7 @@ import {
EXCESS_UTILIZATION_RATE,
ZERO_ADDRESS,
} from '../../../helpers/constants';
import {IReserveParams, iAavePoolAssets, RateMode} from '../../../helpers/types';
import {IReserveParams, iAavePoolAssets, RateMode, tEthereumAddress} from '../../../helpers/types';
import './math';
import {ReserveData, UserReserveData} from './interfaces';
@ -185,7 +185,7 @@ export const calcExpectedReserveDataAfterDeposit = (
expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(
reserveDataBeforeAction.principalStableDebt,
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.lastUpdateTimestamp,
reserveDataBeforeAction.totalStableDebtLastUpdated,
txTimestamp
);
expectedReserveData.totalVariableDebt = calcExpectedTotalVariableDebt(
@ -253,7 +253,7 @@ export const calcExpectedReserveDataAfterWithdraw = (
expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(
reserveDataBeforeAction.principalStableDebt,
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.lastUpdateTimestamp,
reserveDataBeforeAction.totalStableDebtLastUpdated,
txTimestamp
);
expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul(
@ -318,6 +318,7 @@ export const calcExpectedReserveDataAfterBorrow = (
expectedReserveData.lastUpdateTimestamp = txTimestamp;
if (borrowRateMode == RateMode.Stable) {
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt;
const expectedVariableDebtAfterTx = expectedReserveData.scaledVariableDebt.rayMul(
@ -327,7 +328,7 @@ export const calcExpectedReserveDataAfterBorrow = (
const expectedStableDebtUntilTx = calcExpectedTotalStableDebt(
reserveDataBeforeAction.principalStableDebt,
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.lastUpdateTimestamp,
reserveDataBeforeAction.totalStableDebtLastUpdated,
txTimestamp
);
@ -403,7 +404,7 @@ export const calcExpectedReserveDataAfterBorrow = (
expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(
reserveDataBeforeAction.principalStableDebt,
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.lastUpdateTimestamp,
reserveDataBeforeAction.totalStableDebtLastUpdated,
currentTimestamp
);
@ -512,7 +513,7 @@ export const calcExpectedReserveDataAfterRepay = (
const expectedDebt = calcExpectedTotalStableDebt(
reserveDataBeforeAction.principalStableDebt,
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.lastUpdateTimestamp,
reserveDataBeforeAction.totalStableDebtLastUpdated,
txTimestamp
);
@ -597,13 +598,15 @@ export const calcExpectedUserDataAfterBorrow = (
userDataBeforeAction: UserReserveData,
txTimestamp: BigNumber,
currentTimestamp: BigNumber,
txCost: BigNumber
user: tEthereumAddress,
onBehalfOf: tEthereumAddress
): UserReserveData => {
const expectedUserData = <UserReserveData>{};
const amountBorrowedBN = new BigNumber(amountBorrowed);
if (interestRateMode == RateMode.Stable) {
const stableDebtUntilTx = calcExpectedStableDebtTokenBalance(
userDataBeforeAction.principalStableDebt,
userDataBeforeAction.stableBorrowRate,
@ -805,7 +808,7 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
const totalStableDebtUntilTx = calcExpectedTotalStableDebt(
reserveDataBeforeAction.principalStableDebt,
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.lastUpdateTimestamp,
reserveDataBeforeAction.totalStableDebtLastUpdated,
txTimestamp
);
@ -819,7 +822,9 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
expectedReserveData.variableBorrowIndex
);
expectedReserveData.principalStableDebt = expectedReserveData.totalStableDebt = totalStableDebtUntilTx.minus(stableDebt);
expectedReserveData.principalStableDebt = expectedReserveData.totalStableDebt = totalStableDebtUntilTx.minus(
stableDebt
);
expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate(
reserveDataBeforeAction.averageStableBorrowRate,
@ -828,7 +833,6 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
userDataBeforeAction.stableBorrowRate
);
} else {
//swap variable to stable
expectedReserveData.principalStableDebt = expectedReserveData.totalStableDebt = totalStableDebtUntilTx.plus(
@ -953,39 +957,58 @@ export const calcExpectedReserveDataAfterStableRateRebalance = (
expectedReserveData.address = reserveDataBeforeAction.address;
const stableBorrowBalance = calcExpectedStableDebtTokenBalance(
const userStableDebt = calcExpectedStableDebtTokenBalance(
userDataBeforeAction.principalStableDebt,
userDataBeforeAction.stableBorrowRate,
userDataBeforeAction.stableRateLastUpdated,
txTimestamp
);
const debtAccrued = stableBorrowBalance.minus(userDataBeforeAction.principalStableDebt);
expectedReserveData.liquidityIndex = calcExpectedLiquidityIndex(
reserveDataBeforeAction,
txTimestamp
);
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued);
expectedReserveData.variableBorrowIndex = calcExpectedVariableBorrowIndex(
reserveDataBeforeAction,
txTimestamp
);
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt;
expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul(
expectedReserveData.variableBorrowIndex
);
expectedReserveData.principalStableDebt = expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(
reserveDataBeforeAction.principalStableDebt,
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.totalStableDebtLastUpdated,
txTimestamp
);
expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity;
expectedReserveData.totalLiquidity = expectedReserveData.availableLiquidity
.plus(expectedReserveData.totalStableDebt)
.plus(expectedReserveData.totalVariableDebt);
//removing the stable liquidity at the old rate
const avgRateBefore = calcExpectedAverageStableBorrowRate(
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.totalStableDebt.plus(debtAccrued),
stableBorrowBalance.negated(),
expectedReserveData.totalStableDebt,
userStableDebt.negated(),
userDataBeforeAction.stableBorrowRate
);
// adding it again at the new rate
expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate(
avgRateBefore,
reserveDataBeforeAction.totalStableDebt.minus(userDataBeforeAction.principalStableDebt),
stableBorrowBalance,
expectedReserveData.totalStableDebt.minus(userStableDebt),
userStableDebt,
reserveDataBeforeAction.stableBorrowRate
);
expectedReserveData.totalVariableDebt = reserveDataBeforeAction.totalVariableDebt;
expectedReserveData.totalStableDebt = reserveDataBeforeAction.totalStableDebt.plus(debtAccrued);
expectedReserveData.utilizationRate = calcExpectedUtilizationRate(
expectedReserveData.totalStableDebt,
expectedReserveData.totalVariableDebt,
@ -1007,15 +1030,6 @@ export const calcExpectedReserveDataAfterStableRateRebalance = (
expectedReserveData.variableBorrowRate = rates[2];
expectedReserveData.liquidityIndex = calcExpectedLiquidityIndex(
reserveDataBeforeAction,
txTimestamp
);
expectedReserveData.variableBorrowIndex = calcExpectedVariableBorrowIndex(
reserveDataBeforeAction,
txTimestamp
);
return expectedReserveData;
};

View File

@ -25,6 +25,8 @@ export const getReserveData = async (
const variableDebtToken = await getVariableDebtToken(tokenAddresses.variableDebtTokenAddress);
const [principalStableDebt] = await stableDebtToken.getSupplyData();
const totalStableDebtLastUpdated = await stableDebtToken.getTotalSupplyLastUpdated();
const scaledVariableDebt = await variableDebtToken.scaledTotalSupply();
@ -57,6 +59,7 @@ export const getReserveData = async (
liquidityIndex: new BigNumber(reserveData.liquidityIndex.toString()),
variableBorrowIndex: new BigNumber(reserveData.variableBorrowIndex.toString()),
lastUpdateTimestamp: new BigNumber(reserveData.lastUpdateTimestamp),
totalStableDebtLastUpdated: new BigNumber(totalStableDebtLastUpdated),
principalStableDebt: new BigNumber(principalStableDebt.toString()),
scaledVariableDebt: new BigNumber(scaledVariableDebt.toString()),
address: reserve,

View File

@ -34,6 +34,7 @@ export interface ReserveData {
aTokenAddress: string;
marketStableRate: BigNumber;
lastUpdateTimestamp: BigNumber;
totalStableDebtLastUpdated: BigNumber;
liquidityRate: BigNumber;
[key: string]: BigNumber | string;
}

View File

@ -10,7 +10,7 @@ import {executeStory} from './helpers/scenario-engine';
const scenarioFolder = './test/helpers/scenarios/';
const selectedScenarios: string[] = ['swap-rate-mode.json'];
const selectedScenarios: string[] = [];
fs.readdirSync(scenarioFolder).forEach((file) => {
if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;