Skip to content

Commit a7495e6

Browse files
committed
Add study notes for 2025-08-16
1 parent 0b8a703 commit a7495e6

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

Thomas-YHS.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,143 @@ timezone: UTC+5
1515
## Notes
1616

1717
<!-- Content_START -->
18+
# 2025-08-16
19+
20+
想要了解智能合约,了解以太坊,Open Zeppelin是一个无法跳过的东西,一般源码的阅读,智能合约的使用都是从ERC-20开始的,我们也从这里翻开学习的篇章。
21+
22+
对于想要了解ERC-20我建议从OpenZepplin文档开始阅读:https://docs.openzeppelin.com/contracts/5.x/
23+
24+
那么什么是OpenZepplin呢,下面引用官方的介绍:
25+
26+
> **library for secure smart contract development.** Build on a solid foundation of community-vetted code.一个用于安全智能合约开发的库。建立在社区验证的代码坚实基础之上。
27+
>
28+
> - Implementations of standards like [ERC20](https://docs.openzeppelin.com/contracts/5.x/erc20) and [ERC721](https://docs.openzeppelin.com/contracts/5.x/erc721).实现了 ERC20 和 ERC721 等标准。
29+
> - Flexible [role-based permissioning](https://docs.openzeppelin.com/contracts/5.x/access-control) scheme.灵活的角色权限方案。
30+
> - Reusable [Solidity components](https://docs.openzeppelin.com/contracts/5.x/utilities) to build custom contracts and complex decentralized systems.可重用的 Solidity 组件,用于构建自定义合约和复杂的去中心化系统。
31+
32+
安全,标准,灵活的角色权限,可充用的组件
33+
34+
## 扩展合约
35+
36+
OpenZeppelin大多数合约可以通过is来继承调用,例如`contract MyToken is ERC20`
37+
38+
<aside>
39+
🧠
40+
41+
不过不建议通过重写的方式来,使用更建议独立使用。
42+
43+
</aside>
44+
45+
<aside>
46+
🌐
47+
48+
与 `contract` 不同,Solidity `library` 不是通过继承得到的,而是依赖于 `using for` 语法。
49+
50+
OpenZeppelin Contracts has some `library`s: most are in the [Utils](https://docs.openzeppelin.com/contracts/5.x/api/utils) directory.OpenZeppelin Contracts
51+
52+
utils目录中,有大量可供调用的**library**库,[components](https://docs.openzeppelin.com/contracts/5.x/utilities) 这个组件库中也对utils中进行了详细的说明。
53+
54+
</aside>
55+
56+
## 访问控制Access Control
57+
58+
访问控制——也就是说,“谁被允许做这件事”——在智能合约的世界中至关重要。您合约的访问控制可能决定了谁可以铸造代币、投票、冻结转账以及许多其他事情。
59+
60+
### Ownable
61+
62+
Ownable是OpenZeppelin中几乎最常用的包了,其中包含对Owner的设定,对Owner的检查`modifier onlyOwner()` ,还包括对owner的转移和放弃,是权限管理的基础
63+
64+
```solidity
65+
abstract contract Ownable is Context {
66+
address private _owner;
67+
// 无效Owner地址
68+
error OwnableUnauthorizedAccount(address account);
69+
// 非 owner 调用
70+
error OwnableInvalidOwner(address owner);
71+
// 转移 owner 权限日志
72+
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
73+
74+
// 设定合约初始Owner
75+
constructor(address initialOwner) {
76+
if (initialOwner == address(0)) {
77+
revert OwnableInvalidOwner(address(0));
78+
}
79+
_transferOwnership(initialOwner);
80+
}
81+
82+
// 只有合约Owner才能调用的方法
83+
modifier onlyOwner() {
84+
_checkOwner();
85+
_;
86+
}
87+
88+
// 获取合约Owner
89+
function owner() public view virtual returns (address) {
90+
return _owner;
91+
}
92+
93+
function _checkOwner() internal view virtual {
94+
if (owner() != _msgSender()) {
95+
revert OwnableUnauthorizedAccount(_msgSender());
96+
}
97+
}
98+
// 放弃合约Owner的身份
99+
function renounceOwnership() public virtual onlyOwner {
100+
_transferOwnership(address(0));
101+
}
102+
103+
// 转移Owner身份
104+
function transferOwnership(address newOwner) public virtual onlyOwner {
105+
if (newOwner == address(0)) {
106+
revert OwnableInvalidOwner(address(0));
107+
}
108+
_transferOwnership(newOwner);
109+
}
110+
111+
function _transferOwnership(address newOwner) internal virtual {
112+
address oldOwner = _owner;
113+
_owner = newOwner;
114+
emit OwnershipTransferred(oldOwner, newOwner);
115+
}
116+
}
117+
```
118+
119+
---
120+
121+
但是,Ownable也存在一些问题,比如说在进行合约所有权转移的时候可能会出现转移到错误账号的情况,所以Ownable2Step就诞生了!它解决了可能出现的所有权转移错误问题,它要求新所有者通过调用 `acceptOwnership` 明确接受所有权转移。
122+
123+
Note that **a contract can also be the owner of another one**
124+
125+
通过这种方式,您可以使用组合性为您的合约添加额外的访问控制复杂性层。例如,您可以使用由您的项目负责人管理的 2-of-3 多重签名,而不是只有一个常规以太坊账户(外部拥有账户,或 EOA)作为所有者。空间中的知名项目,如 MakerDAO,使用与此类似的系统。
126+
127+
###
128+
129+
## ERC165
130+
131+
**让一个智能合约可以声明并让别人查询它是否实现了某个接口。**
132+
133+
在 ERC-165 之前,如果你和另一个合约交互(比如想调用它的某个函数),**你根本不知道对方支不支持这个接口**
134+
135+
- 如果直接调用不支持的函数,会直接 revert。
136+
- 每次交互前都要写额外的检测逻辑,很麻烦。
137+
138+
ERC-165 就是为了解决这个问题,**提供一个标准化的接口识别方法**
139+
140+
在钱包调用智能合约时会率先,检测是否包含对应的合约接口,如果包含则调用,避免造成revert。
141+
142+
> 其实现逻辑如下:
143+
>
144+
145+
```solidity
146+
function supportsInterface(bytes4 interfaceId) public view virtual returns (bool) {
147+
return interfaceId == type(IERC165).interfaceId;
148+
}
149+
```
150+
151+
通过前四位的interfaceId进行对比,判断是否实现了所需接口。
152+
153+
## ERC165与ERC721的onERC721Received函数
154+
18155
# 2025-08-14
19156

20157
今天完成网路搭建输出了一篇问题记录的笔记,今天会继续阅读ERC20合约和Openzeppelin的相关源码,之后会仔细观看今天技术分享会的录屏。

0 commit comments

Comments
 (0)