Skip to content

Commit

Permalink
test(hardhat): adapt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Oct 31, 2023
1 parent f743546 commit dd47e4c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
6 changes: 5 additions & 1 deletion src/MetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph

if (supplyCap == 0) revert ErrorsLib.UnauthorizedMarket(id);

if (allocation.assets == type(uint256).max) allocation.assets = totalWithdrawn.zeroFloorSub(totalSupplied);
if (allocation.assets == type(uint256).max) {
allocation.assets = totalWithdrawn.zeroFloorSub(totalSupplied);

if (allocation.assets == 0) continue;
}

(uint256 suppliedAssets,) =
MORPHO.supply(allocation.marketParams, allocation.assets, allocation.shares, address(this), hex"");
Expand Down
69 changes: 50 additions & 19 deletions test/hardhat/MetaMorpho.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AbiCoder, MaxUint256, ZeroHash, keccak256, toBigInt } from "ethers";
import { AbiCoder, MaxUint256, ZeroAddress, ZeroHash, keccak256, toBigInt } from "ethers";
import hre from "hardhat";
import _range from "lodash/range";
import { ERC20Mock, OracleMock, MetaMorpho, IMorpho, MetaMorphoFactory, MetaMorpho__factory, IrmMock } from "types";
import { MarketParamsStruct } from "types/@morpho-blue/interfaces/IMorpho";
import { MarketAllocationStruct } from "types/src/MetaMorpho";

import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import { mine } from "@nomicfoundation/hardhat-network-helpers";
Expand Down Expand Up @@ -79,6 +80,7 @@ describe("MetaMorpho", () => {

let supplyCap: bigint;
let allMarketParams: MarketParamsStruct[];
let idleParams: MarketParamsStruct;

const expectedMarket = async (marketParams: MarketParamsStruct) => {
const id = identifier(marketParams);
Expand Down Expand Up @@ -158,6 +160,14 @@ describe("MetaMorpho", () => {
const oracleAddress = await oracle.getAddress();
const irmAddress = await irm.getAddress();

idleParams = {
loanToken: loanAddress,
collateralToken: ZeroAddress,
oracle: ZeroAddress,
irm: ZeroAddress,
lltv: 0n,
};

allMarketParams = _range(1, 1 + nbMarkets).map((i) => ({
loanToken: loanAddress,
collateralToken: collateralAddress,
Expand All @@ -173,6 +183,10 @@ describe("MetaMorpho", () => {
await morpho.createMarket(marketParams);
}

await morpho.enableIrm(idleParams.irm);
await morpho.enableLltv(idleParams.lltv);
await morpho.createMarket(idleParams);

const MetaMorphoFactoryFactory = await hre.ethers.getContractFactory("MetaMorphoFactory", admin);

factory = await MetaMorphoFactoryFactory.deploy(morphoAddress);
Expand Down Expand Up @@ -211,14 +225,24 @@ describe("MetaMorpho", () => {
await metaMorpho.connect(curator).submitCap(marketParams, supplyCap);
}

await metaMorpho.connect(curator).submitCap(idleParams, 2n ** 192n - 1n);

await forwardTimestamp(timelock);

await metaMorpho.connect(admin).acceptCap(identifier(idleParams));

for (const marketParams of allMarketParams) {
await metaMorpho.connect(admin).acceptCap(identifier(marketParams));
}

await metaMorpho.connect(curator).setSupplyQueue(allMarketParams.map(identifier));
await metaMorpho.connect(curator).sortWithdrawQueue(allMarketParams.map((_, i) => nbMarkets - 1 - i));
await metaMorpho.connect(curator).setSupplyQueue(
// Set idle market last.
allMarketParams.map(identifier).concat([identifier(idleParams)]),
);
await metaMorpho.connect(curator).sortWithdrawQueue(
// Keep idle market first.
[0].concat(allMarketParams.map((_, i) => nbMarkets - i)),
);

hre.tracer.nameTags[morphoAddress] = "Morpho";
hre.tracer.nameTags[collateralAddress] = "Collateral";
Expand All @@ -229,7 +253,6 @@ describe("MetaMorpho", () => {
});

it("should simulate gas cost [main]", async () => {
let totalAssets: bigint = toBigInt(0);
for (let i = 0; i < suppliers.length; ++i) {
logProgress("main", i, suppliers.length);

Expand All @@ -238,10 +261,6 @@ describe("MetaMorpho", () => {

await randomForwardTimestamp();

if (totalAssets + assets > supplyCap) {
break;
}

await metaMorpho.connect(supplier).deposit(assets, supplier.address);

await randomForwardTimestamp();
Expand All @@ -266,14 +285,24 @@ describe("MetaMorpho", () => {
}),
);

const withdrawn = allocation
.map(({ marketParams, liquidShares }) => ({
const idlePosition = await morpho.position(identifier(idleParams), metaMorphoAddress);

const withdrawn: MarketAllocationStruct[] = [];

// Always withdraw half from idle.
if (idlePosition.supplyShares > 1n)
withdrawn.push({ marketParams: idleParams, assets: 0n, shares: idlePosition.supplyShares / 2n });

for (const { marketParams, liquidShares } of allocation) {
if (liquidShares === 0n) continue;

withdrawn.push({
marketParams,
assets: 0n,
// Always withdraw all, up to the liquidity.
shares: liquidShares,
}))
.filter(({ shares }) => shares > 0n);
});
}

const withdrawnAssets = allocation.reduce(
(total, { market, liquidShares }) =>
Expand All @@ -284,16 +313,20 @@ describe("MetaMorpho", () => {
// Always consider 90% of withdrawn assets because rates go brrrr.
const marketAssets = (withdrawnAssets * 9n) / 10n / toBigInt(nbMarkets);

const supplied = allocation
.map(({ marketParams }) => ({
const supplied: MarketAllocationStruct[] = [];
for (const { marketParams } of allocation) {
supplied.push({
marketParams,
// Always supply evenly on each market 90% of what the vault withdrawn in total.
assets: marketAssets,
shares: 0n,
}))
.filter(({ assets }) => assets > 0n);
});
}

// await metaMorpho.connect(allocator).reallocate(withdrawn, supplied);
// Always supply remaining to idle.
supplied.push({ marketParams: idleParams, assets: MaxUint256, shares: 0n });

await metaMorpho.connect(allocator).reallocate(withdrawn, supplied);

// Borrow liquidity to generate interest.

Expand All @@ -316,8 +349,6 @@ describe("MetaMorpho", () => {
await mine(); // Include supplyCollateral + borrow in a single block.
}

totalAssets += assets / 2n;

await hre.network.provider.send("evm_setAutomine", [true]);
}
});
Expand Down

0 comments on commit dd47e4c

Please sign in to comment.