武汉加油7星评价
2020-09-19 22:12:06
也许你经常看到ERC20和代币一同出现, ERC20是以太坊定义的一个代币标准。要求我们在实现代币的时候必须要遵守的协议,如指定代币名称、总量、实现代币交易函数等,只有支持了协议才能被以太坊钱包支持。其接口如下:
contract
ERC20Interface { 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 function totalSupply() public constant returns
(uint); function balanceOf(address tokenOwner) public constant returns (uint
balance); function allowance(address tokenOwner, address spender) public
constant returns (uint remaining); function transfer(address to, uint tokens)
public returns (bool success); function approve(address spender, uint tokens)
public returns (bool success); function transferFrom(address from, address to,
uint tokens) public returns (bool success); event Transfer(address indexed from,
address indexed to, uint tokens); event Approval(address indexed tokenOwner,
address indexed spender, uint tokens);}
简单说明一下:
name
: 代币名称symbol: 代币符号decimals: 代币小数点位数,代币的最小单位, 18表示我们可以拥有
.0000000000000000001单位个代币。totalSupply() : 发行代币总量。balanceOf():
查看对应账号的代币余额。transfer(): 实现代币交易,用于给用户发送代币(从我们的账户里)。transferFrom():
实现代币用户之间的交易。allowance(): 控制代币的交易,如可交易账号及资产。approve(): 允许用户可花费的代币数。
代币合约代码:
pragma
solidity ^0.4.16;interface tokenRecipient { function receiveApproval(address
_from, uint256 _value, address _token, bytes _extraData) public; }contract
TokenERC20 { string public name; string public symbol; uint8 public decimals =
18; // 18 是建议的默认值 uint256 public totalSupply; mapping (address => uint256)
public balanceOf; // mapping (address => mapping (address => uint256))
public allowance; event Transfer(address indexed from, address indexed to,
uint256 value); event Burn(address indexed from, uint256 value); function
TokenERC20(uint256 initialSupply, string tokenName, string tokenSymbol) public {
totalSupply = initialSupply * 10 ** uint256(decimals); balanceOf[msg.sender] =
totalSupply; name = tokenName; symbol = tokenSymbol; } function
_transfer(address _from, address _to, uint _value) internal { require(_to !=
0x0); require(balanceOf[_from] >= _value); require(balanceOf[_to] + _value
> balanceOf[_to]); uint previousBalances = balanceOf[_from] + balanceOf[_to];
balanceOf[_from] -= _value; balanceOf[_to] += _value; Transfer(_from, _to,
_value); assert(balanceOf[_from] + balanceOf[_to] == previousBalances); }
function transfer(address _to, uint256 _value) public { _transfer(msg.sender,
_to, _value); } function transferFrom(address _from, address _to, uint256
_value) public returns (bool success) { require(_value <=
allowance[_from][msg.sender]); // Check allowance allowance[_from][msg.sender]
-= _value; _transfer(_from, _to, _value); return true; } function
approve(address _spender, uint256 _value) public returns (bool success) {
allowance[msg.sender][_spender] = _value; return true; } function
approveAndCall(address _spender, uint256 _value, bytes _extraData) public
returns (bool success) { tokenRecipient spender = tokenRecipient(_spender); if
(approve(_spender, _value)) { spender.receiveApproval(msg.sender, _value, this,
_extraData); return true; } } function burn(uint256 _value) public returns (bool
success) { require(balanceOf[msg.sender] >= _value); balanceOf[msg.sender] -=
_value; totalSupply -= _value; Burn(msg.sender, _value); return true; } function
burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value); require(_value <=
allowance[_from][msg.sender]); balanceOf[_from] -= _value;
allowance[_from][msg.sender] -= _value; totalSupply -= _value; Burn(_from,
_value); return true; }}
在开发测试智能合约时,MetaMask和Remix Solidity IDE是两个非常好用的工具,用来完成部署。
安装和配置MetaMask请参考开发、部署第一个去中心化应用,选择了以太坊的测试网络Ropsten,如果你没有余额请点击购买buy,进入的网站可以送一些测试以太币给你,配置好之后,界面应该如下:
浏览器打开Remix Solidity IDE,复制以上源码粘贴上,在右侧选项参考如图的设置:注意Environment和Account和MetaMask保持一致,然后选择合约TokenERC20,填入你想要的发行量,名称及代号,就可以创建合约了。这时MetaMask会弹出一个交易确认框,点SUBMIT。待合约部署交易确认之后,点击交易详情如下图,复制合约地址(下图中红色框内)。打开Metamask界面,切换到TOKENS,点添加合约,出现如下对话框:填入刚刚复制的地址,点ADD,这时你就可以看到你创建的代币了,如图:你已经完成了代币的创建和部署(正式网络和测试网络部署方法一样),可以在Etherscan查询到我们刚刚部署的代币,如文章开头的图片,代币发行量为10000,单位为SB。