Chore/migrate to hardhat#133
Conversation
The latest `solidity-utils` uses package exports for the contracts, which is necessary with Hardhat 3 as it complies with npm resolution rules.
Pull in Hardhat 3 as a dev dependency. Add type `module` to the package.json so that the project is treated as ESM.
Hardhat 3 resolution honours npm transient dependency resolution, hence to ensure the same version of OpenZeppelin Contracts is used throughout contract compilation we add an explicit resolution.
Based on `foundry.toml` create an equivalent `hardhat.config.ts` file. The identical `[profile.default]` and `[profile.ci]` have been collapsed down into the single compilers setting. The `solx` profile has been left out as experimental rather than required for day to day development. Other Foundry-only settings that have been left out: 1. `[fmt]` - formatting will be replaced by prettier in a subsequent commit. 2. libs - npm dependencies will be used for dependency resolution in a subsequent commit.
Hardhat 3 Solidity import resolution can access npm modules directly, hence does not require remappings for forge/openzeppelin/solidity-utils. However, one remapping has been added `src/=src` to allow absolute imports. This is to avoid changing the imports in the Solidity files. It would be a small change to remove this remapping and update the Solidity imports with a small number of relative imports.
Update the package.json to change the test script to run Hardhat. There are no TS/JS tests so invoke Solidity test directly.
Hardhat supports recording a gas snapshot to `./.gas-snapshot`. It uses a slightly different format to Foundry, so the snapshot file has been updated. The `snapshot` script in the `package.json` has been updated to allow writing or updating the snapshot file by running: ```shell npm run snapshot ```
The core CI checks have been ported to using the Hardhat variants, so tests and the gas check. This required installing Node 24 to support running Hardhat. The snapshot check has been encoded as a script in the package.json for local running. The target branch was updated to `main`.
To replace `forge fmt` we add prettier with `prettier-plugin-solidity`. The configuration includes rules for ts, json and solidity. The Solidity file rules are aimed to reduce the number of formatting changes. Format and lint scripts have been added to the `package.json`, and swapped into the makefile.
Based on the config a run of `yarn run format` has updated the formatting.
Ignore solc warnings for code size when coming from tests and for transient storage from the 1inch/solidity-utils npm package.
This is a sketch of an Ignition based deployment for the AquaRouter, retaining the owner lookup from config via a wrapping script, but gaining the advantage of Ignition handling the deployment. Instead of passing in env variables, the `hardhat-keystore` plugin has been included.
| }, | ||
| "dependencies": { | ||
| "forge-std": "github:foundry-rs/forge-std#v1.11.0", | ||
| "@1inch/solidity-utils": "6.9.7", |
There was a problem hiding this comment.
Solidity-utils 6.9.9 exposes its contracts via package.json exports, which Hardhat 3 needs.
| "@openzeppelin/contracts": "5.4.0" | ||
| }, | ||
| "devDependencies": { | ||
| "hardhat": "^3.7.0" |
There was a problem hiding this comment.
This is using a range, but that may not be appropriate for this repo.
| default: { compilers: [aquaCompilerSettings] }, | ||
| production: { compilers: [aquaCompilerSettings] }, |
There was a problem hiding this comment.
The default profile is used for day to day development. production will be used by default during a deployment.
| push: | ||
| branches: | ||
| - master | ||
| - main |
There was a problem hiding this comment.
I think this is the right update if I have read the repo default branch.
|
|
||
| - name: Install deps | ||
| run: forge install | ||
| node-version: 24 |
| }, | ||
| warnings: { | ||
| "test/**/*": { | ||
| "code-size": "off", |
There was a problem hiding this comment.
Turn off code size warnings from Test contracts.
| "code-size": "off", | ||
| }, | ||
| "npm/@1inch/**/*": { | ||
| "transient-storage": "off", |
There was a problem hiding this comment.
Ignore a transient storage warning from the @1inch/solidity-utils package.
| "1": "0x0000000000000000000000000000000000000000" | ||
| } | ||
| "owner": { | ||
| "31337": "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266" |
There was a problem hiding this comment.
This is the first of the default Hardhat accounts, and so available on the dev node 31337.
Migrate
aquato Hardhat 3.Building
The build settings have been ported from
foundry.tomlinto a newhardhat.config.ts, and can be run with:Hardhat 3 is able to resolve imports from node modules without further config. A single
src/=src/remapping has been kept to support absolute imports. We would recommend updating the tests to relative imports, and removing theremappings.txtfile.The
hardhat-ignore-warningsplugin has been added to exclude warnings about test contract sizes.Testing
The Solidity Test suite (50 tests) is passing
yarn testCoverage
A coverage report (including an html report) can be generated with:
Gas Report
The gas snapshot file
.gas-snapshothas been updated to Hardhat's format.To update the snapshot:
To check against the snapshot:
Formatting
Formatting is now managed with
prettierandprettier-plugin-solidity. The settings were chosen to minimize code level changes. The Solidity files have had the new formatting applied.To format all files:
To check the formatting:
Deployment
A sketch of a deployment setup using Hardhat Ignition has been added. The
DEPLOY.mdhas been partially updated to reflect this.The owner continues to be set based on the config. The owner for the development network (31337) has been set to the first built-in Hardhat account.
To run against an in-memory Hardhat network:
To run against a local development node:
yarn hardhat node # In another tab yarn hardhat run ./script/deployAquaRouter.ts --network localhostReview
Note
Reviewing this PR a commit at a time is the easiest approach