fixed swap rate mode tests

This commit is contained in:
The3D 2020-09-21 14:29:59 +02:00
parent c8b044aecf
commit 8792515f5b
2 changed files with 59 additions and 48 deletions

View File

@ -391,7 +391,6 @@ export const calcExpectedReserveDataAfterBorrow = (
expectedReserveData.totalLiquidity expectedReserveData.totalLiquidity
); );
} else { } else {
expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt; expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt;
const totalStableDebtAfterTx = calcExpectedStableDebtTokenBalance( const totalStableDebtAfterTx = calcExpectedStableDebtTokenBalance(
@ -791,41 +790,59 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
txTimestamp txTimestamp
); );
expectedReserveData.liquidityIndex = calcExpectedLiquidityIndex(
reserveDataBeforeAction,
txTimestamp
);
expectedReserveData.variableBorrowIndex = calcExpectedVariableBorrowIndex(
reserveDataBeforeAction,
txTimestamp
);
expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity; expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity;
const totalStableDebtUntilTx = calcExpectedTotalStableDebt(
reserveDataBeforeAction.principalStableDebt,
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.lastUpdateTimestamp,
txTimestamp
);
if (rateMode === RateMode.Stable) { if (rateMode === RateMode.Stable) {
//swap user stable debt to variable //swap user stable debt to variable
const debtAccrued = stableDebt.minus(userDataBeforeAction.principalStableDebt); expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.plus(
stableDebt.rayDiv(expectedReserveData.variableBorrowIndex)
);
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued); expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul(
expectedReserveData.variableBorrowIndex
);
expectedReserveData.principalStableDebt = expectedReserveData.totalStableDebt = totalStableDebtUntilTx.minus(stableDebt);
expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate( expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate(
reserveDataBeforeAction.averageStableBorrowRate, reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.totalStableDebt.plus(debtAccrued), expectedReserveData.principalStableDebt.plus(stableDebt),
stableDebt.negated(), stableDebt.negated(),
userDataBeforeAction.stableBorrowRate userDataBeforeAction.stableBorrowRate
); );
expectedReserveData.totalVariableDebt = reserveDataBeforeAction.totalVariableDebt.plus(
stableDebt
);
expectedReserveData.totalStableDebt = reserveDataBeforeAction.totalStableDebt.minus(
userDataBeforeAction.principalStableDebt
);
} else { } else {
const totalDebtBefore = userDataBeforeAction.scaledVariableDebt.rayMul(
reserveDataBeforeAction.variableBorrowIndex
);
const debtAccrued = variableDebt.minus(totalDebtBefore);
expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued); //swap variable to stable
expectedReserveData.totalVariableDebt = reserveDataBeforeAction.totalVariableDebt; expectedReserveData.principalStableDebt = expectedReserveData.totalStableDebt = totalStableDebtUntilTx.plus(
expectedReserveData.totalStableDebt = reserveDataBeforeAction.totalStableDebt.plus(
variableDebt variableDebt
); );
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.minus(
variableDebt.rayDiv(expectedReserveData.variableBorrowIndex)
);
expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul(
expectedReserveData.variableBorrowIndex
);
expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate( expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate(
reserveDataBeforeAction.averageStableBorrowRate, reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.totalStableDebt, reserveDataBeforeAction.totalStableDebt,
@ -834,6 +851,10 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
); );
} }
expectedReserveData.totalLiquidity = reserveDataBeforeAction.availableLiquidity
.plus(expectedReserveData.totalStableDebt)
.plus(expectedReserveData.totalVariableDebt);
expectedReserveData.utilizationRate = calcExpectedUtilizationRate( expectedReserveData.utilizationRate = calcExpectedUtilizationRate(
expectedReserveData.totalStableDebt, expectedReserveData.totalStableDebt,
expectedReserveData.totalVariableDebt, expectedReserveData.totalVariableDebt,
@ -854,15 +875,6 @@ export const calcExpectedReserveDataAfterSwapRateMode = (
expectedReserveData.variableBorrowRate = rates[2]; expectedReserveData.variableBorrowRate = rates[2];
expectedReserveData.liquidityIndex = calcExpectedLiquidityIndex(
reserveDataBeforeAction,
txTimestamp
);
expectedReserveData.variableBorrowIndex = calcExpectedVariableBorrowIndex(
reserveDataBeforeAction,
txTimestamp
);
return expectedReserveData; return expectedReserveData;
}; };
@ -876,19 +888,19 @@ export const calcExpectedUserDataAfterSwapRateMode = (
): UserReserveData => { ): UserReserveData => {
const expectedUserData = {...userDataBeforeAction}; const expectedUserData = {...userDataBeforeAction};
const variableBorrowBalance = calcExpectedVariableDebtTokenBalance( const stableDebtBalance = calcExpectedStableDebtTokenBalance(
reserveDataBeforeAction,
userDataBeforeAction,
txTimestamp
);
const stableBorrowBalance = calcExpectedStableDebtTokenBalance(
userDataBeforeAction.principalStableDebt, userDataBeforeAction.principalStableDebt,
userDataBeforeAction.stableBorrowRate, userDataBeforeAction.stableBorrowRate,
userDataBeforeAction.stableRateLastUpdated, userDataBeforeAction.stableRateLastUpdated,
txTimestamp txTimestamp
); );
const variableDebtBalance = calcExpectedVariableDebtTokenBalance(
reserveDataBeforeAction,
userDataBeforeAction,
txTimestamp
);
expectedUserData.currentATokenBalance = calcExpectedATokenBalance( expectedUserData.currentATokenBalance = calcExpectedATokenBalance(
reserveDataBeforeAction, reserveDataBeforeAction,
userDataBeforeAction, userDataBeforeAction,
@ -901,31 +913,30 @@ export const calcExpectedUserDataAfterSwapRateMode = (
expectedUserData.stableBorrowRate = new BigNumber(0); expectedUserData.stableBorrowRate = new BigNumber(0);
expectedUserData.principalVariableDebt = expectedUserData.currentVariableDebt = userDataBeforeAction.currentVariableDebt.plus( expectedUserData.scaledVariableDebt = userDataBeforeAction.scaledVariableDebt.plus(
stableBorrowBalance stableDebtBalance.rayDiv(expectedDataAfterAction.variableBorrowIndex)
); );
expectedUserData.variableBorrowIndex = expectedDataAfterAction.variableBorrowIndex; expectedUserData.currentVariableDebt = expectedUserData.scaledVariableDebt.rayMul(
expectedDataAfterAction.variableBorrowIndex
);
expectedUserData.stableRateLastUpdated = new BigNumber(0); expectedUserData.stableRateLastUpdated = new BigNumber(0);
} else { } else {
expectedUserData.principalStableDebt = expectedUserData.currentStableDebt = userDataBeforeAction.currentStableDebt.plus( expectedUserData.principalStableDebt = expectedUserData.currentStableDebt = userDataBeforeAction.currentStableDebt.plus(
variableBorrowBalance variableDebtBalance
); );
//weighted average of the previous and the current //weighted average of the previous and the current
expectedUserData.stableBorrowRate = calcExpectedUserStableRate( expectedUserData.stableBorrowRate = calcExpectedUserStableRate(
userDataBeforeAction.principalStableDebt, stableDebtBalance,
userDataBeforeAction.stableBorrowRate, userDataBeforeAction.stableBorrowRate,
variableBorrowBalance, variableDebtBalance,
reserveDataBeforeAction.stableBorrowRate reserveDataBeforeAction.stableBorrowRate
); );
expectedUserData.stableRateLastUpdated = txTimestamp; expectedUserData.stableRateLastUpdated = txTimestamp;
expectedUserData.currentVariableDebt = expectedUserData.principalVariableDebt = new BigNumber( expectedUserData.currentVariableDebt = expectedUserData.scaledVariableDebt = new BigNumber(0);
0
);
expectedUserData.variableBorrowIndex = new BigNumber(0);
} }
expectedUserData.liquidityRate = expectedDataAfterAction.liquidityRate; expectedUserData.liquidityRate = expectedDataAfterAction.liquidityRate;

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[] = ['borrow-repay-stable.json','borrow-repay-variable.json']; const selectedScenarios: string[] = ['swap-rate-mode.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;