-
Notifications
You must be signed in to change notification settings - Fork 21
/
ERC20.sol
235 lines (195 loc) · 7.88 KB
/
ERC20.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
//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.4;
import './interfaces/IERC20.sol';
import "./ownership/Ownable.sol";
import "./lifecycle/Pausable.sol";
/**
* @title ERC20 Token
* @dev Implementation of the basic ERC-20 standard token with burn and mint functions.
*/
contract ERC20 is IERC20, Ownable, Pausable {
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;
mapping (address => uint256) internal _balances;
mapping (address => mapping (address => uint256)) internal _allowed;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Constructor that gives msg.sender all of existing tokens.
* @param _name The name of the token.
* @param _symbol The symbol of the token.
* @param _decimals The number of decimals of the token.
* @param _totalSupply The total supply of the token.
*/
constructor (
string memory _name,
string memory _symbol,
uint8 _decimals,
uint256 _totalSupply
) {
symbol = _symbol;
name = _name;
decimals = _decimals;
totalSupply = _totalSupply;
_balances[msg.sender] = _totalSupply;
}
/**
* @dev Transfer token for a specified address.
* @param _to The address to transfer to.
* @param _value The amount to be transferred.
* @return A boolean that indicates if the operation was successful.
*/
function transfer(
address _to,
uint256 _value
) external override whenNotPaused returns (bool) {
require(_to != address(0), 'ERC20: to address is not valid');
require(_value <= _balances[msg.sender], 'ERC20: insufficient balance');
_balances[msg.sender] = _balances[msg.sender] - _value;
_balances[_to] = _balances[_to] + _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
/**
* @dev Gets the balance of the specified address.
* @param _owner The address to query the balance of.
* @return balance An uint256 representing the balance
*/
function balanceOf(
address _owner
) external override view returns (uint256 balance) {
return _balances[_owner];
}
/**
* @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
* @param _spender The address which will spend the funds.
* @param _value The amount of tokens to be spent.
* @return A boolean that indicates if the operation was successful.
*/
function approve(
address _spender,
uint256 _value
) external override whenNotPaused returns (bool) {
_allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
/**
* @dev Transfer tokens from one address to another.
* @param _from The address which you want to send tokens from.
* @param _to The address which you want to transfer to.
* @param _value The amount of tokens to be transferred.
* @return A boolean that indicates if the operation was successful
*/
function transferFrom(
address _from,
address _to,
uint256 _value
) external override whenNotPaused returns (bool) {
require(_from != address(0), 'ERC20: from address is not valid');
require(_to != address(0), 'ERC20: to address is not valid');
require(_value <= _balances[_from], 'ERC20: insufficient balance');
require(_value <= _allowed[_from][msg.sender], 'ERC20: transfer from value not allowed');
_allowed[_from][msg.sender] = _allowed[_from][msg.sender] - _value;
_balances[_from] = _balances[_from] - _value;
_balances[_to] = _balances[_to] + _value;
emit Transfer(_from, _to, _value);
return true;
}
/**
* @dev Returns the amount of tokens approved by the owner that can be transferred to the spender's account.
* @param _owner The address of the owner of the tokens.
* @param _spender The address of the spender.
* @return The number of tokens approved.
*/
function allowance(
address _owner,
address _spender
) external override view whenNotPaused returns (uint256) {
return _allowed[_owner][_spender];
}
/**
* @dev Increases the amount of tokens that an owner has allowed to a spender.
* @param _spender The address of the spender.
* @param _addedValue The amount of tokens to increase the allowance by.
* @return A boolean value indicating whether the operation succeeded.
*/
function increaseApproval(
address _spender,
uint256 _addedValue
) external whenNotPaused returns (bool) {
_allowed[msg.sender][_spender] = _allowed[msg.sender][_spender] + _addedValue;
emit Approval(msg.sender, _spender, _allowed[msg.sender][_spender]);
return true;
}
/**
* @dev Decreases the amount of tokens that an owner has allowed to a spender.
* @param _spender The address of the spender.
* @param _subtractedValue The amount of tokens to decrease the allowance by.
* @return A boolean value indicating whether the operation succeeded.
*/
function decreaseApproval(
address _spender,
uint256 _subtractedValue
) external whenNotPaused returns (bool) {
uint256 oldValue = _allowed[msg.sender][_spender];
if (_subtractedValue > oldValue) {
_allowed[msg.sender][_spender] = 0;
} else {
_allowed[msg.sender][_spender] = oldValue - _subtractedValue;
}
emit Approval(msg.sender, _spender, _allowed[msg.sender][_spender]);
return true;
}
/**
* @dev Creates new tokens and assigns them to an address.
* @param _to The address to which the tokens will be minted.
* @param _amount The amount of tokens to be minted.
* @return A boolean value indicating whether the operation succeeded.
*/
function mintTo(
address _to,
uint256 _amount
) external whenNotPaused onlyOwner returns (bool) {
require(_to != address(0), 'ERC20: to address is not valid');
_balances[_to] = _balances[_to] + _amount;
totalSupply = totalSupply + _amount;
emit Transfer(address(0), _to, _amount);
return true;
}
/**
* @dev Burn tokens from the sender's account.
* @param _amount The amount of tokens to burn.
* @return A boolean indicating whether the operation succeeded.
*/
function burn(
uint256 _amount
) external whenNotPaused returns (bool) {
require(_balances[msg.sender] >= _amount, 'ERC20: insufficient balance');
_balances[msg.sender] = _balances[msg.sender] - _amount;
totalSupply = totalSupply - _amount;
emit Transfer(msg.sender, address(0), _amount);
return true;
}
/**
* @dev Burn tokens from a specified account, subject to allowance.
* @param _from The address whose tokens will be burned.
* @param _amount The amount of tokens to burn.
* @return A boolean indicating whether the operation succeeded.
*/
function burnFrom(
address _from,
uint256 _amount
) external whenNotPaused returns (bool) {
require(_from != address(0), 'ERC20: from address is not valid');
require(_balances[_from] >= _amount, 'ERC20: insufficient balance');
require(_amount <= _allowed[_from][msg.sender], 'ERC20: burn from value not allowed');
_allowed[_from][msg.sender] = _allowed[_from][msg.sender] - _amount;
_balances[_from] = _balances[_from] - _amount;
totalSupply = totalSupply - _amount;
emit Transfer(_from, address(0), _amount);
return true;
}
}