搭建高效的开发环境是智能合约开发的第一步。本文详细介绍如何使用Hardhat、Truffle等框架,配置编译、测试和部署工具,帮助开发者快速开始智能合约开发。
一、什么是开发环境
1. 基本概念
Solidity开发环境包括编译器、测试框架、部署工具和开发服务器等组件,用于编写、编译、测试和部署智能合约。选择合适的开发环境可以大大提高开发效率。
2. 开发环境组件
编译器
- Solc编译器
- 将Solidity代码编译成字节码
- 生成ABI接口
- 版本管理
测试框架
- 单元测试
- 集成测试
- 模拟网络
- 断言库
部署工具
- 自动化部署
- 网络管理
- 合约验证
- 版本控制
开发服务器
- 本地区块链
- 快速测试
- 自动挖矿
- 无需等待
二、使用Hardhat搭建
1. 安装Hardhat
创建项目
1 | |
npm install –save-dev hardhat
初始化项目:
1 | |
选择配置
- Create a JavaScript project(推荐)
- Create a TypeScript project
- Create an empty hardhat.config.js
2. 项目结构
目录结构
1 | |
// hardhat.config.js
require(“@nomicfoundation/hardhat-toolbox”);
module.exports = {
solidity: “0.8.19”,
networks: {
hardhat: {
chainId: 1337
},
localhost: {
url: “http://127.0.0.1:8545“
}
};
1 | |
包含工具:
- Hardhat Network
- Ethers.js
- Waffle测试
- Solidity编译器
其他工具:
npm install –save-dev @nomicfoundation/hardhat-verify
npm install –save-dev hardhat-gas-reporter
1 | |
全局安装: npm install -g truffle 项目安装: npm install --save-dev truffle 初始化项目: truffle init ### 3.2 项目结构
├── contracts/ # 智能合约
├── migrations/ # 部署脚本
├── truffle-config.js # 配置文件
// truffle-config.js
development: {
host: “127.0.0.1”,
port: 8545,
network_id: “*”
}
},
compilers: {
solc: {
version: “0.8.19”
}
};
1 | |
安装Ganache CLI: npm install -g ganache-cli 启动Ganache: ganache-cli 特点:
1 | |
安装Remix IDE: npm install -g @remix-project/remixd 连接本地文件:
1 | |
五、配置开发网络
1. Hardhat Network
内置网络:
chainId: 1337,
accounts: {
mnemonic: "test test test test test test test test test test test junk",
count: 20
1 | |
npx hardhat node
1 | |
geth –dev –http –http.addr “0.0.0.0” –http.port 8545
1 | |
配置测试网:
goerli: {
url: https://goerli.infura.io/v3/${INFURA_PROJECT_ID},
accounts: [PRIVATE_KEY]
1 | |
.env
1 | |
加载环境变量:
1 | |
require('dotenv').config(); ## 六、编写和编译合约
1. 创建合约
编写合约:
// contracts/SimpleStorage.sol
pragma solidity ^0.8.19;
contract SimpleStorage {
uint256 public value;
function set(uint256 _value) public {
value = _value;
1 | |
function get() public view returns (uint256) {
return value;
1 | |
2. 编译合约
使用Hardhat: npx hardhat compile 使用Truffle: truffle compile 输出位置:
- Hardhat:
artifacts/contracts/ - Truffle:
build/contracts/
3. 编译配置
solidity: {
version: “0.8.19”,
settings: {
optimizer: {
enabled: true,
runs: 200
}
1 | |
Hardhat测试:
// test/SimpleStorage.test.js
const { expect } = require(“chai”);
const { ethers } = require(“hardhat”);
describe(“SimpleStorage”, function() {
it(“Should set and get value”, async function() {
const SimpleStorage = await ethers.getContractFactory(“SimpleStorage”);
const contract = await SimpleStorage.deploy();
await contract.set(42);
expect(await contract.value()).to.equal(42);
});
1 | |
npx hardhat test
1 | |
八、部署合约
1. Hardhat部署
1 | |
}
1 | |
执行部署:
1 | |
2. Truffle部署
// migrations/2_deploy_contracts.js
module.exports = function(deployer) {
deployer.deploy(SimpleStorage);
};
truffle migrate –network development
1 | |
本文标题: 搭建Solidity开发环境
发布时间: 2024年06月16日 00:00
最后更新: 2026年09月16日 05:40
原始链接: https://haoxiang.eu.org/88ed06f8/
版权声明: 本文著作权归作者所有,均采用CC BY-NC-SA 4.0许可协议,转载请注明出处!

