:2026-04-03 19:00 点击:3
以太坊作为全球领先的区块链平台,不仅仅是一种加密货币,更是一个去中心化的应用(DApps)开发平台,其核心在于智能合约——一种在以太坊虚拟机(EVM)上自动执行的、以代码形式定义的合约,本教程将带你从零开始,逐步了解并掌握以太坊智能合约的开发流程。
在深入编码之前,我们先理解几个基本概念:
要开始以太坊合约开发,你需要准备以下工具:
npm install -g truffle初始化项目: 创建一个新的项目文件夹,并在终端中进入该文件夹,然后运行:
truffle init
这会生成一个标准的Truffle项目结构,包括:
contracts/:存放Solidity智能合约文件。migrations/:存放部署脚本。test/:存放测试脚本。truffle-config.js:Truffle配置文件。编写智能合约:
打开contracts/文件夹,删除默认的Migrations.sol(我们稍后会重新创建一个更简单的),创建一个新的合约文件,例如SimpleStorage.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title SimpleStorage
* @dev 一个简单的存储合约,可以存储和获取一个uint256类型的值。
*/
contract SimpleStorage {
uint256 private storedData;
event DataSet(uint256 newValue);
/**
* @dev 存储一个值
* @param _value 要存储的值
*/
function set(uint256 _value) public {
storedData = _value;
emit DataSet(_value);
}
/**
* @de
v 获取存储的值
* @return 存储的值
*/
function get() public view returns (uint256) {
return storedData;
}
}
SPDX-License-Identifier:许可证标识符。pragma solidity ^0.8.0;:指定Solidity编译器版本,^0.8.0表示兼容0.8.0及以上,但低于0.9.0的版本。contract SimpleStorage { ... }:定义一个名为SimpleStorage的合约。uint256 private storedData;:声明一个私有的无符号256位整型变量,用于存储数据。event DataSet(uint256 newValue);:定义一个事件,用于在数据改变时通知前端。function set(uint256 _value) public { ... }:一个公共函数,用于设置storedData的值。function get() public view returns (uint256) { ... }:一个公共函数,用于获取storedData的值,view表示读取状态不修改区块链状态。在项目根目录的终端中运行:
truffle compile
如果合约语法正确,Truffle会在build/contracts/目录下生成对应的JSON文件,这是编译后的合约字节码和ABI(Application Binary Interface,应用程序二进制接口,是合约与外界交互的接口)。
启动Ganache:打开Ganache UI,选择“QUICKSTART”即可创建一个新的本地区块链,并记下其中列出的一个账户地址及其私钥(通常是第一个账户)。
配置Truffle连接Ganache:
打开truffle-config.js(或truffle.js),在networks对象中添加对本地Ganache网络的配置:
module.exports = {
// ... 其他配置
networks: {
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 7545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
},
// ... 其他配置
};
Ganache默认监听7545端口,网络ID通常为5777,但使用可以匹配任何网络ID,更方便。
编写部署脚本:
打开migrations/文件夹,创建一个新的迁移脚本,例如2_deploy_contracts.js(数字表示执行顺序):
const SimpleStorage = artifacts.require("SimpleStorage");
module.exports = function (deployer) {
deployer.deploy(SimpleStorage);
};
artifacts.require用于告诉Truffle我们要部署哪个合约。
执行部署: 在项目根目录终端运行:
truffle migrate --network development
如果一切顺利,你会看到部署成功的信息,包括合约地址、交易哈希等,你可以在Ganache中看到这笔部署交易。
编写测试是保证合约质量的关键,Truffle支持JavaScript和Solidity测试。
创建测试文件:
在test/文件夹下创建一个JavaScript测试文件,例如simpleStorage.test.js:
const SimpleStorage = artifacts.require("SimpleStorage");
contract("SimpleStorage", (accounts) => {
it("should store the value 89.", async () => {
const simpleStorageInstance = await SimpleStorage.deployed();
await simpleStorageInstance.set(89, { from: accounts[0] });
const storedData = await simpleStorageInstance.get();
assert.equal(storedData, 89, "The value 89 was not stored.");
});
});
contract("SimpleStorage", ...):定义测试合约。it("should store the value 89.", ...):定义一个测试用例。accounts:由Ganache提供的测试账户数组。assert.equal:断言,用于验证实际结果与预期结果是否一致。运行测试: 在项目根目录终端运行:
truffle test --network development
如果测试通过,你会看到类似1 passing的输出。
为了让用户能够与你的合约交互,你需要一个前端界面,这里以React为例(需要先安装Node.js和npm)。
npx create-react-app dapp-frontend cd dapp-frontend
npm install ethers
src/App.js,修改为:
import React, { useState, useEffect }
本文由用户投稿上传,若侵权请提供版权资料并联系删除!