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 === 'marketStableRate' ||
key === 'symbol' || key === 'symbol' ||
key === 'aTokenAddress' || key === 'aTokenAddress' ||
key === 'decimals' key === 'decimals' ||
key === 'totalStableDebtLastUpdated'
) { ) {
// skipping consistency check on accessory data // skipping consistency check on accessory data
return; return;
@ -369,9 +370,23 @@ export const borrow = async (
userDataBefore, userDataBefore,
txTimestamp, txTimestamp,
timestamp, 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(reserveDataAfter, expectedReserveData);
expectEqual(userDataAfter, expectedUserData); expectEqual(userDataAfter, expectedUserData);
@ -664,6 +679,15 @@ export const rebalanceStableBorrowRate = async (
txTimestamp 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(reserveDataAfter, expectedReserveData);
expectEqual(userDataAfter, expectedUserData); expectEqual(userDataAfter, expectedUserData);

View File

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

View File

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

View File

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

View File

@ -10,7 +10,7 @@ import {executeStory} from './helpers/scenario-engine';
const scenarioFolder = './test/helpers/scenarios/'; const scenarioFolder = './test/helpers/scenarios/';
const selectedScenarios: string[] = ['swap-rate-mode.json']; const selectedScenarios: string[] = [];
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;