1
+ pragma solidity ^ 0.4.24 ;
2
+
3
+ import "./IERC20.sol " ;
4
+ import "../SafeMath/SafeMath.sol " ;
5
+
6
+ /**
7
+ * @title Standard ERC20 token
8
+ *
9
+ * @dev Implementation of the basic standard token.
10
+ * https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
11
+ * Originally based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
12
+ */
13
+ contract ERC20 is IERC20 {
14
+ using SafeMath for uint256 ;
15
+
16
+ mapping (address => uint256 ) private _balances;
17
+
18
+ mapping (address => mapping (address => uint256 )) private _allowed;
19
+
20
+ uint256 private _totalSupply;
21
+
22
+ /**
23
+ * @dev Total number of tokens in existence
24
+ */
25
+ function totalSupply () public view returns (uint256 ) {
26
+ return _totalSupply;
27
+ }
28
+
29
+ /**
30
+ * @dev Gets the balance of the specified address.
31
+ * @param owner The address to query the balance of.
32
+ * @return An uint256 representing the amount owned by the passed address.
33
+ */
34
+ function balanceOf (address owner ) public view returns (uint256 ) {
35
+ return _balances[owner];
36
+ }
37
+
38
+ /**
39
+ * @dev Function to check the amount of tokens that an owner allowed to a spender.
40
+ * @param owner address The address which owns the funds.
41
+ * @param spender address The address which will spend the funds.
42
+ * @return A uint256 specifying the amount of tokens still available for the spender.
43
+ */
44
+ function allowance (
45
+ address owner ,
46
+ address spender
47
+ )
48
+ public
49
+ view
50
+ returns (uint256 )
51
+ {
52
+ return _allowed[owner][spender];
53
+ }
54
+
55
+ /**
56
+ * @dev Transfer token for a specified address
57
+ * @param to The address to transfer to.
58
+ * @param value The amount to be transferred.
59
+ */
60
+ function transfer (address to , uint256 value ) public returns (bool ) {
61
+ require (value <= _balances[msg .sender ]);
62
+ require (to != address (0 ));
63
+
64
+ _balances[msg .sender ] = _balances[msg .sender ].sub (value);
65
+ _balances[to] = _balances[to].add (value);
66
+ emit Transfer (msg .sender , to, value);
67
+ return true ;
68
+ }
69
+
70
+ /**
71
+ * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
72
+ * Beware that changing an allowance with this method brings the risk that someone may use both the old
73
+ * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
74
+ * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
75
+ * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
76
+ * @param spender The address which will spend the funds.
77
+ * @param value The amount of tokens to be spent.
78
+ */
79
+ function approve (address spender , uint256 value ) public returns (bool ) {
80
+ require (spender != address (0 ));
81
+
82
+ _allowed[msg .sender ][spender] = value;
83
+ emit Approval (msg .sender , spender, value);
84
+ return true ;
85
+ }
86
+
87
+ /**
88
+ * @dev Transfer tokens from one address to another
89
+ * @param from address The address which you want to send tokens from
90
+ * @param to address The address which you want to transfer to
91
+ * @param value uint256 the amount of tokens to be transferred
92
+ */
93
+ function transferFrom (
94
+ address from ,
95
+ address to ,
96
+ uint256 value
97
+ )
98
+ public
99
+ returns (bool )
100
+ {
101
+ require (value <= _balances[from]);
102
+ require (value <= _allowed[from][msg .sender ]);
103
+ require (to != address (0 ));
104
+
105
+ _balances[from] = _balances[from].sub (value);
106
+ _balances[to] = _balances[to].add (value);
107
+ _allowed[from][msg .sender ] = _allowed[from][msg .sender ].sub (value);
108
+ emit Transfer (from, to, value);
109
+ return true ;
110
+ }
111
+
112
+ /**
113
+ * @dev Increase the amount of tokens that an owner allowed to a spender.
114
+ * approve should be called when allowed_[_spender] == 0. To increment
115
+ * allowed value is better to use this function to avoid 2 calls (and wait until
116
+ * the first transaction is mined)
117
+ * From MonolithDAO Token.sol
118
+ * @param spender The address which will spend the funds.
119
+ * @param addedValue The amount of tokens to increase the allowance by.
120
+ */
121
+ function increaseAllowance (
122
+ address spender ,
123
+ uint256 addedValue
124
+ )
125
+ public
126
+ returns (bool )
127
+ {
128
+ require (spender != address (0 ));
129
+
130
+ _allowed[msg .sender ][spender] = (
131
+ _allowed[msg .sender ][spender].add (addedValue));
132
+ emit Approval (msg .sender , spender, _allowed[msg .sender ][spender]);
133
+ return true ;
134
+ }
135
+
136
+ /**
137
+ * @dev Decrease the amount of tokens that an owner allowed to a spender.
138
+ * approve should be called when allowed_[_spender] == 0. To decrement
139
+ * allowed value is better to use this function to avoid 2 calls (and wait until
140
+ * the first transaction is mined)
141
+ * From MonolithDAO Token.sol
142
+ * @param spender The address which will spend the funds.
143
+ * @param subtractedValue The amount of tokens to decrease the allowance by.
144
+ */
145
+ function decreaseAllowance (
146
+ address spender ,
147
+ uint256 subtractedValue
148
+ )
149
+ public
150
+ returns (bool )
151
+ {
152
+ require (spender != address (0 ));
153
+
154
+ _allowed[msg .sender ][spender] = (
155
+ _allowed[msg .sender ][spender].sub (subtractedValue));
156
+ emit Approval (msg .sender , spender, _allowed[msg .sender ][spender]);
157
+ return true ;
158
+ }
159
+
160
+ /**
161
+ * @dev Internal function that mints an amount of the token and assigns it to
162
+ * an account. This encapsulates the modification of balances such that the
163
+ * proper events are emitted.
164
+ * @param account The account that will receive the created tokens.
165
+ * @param amount The amount that will be created.
166
+ */
167
+ function _mint (address account , uint256 amount ) internal {
168
+ require (account != 0 );
169
+ _totalSupply = _totalSupply.add (amount);
170
+ _balances[account] = _balances[account].add (amount);
171
+ emit Transfer (address (0 ), account, amount);
172
+ }
173
+
174
+ /**
175
+ * @dev Internal function that burns an amount of the token of a given
176
+ * account.
177
+ * @param account The account whose tokens will be burnt.
178
+ * @param amount The amount that will be burnt.
179
+ */
180
+ function _burn (address account , uint256 amount ) internal {
181
+ require (account != 0 );
182
+ require (amount <= _balances[account]);
183
+
184
+ _totalSupply = _totalSupply.sub (amount);
185
+ _balances[account] = _balances[account].sub (amount);
186
+ emit Transfer (account, address (0 ), amount);
187
+ }
188
+
189
+ /**
190
+ * @dev Internal function that burns an amount of the token of a given
191
+ * account, deducting from the sender's allowance for said account. Uses the
192
+ * internal burn function.
193
+ * @param account The account whose tokens will be burnt.
194
+ * @param amount The amount that will be burnt.
195
+ */
196
+ function _burnFrom (address account , uint256 amount ) internal {
197
+ require (amount <= _allowed[account][msg .sender ]);
198
+
199
+ // Should https://github.com/OpenZeppelin/zeppelin-solidity/issues/707 be accepted,
200
+ // this function needs to emit an event with the updated approval.
201
+ _allowed[account][msg .sender ] = _allowed[account][msg .sender ].sub (
202
+ amount);
203
+ _burn (account, amount);
204
+ }
205
+ }
0 commit comments