-
Notifications
You must be signed in to change notification settings - Fork 8
/
ERC223Token.sol
237 lines (196 loc) · 6.64 KB
/
ERC223Token.sol
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.4;
// https://www.ethereum.org/token
interface tokenRecipient {
function receiveApproval( address from, uint256 value, bytes calldata data )
external;
}
// ERC223
interface ContractReceiver {
function tokenFallback( address from, uint value, bytes calldata data )
external;
}
// ERC20 token with added ERC223 and Ethereum-Token support
//
// Blend of multiple interfaces:
// - https://theethereum.wiki/w/index.php/ERC20_Token_Standard
// - https://www.ethereum.org/token (uncontrolled, non-standard)
// - https://github.com/Dexaran/ERC23-tokens/blob/Recommended/ERC223_Token.sol
contract ERC223Token
{
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;
mapping( address => uint256 ) balances_;
mapping( address => mapping(address => uint256) ) allowances_;
// ERC20
event Approval( address indexed owner,
address indexed spender,
uint value );
event Transfer( address indexed from,
address indexed to,
uint256 value );
// bytes data ); use ERC20 version instead
// Ethereum Token
event Burn( address indexed from, uint256 value );
constructor( uint256 initialSupply,
string memory tokenName,
uint8 decimalUnits,
string memory tokenSymbol )
{
totalSupply = initialSupply * 10 ** uint256(decimalUnits);
balances_[msg.sender] = totalSupply;
name = tokenName;
decimals = decimalUnits;
symbol = tokenSymbol;
emit Transfer( address(0), msg.sender, totalSupply );
}
receive() external payable { revert("does not accept eth"); }
fallback() external payable { revert("calldata does not match a function"); }
// ERC20
function balanceOf( address owner ) public view returns (uint) {
return balances_[owner];
}
// ERC20
//
// WARNING! When changing the approval amount, first set it back to zero
// AND wait until the transaction is mined. Only afterwards set the new
// amount. Otherwise you may be prone to a race condition attack.
// See: https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
function approve( address spender, uint256 value ) public
returns (bool success)
{
allowances_[msg.sender][spender] = value;
emit Approval( msg.sender, spender, value );
return true;
}
// recommended fix for known attack on any ERC20
function safeApprove( address _spender,
uint256 _currentValue,
uint256 _value ) public
returns (bool success) {
// If current allowance for _spender is equal to _currentValue, then
// overwrite it with _value and return true, otherwise return false.
if (allowances_[msg.sender][_spender] == _currentValue)
return approve(_spender, _value);
return false;
}
// ERC20
function allowance( address owner, address spender ) public view
returns (uint256 remaining)
{
return allowances_[owner][spender];
}
// ERC20
function transfer(address to, uint256 value) public returns (bool success)
{
bytes memory empty; // null
_transfer( msg.sender, to, value, empty );
return true;
}
// ERC20
function transferFrom( address from, address to, uint256 value ) public
returns (bool success)
{
require( value <= allowances_[from][msg.sender] );
allowances_[from][msg.sender] -= value;
bytes memory empty;
_transfer( from, to, value, empty );
return true;
}
// Ethereum Token
function approveAndCall( address spender,
uint256 value,
bytes calldata context ) public
returns (bool success)
{
if ( approve(spender, value) )
{
tokenRecipient recip = tokenRecipient( spender );
recip.receiveApproval( msg.sender, value, context );
return true;
}
return false;
}
// Ethereum Token
function burn( uint256 value ) public
returns (bool success)
{
require( balances_[msg.sender] >= value );
balances_[msg.sender] -= value;
totalSupply -= value;
emit Burn( msg.sender, value );
return true;
}
// Ethereum Token
function burnFrom( address from, uint256 value ) public
returns (bool success)
{
require( balances_[from] >= value );
require( value <= allowances_[from][msg.sender] );
balances_[from] -= value;
allowances_[from][msg.sender] -= value;
totalSupply -= value;
emit Burn( from, value );
return true;
}
// ERC223 Transfer and invoke specified callback
function transfer( address to,
uint value,
bytes calldata data,
string calldata custom_fallback )
public returns (bool success)
{
_transfer( msg.sender, to, value, data );
ContractReceiver rx = ContractReceiver( to );
// https://docs.soliditylang.org/en/v0.5.1/050-breaking-changes.html#semantic-and-syntactic-changes
(bool resok, bytes memory resdata) =
address(rx).call( abi.encodeWithSignature(custom_fallback,
msg.sender, value, data) );
require( resok, "custom fallback failed" );
if (resdata.length > 0) {} // suppress warning
return true;
}
// ERC223 Transfer to a contract or externally-owned account
function transfer( address to, uint value, bytes calldata data ) public
returns (bool success)
{
if (isContract(to)) {
return transferToContract( to, value, data );
}
_transfer( msg.sender, to, value, data );
return true;
}
// ERC223 Transfer to contract and invoke tokenFallback() method
function transferToContract( address to, uint value, bytes calldata data )
private returns (bool success)
{
_transfer( msg.sender, to, value, data );
ContractReceiver rx = ContractReceiver(to);
rx.tokenFallback(msg.sender, value, data);
return true;
}
// ERC223 fetch contract size (must be nonzero to be a contract)
function isContract( address _addr ) private view returns (bool)
{
uint length;
assembly { length := extcodesize(_addr) }
return (length > 0);
}
function _transfer( address from,
address to,
uint value,
bytes memory data ) internal
{
require( to != address(0x0) );
require( balances_[from] >= value );
require( balances_[to] + value > balances_[to] ); // catch overflow
balances_[from] -= value;
balances_[to] += value;
//Transfer( from, to, value, data ); ERC223-compat version
bytes memory empty;
empty = data;
emit Transfer( from, to, value ); // ERC20-compat version
}
}