1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| ### 六、2 完整实现
功能完整代币: contract MyToken { string public name; string public symbol; uint8 public decimals; address public owner; bool public paused; mapping(address => bool) public blacklisted; event Mint(address indexed to, uint256 amount); event Burn(address indexed from, uint256 amount); event Paused(address account); event Unpaused(address account); modifier onlyOwner() { require(msg.sender == owner, "Not owner"); _; } modifier whenNotPaused() { require(!paused, "Token is paused"); _; } modifier notBlacklisted(address account) { require(!blacklisted[account], "Account is blacklisted"); _; } constructor( string memory _name, string memory _symbol, uint8 _decimals, uint256 _initialSupply ) { name = _name; symbol = _symbol; decimals = _decimals; owner = msg.sender; } } // 转账函数:包含暂停和黑名单检查 function transfer(address to, uint256 amount) public whenNotPaused notBlacklisted(msg.sender) notBlacklisted(to) returns (bool) { } function approve(address spender, uint256 amount) { } } function transferFrom(address from, address to, uint256 amount) notBlacklisted(from) { } // 增发代币:只有owner可以调用 function mint(address to, uint256 amount) public onlyOwner { totalSupply += amount; emit Mint(to, amount); emit Transfer(address(0), to, amount); } // 销毁代币:用户销毁自己的代币 function burn(uint256 amount) public { totalSupply -= amount; emit Burn(msg.sender, amount); emit Transfer(msg.sender, address(0), amount); } // 暂停代币:只有owner可以调用 function pause() public onlyOwner { paused = true; emit Paused(msg.sender); } // 恢复代币:只有owner可以调用 function unpause() public onlyOwner { paused = false; emit Unpaused(msg.sender); } // 加入黑名单:只有owner可以调用 function addToBlacklist(address account) public onlyOwner { blacklisted[account] = true; } // 移除黑名单:只有owner可以调用 function removeFromBlacklist(address account) public onlyOwner { blacklisted[account] = false; }
|