温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

solidity智能合约[55]-token

发布时间:2020-07-02 11:51:29 来源:网络 阅读:504 作者:jonson_jackson 栏目:开发技术

数字货币

货币是用来交换、偿还债务的媒介。古代货币为金、银、贝壳等实物。现代中央银行发行的纸币等。相对于古代的一般等价物而言
现在的货币本质上是由政府信用的背书。其本身并没有价值。 同理、比特币与以太币本身并没有价值,但是依托于区块链网络的特性,使得其拥有货币的完美属性。包括:
·便携
·耐用
·可分割
·可识别
·可替代
·稀缺且难以仿冒

数字货币引入

如下是一段简单的代币代码。balanceOf映射表存储金额。构造函数,定义了初始化发行与管理者。 transfer函数定义了转账操作。完成了货币存储与转移的功能。 本质上,此货币就是存储在balanceOf映射表中的数字。数字本身并没有价值,只有当你认为他有价值的时候,它才会有价值。所以,数字货币的背后,常常是对应与一定的商业价值。例如公司的股权等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pragma solidity ^0.4.23;


contract tokenDemo{
   mapping(address=>uint) public balanceOf;
   address owner;

   constructor(uint initSupply) public {
       balanceOf[msg.sender] = initSupply;
       owner = msg.sender;
   }

   function transfer(address _to,uint _value) public {
       balanceOf[owner] -= _value;
        balanceOf[_to] += _value;
   }
}

一般代币合约的缺陷

如上面写好的简单代币demo,有三种缺陷。
1、没有权限的控制,任何人都可以调用transfer进行转账
2、没有防止溢出***
3、功能有限
4、没有统一的规范。

想象一下,如果每一个人或企业都写一个属于自己的代币合约。每一个合约都有自己的查询资金的函数名。都有自己的转账的函数名。那么每一次当我们需要用到其他人的代币,都需要查询,此代币合约中,每一个函数的功能。大大降低了效率。
这就为我们引出了ERC20代币。

ERC20协议

ERC是以太坊征求意见( Ethereum Request for Comment-20)的缩写. 20代表它的序号。其规范了代币合约,也就意味着,一旦合约满足了ERC20代币的规范,那么其必然有规范的函数标准。如下,就是ERC20代币协议规定的不同的函数及其功能。ERC20协议只是定义了函数的定义与功能,需要代币设计者自定义的实现函数功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
interface ERC20Interface {
    //总发行数量
   function totalSupply()  external returns (uint);
   //查询数量
   function balanceOf(address tokenOwner)  external returns (uint balance);
   //查询授权数量
   function allowance(address tokenOwner, address spender)  external returns (uint remaining);
    //转账
   function transfer(address to, uint tokens) external  returns (bool success);
   //授权
   function approve(address spender, uint tokens) external returns (bool success);
   //授权转账
   function transferFrom(address from, address to, uint tokens) external returns (bool success);

   event Transfer(address indexed from, address indexed to, uint tokens);
   event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
pragma solidity ^0.4.18;

// ----------------------------------------------------------------------------
// '' token contract
//
// 部署地址 :
// 标志      : LOVE
// 名字    :  LOVE TOKEN
// 总供应量 100000000000000000000000000
// 精度    : 18

// ---------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// SafeMath安全库
// ----------------------------------------------------------------------------
contract SafeMath {
   function safeAdd(uint a, uint b) public pure returns (uint c) {
       c = a + b;
       require(c >= a);
   }
   function safeSub(uint a, uint b) public pure returns (uint c) {
       require(b <= a);
       c = a - b;
   }
   function safeMul(uint a, uint b) public pure returns (uint c) {
       c = a * b;
       require(a == 0 || c / a == b);
   }
   function safeDiv(uint a, uint b) public pure returns (uint c) {
       require(b > 0);
       c = a / b;
   }
}


// ----------------------------------------------------------------------------
// ERC20 代币标准
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// ----------------------------------------------------------------------------
contract ERC20Interface {
   //总发行数量
   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);
}


// ----------------------------------------------------------------------------
// 所有者合约
// ----------------------------------------------------------------------------
contract Owned {
   address public owner;
   address public newOwner;

   event OwnershipTransferred(address indexed _from, address indexed _to);

   function Owned() public {
       owner = msg.sender;
   }

   modifier onlyOwner {
       require(msg.sender == owner);
       _;
   }

   function transferOwnership(address _newOwner) public onlyOwner {
       newOwner = _newOwner;
   }
   function acceptOwnership() public {
       require(msg.sender == newOwner);
       OwnershipTransferred(owner, newOwner);
       owner = newOwner;
       newOwner = address(0);
   }
}


// ----------------------------------------------------------------------------
// ERC20代币,增加标志、名字、精度
// 代币转移
// ----------------------------------------------------------------------------
contract LOVEToken is ERC20Interface, Owned, SafeMath {
   string public symbol;
   string public  name;
   uint8 public decimals;
   uint public _totalSupply;

   mapping(address => uint) balances;
   mapping(address => mapping(address => uint)) allowed;


   // ------------------------------------------------------------------------
   // 构造函数
   // ------------------------------------------------------------------------
   function LOVEToken() public {
       symbol = "LOVER";
       name = "LOVER Token";
       decimals = 18;
       _totalSupply = 100000000000000000000000000;

       balances[0x6AFe57C1F589C4744ab9FF4ac8899080695a6f5e] = _totalSupply;

       Transfer(address(0), 0x6AFe57C1F589C4744ab9FF4ac8899080695a6f5e, _totalSupply);
   }


   // ------------------------------------------------------------------------
   // 总供应量
   // ------------------------------------------------------------------------
   function totalSupply() public constant returns (uint) {
       return _totalSupply  - balances[address(0)];
   }


   // ------------------------------------------------------------------------
   // 得到资金的数量
   // ------------------------------------------------------------------------
   function balanceOf(address tokenOwner) public constant returns (uint balance) {
       return balances[tokenOwner];
   }


   // ------------------------------------------------------------------------
   // 转账从代币拥有者的账户到其他账户
   // - 所有者的账户必须有充足的资金去转账
   // - 0值的转账也是被允许的
   // ------------------------------------------------------------------------
   function transfer(address to, uint tokens) public returns (bool success) {
       balances[msg.sender] = safeSub(balances[msg.sender], tokens);
       balances[to] = safeAdd(balances[to], tokens);
       Transfer(msg.sender, to, tokens);
       return true;
   }


   // ------------------------------------------------------------------------
   // 授权
   // ------------------------------------------------------------------------
   function approve(address spender, uint tokens) public returns (bool success) {
       allowed[msg.sender][spender] = tokens;
       Approval(msg.sender, spender, tokens);
       return true;
   }


   // ------------------------------------------------------------------------
   // 和approve连接在一起
   //
   // The calling account must already have sufficient tokens approve(...)-d
   // for spending from the from account and
   // - From account must have sufficient balance to transfer
   // - Spender must have sufficient allowance to transfer
   // - 0 value transfers are allowed
   // ------------------------------------------------------------------------
   function transferFrom(address from, address to, uint tokens) public returns (bool success) {
       balances[from] = safeSub(balances[from], tokens);
       allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
       balances[to] = safeAdd(balances[to], tokens);
       Transfer(from, to, tokens);
       return true;
   }


   // ------------------------------------------------------------------------
   // 返回授权数量
   // ------------------------------------------------------------------------
   function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
       return allowed[tokenOwner][spender];
   }


   // ------------------------------------------------------------------------
   // 合约不接受以太币
   // ------------------------------------------------------------------------
   function () public payable {
       revert();
   }


      // ------------------------------------------------------------------------
     // Owner can transfer out any accidentally sent ERC20 tokens
     //所有者能够转移任何ERC20代币的接口
    // ------------------------------------------------------------------------
   function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {
       return ERC20Interface(tokenAddress).transfer(owner, tokens);
   }
}
  • 本文链接: https://dreamerjonson.com/2018/11/26/solidity-55-token/

  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

solidity智能合约[55]-token


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI