iT邦幫忙

2024 iThome 鐵人賽

DAY 25
0
  1. 加入Pausable(暫停功能)
    暫停功能讓合約擁有者能夠在某些情況下暫停代幣交易,可以使用一個paused狀態變量來控制是否允許代幣交易,並在transfer和其他函數中加入檢查。
bool public paused = false;

modifier whenNotPaused() {
    require(!paused, "Token transfers are paused");
    _;
}

function pause() public {
    require(msg.sender == owner, "Only the owner can pause");
    paused = true;
}

function unpause() public {
    require(msg.sender == owner, "Only the owner can unpause");
    paused = false;
}

function transfer(address _to, uint256 _value) public whenNotPaused returns (bool success) {
    require(balanceOf[msg.sender] >= _value, "Insufficient balance");
    balanceOf[msg.sender] -= _value;
    balanceOf[_to] += _value;
    emit Transfer(msg.sender, _to, _value);
    return true;
}
  1. 實現Owner更換功能
    允許合約擁有者將擁有權轉讓給其他人。
address public owner;

modifier onlyOwner() {
    require(msg.sender == owner, "Caller is not the owner");
    _;
}

function transferOwnership(address newOwner) public onlyOwner {
    require(newOwner != address(0), "New owner is the zero address");
    owner = newOwner;
}

上一篇
Day24 增發代幣、銷毀代幣
下一篇
Day26 轉移功能
系列文
Token相關的應用場景和技術領域30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言