Below is a sample migration file (1_simple.migration.ts) written in ESM, which is the default for Hardhat 3 projects:
import type { Deployer } from "@solarity/hardhat-migrate/dist/src/internal/deployer/Deployer.js";
import { PublicReporter as Reporter } from "@solarity/hardhat-migrate/dist/src/internal/tools/reporters/PublicReporter.js";
import { ERC20Mock__factory } from "../typechain-types";
import { ethers } from "ethers";
const TOKEN_OWNER = "0x1E3953B6ee74461169A3E346060AE27bD0B5bF2B";
export default async function (deployer: Deployer) {
const token = await deployer.deploy(ERC20Mock__factory, ["Example Token", "ET", 18]);
await (
await token.mint(TOKEN_OWNER, ethers.parseEther("1000"), {
customData: { txName: "Mint allocation" },
})
).wait();
await Reporter.reportContractsMD(["Example Token", await token.getAddress()]);
}This example illustrates the basic principles of how migrations operate:
- The core component is the
Deployerobject, which acts as a wrapper for the @ethers library, facilitating the deployment and processing of contracts. - The
Reporterclass, a static entity, logs intermediary information into the console. - It is required to import contract factories.
- All relevant constants can be defined if necessary.
- The migration file's main body grants access to the deployer object, allowing for contract deployment and supporting recovery from failures in previous migration runs.
- Standard transaction-sending processes are used without special wrappers.
- The migration concludes with the
PublicReporterhelper summarizing the migration details.