链眼社区:专注于区块链安全,区块链数据分析, 区块链信息整合,区块链技术服务和区块链技术咨询。

solidity智能合约数字货币发行(上)
扫地僧
2021-08-07 00:03:01

数字货币

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

数字货币引入

如下是一段简单的代币代码。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);
   }
}

ERC20代币的缺陷

1、当为合约转移了一笔钱的时候,这笔钱就会永久的消失。因为合约不能够自己去执行转账、提钱的操作。
如下面一些ERC20代币因为误操作带来的损失。

1
2
3
4
5
6
7
8
9
10
11
12
13
QTUM, $1,204,273 lost. watch on Etherscan

EOS, $1,015,131 lost. watch on Etherscan

GNT, $249,627 lost. watch on Etherscan

STORJ, $217,477 lost. watch on Etherscan

Tronix , $201,232 lost. watch on Etherscan

DGD, $151,826 lost. watch on Etherscan

OMG, $149,941 lost. watch on Etherscan

ERC223代币引入

ERC223协议正是为了解决ERC20代币所带来的缺陷而设计的。

ERC223代币仍然可以给某一个合约转账,但是其必须要实现方法:
function tokenFallback(address _from, uint _value, bytes data)

ERC223代币还有一个重要的特征就是需要识别一个账户地址,其是外部账户还是合约账户。

内联汇编判断账户地址类型

1
2
3
4
5
6
7
8
function isContract(address _addr)  returns (bool is_contract) {
    uint length;
    assembly {

          length := extcodesize(_addr)
    }
    return (length>0);
  }

ERC223代币+ERC20代币

erc223协议有许多和erc20协议相同的方法,在下面我们将实现一个继承了ERC20、ERC223接口的代币合约

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
pragma solidity ^0.4.23;

import "./safemathlibrary.sol";

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);
}

interface ContractReceiver {
   function tokenFallback(address _from, uint _value, bytes _data);
}

interface ERC223 {
 //可以发动给合约。
 function transfer(address to, uint value, bytes data) public returns (bool ok);
 event Transfer(address indexed from, address indexed to, uint value, bytes indexed data);
}


contract Owned {
   address public owner;
   address public newOwner;

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

   constructor() 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);
       emit OwnershipTransferred(owner, newOwner);
       owner = newOwner;
       newOwner = address(0);
   }
}


contract LoveToken is ERC20Interface,ERC223,Owned {
   using SafeMath for uint;

   string public symbol;
   string public  name;
   uint8 public decimals;
   uint _totalSupply;

   mapping(address => uint) balances;

   mapping(address => mapping(address => uint)) allowed;// A --->B   5 token



   constructor() public {
      symbol="LOVE";
      name="LOVE TOKEN";
      decimals=18;
      _totalSupply = 10000 * 10 **18;
      balances[owner] = _totalSupply;
      emit Transfer(address(0),owner,_totalSupply);
   }

function isContract(address _addr)  returns (bool is_contract) {
     uint length;
     assembly {

           length := extcodesize(_addr)
     }
     return (length>0);
   }

   function totalSupply() public view returns (uint) {
      return  _totalSupply.sub(balances[address(0)]);
   }


   function balanceOf(address tokenOwner) public view returns (uint balance) {
      return balances[tokenOwner];
   }



   function transfer(address to, uint tokens) public returns (bool success) {
      balances[msg.sender] = balances[msg.sender].sub(tokens);
        balances[to] = balances[to].add(tokens);
        emit Transfer(msg.sender,to,tokens);
        return true;
   }


//转账给合约
function transfer(address to, uint value, bytes data) public returns (bool ok){

   if(isContract(to)){
        balances[msg.sender] = balances[msg.sender].sub(value);
        balances[to] = balances[to].add(value);
        ContractReceiver c =  ContractReceiver(to);
        c.tokenFallback(msg.sender,value,data);
        emit Transfer(msg.sender,to,value,data);

   }

}
   function approve(address spender, uint tokens) public returns (bool success) {
      allowed[msg.sender][spender] = tokens;

      emit Approval(msg.sender,spender,tokens);
      return true;
   }



   function transferFrom(address from, address to, uint tokens) public returns (bool success) {
      balances[from]  = balances[from].sub(tokens);
      allowed[from][msg.sender] =   allowed[from][msg.sender].sub(tokens);

       balances[to] =    balances[to].add(tokens);

      emit Transfer(from,to,tokens);
      return true;

   }


   function allowance(address tokenOwner, address spender) public view returns (uint remaining) {
     return allowed[tokenOwner][spender];
   }

}


合作伙伴