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 {ContractReceipt} from 'ethers';
import {AToken} from '../../types/AToken'; import {AToken} from '../../types/AToken';
import {RateMode, tEthereumAddress} from '../../helpers/types'; import {RateMode, tEthereumAddress} from '../../helpers/types';
import { time } from 'console';
const {expect} = chai; const {expect} = chai;
@ -262,10 +263,6 @@ export const withdraw = async (
txCost txCost
); );
const actualAmountWithdrawn = userDataBefore.currentATokenBalance.minus(
expectedUserData.currentATokenBalance
);
expectEqual(reserveDataAfter, expectedReserveData); expectEqual(reserveDataAfter, expectedReserveData);
expectEqual(userDataAfter, expectedUserData); expectEqual(userDataAfter, expectedUserData);
@ -375,16 +372,6 @@ export const borrow = async (
txCost 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(reserveDataAfter, expectedReserveData);
expectEqual(userDataAfter, expectedUserData); expectEqual(userDataAfter, expectedUserData);
@ -489,14 +476,6 @@ export const repay = async (
txCost 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(reserveDataAfter, expectedReserveData);
expectEqual(userDataAfter, expectedUserData); expectEqual(userDataAfter, expectedUserData);

View File

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

View File

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

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