Skip to content

Commit

Permalink
Merge pull request #90 from morpho-org/fix/bulker
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-devatom authored Aug 24, 2023
2 parents 2071e80 + 46ce31e commit 3f4fc04
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/simulation/MorphoAaveV3Simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,8 @@ export class MorphoAaveV3Simulator extends MorphoAaveV3DataEmitter {
const p2pAmount = minBN(amount, marketData.morphoSupplyOnPool);
const poolAmount = amount.sub(p2pAmount);

if (marketData.poolLiquidity.lt(poolAmount))
// throw if total borrow amount exceeds available liquidity
if (marketData.poolLiquidity.lt(amount))
return this._raiseError(index, ErrorCode.notEnoughLiquidity, operation);

const borrowCapacity = data.getUserMaxCapacity(
Expand All @@ -597,9 +598,13 @@ export class MorphoAaveV3Simulator extends MorphoAaveV3DataEmitter {
const morphoSupplyInP2P = marketData.morphoSupplyInP2P.add(p2pAmount); // Matched
const poolBorrow = marketData.poolBorrow.add(poolAmount);
const totalMorphoBorrow = marketData.totalMorphoBorrow.add(amount);
const poolLiquidity = marketData.poolLiquidity.sub(p2pAmount); // Matched
const poolLiquidity = marketData.poolLiquidity.sub(amount); // pool borrow + matching of the supplier

if (marketConfig.borrowCap.gt(0) && poolBorrow.gt(marketConfig.borrowCap)) {
// throw if total borrow amount exceeds borrow cap
if (
marketConfig.borrowCap.gt(0) &&
poolBorrow.add(p2pAmount).gt(marketConfig.borrowCap)
) {
return this._raiseError(index, ErrorCode.borrowCapReached, operation);
}

Expand Down
30 changes: 30 additions & 0 deletions tests/units/simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,36 @@ describe("Simulator", () => {
});
});

it("Should not be able to borrow more than pool liquidity even if partially matched", async () => {
const errors = subscribeErrors();
const marketData = simulator.getMarketsData()[Underlying.weth]!;
expect(marketData.morphoSupplyOnPool).toBnGt(constants.WeiPerEther); // some supply should be matchable for the test to be relevant
const amountToBorrow = marketData.poolLiquidity.add(
marketData.morphoSupplyOnPool.div(2) // normally, "morphoSupplyOnPool" could be matched but the whole amount is exceeding pool liquidity
);
const supplyCollateralCapacity = simulator.getUserMaxCapacity(
Underlying.dai,
TransactionType.supplyCollateral
)!.amount;
simulator.simulate([
{
type: TransactionType.supplyCollateral,
amount: supplyCollateralCapacity,
underlyingAddress: Underlying.dai,
},
{
type: TransactionType.borrow,
amount: amountToBorrow,
underlyingAddress: Underlying.weth,
},
]);
await sleep(100);
// The error is notEnoughLiquidity.
expect(
errors.find((e) => e.errorCode === ErrorCode.notEnoughLiquidity)
).toBeDefined();
});

describe("On Repay", () => {
it("Should increase borrowCapacity", async () => {
const initialBorrowCapacity = simulator.getUserMaxCapacity(
Expand Down

0 comments on commit 3f4fc04

Please sign in to comment.