欢迎来到 Truffle 入门教程!以下是使用 Truffle 框架开发以太坊智能合约的步骤:
1. 安装 Truffle 📦
- 首先,确保已安装 Node.js(建议使用 LTS 版本)
- 通过 npm 安装 Truffle:
npm install -g truffle
2. 初始化项目 🌱
- 创建新项目目录并进入:
mkdir my-first-truffle && cd my-first-truffle
- 初始化 Truffle 项目:
truffle init
3. 编写智能合约 📝
- 在
contracts/
目录下创建MyContract.sol
:// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract MyContract { uint public value; function setValue(uint _newValue) public { value = _newValue; } }
4. 编译合约 🔧
- 运行编译命令:
truffle compile
5. 部署合约 🚀
- 配置
truffle-config.js
连接以太坊网络 - 部署合约:
truffle migrate
6. 测试合约 🧪
- 编写测试用例:
const MyContract = artifacts.require("MyContract"); contract("MyContract", accounts => { it("should set and get value", async () => { const instance = await MyContract.deployed(); await instance.setValue(42); const result = await instance.value(); assert.equal(result, 42); }); });
需要进一步了解合约开发?请访问 truffle_docs/developing_contracts 深入学习!