string public constant name = "Token Name"; string public constant symbol = "SYM"; uint8 public constant decimals = 18; // 18 is the most common number of decimal places
functiontotalSupply() public constant returns (uint); functionbalanceOf(address tokenOwner) public constant returns (uint balance); functionallowance(address tokenOwner, address spender) public constant returns (uint remaining); functiontransfer(address to, uint tokens) public returns (bool success); functionapprove(address spender, uint tokens) public returns (bool success); functiontransferFrom(address from, address to, uint tokens) public returns (bool success);
//noinspection ALL contract TokenERC20 { string public name; string public symbol; uint8 public decimals = 18; // decimals 可以有的小数点个数,最小的代币单位。18 是建议的默认值 uint256 public totalSupply;
// 用mapping保存每个地址对应的余额 mapping (address => uint256) public balanceOf; // 存储对账号的控制 mapping (address =>mapping (address => uint256)) public allowance;
// 以下用来检查交易, uint previousBalances = balanceOf[_from] + balanceOf[_to]; // Subtract from the sender balanceOf[_from] -= _value; // Add the same to the recipient balanceOf[_to] += _value; Transfer(_from, _to, _value);
/** * 销毁我(创建交易者)账户中指定个代币 */ functionburn(uint256 _value) public returns (bool success) { require(balanceOf[msg.sender] >= _value); // Check if the sender has enough balanceOf[msg.sender] -= _value; // Subtract from the sender totalSupply -= _value; // Updates totalSupply Burn(msg.sender, _value); returntrue; }
/** * 销毁用户账户中指定个代币 * * Remove `_value` tokens from the system irreversibly on behalf of `_from`. * * @param _from the address of the sender * @param _value the amount of money to burn */ functionburnFrom(address _from, uint256 _value) public returns (bool success) { require(balanceOf[_from] >= _value); // Check if the targeted balance is enough require(_value <= allowance[_from][msg.sender]); // Check allowance balanceOf[_from] -= _value; // Subtract from the targeted balance allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance totalSupply -= _value; // Update totalSupply Burn(_from, _value); returntrue; } }
部署代币合约
部署代币合约有两种方式,一种是使用客户端的钱包,一种是使用 Remix + MetaMask 钱包,我们今天采用后者, 如果你还没有安装 MetaMask 钱包。请先到 Chrome 浏览器插件中心去安装, 点击 这里 直接去到 钱包安装页面。安装完成之后选择 Ropsten Test Net 测试网络。
账户:和比特币,以太坊账户体系不同的是,EOS 账户是由两个公钥生成,分别代表 Owner 权限和 Active 权限。这也是 EOS 账户能够实现复杂的权限控制的原因 (这个我们后面会详细讲解 EOS 账户的权限模型)。需要注意的是,EOS 账户和钱包没有从属关系,它们是平行的,各司其职,账户用来转账,发布和调用合约, 而钱包知识用来存储账户的私钥而已。钱包是存储在本地节点的,而账户是存储在区块链上的。