Solidity开发环境搭建 #
一、开发环境概述 #
Solidity智能合约开发有多种环境选择,从简单到专业:
| 环境 | 特点 | 适用场景 |
|---|---|---|
| Remix IDE | 在线开发,无需安装 | 初学者、快速原型 |
| Hardhat | 专业框架,生态丰富 | 生产级项目开发 |
| Foundry | 高性能,Rust编写 | 高级开发者、测试驱动 |
| VS Code + 插件 | 本地开发,功能完整 | 日常开发 |
二、Remix IDE #
2.1 简介 #
Remix是以太坊官方提供的在线IDE,无需安装,打开浏览器即可使用。
优点:
- 零配置,即开即用
- 内置编译器和部署环境
- 支持插件扩展
- 适合快速测试和学习
访问地址: https://remix.ethereum.org
2.2 使用步骤 #
1. 打开Remix
访问 https://remix.ethereum.org,界面如下:
text
┌─────────────────────────────────────────────────────┐
│ 文件浏览器 │ 代码编辑器 │ 编译/部署面板 │
│ │ │ │
│ contracts/ │ // 代码 │ Compiler │
│ *.sol │ 在这里 │ Deploy │
│ │ 编写 │ Test │
└─────────────────────────────────────────────────────┘
2. 创建新文件
点击左侧文件浏览器中的"Create New File"图标,创建Hello.sol文件。
3. 编写合约
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Hello {
string public greet = "Hello, Solidity!";
function setGreet(string memory _greet) public {
greet = _greet;
}
}
4. 编译合约
- 点击左侧"SOLIDITY COMPILER"图标
- 选择编译器版本(建议使用最新稳定版)
- 点击"Compile Hello.sol"
5. 部署合约
- 点击左侧"DEPLOY & RUN TRANSACTIONS"图标
- 选择环境(JavaScript VM用于测试)
- 点击"Deploy"按钮
6. 交互测试
部署成功后,在下方"Deployed Contracts"中展开合约,调用函数进行测试。
2.3 Remix常用快捷键 #
| 快捷键 | 功能 |
|---|---|
| Ctrl+S | 保存并编译 |
| Ctrl+Space | 代码提示 |
| Ctrl+/ | 注释/取消注释 |
三、Hardhat #
3.1 简介 #
Hardhat是以太坊开发的专业框架,提供编译、测试、部署等完整功能。
优点:
- 功能完整,生态丰富
- TypeScript支持
- 插件系统强大
- 测试网络集成
3.2 安装步骤 #
前置要求:
- Node.js >= 16.0
- npm 或 yarn
1. 创建项目目录
bash
mkdir my-hardhat-project
cd my-hardhat-project
2. 初始化项目
bash
npm init -y
3. 安装Hardhat
bash
npm install --save-dev hardhat
4. 初始化Hardhat项目
bash
npx hardhat init
选择"Create a JavaScript project"或"Create a TypeScript project"。
5. 项目结构
text
my-hardhat-project/
├── contracts/ # 合约目录
│ └── Lock.sol
├── scripts/ # 脚本目录
│ └── deploy.js
├── test/ # 测试目录
│ └── Lock.js
├── hardhat.config.js # 配置文件
└── package.json
3.3 编写合约 #
在contracts/目录下创建SimpleStorage.sol:
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 public storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
}
3.4 编译合约 #
bash
npx hardhat compile
输出:
text
Compiled 1 Solidity file successfully
3.5 编写测试 #
在test/目录下创建SimpleStorage.js:
javascript
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("SimpleStorage", function () {
it("Should store and retrieve a value", async function () {
const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
const storage = await SimpleStorage.deploy();
await storage.deployed();
await storage.set(42);
expect(await storage.get()).to.equal(42);
});
});
3.6 运行测试 #
bash
npx hardhat test
3.7 部署合约 #
在scripts/目录下创建deploy.js:
javascript
const hre = require("hardhat");
async function main() {
const SimpleStorage = await hre.ethers.getContractFactory("SimpleStorage");
const storage = await SimpleStorage.deploy();
await storage.deployed();
console.log("SimpleStorage deployed to:", storage.address);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
运行部署脚本:
bash
npx hardhat run scripts/deploy.js
3.8 常用Hardhat命令 #
| 命令 | 功能 |
|---|---|
npx hardhat compile |
编译合约 |
npx hardhat test |
运行测试 |
npx hardhat run scripts/deploy.js |
部署合约 |
npx hardhat node |
启动本地节点 |
npx hardhat clean |
清理编译文件 |
四、Foundry #
4.1 简介 #
Foundry是用Rust编写的高性能以太坊开发框架,编译和测试速度极快。
优点:
- 编译速度极快
- 测试执行迅速
- Solidity编写测试
- 内置模糊测试
4.2 安装步骤 #
macOS/Linux:
bash
curl -L https://foundry.paradigm.xyz | bash
foundryup
Windows (PowerShell):
powershell
Invoke-WebRequest https://foundry.paradigm.xyz | Invoke-Expression
foundryup
4.3 创建项目 #
bash
forge init my-foundry-project
cd my-foundry-project
项目结构:
text
my-foundry-project/
├── src/ # 合约目录
│ └── Counter.sol
├── test/ # 测试目录
│ └── Counter.t.sol
├── script/ # 脚本目录
│ └── Counter.s.sol
├── lib/ # 依赖库
└── foundry.toml # 配置文件
4.4 编写合约 #
在src/目录下创建SimpleStorage.sol:
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 public storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
}
4.5 编写测试 #
在test/目录下创建SimpleStorage.t.sol:
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import "../src/SimpleStorage.sol";
contract SimpleStorageTest is Test {
SimpleStorage storage;
function setUp() public {
storage = new SimpleStorage();
}
function testSetAndGet() public {
storage.set(42);
assertEq(storage.get(), 42);
}
}
4.6 运行测试 #
bash
forge test
输出:
text
Running 1 test for test/SimpleStorage.t.sol:SimpleStorageTest
[PASS] testSetAndGet() (gas: 12345)
Test result: ok. 1 passed; 0 failed; finished in 1.23s
4.7 部署合约 #
bash
forge create src/SimpleStorage.sol:SimpleStorage --rpc-url $RPC_URL --private-key $PRIVATE_KEY
4.8 常用Foundry命令 #
| 命令 | 功能 |
|---|---|
forge build |
编译合约 |
forge test |
运行测试 |
forge create |
部署合约 |
forge install |
安装依赖 |
cast |
链交互工具 |
anvil |
本地节点 |
五、VS Code配置 #
5.1 安装插件 #
推荐安装以下VS Code插件:
| 插件 | 功能 |
|---|---|
| Solidity (Nomic Foundation) | 语法高亮、代码提示 |
| Hardhat Solidity | Hardhat集成 |
| Prettier Solidity | 代码格式化 |
5.2 配置settings.json #
json
{
"solidity.compileUsingRemoteVersion": "latest",
"solidity.packageDefaultDependenciesDirectory": "node_modules",
"solidity.packageDefaultDependenciesContractsDirectory": "contracts",
"[solidity]": {
"editor.defaultFormatter": "NomicFoundation.hardhat-solidity",
"editor.formatOnSave": true
}
}
六、环境对比与选择 #
6.1 功能对比 #
| 功能 | Remix | Hardhat | Foundry |
|---|---|---|---|
| 安装难度 | 无需安装 | 中等 | 中等 |
| 编译速度 | 一般 | 一般 | 极快 |
| 测试速度 | 一般 | 一般 | 极快 |
| 测试语言 | - | JavaScript/TypeScript | Solidity |
| 调试功能 | 内置 | 插件支持 | 内置 |
| 插件生态 | 内置插件 | 丰富 | 发展中 |
6.2 选择建议 #
| 场景 | 推荐环境 |
|---|---|
| 初学者学习 | Remix |
| 快速原型验证 | Remix |
| 生产级项目 | Hardhat |
| 高频测试迭代 | Foundry |
| 团队协作开发 | Hardhat/Foundry |
七、总结 #
开发环境搭建要点:
| 环境 | 安装方式 | 适用场景 |
|---|---|---|
| Remix | 浏览器访问 | 初学者、快速测试 |
| Hardhat | npm安装 | 生产项目 |
| Foundry | foundryup安装 | 高性能开发 |
下一步,让我们编写第一个Solidity智能合约!
最后更新:2026-03-27