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
| // 使用solidity0.5.0及以上版本 pragma solidity ^0.5.0;
contract FilxToken { string public name = "FILX Token"; //Optional string public symbol = "FILX"; //Optional uint256 public totalSupply;
// 注册Transfer和Approval两个交易事件 event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval( address indexed _owner, address indexed _spender, uint256 _value );
// 创建balanceOf和allowance两个键值对 mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance;
constructor(uint256 _initialSupply) public { // 令当前交易地址的余额等于_initialSupply balanceOf[msg.sender] = _initialSupply; totalSupply = _initialSupply; }
// 创建转账函数 function transfer(address _to, uint256 _value) public returns (bool success) { // 判断当前交易地址的余额是否大于等于_value,否则抛出错误"Not enough balance" require(balanceOf[msg.sender] >= _value, "Not enough balance"); // 是则当前交易地址的余额减去_value balanceOf[msg.sender] -= _value; // 转出地址的余额加_value balanceOf[_to] += _value; // 触发Transfer事件 emit Transfer(msg.sender, _to, _value); // 返回true return true; }
// 创建准许函数 function approve(address _spender, uint256 _value) public returns (bool success) { allowance[msg.sender][_spender] = _value; emit Approval(msg.sender, _spender, _value); return true; }
// 创建自定义转出账户的转账函数 function transferFrom( address _from, address _to, uint256 _value ) public returns (bool success) { // 判断当前转出账户的余额是否大于等于_value require( balanceOf[_from] >= _value, "_from does not have enough tokens" ); // 判断当前津贴转出账户的余额是否大于等于_value require( allowance[_from][msg.sender] >= _value, "Spender limit exceeded" ); balanceOf[_from] -= _value; balanceOf[_to] += _value; allowance[_from][msg.sender] -= _value; emit Transfer(_from, _to, _value); return true; } }
|