在开始之前,我们先简单理解两个核心概念:
无需编程基础?不,至少需要一点Python或JavaScript基础(理解变量、函数、循环即可),以及一颗耐心,准备以下工具:

以太坊开发的核心工具是 Truffle Suite(开发框架)和 Ganache(本地区块链节点),简单理解:Ganache是你的“私人以太坊网络”,可以随意测试转账、部署合约;Truffle则帮你管理代码、编译合约、部署到网络。
node -v 和 npm -v 确认安装成功。 npm install -g ganache),运行 ganache 启动。 npm install -g truffle(全局安装),后续用 truffle version 确认安装成功。 我们用Truffle初始化一个项目,并编写一个简单的“智能合约”——一个能存储和读取数字的“数字钱包”。
在终端进入你想存放项目的文件夹,运行:
mkdir my-first-dapp cd my-first-dapp truffle init
执行后,会生成以下文件夹结构:
contracts/:存放智能合约代码(Solidity语言)。 migrations/:部署脚本(告诉Truffle如何部署合约)。 test/:测试文件。 truffle-config.js:Truffle配置文件。 打开 contracts/ 文件夹,删除默认的 Migrations.sol,新建 SimpleStorage.sol,输入以下代码:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private storedData; // 存储数字的变量(private表示外部不可直接访问)
// 存储数字的函数
function set(uint256 x) public {
storedData = x;
}
// 读取数字的函数
function get() public view returns (uint256) {
return storedData;
}
}
代码解释:

SPDX-License-Identifier:开源协议(MIT表示可自由使用)。 pragma solidity ^0.8.0:指定Solidity版本(0.8.0以上,兼容0.8.x)。 contract:智能合约的“容器”,类似Python的class或Java的class。 uint256:256位无符号整数(0到2²⁵⁶-1)。 public:函数修饰符,表示外部可调用。 view:表示函数只读,不修改区块链状态。 智能合约是“文本”,需要编译成机器能识别的“字节码”,再部署到区块链上(这里先部署到本地Ganache)。
终端运行:
truffle compile
成功后,build/contracts/ 目录会生成 SimpleStorage.json,这是合约的“ABI文件”(应用程序二进制接口,定义了合约与外部交互的接口)。
打开 migrations/ 文件夹,新建 2_deploy_simple_storage.js(数字表示执行顺序),输入:
const SimpleStorage = artifacts.require("SimpleStorage");
module.exports = function (deployer) {
deployer.deploy(SimpleStorage);
};
解释:artifacts.require 引用合约,deployer.deploy 告诉Truffle部署这个合约。
终端运行:

truffle develop
会启动一个本地开发节点(类似Ganache,但集成在Truffle中),并进入交互式命令行(truffle (develop)>),输入:
migrate --network develop
(如果之前没关闭Ganache,也可在 truffle-config.js 中配置本地网络,然后直接用 truffle migrate 部署到Ganache)。
看到 Deploying 'SimpleStorage'... 并提示 success 后,合约就部署成功了!记下合约地址(如 0x5FbDB2315678afecb367f032d93F642f64180aa3),后续要用它调用合约。
部署完合约,怎么“使用”它?比如调用 set 存储数字,再用 get 读取,这里我们用 Web.js(以太坊JavaScript库)在网页中交互。
在项目根目录新建 src/ 文件夹,里面创建 index.html 和 app.js。
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> My First DApp</title>
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js" type="application/javascript"></script>
</head>
<body>
<h1>Simple Storage DApp</h1>
<input type="number" id="numberInput" placeholder="输入数字">
<button onclick="setNumber()">存储数字</button>
<button onclick="getNumber()">读取数字</button>
<p id="result">结果:--</p>
<script src="app.js"></script>
</body>
</html>
app.js:
// 合约ABI(从build/contracts/SimpleStorage.json复制)
const contractABI = [
{
"inputs": [],
"name": "get",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{"internalType": "uint256", "name": "x", "type": "uint256"}],
"name": "set",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
// 合约地址(部署后终端输出的地址)
const contractAddress = "0x5FbDB2315678afecb367f032d93F642f64180aa3";
// 初始化以太坊Provider(连接到本地网络)
const provider = new ethers.providers.Web3Provider(web3.currentProvider);
// 获取MetaMask账户
async function getAccount() {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
return accounts[0];
}
// 存储数字
async function set
免责声明:本文为转载,非本网原创内容,不代表本网观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
如有疑问请发送邮件至:bangqikeconnect@gmail.com