diff --git a/test/helpers/actions.ts b/test/helpers/actions.ts index 4921e66c..c9bf4bcf 100644 --- a/test/helpers/actions.ts +++ b/test/helpers/actions.ts @@ -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); diff --git a/test/helpers/scenarios/borrow-repay-stable.json b/test/helpers/scenarios/borrow-repay-stable.json index 9d2348e9..87e7de0b 100644 --- a/test/helpers/scenarios/borrow-repay-stable.json +++ b/test/helpers/scenarios/borrow-repay-stable.json @@ -626,6 +626,17 @@ }, "expected": "success" }, + { + "name": "repay", + "args": { + "reserve": "DAI", + "amount": "-1", + "user": "1", + "onBehalfOf": "1", + "borrowRateMode": "variable" + }, + "expected": "success" + }, { "name": "withdraw", "args": { diff --git a/test/helpers/utils/calculations.ts b/test/helpers/utils/calculations.ts index 3cd05b69..671e6205 100644 --- a/test/helpers/utils/calculations.ts +++ b/test/helpers/utils/calculations.ts @@ -97,8 +97,6 @@ export const calcExpectedUserDataAfterWithdraw = ( currentTimestamp: BigNumber, txCost: BigNumber ): UserReserveData => { - console.log('Checking withdraw'); - const expectedUserData = {}; 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 diff --git a/test/scenario.spec.ts b/test/scenario.spec.ts index d9edd4ac..be3f2977 100644 --- a/test/scenario.spec.ts +++ b/test/scenario.spec.ts @@ -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;