Fixed borrow tests

This commit is contained in:
The3D 2020-09-21 12:29:33 +02:00
parent f188a21221
commit c8b044aecf
4 changed files with 44 additions and 43 deletions

View File

@ -32,6 +32,7 @@ import {waitForTx} from '../__setup.spec';
import {ContractReceipt} from 'ethers';
import {AToken} from '../../types/AToken';
import {RateMode, tEthereumAddress} from '../../helpers/types';
import { time } from 'console';
const {expect} = chai;
@ -262,10 +263,6 @@ export const withdraw = async (
txCost
);
const actualAmountWithdrawn = userDataBefore.currentATokenBalance.minus(
expectedUserData.currentATokenBalance
);
expectEqual(reserveDataAfter, expectedReserveData);
expectEqual(userDataAfter, expectedUserData);
@ -375,16 +372,6 @@ export const borrow = async (
txCost
);
console.log("total stable debt actual: ", reserveDataAfter.totalStableDebt.toFixed());
console.log("total stable debt expected: ", expectedReserveData.totalStableDebt.toFixed());
console.log("total avg stable rate actual: ", reserveDataAfter.averageStableBorrowRate.toFixed());
console.log("total avg stable rate expected: ", expectedReserveData.averageStableBorrowRate.toFixed());
console.log("total variable debt actual: ", reserveDataAfter.totalVariableDebt.toFixed());
console.log("total variable debt expected: ", expectedReserveData.totalVariableDebt.toFixed());
console.log("variable borrow rate actual: ", reserveDataAfter.variableBorrowRate.toFixed());
console.log("variable borrow rate expected: ", expectedReserveData.variableBorrowRate.toFixed());
expectEqual(reserveDataAfter, expectedReserveData);
expectEqual(userDataAfter, expectedUserData);
@ -489,14 +476,6 @@ export const repay = async (
txCost
);
console.log("total stable debt actual: ", reserveDataAfter.totalStableDebt.toFixed());
console.log("total stable debt expected: ", expectedReserveData.totalStableDebt.toFixed());
console.log("total variable debt actual: ", reserveDataAfter.totalVariableDebt.toFixed());
console.log("total variable debt expected: ", expectedReserveData.totalVariableDebt.toFixed());
expectEqual(reserveDataAfter, expectedReserveData);
expectEqual(userDataAfter, expectedUserData);

View File

@ -626,6 +626,17 @@
},
"expected": "success"
},
{
"name": "repay",
"args": {
"reserve": "DAI",
"amount": "-1",
"user": "1",
"onBehalfOf": "1",
"borrowRateMode": "variable"
},
"expected": "success"
},
{
"name": "withdraw",
"args": {

View File

@ -97,8 +97,6 @@ export const calcExpectedUserDataAfterWithdraw = (
currentTimestamp: BigNumber,
txCost: BigNumber
): UserReserveData => {
console.log('Checking withdraw');
const expectedUserData = <UserReserveData>{};
const aTokenBalance = calcExpectedATokenBalance(
@ -236,9 +234,6 @@ export const calcExpectedReserveDataAfterWithdraw = (
).toFixed();
}
expectedReserveData.totalLiquidity = new BigNumber(reserveDataBeforeAction.totalLiquidity).minus(
amountWithdrawn
);
expectedReserveData.availableLiquidity = new BigNumber(
reserveDataBeforeAction.availableLiquidity
).minus(amountWithdrawn);
@ -261,13 +256,17 @@ export const calcExpectedReserveDataAfterWithdraw = (
reserveDataBeforeAction.lastUpdateTimestamp,
txTimestamp
);
expectedReserveData.totalVariableDebt = calcExpectedTotalVariableDebt(
reserveDataBeforeAction,
expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul(
expectedReserveData.variableBorrowIndex
);
expectedReserveData.averageStableBorrowRate = reserveDataBeforeAction.averageStableBorrowRate;
expectedReserveData.totalLiquidity = new BigNumber(reserveDataBeforeAction.availableLiquidity)
.minus(amountWithdrawn)
.plus(expectedReserveData.totalVariableDebt)
.plus(expectedReserveData.totalStableDebt);
expectedReserveData.utilizationRate = calcExpectedUtilizationRate(
expectedReserveData.totalStableDebt,
expectedReserveData.totalVariableDebt,
@ -319,12 +318,10 @@ export const calcExpectedReserveDataAfterBorrow = (
expectedReserveData.lastUpdateTimestamp = txTimestamp;
if (borrowRateMode == RateMode.Stable) {
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt;
const expectedVariableDebtAfterTx = calcExpectedTotalVariableDebt(
reserveDataBeforeAction,
txTimestamp
const expectedVariableDebtAfterTx = expectedReserveData.scaledVariableDebt.rayMul(
expectedReserveData.variableBorrowIndex
);
const expectedStableDebtUntilTx = calcExpectedTotalStableDebt(
@ -394,28 +391,38 @@ export const calcExpectedReserveDataAfterBorrow = (
expectedReserveData.totalLiquidity
);
} else {
expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt;
expectedReserveData.totalStableDebt = calcExpectedStableDebtTokenBalance(
userDataBeforeAction.principalStableDebt,
userDataBeforeAction.stableBorrowRate,
userDataBeforeAction.stableRateLastUpdated,
const totalStableDebtAfterTx = calcExpectedStableDebtTokenBalance(
reserveDataBeforeAction.principalStableDebt,
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.lastUpdateTimestamp,
txTimestamp
);
expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(
reserveDataBeforeAction.principalStableDebt,
reserveDataBeforeAction.averageStableBorrowRate,
reserveDataBeforeAction.lastUpdateTimestamp,
currentTimestamp
);
expectedReserveData.averageStableBorrowRate = reserveDataBeforeAction.averageStableBorrowRate;
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.plus(
amountBorrowedBN.rayDiv(expectedReserveData.variableBorrowIndex)
);
const totalVariableDebtAfterTx = expectedReserveData.scaledVariableDebt.rayMul(
expectedReserveData.variableBorrowIndex
);
const utilizationRateAfterTx = calcExpectedUtilizationRate(
expectedReserveData.totalStableDebt,
totalStableDebtAfterTx,
totalVariableDebtAfterTx,
expectedReserveData.availableLiquidity
.plus(expectedReserveData.totalStableDebt)
.plus(totalStableDebtAfterTx)
.plus(totalVariableDebtAfterTx)
);
@ -423,7 +430,7 @@ export const calcExpectedReserveDataAfterBorrow = (
reserveDataBeforeAction.symbol,
reserveDataBeforeAction.marketStableRate,
utilizationRateAfterTx,
expectedReserveData.totalStableDebt,
totalStableDebtAfterTx,
totalVariableDebtAfterTx,
expectedReserveData.averageStableBorrowRate
);
@ -513,6 +520,7 @@ export const calcExpectedReserveDataAfterRepay = (
expectedReserveData.principalStableDebt = expectedReserveData.totalStableDebt = expectedDebt.minus(
amountRepaidBN
);
//due to accumulation errors, the total stable debt might be smaller than the last user debt.
//in this case we simply set the total supply and avg stable rate to 0.
if (expectedReserveData.principalStableDebt.lt(0)) {
@ -530,7 +538,10 @@ export const calcExpectedReserveDataAfterRepay = (
}
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt;
expectedReserveData.totalVariableDebt = reserveDataBeforeAction.totalVariableDebt;
expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul(
expectedReserveData.variableBorrowIndex
);
} else {
expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.minus(
amountRepaidBN.rayDiv(expectedReserveData.variableBorrowIndex)
@ -694,7 +705,7 @@ export const calcExpectedUserDataAfterRepay = (
if (rateMode == RateMode.Stable) {
expectedUserData.scaledVariableDebt = userDataBeforeAction.scaledVariableDebt;
expectedUserData.currentVariableDebt = userDataBeforeAction.currentVariableDebt;
expectedUserData.currentVariableDebt = variableDebt;
expectedUserData.principalStableDebt = expectedUserData.currentStableDebt = stableDebt.minus(
totalRepaidBN

View File

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