-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from morpho-org/test/hardhat-tests
test(hardhat): add hardhat tests
- Loading branch information
Showing
14 changed files
with
6,565 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: Hardhat | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
yarn-test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
|
||
- name: Install Foundry | ||
uses: foundry-rs/foundry-toolchain@v1 | ||
|
||
- name: Install node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18 | ||
cache: yarn | ||
|
||
- name: Install dependencies | ||
run: yarn install --frozen-lockfile | ||
|
||
- name: Save hardhat cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: | | ||
cache_hardhat | ||
artifacts | ||
key: ${{ github.ref_name }}-hardhat | ||
|
||
- name: Run Hardhat tests | ||
run: yarn test:hardhat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,13 @@ docs/ | |
|
||
# Dotenv file | ||
.env | ||
|
||
# Node.js | ||
node_modules/ | ||
|
||
# Hardhat | ||
/types | ||
/cache_hardhat | ||
/artifacts | ||
|
||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
git submodule update --init --recursive | ||
yarn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
git submodule update --init --recursive | ||
yarn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
npx lint-staged |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
yarn commitlint --edit "${1}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as dotenv from "dotenv"; | ||
import "ethers-maths"; | ||
import "hardhat-gas-reporter"; | ||
import "hardhat-tracer"; | ||
import { HardhatUserConfig } from "hardhat/config"; | ||
import "solidity-coverage"; | ||
|
||
import "@nomicfoundation/hardhat-chai-matchers"; | ||
import "@nomicfoundation/hardhat-ethers"; | ||
import "@nomicfoundation/hardhat-foundry"; | ||
import "@nomicfoundation/hardhat-network-helpers"; | ||
import "@typechain/hardhat"; | ||
|
||
|
||
dotenv.config(); | ||
|
||
const config: HardhatUserConfig = { | ||
defaultNetwork: "hardhat", | ||
networks: { | ||
hardhat: { | ||
chainId: 1, | ||
gasPrice: 0, | ||
initialBaseFeePerGas: 0, | ||
allowBlocksWithSameTimestamp: true, | ||
}, | ||
}, | ||
solidity: { | ||
compilers: [ | ||
{ | ||
version: "0.8.19", | ||
settings: { | ||
optimizer: { | ||
enabled: true, | ||
runs: 200, | ||
}, | ||
viaIR: true, | ||
}, | ||
}, | ||
], | ||
}, | ||
mocha: { | ||
timeout: 3000000, | ||
}, | ||
typechain: { | ||
target: "ethers-v6", | ||
outDir: "types/", | ||
externalArtifacts: ["deps/**/*.json"], | ||
}, | ||
tracer: { | ||
defaultVerbosity: 1, | ||
gasCost: true, | ||
}, | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
{ | ||
"name": "morpho-blue-irm", | ||
"description": "Morpho Blue IRM Contracts", | ||
"license": "GPL-2.0-or-later", | ||
"version": "0.0.0", | ||
"main": "lib/index.js", | ||
"bin": "lib/cli.js", | ||
"files": [ | ||
"lib/**/*" | ||
], | ||
"scripts": { | ||
"prepare": "husky install && forge install", | ||
"build:forge": "FOUNDRY_PROFILE=build forge build", | ||
"build:hardhat": "npx hardhat compile", | ||
"test:forge": "FOUNDRY_PROFILE=test forge test", | ||
"test:hardhat": "npx hardhat test", | ||
"lint": "yarn lint:forge && yarn lint:ts", | ||
"lint:ts": "prettier --check test/hardhat", | ||
"lint:forge": "forge fmt --check", | ||
"lint:fix": "yarn lint:forge:fix && yarn lint:ts:fix", | ||
"lint:ts:fix": "prettier --write test/hardhat", | ||
"lint:forge:fix": "forge fmt", | ||
"clean": "npx hardhat clean && forge clean" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/morpho-org/morpho-blue-irm.git" | ||
}, | ||
"author": { | ||
"name": "Morpho Labs", | ||
"email": "security@morpho.org", | ||
"url": "https://github.com/morpho-labs" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/morpho-org/morpho-blue-irm/issues" | ||
}, | ||
"homepage": "https://github.com/morpho-org/morpho-blue-irm#readme", | ||
"dependencies": { | ||
"ethers": "^6.7.1", | ||
"ethers-maths": "^4.0.2", | ||
"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", | ||
"@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/mocha": "^10.0.1", | ||
"@types/node": "^20.5.4", | ||
"chai": "^4.3.7", | ||
"dotenv": "^16.3.1", | ||
"hardhat": "^2.17.1", | ||
"hardhat-gas-reporter": "^1.0.9", | ||
"hardhat-tracer": "^2.6.0", | ||
"husky": "^8.0.3", | ||
"lint-staged": "^14.0.1", | ||
"prettier": "^3.0.2", | ||
"solidity-coverage": "^0.8.4", | ||
"ts-node": "^10.9.1", | ||
"typechain": "^8.3.1", | ||
"typescript": "^5.1.6" | ||
}, | ||
"lint-staged": { | ||
"*.sol": "forge fmt", | ||
"*.js": "prettier", | ||
"*.ts": "prettier", | ||
"*.json": "prettier", | ||
"*.yml": "prettier" | ||
}, | ||
"commitlint": { | ||
"extends": [ | ||
"@commitlint/config-conventional" | ||
] | ||
}, | ||
"prettier": { | ||
"printWidth": 120, | ||
"plugins": [ | ||
"@trivago/prettier-plugin-sort-imports" | ||
], | ||
"importOrder": [ | ||
"^@", | ||
"^\\.\\.", | ||
"^\\." | ||
], | ||
"importOrderSeparation": true | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
test/AdaptiveCurveIrmTest.sol → test/forge/AdaptiveCurveIrmTest.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { AbiCoder, keccak256, toBigInt } from "ethers"; | ||
import hre from "hardhat"; | ||
import _range from "lodash/range"; | ||
import { AdaptiveCurveIrm } from "types"; | ||
import { MarketParamsStruct } from "types/lib/morpho-blue/src/interfaces/IIrm"; | ||
|
||
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"; | ||
import { setNextBlockTimestamp } from "@nomicfoundation/hardhat-network-helpers/dist/src/helpers/time"; | ||
|
||
let seed = 42; | ||
const random = () => { | ||
seed = (seed * 16807) % 2147483647; | ||
|
||
return (seed - 1) / 2147483646; | ||
}; | ||
|
||
const identifier = (marketParams: MarketParamsStruct) => { | ||
const encodedMarket = AbiCoder.defaultAbiCoder().encode( | ||
["address", "address", "address", "address", "uint256"], | ||
Object.values(marketParams), | ||
); | ||
|
||
return Buffer.from(keccak256(encodedMarket).slice(2), "hex"); | ||
}; | ||
|
||
const logProgress = (name: string, i: number, max: number) => { | ||
if (i % 10 == 0) console.log("[" + name + "]", Math.floor((100 * i) / max), "%"); | ||
}; | ||
|
||
const randomForwardTimestamp = async () => { | ||
const block = await hre.ethers.provider.getBlock("latest"); | ||
const elapsed = random() < 1 / 2 ? 0 : (1 + Math.floor(random() * 100)) * 12; // 50% of the time, don't go forward in time. | ||
|
||
const newTimestamp = block!.timestamp + elapsed; | ||
|
||
await setNextBlockTimestamp(block!.timestamp + elapsed); | ||
|
||
return newTimestamp; | ||
}; | ||
|
||
describe("irm", () => { | ||
let admin: SignerWithAddress; | ||
|
||
let irm: AdaptiveCurveIrm; | ||
|
||
let marketParams: MarketParamsStruct; | ||
|
||
beforeEach(async () => { | ||
[admin] = await hre.ethers.getSigners(); | ||
|
||
const AdaptiveCurveIrmFactory = await hre.ethers.getContractFactory("AdaptiveCurveIrm", admin); | ||
|
||
irm = await AdaptiveCurveIrmFactory.deploy( | ||
await admin.getAddress(), | ||
4000000000000000000n, | ||
1585489599188n, | ||
900000000000000000n, | ||
317097919n, | ||
); | ||
|
||
const irmAddress = await irm.getAddress(); | ||
|
||
marketParams = { | ||
// Non-zero address to include calldata gas cost. | ||
collateralToken: irmAddress, | ||
loanToken: irmAddress, | ||
oracle: irmAddress, | ||
irm: irmAddress, | ||
lltv: 0, | ||
}; | ||
|
||
hre.tracer.nameTags[irmAddress] = "IRM"; | ||
}); | ||
|
||
it("should simulate gas cost [main]", async () => { | ||
for (let i = 0; i < 200; ++i) { | ||
logProgress("main", i, 200); | ||
|
||
const lastUpdate = await randomForwardTimestamp(); | ||
|
||
const totalSupplyAssets = BigInt.WAD * toBigInt(1 + Math.floor(random() * 100)); | ||
const totalBorrowAssets = totalSupplyAssets.percentMul(toBigInt(Math.floor(random() * BigInt.PERCENT.toFloat()))); | ||
|
||
await irm.borrowRate(marketParams, { | ||
fee: 0, | ||
lastUpdate, | ||
totalSupplyAssets: totalSupplyAssets, | ||
totalBorrowAssets: totalBorrowAssets, | ||
// Non-zero shares to include calldata gas cost. | ||
totalSupplyShares: 1000000000000n, | ||
totalBorrowShares: 1000000000000n, | ||
}); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2020", | ||
"module": "nodenext", | ||
"moduleResolution": "nodenext", | ||
"outDir": "dist", | ||
"baseUrl": ".", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"resolveJsonModule": true, | ||
"declaration": true | ||
}, | ||
"include": ["types", "test/hardhat"], | ||
"files": ["hardhat.config.ts"] | ||
} |
Oops, something went wrong.