广告位
468x60
正文内嵌广告1
¥500/月
课程简介
课程简介
本课程专为区块链开发初学者设计,系统讲解以太坊智能合约开发全流程。从区块链基础概念切入,逐步深入Solidity语言特性、Remix IDE开发环境配置、智能合约编译部署等核心技能。通过转账合约、投票合约等典型场景实战,掌握Gas费用优化、安全编码规范等关键技术点。课程结束时学员可独立完成基础智能合约开发,为后续DApp开发或智能合约审计打下坚实基础。
课程大纲
第1章第一章 以太坊基础
- 1.1 区块链与以太坊概述30min
- 1.2 以太坊账户与交易模型45min
- 1.3 Gas机制与费用计算45min
第2章第二章 Solidity基础
- 2.1 开发环境搭建60min
- 2.2 基本语法与数据类型60min
- 2.3 函数与修饰符60min
第3章第三章 智能合约实战
- 3.1 转账合约开发90min
- 3.2 投票合约实现90min
广告位
336x280
正文内嵌广告2
¥700/月
课程资料
以太坊开发基础:从零开始学智能合约
第一章 以太坊基础
1.1 区块链与以太坊概述
区块链是一种去中心化的分布式账本技术,其核心特征包括:
- 不可篡改:通过密码学哈希链保证数据完整性
- 去中心化:无中心化机构控制,所有节点平等
- 共识机制:通过PoW/PoS等算法达成数据一致性
以太坊(Ethereum)是区块链2.0的代表,在比特币基础上引入**智能合约(Smart Contract)**功能。其核心组件包括:
- EVM(以太坊虚拟机):执行智能合约的沙盒环境
- Solidity:专为EVM设计的合约编程语言
- Gas机制:防止滥用计算资源的经济模型
graph LR
A[用户发起交易] --> B[EVM执行合约]
B --> C[消耗Gas]
C --> D{执行成功?}
D -->|是| E[更新状态]
D -->|否| F[回滚交易]
1.2 以太坊账户与交易模型
以太坊存在两种账户类型:
| 账户类型 | 特点 | 地址生成 |
|---|---|---|
| 外部账户(EOA) | 由私钥控制,可发起交易 | 通过SECP256k1椭圆曲线生成 |
| 合约账户 | 包含合约代码,由EOA创建 | 通过CREATE操作码生成 |
典型交易流程:
- EOA签名交易数据
- 广播至全网节点
- 矿工验证并打包进区块
- 全网节点执行并更新状态
1.3 Gas机制与费用计算
Gas是执行合约操作所需的计算单位,其核心作用:
- 防止无限循环攻击
- 激励矿工处理交易
- 衡量计算复杂度
Gas费用计算公式:
总费用 = Gas Limit × Gas Price (Gwei)
示例:转账交易通常消耗21,000 Gas,当Gas Price为50 Gwei时:
总费用 = 21,000 × 50 = 1,050,000 Gwei ≈ 0.00105 ETH
本节小结
- 理解区块链与以太坊的核心差异
- 掌握两类账户的交互机制
- 学会计算Gas费用避免部署失败
第二章 Solidity基础
2.1 开发环境搭建
推荐使用Remix IDE进行开发:
- 访问 remix.ethereum.org
- 创建新文件
SimpleStorage.sol - 配置编译器版本(建议0.8.x)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
2.2 基本语法与数据类型
Solidity核心数据类型:
| 类型 | 示例 | 范围 |
|---|---|---|
| uint | uint256 | 0 ~ 2^256-1 |
| int | int256 | -2255 ~ 2255-1 |
| address | address payable | 20字节以太坊地址 |
| bool | true/false | 逻辑值 |
关键特性:
- 可见性修饰符:
public/private/internal/external - 状态变量:存储在区块链上的变量
- 常量:
constant或immutable声明
2.3 函数与修饰符
典型函数结构:
function transfer(
address to,
uint256 amount
)
public
payable
returns (bool)
{
require(amount > 0, "Amount must be positive");
(bool sent, ) = to.call{value: amount}("");
require(sent, "Transfer failed");
return true;
}
常用修饰符:
view:不修改状态pure:不读取也不修改状态payable:可接收ETHrequire:条件检查
本节小结
- 掌握Remix IDE基础操作
- 理解Solidity数据类型系统
- 学会编写安全函数与修饰符
第三章 智能合约实战
3.1 转账合约开发
实现一个多签钱包合约:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MultiSigWallet {
address[] public owners;
uint public requiredConfirmations;
mapping(address => bool) public isOwner;
struct Transaction {
address to;
uint value;
bytes data;
bool executed;
uint confirmations;
}
Transaction[] public transactions;
mapping(uint => mapping(address => bool)) public confirmed;
constructor(address[] memory _owners, uint _required) {
require(_owners.length > 0, "Owners required");
require(_required > 0 && _required <= _owners.length, "Invalid confirmations");
for (uint i = 0; i < _owners.length; i++) {
address owner = _owners[i];
require(!isOwner[owner], "Duplicate owner");
isOwner[owner] = true;
owners.push(owner);
}
requiredConfirmations = _required;
}
function submitTransaction(
address to,
uint value,
bytes memory data
) public returns (uint) {
uint transactionId = transactions.length;
transactions.push(
Transaction(to, value, data, false, 0)
);
return transactionId;
}
function confirmTransaction(uint transactionId) public {
Transaction storage tx = transactions[transactionId];
require(!tx.executed, "Transaction executed");
require(!confirmed[transactionId][msg.sender], "Already confirmed");
confirmed[transactionId][msg.sender] = true;
tx.confirmations++;
if (tx.confirmations >= requiredConfirmations) {
(bool sent, ) = tx.to.call{value: tx.value}(tx.data);
require(sent, "Transfer failed");
tx.executed = true;
}
}
}
3.2 投票合约实现
安全投票合约要点:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Voting {
struct Voter {
bool isRegistered;
bool hasVoted;
uint weight;
}
struct Proposal {
uint voteCount;
}
address public chairperson;
mapping(address => Voter) public voters;
Proposal[] public proposals;
constructor(address[] memory proposalNames) {
chairperson = msg.sender;
for (uint i = 0; i < proposalNames.length; i++) {
proposals.push(Proposal(0));
}
}
function registerVoter(address voter) public {
require(msg.sender == chairperson, "Only chairperson");
require(!voters[voter].isRegistered, "Already registered");
voters[voter] = Voter(true, false, 1);
}
function vote(uint proposalId) public {
Voter storage sender = voters[msg.sender];
require(sender.isRegistered, "Not registered");
require(!sender.hasVoted, "Already voted");
require(proposalId < proposals.length, "Invalid proposal");
proposals[proposalId].voteCount += sender.weight;
sender.hasVoted = true;
}
function winningProposal() public view returns (uint) {
uint winningVoteCount = 0;
uint winningProposalId = 0;
for (uint i = 0; i < proposals.length; i++) {
if (proposals[i].voteCount > winningVoteCount) {
winningVoteCount = proposals[i].voteCount;
winningProposalId = i;
}
}
return winningProposalId;
}
}
部署检查清单:
- 编译时选择正确编译器版本
- 部署前测试所有函数
- 验证Gas消耗是否合理
- 检查事件日志输出
本节小结
- 掌握转账合约的多签安全模式
- 理解投票合约的权限控制
- 学会使用Remix部署与调试
课程总结
本课程系统讲解了以太坊智能合约开发的核心技能:
- 基础理论:区块链原理、Gas机制、账户模型
- 语言特性:Solidity数据类型、函数修饰符、安全编码
- 实战开发:转账合约、投票合约的完整实现
后续学习建议:
- 深入学习Ethereum Yellow Paper
- 实践更复杂的DeFi协议开发
- 学习智能合约安全审计技巧
完成本课程后,学员可胜任初级区块链开发工程师岗位,或继续学习DApp开发进阶课程。
广告位
300x250
正文内嵌广告3
¥600/月