:2026-02-12 1:03 点击:6
以太坊作为全球领先的智能合约平台,不仅仅是一种加密货币,更是一个去中心化的、可编程的区块链操作系统,为构建去中心化应用(DApps)提供了强大的基础设施,本文旨在为开发者提供一份详尽、循序渐进的以太坊开发攻略,从环境搭建到智能合约编写,再到前后端交互,助你迈出以太坊开发的第一步,并逐步构建出自己的DApp。
在正式开始编码之前,我们需要了解一些核心概念并搭建好开发环境。
npm install -g yarn)。npx hardhat,然后按照提示初始化一个新的Hardhat项目,选择"Create a JavaScript project"(或TypeScript),回答相关问题,安装依赖。智能合约是DApp的核心。
版本声明:pragma solidity ^0.8.0; (指定编译器版本)
合约结构:
contract HelloWorld {
// 状态变量
string public greeting;
// 构造函数
constructor(string memory _greeting) {
greeting = _greeting;
}
// 函数
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
function getGreeting() public view returns (string memory) {
return greeting;
}
}
数据类型:uint (无符号整数), int (有符号整数), bool, address, string, bytes, 数组,映射(mapping)等。
可见性修饰符:public (自动生成getter函数), private, internal, external。
状态修饰符:view (不修改状态), pure (不读取也不修改状态), payable (可接收以太币)。
特殊函数:constructor (构造函数,仅调用一次), fallback() / receive() (接收以太币或调用不存在函数时触发)。
a. 创建合约:
contracts目录下创建新的Solidity文件,如HelloWorld.sol。b. 编写测试:
test目录下创建测试文件,如helloWorld.test.js(使用Mocha和Chai框架)。
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("HelloWorld", function () { it("Should return the new greeting once changed", async function () { const HelloWorld = await ethers.getContractFactory("HelloWorld"); const helloWorld = await HelloWorld.deploy("Hello, world!"); await helloWorld.deployed();
expect(await helloWorld.getGreeting()).to.equal("Hello, world!");
const setGreetingTx = await helloWorld.setGreeting("Hola, mundo!");
// 等待交易被挖矿
await setGreetingTx.wait();
expect(await helloWorld.getGreeting()).to.equal("Hola, mundo!");
});
c. 运行测试:
npx hardhat testd. 编译合约:
npx hardhat compile (Hardhat会自动处理编译过程)部署合约是将智能合约部署到以太坊网络(测试网或主网)的过程。
scripts目录下找到或创建部署脚本,如deploy.js:async function main() {
const HelloWorld = await ethers.getContractFactory("HelloWorld");
const helloWorld = await HelloWorld.deploy("Hello Hardhat!");
await helloWorld.deployed();
console.log("HelloWorld deployed to:", helloWorld.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
a. 获取测试ETH:

b. 配置Hardhat网络:
hardhat.config.js中添加Goerli网络配置:
require("@nomicfoundation/hardhat-toolbox");
require('dotenv').config(); // 安装 dotenv: npm install dotenv
/* @type import('hardhat/config').HardhatUserConfig / module.exports = { solidity: "0.8.17", networks: { goerli: { url: process.env.GOERLI_URL, // 从.env文件中读取 accounts: [process.env.PRIVATE_KEY], // 从.env文件中读取 }, }, };
* 在项目根目录创建`.env`文件(添加到`.gitignore`):
GOERLI_URL=https://eth-goerli.g.alchemy.com/v2/YOUR_ALCHEMY_API_KEY
PRIVATE_KEY=你的MetaMask账户私钥 (0x开头)
```
* `GOERLI_URL` 可以从Alchemy或Infura等节点服务商获取免费额度。
* `PRIVATE_KEY` 从MetaMask导出(极度小心,切勿泄露!)。
c. 执行部署:
npx hardhat run scripts/deploy.js --network goerli前端是用户与DApp交互的界面,通常使用Web3.js或Ethers.js库与以太坊网络和智能合约通信。
create-react-app或vite等工具创建React/Vue项目。npx create-react-app my-dapp cd my-dapp npm install ethers
在前端代码中(如App.js),使用Ethers.js连接MetaMask:
import { useState, useEffect } from 'react';
import { ethers } from 'ethers';
function App() {
const [account, setAccount] = useState(null);
const [contract, setContract] = useState(null);
const [greeting, setGreeting] = useState('');
本文由用户投稿上传,若侵权请提供版权资料并联系删除!