快速开始
本指南将帮助您快速上手 Solidity 智能合约开发。
前置要求
在开始之前,请确保您已经:
- 了解基本的编程概念
- 熟悉 JavaScript/TypeScript(推荐)
- 对区块链和智能合约有基本认识
第一个智能合约
让我们从一个简单的智能合约开始:
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract HelloWorld {
string public message;
constructor(string memory _message) {
message = _message;
}
function setMessage(string memory _message) public {
message = _message;
}
function getMessage() public view returns (string memory) {
return message;
}
}
合约解释
SPDX-License-Identifier
:声明代码的开源协议pragma solidity ^0.8.20
:指定编译器版本contract HelloWorld
:定义合约名称string public message
:声明状态变量constructor
:构造函数,部署时调用setMessage
:修改消息的函数getMessage
:读取消息的函数
部署和测试
使用 Remix IDE
- 打开 Remix IDE
- 创建新文件
HelloWorld.sol
- 复制上面的代码
- 点击编译按钮
- 在部署页面:
- 选择环境(如 Remix VM)
- 输入构造函数参数
- 点击部署
- 与合约交互:
- 调用
setMessage
函数 - 查看
message
变量的值
- 调用
使用 Hardhat
创建项目:
bashmkdir my-project cd my-project npm init -y npm install --save-dev hardhat npx hardhat init
创建合约:
bashnpx hardhat create HelloWorld.sol
编写测试:
typescriptconst { expect } = require("chai"); describe("HelloWorld", function () { it("Should return the new message", async function () { const HelloWorld = await ethers.getContractFactory("HelloWorld"); const hello = await HelloWorld.deploy("Hello, World!"); await hello.deployed(); expect(await hello.getMessage()).to.equal("Hello, World!"); await hello.setMessage("Hello, Solidity!"); expect(await hello.getMessage()).to.equal("Hello, Solidity!"); }); });
运行测试:
bashnpx hardhat test
下一步
常见问题
1. 合约部署失败怎么办?
- 检查编译器版本是否正确
- 确保构造函数参数正确
- 查看错误信息进行调试
2. Gas 费用过高怎么处理?
- 优化合约代码
- 使用测试网进行开发
- 参考 Gas 优化技巧
3. 如何调试合约?
- 使用 Remix 调试器
- 添加事件日志
- 编写单元测试