diff --git a/helpers/constants.ts b/helpers/constants.ts index ea2ab8bc..708fe98d 100644 --- a/helpers/constants.ts +++ b/helpers/constants.ts @@ -4,6 +4,8 @@ import BigNumber from 'bignumber.js'; // MATH // ---------------- +export const PERCENTAGE_FACTOR = '10000'; +export const HALF_PERCENTAGE = '5000'; export const WAD = Math.pow(10, 18).toString(); export const HALF_WAD = new BigNumber(WAD).multipliedBy(0.5).toString(); export const RAY = new BigNumber(10).exponentiatedBy(27).toFixed(); diff --git a/test/configurator.spec.ts b/test/configurator.spec.ts index 7d8a64d4..6a5dd66f 100644 --- a/test/configurator.spec.ts +++ b/test/configurator.spec.ts @@ -80,7 +80,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { expect(liquidationThreshold).to.be.equal(8000); expect(liquidationBonus).to.be.equal(10500); expect(stableBorrowRateEnabled).to.be.equal(true); - expect(reserveFactor).to.be.equal(0); + expect(reserveFactor).to.be.equal(1000); }); it('Unfreezes the ETH reserve', async () => { @@ -107,7 +107,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { expect(liquidationThreshold).to.be.equal(8000); expect(liquidationBonus).to.be.equal(10500); expect(stableBorrowRateEnabled).to.be.equal(true); - expect(reserveFactor).to.be.equal(0); + expect(reserveFactor).to.be.equal(1000); }); it('Check the onlyAaveAdmin on freezeReserve ', async () => { @@ -149,7 +149,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { expect(liquidationThreshold).to.be.equal(8000); expect(liquidationBonus).to.be.equal(10500); expect(stableBorrowRateEnabled).to.be.equal(true); - expect(reserveFactor).to.be.equal(0); + expect(reserveFactor).to.be.equal(1000); }); it('Activates the ETH reserve for borrowing', async () => { @@ -177,7 +177,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { expect(liquidationThreshold).to.be.equal(8000); expect(liquidationBonus).to.be.equal(10500); expect(stableBorrowRateEnabled).to.be.equal(true); - expect(reserveFactor).to.be.equal(0); + expect(reserveFactor).to.be.equal(1000); expect(variableBorrowIndex.toString()).to.be.equal(RAY); }); @@ -222,7 +222,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { expect(liquidationThreshold).to.be.equal(0); expect(liquidationBonus).to.be.equal(0); expect(stableBorrowRateEnabled).to.be.equal(true); - expect(reserveFactor).to.be.equal(0); + expect(reserveFactor).to.be.equal(1000); }); it('Activates the ETH reserve as collateral', async () => { @@ -249,7 +249,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { expect(liquidationThreshold).to.be.equal(8000); expect(liquidationBonus).to.be.equal(10500); expect(stableBorrowRateEnabled).to.be.equal(true); - expect(reserveFactor).to.be.equal(0); + expect(reserveFactor).to.be.equal(1000); }); it('Check the onlyAaveAdmin on configureReserveAsCollateral ', async () => { @@ -285,7 +285,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { expect(liquidationThreshold).to.be.equal(8000); expect(liquidationBonus).to.be.equal(10500); expect(stableBorrowRateEnabled).to.be.equal(false); - expect(reserveFactor).to.be.equal(0); + expect(reserveFactor).to.be.equal(1000); }); it('Enables stable borrow rate on the ETH reserve', async () => { @@ -311,7 +311,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { expect(liquidationThreshold).to.be.equal(8000); expect(liquidationBonus).to.be.equal(10500); expect(stableBorrowRateEnabled).to.be.equal(true); - expect(reserveFactor).to.be.equal(0); + expect(reserveFactor).to.be.equal(1000); }); it('Check the onlyAaveAdmin on disableReserveStableRate', async () => { diff --git a/test/helpers/utils/calculations.ts b/test/helpers/utils/calculations.ts index b334a7f1..db923176 100644 --- a/test/helpers/utils/calculations.ts +++ b/test/helpers/utils/calculations.ts @@ -5,10 +5,16 @@ import { MAX_UINT_AMOUNT, OPTIMAL_UTILIZATION_RATE, EXCESS_UTILIZATION_RATE, + PERCENTAGE_FACTOR, } from '../../../helpers/constants'; -import {IReserveParams, iAavePoolAssets, RateMode, tEthereumAddress} from '../../../helpers/types'; +import { + IReserveParams, + iAavePoolAssets, + RateMode, + tEthereumAddress, +} from '../../../helpers/types'; import './math'; -import {ReserveData, UserReserveData} from './interfaces'; +import { ReserveData, UserReserveData } from './interfaces'; export const strToBN = (amount: string): BigNumber => new BigNumber(amount); @@ -758,7 +764,7 @@ export const calcExpectedUserDataAfterSetUseAsCollateral = ( userDataBeforeAction: UserReserveData, txCost: BigNumber ): UserReserveData => { - const expectedUserData = {...userDataBeforeAction}; + const expectedUserData = { ...userDataBeforeAction }; expectedUserData.usageAsCollateralEnabled = useAsCollateral; @@ -885,7 +891,7 @@ export const calcExpectedUserDataAfterSwapRateMode = ( txCost: BigNumber, txTimestamp: BigNumber ): UserReserveData => { - const expectedUserData = {...userDataBeforeAction}; + const expectedUserData = { ...userDataBeforeAction }; const stableDebtBalance = calcExpectedStableDebtTokenBalance( userDataBeforeAction.principalStableDebt, @@ -1035,7 +1041,7 @@ export const calcExpectedUserDataAfterStableRateRebalance = ( txCost: BigNumber, txTimestamp: BigNumber ): UserReserveData => { - const expectedUserData = {...userDataBeforeAction}; + const expectedUserData = { ...userDataBeforeAction }; expectedUserData.principalVariableDebt = calcExpectedVariableDebtTokenBalance( reserveDataBeforeAction, @@ -1084,7 +1090,7 @@ export const calcExpectedATokenBalance = ( ) => { const index = calcExpectedReserveNormalizedIncome(reserveData, currentTimestamp); - const {scaledATokenBalance: scaledBalanceBeforeAction} = userData; + const { scaledATokenBalance: scaledBalanceBeforeAction } = userData; return scaledBalanceBeforeAction.rayMul(index); }; @@ -1119,7 +1125,7 @@ export const calcExpectedVariableDebtTokenBalance = ( currentTimestamp ); - const {scaledVariableDebt} = userData; + const { scaledVariableDebt } = userData; return scaledVariableDebt.rayMul(normalizedDebt); }; @@ -1202,7 +1208,7 @@ export const calcExpectedInterestRates = ( totalVariableDebt: BigNumber, averageStableBorrowRate: BigNumber ): BigNumber[] => { - const {reservesParams} = configuration; + const { reservesParams } = configuration; const reserveIndex = Object.keys(reservesParams).findIndex((value) => value === reserveSymbol); const [, reserveConfiguration] = (Object.entries(reservesParams) as [string, IReserveParams][])[ @@ -1248,7 +1254,9 @@ export const calcExpectedInterestRates = ( variableBorrowRate, averageStableBorrowRate ); - const liquidityRate = expectedOverallRate.rayMul(utilizationRate); + const liquidityRate = expectedOverallRate + .rayMul(utilizationRate) + .percentMul(new BigNumber(PERCENTAGE_FACTOR).minus(reserveConfiguration.reserveFactor)); return [liquidityRate, stableBorrowRate, variableBorrowRate]; }; @@ -1292,7 +1300,7 @@ const calcExpectedReserveNormalizedIncome = ( reserveData: ReserveData, currentTimestamp: BigNumber ) => { - const {liquidityRate, liquidityIndex, lastUpdateTimestamp} = reserveData; + const { liquidityRate, liquidityIndex, lastUpdateTimestamp } = reserveData; //if utilization rate is 0, nothing to compound if (liquidityRate.eq('0')) { diff --git a/test/helpers/utils/math.ts b/test/helpers/utils/math.ts index df6165d2..66be629c 100644 --- a/test/helpers/utils/math.ts +++ b/test/helpers/utils/math.ts @@ -1,5 +1,5 @@ import BigNumber from 'bignumber.js'; -import {RAY, WAD, HALF_RAY, HALF_WAD, WAD_RAY_RATIO} from '../../../helpers/constants'; +import {RAY, WAD, HALF_RAY, HALF_WAD, WAD_RAY_RATIO, HALF_PERCENTAGE, PERCENTAGE_FACTOR} from '../../../helpers/constants'; declare module 'bignumber.js' { interface BigNumber { @@ -7,10 +7,13 @@ declare module 'bignumber.js' { wad: () => BigNumber; halfRay: () => BigNumber; halfWad: () => BigNumber; + halfPercentage: () => BigNumber; wadMul: (a: BigNumber) => BigNumber; wadDiv: (a: BigNumber) => BigNumber; rayMul: (a: BigNumber) => BigNumber; rayDiv: (a: BigNumber) => BigNumber; + percentMul: (a: BigNumber) => BigNumber; + percentDiv: (a: BigNumber) => BigNumber; rayToWad: () => BigNumber; wadToRay: () => BigNumber; } @@ -64,3 +67,20 @@ BigNumber.prototype.rayToWad = function (): BigNumber { BigNumber.prototype.wadToRay = function (): BigNumber { return this.multipliedBy(WAD_RAY_RATIO).decimalPlaces(0, BigNumber.ROUND_DOWN); }; + + + + +BigNumber.prototype.halfPercentage = (): BigNumber => { + return new BigNumber(HALF_PERCENTAGE).decimalPlaces(0, BigNumber.ROUND_DOWN); +}; + +BigNumber.prototype.percentMul = function (b: BigNumber): BigNumber { + return this.halfPercentage().plus(this.multipliedBy(b)).div(PERCENTAGE_FACTOR).decimalPlaces(0, BigNumber.ROUND_DOWN); +}; + +BigNumber.prototype.percentDiv = function (a: BigNumber): BigNumber { + const halfA = a.div(2).decimalPlaces(0, BigNumber.ROUND_DOWN); + + return halfA.plus(this.multipliedBy(PERCENTAGE_FACTOR)).div(a).decimalPlaces(0, BigNumber.ROUND_DOWN); +}; \ No newline at end of file