From 5b38bb144b3025fe6fcc9f25fe23e00e5390db87 Mon Sep 17 00:00:00 2001 From: The3D Date: Thu, 17 Sep 2020 11:52:10 +0200 Subject: [PATCH] Updated borrow calculations on scenarios --- test/helpers/utils/calculations.ts | 115 ++++++++++++++++++----------- test/scenario.spec.ts | 2 +- 2 files changed, 74 insertions(+), 43 deletions(-) diff --git a/test/helpers/utils/calculations.ts b/test/helpers/utils/calculations.ts index fc98ccd4..f8ccc4ce 100644 --- a/test/helpers/utils/calculations.ts +++ b/test/helpers/utils/calculations.ts @@ -185,7 +185,6 @@ export const calcExpectedReserveDataAfterDeposit = ( expectedReserveData.variableBorrowIndex ); - expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt; expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt; @@ -205,7 +204,7 @@ export const calcExpectedReserveDataAfterDeposit = ( expectedReserveData.liquidityRate = rates[0]; expectedReserveData.stableBorrowRate = rates[1]; expectedReserveData.variableBorrowRate = rates[2]; - + return expectedReserveData; }; @@ -246,8 +245,14 @@ export const calcExpectedReserveDataAfterWithdraw = ( txTimestamp ); - expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt(reserveDataBeforeAction, txTimestamp); - expectedReserveData.totalVariableDebt = calcExpectedTotalVariableDebt(reserveDataBeforeAction, expectedReserveData.variableBorrowIndex); + expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt( + reserveDataBeforeAction, + txTimestamp + ); + expectedReserveData.totalVariableDebt = calcExpectedTotalVariableDebt( + reserveDataBeforeAction, + expectedReserveData.variableBorrowIndex + ); expectedReserveData.averageStableBorrowRate = reserveDataBeforeAction.averageStableBorrowRate; @@ -295,60 +300,85 @@ export const calcExpectedReserveDataAfterBorrow = ( ); if (borrowRateMode == RateMode.Stable) { + expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt; + expectedReserveData.totalVariableDebt = reserveDataBeforeAction.scaledVariableDebt.rayMul( + expectedReserveData.variableBorrowIndex + ); - //if the borrow rate mode - - expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued); - - + const totalStableDebtUntilTx = calcExpectedTotalStableDebt( + reserveDataBeforeAction, + txTimestamp + ); expectedReserveData.averageStableBorrowRate = calcExpectedAverageStableBorrowRate( reserveDataBeforeAction.averageStableBorrowRate, - reserveDataBeforeAction.totalStableDebt.plus(debtAccrued), + totalStableDebtUntilTx, amountBorrowedBN, reserveDataBeforeAction.stableBorrowRate ); - expectedReserveData.totalVariableDebt = reserveDataBeforeAction.totalVariableDebt; - } else { - const variableDebtBefore = userDataBeforeAction.scaledVariableDebt.rayMul( - reserveDataBeforeAction.variableBorrowIndex + + expectedReserveData.totalStableDebt = calcExpectedTotalStableDebt( + { + ...reserveDataBeforeAction, + principalStableDebt: totalStableDebtUntilTx.plus(amountBorrowedBN), + averageStableBorrowRate: expectedReserveData.averageStableBorrowRate, + }, + currentTimestamp ); - const debtAccrued = userVariableDebt.minus(variableDebtBefore); - expectedReserveData.totalLiquidity = reserveDataBeforeAction.totalLiquidity.plus(debtAccrued); - expectedReserveData.totalVariableDebt = reserveDataBeforeAction.totalVariableDebt - .plus(amountBorrowedBN) - .plus(debtAccrued); - expectedReserveData.totalStableDebt = reserveDataBeforeAction.totalStableDebt; - expectedReserveData.averageStableBorrowRate = reserveDataBeforeAction.averageStableBorrowRate; + expectedReserveData.totalLiquidity = reserveDataBeforeAction.availableLiquidity + .minus(amountBorrowedBN) + .plus(expectedReserveData.totalVariableDebt) + .plus(expectedReserveData.totalStableDebt); + } else { + expectedReserveData.principalStableDebt = reserveDataBeforeAction.principalStableDebt; + expectedReserveData.totalStableDebt = calcExpectedStableDebtTokenBalance( + userDataBeforeAction, + currentTimestamp + ); + expectedReserveData.scaledVariableDebt = reserveDataBeforeAction.scaledVariableDebt.plus( + amountBorrowedBN.rayDiv(expectedReserveData.variableBorrowIndex) + ); + const totalVariableDebtAfterTx = reserveDataBeforeAction.scaledVariableDebt.rayMul( + expectedReserveData.variableBorrowIndex + ); + + const rates = calcExpectedInterestRates( + reserveDataBeforeAction.symbol, + reserveDataBeforeAction.marketStableRate, + expectedReserveData.utilizationRate, + expectedReserveData.totalStableDebt, + totalVariableDebtAfterTx, + expectedReserveData.averageStableBorrowRate + ); + + expectedReserveData.liquidityRate = rates[0]; + + expectedReserveData.stableBorrowRate = rates[1]; + + expectedReserveData.variableBorrowRate = rates[2]; + + expectedReserveData.lastUpdateTimestamp = txTimestamp; + + expectedReserveData.totalVariableDebt = expectedReserveData.scaledVariableDebt.rayMul( + calcExpectedReserveNormalizedDebt(expectedReserveData, currentTimestamp) + ); } expectedReserveData.availableLiquidity = reserveDataBeforeAction.availableLiquidity.minus( amountBorrowedBN ); + expectedReserveData.totalLiquidity = expectedReserveData.availableLiquidity + .plus(expectedReserveData.totalStableDebt) + .plus(expectedReserveData.totalVariableDebt); + expectedReserveData.utilizationRate = calcExpectedUtilizationRate( expectedReserveData.totalStableDebt, expectedReserveData.totalVariableDebt, expectedReserveData.totalLiquidity ); - const rates = calcExpectedInterestRates( - reserveDataBeforeAction.symbol, - reserveDataBeforeAction.marketStableRate, - expectedReserveData.utilizationRate, - expectedReserveData.totalStableDebt, - expectedReserveData.totalVariableDebt, - expectedReserveData.averageStableBorrowRate - ); - expectedReserveData.liquidityRate = rates[0]; - - expectedReserveData.stableBorrowRate = rates[1]; - - expectedReserveData.variableBorrowRate = rates[2]; - - expectedReserveData.lastUpdateTimestamp = txTimestamp; - return expectedReserveData; }; @@ -1229,7 +1259,6 @@ const calcExpectedVariableBorrowIndex = (reserveData: ReserveData, timestamp: Bi }; const calcExpectedTotalStableDebt = (reserveData: ReserveData, timestamp: BigNumber) => { - const cumulatedInterest = calcCompoundedInterest( reserveData.averageStableBorrowRate, timestamp, @@ -1237,9 +1266,11 @@ const calcExpectedTotalStableDebt = (reserveData: ReserveData, timestamp: BigNum ); return cumulatedInterest.rayMul(reserveData.principalStableDebt); -} - -const calcExpectedTotalVariableDebt = (reserveData: ReserveData, expectedVariableDebtIndex: BigNumber) => { +}; +const calcExpectedTotalVariableDebt = ( + reserveData: ReserveData, + expectedVariableDebtIndex: BigNumber +) => { return reserveData.scaledVariableDebt.rayMul(expectedVariableDebtIndex); -} +}; diff --git a/test/scenario.spec.ts b/test/scenario.spec.ts index 98e0c164..17830d1c 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-variable.json']; +const selectedScenarios: string[] = ['deposit.json']; fs.readdirSync(scenarioFolder).forEach((file) => { if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;