根据ERC-20标准定义接口
pragma solidity ^0.4.0;
contract ERC20Interface {
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
function name() public view returns (string name);
function symbol() public view returns (string symbol);
function decimals() public view returns (uint8 decimals);
function totalSupply() public view returns (uint256 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);
}
代码实现一个ERC-20代币合约
pragma solidity ^0.4.0;
import "./ERC20Interface.sol";
contract ERC20 is ERC20Interface{
string private _name = "MyToken";
string private _symbol = "MYT";
uint8 private _decimals = 3;
uint256 private _totalSupply = 10 * (10 ** uint256(_decimals));
mapping(address=>uint256) private _balances;
mapping(address=>mapping(address=>uint256)) private _allowances;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
function ERC20(){
_balances[msg.sender] = _totalSupply;
}
function name() public view returns (string name){
name = _name;
}
function symbol() public view returns (string symbol){
symbol = _symbol;
}
function decimals() public view returns (uint8 decimals){
decimals = _decimals;
}
function totalSupply() public view returns (uint256 totalSupply){
totalSupply = _totalSupply;
}
function balanceOf(address _owner) public view returns (uint256 balance){
balance = _balances[_owner];
}
function transfer(address _to, uint256 _value) public returns (bool success){
require(_balances[msg.sender] >= _value);
_balances[msg.sender] -= _value;
_balances[_to] += _value;
Transfer(msg.sender, _to, _value);
success = true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success){
require(_balances[_from] >= _value);
require(_allowances[_from][msg.sender] >= _value);
_balances[_from] -= _value;
_allowances[_from][msg.sender] -= _value;
_balances[_to] += _value;
Transfer(_from, _to, _value);
success = true;
}
function approve(address _spender, uint256 _value) public returns (bool success){
_allowances[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
success = true;
}
function allowance(address _owner, address _spender) public view returns (uint256 remaining){
remaining = _allowances[_owner][_spender];
}
}
代码说明
- 代币名称为 “MyToken”
- 代币标识符为 “MYT”
- 小数位数为3
- 代币初始总供给为 10 * 1000 = 10000
- 账户余额映射
- 账户配额映射,这里是两层映射,例如账户A授信给账户B额度为value的配额
- 代币转移事件,事件内容为:将账户
_from
的代币转移到账户_to
中,金额为_value
。indexed
关键字是对事件的字段做索引,具体用途后面会说。 - 配额许可事件,事件内容为:拥有者
_owner
许可给_spender
配额,额度为_value
- 构造方法,将代币的初始总供给都分配给合约的部署账户。合约的构造方法只在合约部署时执行一次。
- 公共方法(合约外部可访问),返回合约名称。
view
关键子表示函数只查询状态变量,而不写入。函数的view
关键字与constant
等价。 - 公共方法,返回合约标识符。
- 公共方法,返回合约小数位。
- 公共方法,返回合约总供给额。
- 公共方法,查询账户
_owner
的账户余额。 - 公共方法,将合约调用者的代币转移到账户
_to
中,金额为_value
,转移成功返回true。 - 验证合约调用者(msg.sender)账户余额是否大于等于
_value
- 合约调用者账户余额减去
_value
- 接收账户
_to
账户余额额加上_value
- 发送Transfer()事件
- 返回转移成功(true)
- 公共方法,将账户
_from
的代币转移到账户_to
中,金额为_value
,转移成功返回true - 验证账户
_from
的代币余额是否大于等于_value
- 验证账户
_from
给合约调用者msg.sender
的配额是否大于等于_value
- 账户
_from
的账户余额减去_value
- 账户
_from
给合约调用者(msg.sender)的配额减去_value
- 接收账户
_to
账户余额额加上_value
- 发送Transfer()事件
- 返回转移成功
true
- 公共方法,合约调用者批准给账户
_spender
配额,额度为_value
,批准成功返回true - 合约调用者
msg.sender
对账户_spender
的配额等于_value
- 发送Approval()事件
- 返回批准成功
true
- 公共方法,查询账户
_owner
批准给账户_spender
的配额 - 返回账户
_owner
批准给账户_spender
的配额
参考文档
https://github.com/ConsenSys/Tokens/blob/master/contracts/eip20/EIP20Interface.sol
https://github.com/ConsenSys/Tokens/blob/master/contracts/eip20/EIP20.sol
网友评论