Skip to content

Commit 20b78f0

Browse files
committed
Add study notes for 2025-08-20
1 parent 70dd617 commit 20b78f0

1 file changed

Lines changed: 176 additions & 0 deletions

File tree

Sillyzhe.md

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

1717
<!-- Content_START -->
18+
# 2025-08-20
19+
20+
### 错误处理优化
21+
22+
**使用自定义错误替代字符串消息**:
23+
24+
```ts
25+
// 未优化: 字符串错误消息占用更多存储空间
26+
function transfer(address to, uint256 amount) external {
27+
require(balances[msg.sender] >= amount, 'Insufficient balance');
28+
// 转账逻辑
29+
}
30+
31+
// 优化: 自定义错误更高效 (Solidity 0.8.4+)
32+
error InsufficientBalance(address sender, uint256 balance, uint256 amount);
33+
34+
function transfer(address to, uint256 amount) external {
35+
if (balances[msg.sender] < amount) {
36+
revert InsufficientBalance(msg.sender, balances[msg.sender], amount);
37+
}
38+
// 转账逻辑
39+
}
40+
41+
```
42+
43+
**使用 if/revert 替代 require**:
44+
45+
```ts
46+
// 未优化: require 包含字符串,占用更多 Gas
47+
function withdraw(uint256 amount) external {
48+
require(balances[msg.sender] >= amount, 'Insufficient balance');
49+
// 提款逻辑
50+
}
51+
52+
// 优化: if/revert 组合更高效
53+
function withdraw(uint256 amount) external {
54+
if (balances[msg.sender] < amount) revert();
55+
// 提款逻辑
56+
}
57+
58+
```
59+
60+
**合并条件检查**:
61+
62+
```ts
63+
// 未优化: 多个独立条件检查
64+
function processTransaction(uint256 amount) external {
65+
require(amount > 0, 'Amount must be positive');
66+
require(amount <= maxAmount, 'Amount too large');
67+
require(balances[msg.sender] >= amount, 'Insufficient balance');
68+
69+
// 处理交易
70+
}
71+
72+
// 优化: 合并条件检查减少操作码
73+
function processTransaction(uint256 amount) external {
74+
require(amount > 0 && amount <= maxAmount && balances[msg.sender] >= amount, 'Invalid transaction');
75+
76+
// 处理交易
77+
}
78+
79+
```
80+
81+
### 事件和日志优化
82+
83+
**避免过多索引**:
84+
85+
```ts
86+
// 未优化: 过多索引参数 (每个额外索引增加约400 Gas)
87+
event Transfer(address indexed from, address indexed to, address indexed token, uint256 amount);
88+
89+
// 优化: 限制索引参数到必要字段
90+
event Transfer(
91+
address indexed from,
92+
address indexed to,
93+
address token, // 非索引
94+
uint256 amount
95+
);
96+
97+
```
98+
99+
**批量事件**:
100+
101+
```ts
102+
// 未优化: 每个操作都发出事件
103+
function batchTransfer(address[] memory recipients, uint256[] memory amounts) external {
104+
for (uint256 i = 0; i < recipients.length; i++) {
105+
balances[msg.sender] -= amounts[i];
106+
balances[recipients[i]] += amounts[i];
107+
emit Transfer(msg.sender, recipients[i], amounts[i]);
108+
}
109+
}
110+
111+
// 优化: 为整批操作发出单个事件
112+
function batchTransfer(address[] memory recipients, uint256[] memory amounts) external {
113+
uint256 totalAmount = 0;
114+
for (uint256 i = 0; i < recipients.length; i++) {
115+
balances[msg.sender] -= amounts[i];
116+
balances[recipients[i]] += amounts[i];
117+
totalAmount += amounts[i];
118+
}
119+
emit BatchTransfer(msg.sender, recipients, amounts, totalAmount);
120+
}
121+
122+
```
123+
124+
**压缩事件数据**:
125+
126+
```ts
127+
// 未优化: 包含冗余或可导出数据
128+
event ComplexEvent(
129+
address indexed user,
130+
uint256 amount,
131+
uint256 fee,
132+
uint256 total, // 冗余,可从amount和fee计算
133+
uint256 timestamp // 冗余,区块已包含时间戳
134+
);
135+
136+
// 优化: 只包含必要数据
137+
event StreamlinedEvent(address indexed user, uint256 amount, uint256 fee);
138+
139+
```
140+
141+
### 批量操作优化
142+
143+
**实现批量转账**:
144+
145+
```ts
146+
// 未优化: 单个转账,每次都需基础Gas成本
147+
function transfer(address to, uint256 amount) external {
148+
balances[msg.sender] -= amount;
149+
balances[to] += amount;
150+
emit Transfer(msg.sender, to, amount);
151+
}
152+
153+
// 优化: 批量转账分摊固定成本
154+
function batchTransfer(address[] calldata recipients, uint256[] calldata amounts) external {
155+
require(recipients.length == amounts.length, 'Length mismatch');
156+
157+
uint256 totalAmount = 0;
158+
for (uint256 i = 0; i < recipients.length; i++) {
159+
totalAmount += amounts[i];
160+
}
161+
162+
require(balances[msg.sender] >= totalAmount, 'Insufficient balance');
163+
164+
balances[msg.sender] -= totalAmount;
165+
166+
for (uint256 i = 0; i < recipients.length; i++) {
167+
balances[recipients[i]] += amounts[i];
168+
emit Transfer(msg.sender, recipients[i], amounts[i]);
169+
}
170+
}
171+
172+
```
173+
174+
**批量铸造和批量销毁**:
175+
176+
```ts
177+
// 优化: NFT批量铸造
178+
function batchMint(address to, uint256[] calldata tokenIds) external {
179+
for (uint256 i = 0; i < tokenIds.length; i++) {
180+
_mint(to, tokenIds[i]);
181+
}
182+
}
183+
184+
// 优化: 批量授权
185+
function setApprovalForMany(address operator, uint256[] calldata tokenIds, bool approved) external {
186+
for (uint256 i = 0; i < tokenIds.length; i++) {
187+
tokenApprovals[tokenIds[i]] = approved ? operator : address(0);
188+
emit Approval(ownerOf(tokenIds[i]), operator, tokenIds[i]);
189+
}
190+
}
191+
192+
```
193+
18194
# 2025-08-19
19195

20196
## 优化 Gas

0 commit comments

Comments
 (0)