一个简单的 ERC20 合约

概述

ERC20 标准接口。

样例

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
77
78
79
80
81
82
83
84
85
pragma solidity ^0.4.25;

contract ERC20 {
function totalSupply() public constant returns (uint supply); // 总供应量
function balanceOf( address who ) public constant returns (uint value); // 指定账号余额
function allowance( address owner, address spender ) public constant returns (uint _allowance); // 限额

function transfer( address to, uint value) public returns (bool ok); // 转账
function transferFrom( address from, address to, uint value) public returns (bool ok); // 从他人处转账
function approve( address spender, uint value ) public returns (bool ok); // 设置允许量值

event Transfer( address indexed from, address indexed to, uint value); // 转账事件
event Approval( address indexed owner, address indexed spender, uint value); // 允许事件
}


contract MyToken is ERC20 {

string public name; // 代币名称
string public symbol; // 代币符号
uint8 public decimals = 8; // 小数点位

address _cfo;
mapping (address => uint256) _balances;
uint256 _supply;
mapping (address => mapping (address => uint256)) _approvals;

constructor (string n, string s, uint8 d, uint256 supply) public {
name = n;
symbol = s;
decimals = d;
_cfo = msg.sender;
_supply = supply * 10 ** uint256(d);
_balances[_cfo] = _supply;
}

modifier onlyCFO() {
require(msg.sender == _cfo);
_;
}

function totalSupply() public constant returns (uint256) {
return _supply;
}

function balanceOf(address src) public constant returns (uint256) {
return _balances[src];
}

function allowance(address src, address guy) public constant returns (uint256) {
return _approvals[src][guy];
}

function transfer(address dst, uint wad) public returns (bool) {
assert(_balances[msg.sender] >= wad);

_balances[msg.sender] = _balances[msg.sender] - wad;
_balances[dst] = _balances[dst] + wad;

emit Transfer(msg.sender, dst, wad);

return true;
}

function transferFrom(address src, address dst, uint wad) public returns (bool) {
assert(_balances[src] >= wad);
assert(_approvals[src][msg.sender] >= wad);

_approvals[src][msg.sender] = _approvals[src][msg.sender] - wad;
_balances[src] = _balances[src] - wad;
_balances[dst] = _balances[dst] + wad;

emit Transfer(src, dst, wad);

return true;
}

function approve(address guy, uint256 wad) public returns (bool) {
_approvals[msg.sender][guy] = wad;

emit Approval(msg.sender, guy, wad);

return true;
}
}
您的支持将鼓励我继续创作!
0%