Web3 Study Log 003
2025-7-5
这几天各种各样的琐事,处理完了,真的烦,估计能消停一段时间了…
今天终于能够坐下来好好学习,今天学习了chainlink的使用,能够获取 ETH/USD 实时价格,然后写了一个简单的众筹项目,用到现在学习到所有知识,智能合约涉及到钱的地方,确实要谨慎谨慎再谨慎,今天一个提款条件写错了,钱筹集完整之后,提不出来…幸好只是Testnet。
明天准备继续写一个项目,DeFi 预售合约TokenPresale,说项目可能太夸张了,就是一道综合练习题,巩固下目前所学习的知识。背景为:一个新项目要发币,在正式上线前对早期支持者进行预售。规则如下:
Web3 Study Log 003
2025-07-05
been dealing with a bunch of random life stuff lately — finally cleared up
man it was annoying lol… but looks like i’ll finally get some peace for a while
today i finally sat down to actually study
learned how to use Chainlink to fetch real-time ETH/USD price
then built a simple crowdfunding contract that brings together everything i’ve learned so far
any time a smart contract handles funds… you really gotta be careful
made a mistake in the withdraw condition — funds hit the goal but couldn’t be withdrawn lol
good thing it was just on testnet
tomorrow i’m planning to build another contract — a TokenPresale for a fake DeFi project
not really a full project tbh, more like a comprehensive practice exercise
goal is just to solidify what i’ve learned so far
here’s the idea:
通过构建一个具备以下功能的智能合约,系统掌握 Solidity 中的重要语法与实际开发场景:
AggregatorV3Interface priceFeed = AggregatorV3Interface(address);
(, int answer,,,) = priceFeed.latestRoundData();
uint256 usd = (ethPrice * ethAmountInWei) / 1e18;
require(!goalReached, "Goal already reached");
modifier onlyOwner {
if (msg.sender != i_owner) revert NotOwner();
_;
}
modifier fundSuccessOrTimeout {
if (!(block.timestamp >= i_deadline || getTotalRaisedInUsd() >= i_goal)) {
revert NotFinish();
}
_;
}
方法 | gas 限制 | 返回值 | 推荐程度 |
---|---|---|---|
transfer | 固定 2300 gas | 无返回值 | 不推荐 |
send | 固定 2300 gas | 返回 bool | 不推荐 |
call | 可设定 gas / value / calldata | 返回 (bool, bytes) | 推荐 ✅ |
常见调用方式:
(bool success, ) = payable(msg.sender).call{value: amount}("");
require(success, "Call failed");
block.timestamp >= i_deadline || getTotalRaisedInUsd() >= i_goal
将 ETH 转 USD 的逻辑封装为 library PriceConverter,用法如下:
using PriceConverter for uint256;
totalAmount.getLatestETHPriceInUSD();
注意事项:
if (msg.data.length == 0) {
// receive() triggered
} else {
// fallback() triggered
}
口诀总结:
转账没数据:走 receive()
其他情况:走 fallback()
想看有没有数据:看 msg.data.length