當一個 ERC20 的合約出了意外,或者是即將要進行主網映射,需要做暫停交易的時候應該怎麼辦呢?
今天就讓我們來探討這個 Pausable 的機制怎麼實作吧!
本日合約:
pragma solidity ^0.4.25;
contract Pausable {
bool private _paused;
constructor() public {
_paused = false;
}
modifier whenPaused() {
require(_paused);
_;
}
modifier whenNotPaused() {
require(!_paused);
_;
}
event Pause(address addr);
event Unpause(address addr);
function pause() public whenNotPaused returns (bool) {
_paused = true;
emit Pause(msg.sender);
return true;
}
function unpause() public whenPaused returns (bool) {
_paused = false;
emit Unpause(msg.sender);
return true;
}
function transfer() public whenNotPaused returns (bool) {
//...
}
}
本日影片:
https://youtu.be/KWlnAyn7uLY
Smart Contract 實戰教學播放清單:
https://www.youtube.com/playlist?list=PLHmOMPRfmOxSJcrlwyandWYiuP9ZAMYoF