怎么生成以太坊ERC20合约Token?
所需工具
- Chrome扩展metamask
- 以太坊合约在线编译工具 remix
示例合约
注意保存好自己的合约源代码,留待验证时使用!!!
pragma solidity ^0.5.17;
contract Token {
uint256 public totalSupply;
function balanceOf(address _owner) public view returns (uint256 balance);
function transfer(address _to, uint256 _value) public returns (bool success);
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
function approve(address _spender, uint256 _value) public returns (bool success);
function allowance(address _owner, address _spender) public view returns (uint256 remaining);
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
contract StandardToken is Token {
function transfer(address _to, uint256 _value) public returns (bool success) {
//Default assumes totalSupply can't be over max (2^256 - 1).
//If your token leaves out totalSupply and can issue more tokens as time goes on, you need to check if it doesn't wrap.
//Replace the if with this one instead.
//if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
if (balances[msg.sender] >= _value && _value > 0) {
balances[msg.sender] -= _value;
balances[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
} else { return false; }
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
//same as above. Replace this line with the following if you want to protect against wrapping uints.
//if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0) {
balances[_to] += _value;
balances[_from] -= _value;
allowed[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
} else { return false; }
}
function balanceOf(address _owner) public view returns (uint256 balance) {
return balances[_owner];
}
function approve(address _spender, uint256 _value) public returns (bool success) {
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) public view returns (uint256 remaining) {
return allowed[_owner][_spender];
}
mapping (address => uint256) balances;
mapping (address => mapping (address => uint256)) allowed;
}
contract Test is StandardToken {
string public name; //fancy name
uint8 public decimals; //How many decimals to show. ie. There could 1000 base units with 3 decimals.
string public symbol; //An identifier
string public version = "1.0"; // 0.1 standard. Just an arbitrary versioning scheme.
function TokenInit() public{
balances[msg.sender] = 1000000000000; // Give the creator all initial tokens
totalSupply = 1000000000000; // Update total supply
name = "Test"; // Set the name for display purposes
decimals = 4; // Amount of decimals for display purposes
symbol = "Test"; // Set the symbol for display purposes
}
}
修改的内容是最后一个类, 合约名称、初始量、给合约生成者的量等,注意初始量和给合约生成者的量是实际总量乘以10的精度次方。
步骤
- 安装Chrome的metamask扩展
- 生成账号并充值ETH,整个流程下来大约需要0.02~0.04个ETH
- 打开remix,新建Solidity文件,复制以上合约到编辑框中并做相应的修改
- 选择合适的编译器版本,这里用的是0.5.17,点击
compile
- 编译完成后切换到
Run
选项卡,点击Deploy
,并在metamask弹出的窗口中确认 - 合约生成完成后,调用
TokenInit
初始化 - 调用
transfer
方法,将初始化好的Token转移给自己或另一个地址。 - 结束。
Update 2020.6.8
旧版代码在使用0.5+的编译器编译时,会报如下错误
The state mutability modifier "constant" was removed in version 0.5.0. Use "view" or "pure" instead
相比旧版只兼容到0.4.12的合约代码,新代码兼容0.5.17,主要是修改了constant
修饰符为view
,以及添加public
emit
。
0.5以上的Solidity什么时候用view
或pure
?
如果是往存储里写东西,则什么标签也不用加;如果只读不写,用view
;如果和读写存储没关系,比如1+1,则用pure
。