Skip to content

Commit

Permalink
Merge pull request #85 from morpho-org/fix/BC-computation
Browse files Browse the repository at this point in the history
fix: Borrow capacity computation
  • Loading branch information
oumar-fall authored Aug 15, 2023
2 parents 7cbefc7 + 2c9b223 commit a5584e8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/MorphoAaveV3DataHolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class MorphoAaveV3DataHolder {
);

borrowCapacity = borrowCapacity.add(
PercentMath.percentMul(
this.__MATH__.percentMulDown(
collateralReduced,
marketConfig.borrowableFactor
)
Expand Down
3 changes: 2 additions & 1 deletion src/simulation/MorphoAaveV3Simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/bulker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);

Expand Down
8 changes: 6 additions & 2 deletions tests/helpers/bn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
2 changes: 1 addition & 1 deletion tests/units/computeUserData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
8 changes: 4 additions & 4 deletions tests/units/simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit a5584e8

Please sign in to comment.