Skip to content

Commit 9aa382c

Browse files
committed
Add study notes for 2025-08-17
1 parent d1d135b commit 9aa382c

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

15998431520.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,69 @@ web2前端开发,想要转型web3,喜欢游戏、运动,目前base杭州
1515
## Notes
1616

1717
<!-- Content_START -->
18+
# 2025-08-17
19+
20+
通过了ethernaut 第二关,学习了不少 solidity 的语法,现在可以读懂简单的逻辑,下面是我学习的代码注释,结合 `help()` 提供的方法,还有自习阅读通关提示,可以寻求AI帮助(24小时的免费导师)。
21+
22+
```solidity
23+
// SPDX-License-Identifier: MIT
24+
pragma solidity ^0.8.0;
25+
26+
contract Fallback {
27+
mapping(address => uint256) public contributions;
28+
address public owner;
29+
30+
constructor() {
31+
// 默认 owner 是合约创建者
32+
owner = msg.sender;
33+
// 默认 owner 的贡献值是 1000 eth
34+
contributions[msg.sender] = 1000 * (1 ether);
35+
}
36+
37+
// 自定义 函数修饰符
38+
modifier onlyOwner() {
39+
// 如果触发者不是 owner 则抛出 "caller is not the owner"
40+
require(msg.sender == owner, "caller is not the owner");
41+
// 符合条件则继续执行
42+
// _ 代表执行原函数逻辑
43+
_;
44+
}
45+
46+
// 提交贡献值(通过 ABI 调用)
47+
// payable 函数修饰符 允许交易以太币
48+
// 需要传入一个对象 { value: 1 }, 1 代表 1 wei
49+
function contribute() public payable {
50+
require(msg.value < 0.001 ether);
51+
contributions[msg.sender] += msg.value;
52+
if (contributions[msg.sender] > contributions[owner]) {
53+
owner = msg.sender;
54+
}
55+
}
56+
57+
// 查询贡献值
58+
// view 函数修饰符 表示只读,在调用查询时,不消耗 gas (一种优化手段)
59+
function getContribution() public view returns (uint256) {
60+
return contributions[msg.sender];
61+
}
62+
63+
// public 函数修饰符 内外都可调用
64+
// onlyOwner 自定义修饰符,需参照函数实现
65+
function withdraw() public onlyOwner {
66+
payable(owner).transfer(address(this).balance);
67+
}
68+
69+
// external 函数修饰符,只可外部调用,节省 gas
70+
// payable 函数修饰符 允许交易以太币
71+
// 类似于钩子函数,外部转账时会触发
72+
receive() external payable {
73+
// require 类似于 if,满足条件继续执行
74+
require(msg.value > 0 && contributions[msg.sender] > 0);
75+
// 满足 ‘转账金额 > 0’ 且 ‘贡献值 > 0’ 则会成为 owner
76+
owner = msg.sender;
77+
}
78+
}
79+
```
80+
1881
# 2025-08-16
1982

2083
Ethernaut 第一关过了,熟悉了 `contract` 的一些方法,争取睡觉前把前三关都打过去。

0 commit comments

Comments
 (0)