From 582973955b33c8b4641dbd27377e73317f3c422f Mon Sep 17 00:00:00 2001 From: Oumar Date: Tue, 15 Aug 2023 15:04:10 +0200 Subject: [PATCH 1/3] fix: Borrow capacity computation --- src/MorphoAaveV3DataHolder.ts | 2 +- src/simulation/MorphoAaveV3Simulator.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/MorphoAaveV3DataHolder.ts b/src/MorphoAaveV3DataHolder.ts index 233ba04..660f866 100644 --- a/src/MorphoAaveV3DataHolder.ts +++ b/src/MorphoAaveV3DataHolder.ts @@ -106,7 +106,7 @@ export class MorphoAaveV3DataHolder { ); borrowCapacity = borrowCapacity.add( - PercentMath.percentMul( + this.__MATH__.percentMulDown( collateralReduced, marketConfig.borrowableFactor ) diff --git a/src/simulation/MorphoAaveV3Simulator.ts b/src/simulation/MorphoAaveV3Simulator.ts index 4480414..fad4f41 100644 --- a/src/simulation/MorphoAaveV3Simulator.ts +++ b/src/simulation/MorphoAaveV3Simulator.ts @@ -796,7 +796,8 @@ export class MorphoAaveV3Simulator extends MorphoAaveV3DataEmitter { operation ); - const totalCollateral = userMarketData.totalCollateral.add(amount); + // Since it's gonna be scaled and unscaled, the value is gonna be reduced by one + const totalCollateral = userMarketData.totalCollateral.add(amount.sub(1)); const newUserMarketData: UserMarketData = { ...userMarketData, From afacad7706ce9e84f6e2676987f79df44bb19508 Mon Sep 17 00:00:00 2001 From: Oumar Date: Tue, 15 Aug 2023 15:19:09 +0200 Subject: [PATCH 2/3] test: update tests --- tests/units/computeUserData.test.ts | 2 +- tests/units/simulator.test.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/units/computeUserData.test.ts b/tests/units/computeUserData.test.ts index 8e05f03..3768a47 100644 --- a/tests/units/computeUserData.test.ts +++ b/tests/units/computeUserData.test.ts @@ -131,7 +131,7 @@ describe("computeUserData", () => { .div(underlyingUnit); expectedBorrowCapacity = expectedBorrowCapacity.add( - PercentMath.percentMul( + __MATHS__.percentMulDown( userCollateralUSD.mul(LT_LOWER_BOUND.sub(1)).div(LT_LOWER_BOUND), // the borrow capacity is reduced by a small amount borrowableFactor ) diff --git a/tests/units/simulator.test.ts b/tests/units/simulator.test.ts index 11fec19..153edd8 100644 --- a/tests/units/simulator.test.ts +++ b/tests/units/simulator.test.ts @@ -189,9 +189,9 @@ describe("Simulator", () => { ]); await sleep(100); - const finalTotalCollateral = initialTotalCollateral.add( - supplyCollateralAmount - ); + const finalTotalCollateral = initialTotalCollateral + .add(supplyCollateralAmount) + .sub(1); expect(totalCollateral).toBnEq(finalTotalCollateral); }); @@ -267,7 +267,7 @@ describe("Simulator", () => { TransactionType.borrow )!.amount; - const expectedBorrowCapacity = BigNumber.from("717673393785148514851485"); + const expectedBorrowCapacity = BigNumber.from("717673393785138613861386"); expect(initialBorrowCapacity).toBnEq(expectedBorrowCapacity); simulator.simulate([ From 2c9b22384e262c1b6e75cdb8afc706a7de076bf5 Mon Sep 17 00:00:00 2001 From: Oumar Date: Tue, 15 Aug 2023 16:06:22 +0200 Subject: [PATCH 3/3] test: update e2e tests --- tests/e2e/bulker.test.ts | 4 ++-- tests/helpers/bn.ts | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/e2e/bulker.test.ts b/tests/e2e/bulker.test.ts index 4631f64..f95fead 100644 --- a/tests/e2e/bulker.test.ts +++ b/tests/e2e/bulker.test.ts @@ -541,8 +541,8 @@ describe("MorphoAaveV3 Bulker", () => { weth.address, morphoUser.address ); - expect(ma3Balance).to.equal( - amountToBorrow, + expect( + approxEqual(ma3Balance, amountToBorrow, 1), `expected ma3 weth balance is ${amountToBorrow}, received ${ma3Balance}` ); diff --git a/tests/helpers/bn.ts b/tests/helpers/bn.ts index 194b81f..17dedc9 100644 --- a/tests/helpers/bn.ts +++ b/tests/helpers/bn.ts @@ -2,8 +2,12 @@ import { BigNumber, BigNumberish } from "ethers"; const APPROX_EQUAL_THRESHOLD = 10; -export const approxEqual = (a: BigNumberish, b: BigNumberish) => { +export const approxEqual = ( + a: BigNumberish, + b: BigNumberish, + delta = APPROX_EQUAL_THRESHOLD +) => { a = BigNumber.from(a); b = BigNumber.from(b); - return a.sub(b).abs().lte(APPROX_EQUAL_THRESHOLD); + return a.sub(b).abs().lte(delta); };