#鐵人賽 #ethereum #solidity
在開始後面的範例之前,要先來介紹「事件」,看看要如何在區塊鏈上寫下日誌,用來方便後續的監看與查詢資料。
介紹的功能有:
本日影片: https://youtu.be/EClEsFc89-A
本日合約:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract PiggyBank {
address public owner;
event Create(address owner, uint256 value);
event Receive(address indexed sender, uint256 value);
event Withdraw(address indexed owner, uint256 indexed value);
modifier onlyOwner() {
require(owner == msg.sender, "Only owner can call this function");
_;
}
constructor() payable {
owner = msg.sender;
emit Create(owner, msg.value);
}
receive() payable external {
emit Receive(msg.sender, msg.value);
}
function withdraw() onlyOwner external {
address payable Receiver = payable(msg.sender);
uint256 value = address(this).balance;
Receiver.transfer(value);
emit Withdraw(Receiver, value);
}
}
本影片提到的連結:
「Remix IDE」: https://remix.ethereum.org/
「在 2022 年,我們該如何寫智能合約」: https://ithelp.ithome.com.tw/users/20083367/ironman/5019
「那些關於 Ethereum 的事」: https://ithelp.ithome.com.tw/users/20083367/ironman/5136
「一本關於 Ethereum 與 Solidity 智能合約的書」: https://solidity.tw
「文章或主題許願池」: https://github.com/hydai/solidity-book/issues
「本系列播放清單」: https://www.youtube.com/playlist?list=PLHmOMPRfmOxQYDnXAc1hKY6ra4WDU8ZlM