:2026-02-22 7:30 点击:20
以太坊作为全球领先的智能合约平台,不仅仅是一种加密货币,更是一个去中心化的应用(DApp)生态系统,它为开发者提供了强大的工具和框架,使得构建和部署去中心化应用成为可能,本教程将带你踏以太坊开发的学习之旅,从基础概念到实践操作,一步步引导你构建你的第一个DApp。
以太坊开发基础:你需要了解什么?
在开始编码之前,我们需要理解几个核心概念:
开发环境搭建
准备好开发环境是以太坊开发的第一步:
npm install -g truffle
npm install -g ganache-cli
创建你的第一个以太坊项目(使用Truffle)
初始化项目: 创建一个新的项目文件夹,并在其中初始化Truffle项目:
mkdir my-first-dapp cd my-first-dapp truffle init
这会生成一些标准目录结构:contracts/(存放智能合约)、migrations/(部署脚本)、test/(测试文件)等。
编写智能合约:
打开contracts目录,删除默认的Migrations.sol,创建一个新的合约文件,例如SimpleStorage.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
}
这个合约非常简单,包含一个set函数用于存储一个无符号整数,和一个get函数用于读取这个整数。
编写迁移脚本:
打开migrations目录,创建一个新的迁移脚本文件,例如2_deploy_simple_storage.js:
const SimpleStorage = artifacts.require("SimpleStorage");
module.exports = function (deployer) {
deployer.deploy(SimpleStorage);
};
这个脚本告诉Truffle如何部署我们的SimpleStorage合约。
启动Ganache: 打开Ganache,确保选择“QUICKSTART”模式,它会启动一个本地私有区块链,并提供你10个测试账户,每个账户都有100个测试ETH。
配置Truffle连接Ganache:
在项目根目录下创建或修改truffle-config.js文件(如果truffle init没生成的话),配置网络以连接到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)
},
},
compilers: {
solc: {
version: "0.8.0", // Fetch exact version from solc-bin (default: truffle's version)
}
}
};
注意端口号(7545)要与你Ganache中显示的端口一致。
编译合约: 在项目根目录的终端中运行:
truffle compile
如果成功,你会在build/contracts目录下看到编译后的合约JSON文件。
部署合约: 运行以下命令部署合约到Ganache:
truffle migrate --network development
你会看到Ganache界面中出现了新的交易记录,部署成功后,build/contracts/SimpleStorage.json中会包含合约的部署地址。
与你的智能合约交互
创建前端界面:
在项目根目录下创建一个src文件夹,用于存放前端代码,在src中创建index.html和app.js。
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport
" content="width=device-width, initial-scale=1.0">
<title>Simple Storage 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>
<div>
<label for="valueInput">Set Value:</label>
<input type="number" id="valueInput" placeholder="Enter a number">
<button onclick="setValue()">Set</button>
</div>
<div>
<button onclick="getValue()">Get Value</button>
<p>Current Value: <span id="currentValue">0</span></p>
</div>
<script src="app.js"></script>
</body>
</html>
app.js:
let contract;
const contractAddress = "YOUR_CONTRACT_ADDRESS_HERE"; // 替换为部署合约时得到的地址
const contractABI = [/* 这里粘贴SimpleStorage的ABI */]; // 稍后从编译后的JSON中获取
// 初始化 ethers
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
async function init() {
// 请求用户授权连接钱包
await window.ethereum.request({ method: 'eth_requestAccounts' });
// 连接到合约
contract = new ethers.Contract(contractAddress, contractABI, signer);
getValue();
}
async function setValue() {
const valueInput = document.getElementById("valueInput");
const value = valueInput.value;
if (value) {
try {
const tx = await contract.set(value);
await tx.wait();
alert("Value set successfully!");
getValue();
} catch (error) {
console.error(error);
alert("Error setting value");
}
}
}
async function getValue() {
try {
const value = await contract.get();
document.getElementById("currentValue").textContent = value.toString();
} catch (error) {
console.error(error);
alert("Error getting value");
}
}
// 页面加载完成后初始化
window.addEventListener('load', init);
注意:你需要将YOUR_CONTRACT_ADDRESS_HERE替换为实际部署的合约地址,并将contractABI替换为SimpleStorage.json中的abi数组,你可以从build/contracts/SimpleStorage.json中复制ABI。
**启动前端
本文由用户投稿上传,若侵权请提供版权资料并联系删除!