Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve hardhat gas tests #103

Merged
merged 6 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/hardhat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ concurrency:

jobs:
build-contracts:
name: Compilation
name: Gas tests
runs-on: ubuntu-latest

steps:
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,35 @@
"homepage": "https://github.com/morpho-labs/morpho-stack#readme",
"dependencies": {
"ethers": "^6.7.1",
"ethers-maths": "^4.0.2",
"ethers-maths": "^4.1.0",
"lodash": "^4.17.21"
},
"devDependencies": {
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@nomicfoundation/hardhat-chai-matchers": "^2.0.2",
"@nomicfoundation/hardhat-ethers": "^3.0.4",
"@nomicfoundation/hardhat-foundry": "^1.0.3",
"@nomicfoundation/hardhat-network-helpers": "^1.0.8",
"@nomicfoundation/hardhat-foundry": "^1.1.1",
"@nomicfoundation/hardhat-network-helpers": "^1.0.9",
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
"@typechain/ethers-v6": "^0.5.0",
"@typechain/hardhat": "^9.0.0",
"@types/chai": "^4.3.5",
"@types/lodash": "^4.14.197",
"@types/chai": "^4.3.6",
"@types/lodash": "^4.14.198",
"@types/mocha": "^10.0.1",
"@types/node": "^20.5.4",
"chai": "^4.3.7",
"@types/node": "^20.6.3",
"chai": "^4.3.8",
"dotenv": "^16.3.1",
"hardhat": "^2.17.1",
"hardhat": "^2.17.3",
"hardhat-gas-reporter": "^1.0.9",
"hardhat-tracer": "^2.6.0",
"husky": "^8.0.3",
"lint-staged": "^14.0.1",
"prettier": "^3.0.2",
"prettier": "^3.0.3",
"solidity-coverage": "^0.8.4",
"ts-node": "^10.9.1",
"typechain": "^8.3.1",
"typescript": "^5.1.6"
"typescript": "^5.2.2"
},
"lint-staged": {
"*.sol": "forge fmt",
Expand All @@ -94,4 +94,4 @@
],
"importOrderSeparation": true
}
}
}
5 changes: 2 additions & 3 deletions src/MetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
uint96 public fee;
address public feeRecipient;

uint96 public timelock;
uint256 public timelock;
address public guardian;

address public rewardsDistributor;
Expand Down Expand Up @@ -480,8 +480,7 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
}

function _setTimelock(uint256 newTimelock) internal {
// Safe "unchecked" cast because newTimelock <= MAX_TIMELOCK.
timelock = uint96(newTimelock);
timelock = newTimelock;

emit EventsLib.SetTimelock(newTimelock);

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IMetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface IMetaMorpho is IERC4626 {
function fee() external view returns (uint96);
function feeRecipient() external view returns (address);
function rewardsDistributor() external view returns (address);
function timelock() external view returns (uint96);
function timelock() external view returns (uint256);
function supplyQueue(uint256) external view returns (Id);
function withdrawQueue(uint256) external view returns (Id);
function config(Id) external view returns (uint192 cap, uint64 withdrawRank);
Expand Down
31 changes: 25 additions & 6 deletions test/hardhat/MetaMorpho.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe("MetaMorpho", () => {
for (const marketParams of allMarketParams) {
await metaMorpho
.connect(riskManager)
.submitCap(marketParams, (BigInt.WAD * 100n * toBigInt(suppliers.length)) / toBigInt(allMarketParams.length));
.submitCap(marketParams, (BigInt.WAD * 10n * toBigInt(suppliers.length)) / toBigInt(allMarketParams.length));
}

await metaMorpho.connect(riskManager).setSupplyQueue(allMarketParams.map(identifier));
Expand Down Expand Up @@ -173,9 +173,28 @@ describe("MetaMorpho", () => {

await randomForwardTimestamp();

const allocation = await Promise.all(
allMarketParams.map(async (marketParams) => {
const id = identifier(marketParams);
const market = await morpho.market(id);
const position = await morpho.position(id, await metaMorpho.getAddress());

const assets = position.supplyShares
.mulDivDown(market.totalSupplyAssets + 1n, market.totalSupplyShares + 10n ** 6n)
.min(market.totalSupplyAssets - market.totalBorrowAssets);

return {
marketParams,
assets,
};
}),
);

await metaMorpho.connect(allocator).reallocate(
[{ marketParams: allMarketParams[0], assets: assets / 2n }],
allMarketParams.map((marketParams) => ({ marketParams, assets: assets / toBigInt(nbMarkets + 1) / 2n })),
allocation.filter(({ assets }) => assets > 0n),
allocation
.map(({ marketParams, assets }) => ({ marketParams, assets: (assets * 3n) / 4n }))
.filter(({ assets }) => assets > 0n),
);

const borrower = borrowers[i];
Expand All @@ -184,15 +203,15 @@ describe("MetaMorpho", () => {
const market = await morpho.market(identifier(marketParams));
const liquidity = market.totalSupplyAssets - market.totalBorrowAssets;

assets = liquidity / 2n;
if (liquidity < 2n) break;

await randomForwardTimestamp();

await morpho.connect(borrower).supplyCollateral(marketParams, assets, borrower.address, "0x");
await morpho.connect(borrower).supplyCollateral(marketParams, liquidity, borrower.address, "0x");

await randomForwardTimestamp();

await morpho.connect(borrower).borrow(marketParams, assets / 3n, 0, borrower.address, borrower.address);
await morpho.connect(borrower).borrow(marketParams, liquidity / 2n, 0, borrower.address, borrower.address);
}
}
});
Expand Down
Loading
Loading