部署智能合约的简易步骤
发布: (2025年12月19日 GMT+8 19:43)
4 min read
原文: Dev.to
Source: Dev.to
工具与软件
- VS Code / Cursor
- NodeJS (22.10.0 或更高的 LTS 版本)
账户
前置条件
node --version
npx --version
概览
.
├── artifacts
│ ├── artifacts.d.ts
│ ├── build-info
│ │ ├── solc-0_8_28-5d5e77a08f43f8366c520176a292b83d65132357.json
│ │ └── solc-0_8_28-5d5e77a08f43f8366c520176a292b83d65132357.output.json
│ └── contracts
│ └── Counter.sol
│ ├── artifacts.d.ts
│ └── Counter.json
├── cache
│ ├── build-info
│ ├── compile-cache.json
│ └── test-artifacts
│ ├── build-info
│ │ ├── solc-0_8_28-58a47ee13fa4eb204cf4c42c730b3f2707de3d94.json
│ │ └── solc-0_8_28-58a47ee13fa4eb204cf4c42c730b3f2707de3d94.output.json
│ └── contracts
│ └── Counter.t.sol
│ └── CounterTest.json
├── contracts
│ ├── Counter.sol
│ └── Counter.t.sol
├── coverage
│ ├── data
│ │ └── solidity
│ │ └── a23a0f3b-7ab8-4f87-8e19-8279db5ae7a4.json
│ └── lcov.info
├── hardhat.config.ts
├── ignition
│ ├── deployments
│ │ ├── chain-1
│ │ │ ├── artifacts
│ │ │ │ └── CounterModule#Counter.json
│ │ │ ├── build-info
│ │ │ │ └── solc-0_8_28-5d5e77a08f43f8366c520176a292b83d65132357.json
│ │ │ └── journal.jsonl
│ │ ├── chain-11155111
│ │ │ ├── artifacts
│ │ │ │ └── CounterModule#Counter.json
│ │ │ ├── build-info
│ │ │ │ └── solc-0_8_28-5d5e77a08f43f8366c520176a292b83d65132357.json
│ │ │ ├── deployed_addresses.json
│ │ │ └── journal.jsonl
│ │ └── chain-31337
│ │ ├── artifacts
│ │ │ └── CounterModule#Counter.json
│ │ ├── build-info
│ │ │ └── solc-0_8_28-5d5e77a08f43f8366c520176a292b83d65132357.json
│ │ ├── deployed_addresses.json
│ │ └── journal.jsonl
│ └── modules
│ └── Counter.ts
├── package-lock.json
├── package.json
├── README.md
├── test
│ └── Counter.ts
└── tsconfig.json
项目创建
mkdir hardhat-tutorial
cd hardhat-tutorial
npx hardhat --init
编写智能合约
contracts/Counter.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;
contract Counter {
uint public x;
event Increment(uint by);
function inc() public {
x += 1;
emit Increment(1);
}
function incBy(uint by) public {
require(by > 0, "incBy: increment should be positive");
x += by;
emit Increment(by);
}
}
// Random number to make this contract file unique: 185640209
♾️ 编译项目
npx hardhat build
测试编写
contracts/Counter.t.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;
import { Counter } from "./Counter.sol";
contract CounterTest {
Counter counter;
function setUp() public {
counter = new Counter();
}
function test_InitialValueIsZero() public view {
require(counter.x() == 0, "x should start at 0");
}
function test_IncIncreasesByOne() public {
counter.inc();
require(counter.x() == 1, "inc should increase x by 1");
}
function test_IncByIncreasesByGivenAmount() public {
counter.incBy(3);
require(counter.x() == 3, "incBy should increase x by the given amount");
}
}
🧪 测试
npx hardhat test
npx hardhat test solidity
使用 Hardhat
👷 安装
npm add --save-dev @nomicfoundation/hardhat-toolbox-viem @nomicfoundation/hardhat-ignition viem
➕ 添加到 Hardhat 配置
hardhat.config.ts
import hardhatToolboxViemPlugin from "@nomicfoundation/hardhat-toolbox-viem";
import { defineConfig } from "hardhat/config";
export default defineConfig({
plugins: [hardhatToolboxViemPlugin],
solidity: {
version: "0.8.28",
},
});
编写测试
test/Counter.ts
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { network } from "hardhat";
describe("Counter", async function () {
const { viem } = await network.connect();
const publicClient = await viem.getPublicClient();
it("The sum of the Increment events should match the current value", async function () {
const counter = await viem.deployContract("Counter");
const deploymentBlockNumber = await publicClient.getBlockNumber();
// run a series of increments
for (let i = 1n; i {
// module implementation goes here
});
计数器部署示例
{
const counter = m.contract("Counter");
m.call(counter, "incBy", [5n]);
return { counter };
}
🧪 本地尝试
npx hardhat ignition deploy ignition/modules/Counter.ts
🚀 运行本地区块链网络
npx hardhat node
🚀 本地部署合约
npx hardhat ignition deploy ignition/modules/Counter.ts --network localhost
部署到实时网络
前置条件
-
创建一个 Alchemy 账户。
-
创建一个 MetaMask 账户。
-
安装 dotenv:
npm install dotenv
📄 配置 (hardhat.config.ts)
import hardhatToolboxViemPlugin from "@nomicfoundation/hardhat-toolbox-viem";
import { defineConfig } from "hardhat/config";
import dotenv from "dotenv";
dotenv.config();
export default defineConfig({
plugins: [hardhatToolboxViemPlugin],
solidity: {
version: "0.8.28",
},
networks: {
sepolia: {
type: "http",
url: process.env.SEPOLIA_RPC_URL || "",
accounts: process.env.SEPOLIA_PRIVATE_KEY ? [process.env.SEPOLIA_PRIVATE_KEY] : [],
},
},
});
🏃♂️ 部署到 Sepolia
npx hardhat ignition deploy ignition/modules/Counter.ts --network sepolia 