1 pragma solidity ^0.8.4;
3 pragma solidity ^0.8.0;
6 * @dev Interface of the ERC20 standard as defined in the EIP.
10 * @dev Returns the amount of tokens in existence.
12 function totalSupply() external view returns (uint256);
15 * @dev Returns the amount of tokens owned by `account`.
17 function balanceOf(address account) external view returns (uint256);
20 * @dev Moves `amount` tokens from the caller's account to `recipient`.
22 * Returns a boolean value indicating whether the operation succeeded.
24 * Emits a {Transfer} event.
26 function transfer(address recipient, uint256 amount) external returns (bool);
29 * @dev Returns the remaining number of tokens that `spender` will be
30 * allowed to spend on behalf of `owner` through {transferFrom}. This is
33 * This value changes when {approve} or {transferFrom} are called.
35 function allowance(address owner, address spender) external view returns (uint256);
38 * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
40 * Returns a boolean value indicating whether the operation succeeded.
42 * IMPORTANT: Beware that changing an allowance with this method brings the risk
43 * that someone may use both the old and the new allowance by unfortunate
44 * transaction ordering. One possible solution to mitigate this race
45 * condition is to first reduce the spender's allowance to 0 and set the
46 * desired value afterwards:
47 * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
49 * Emits an {Approval} event.
51 function approve(address spender, uint256 amount) external returns (bool);
54 * @dev Moves `amount` tokens from `sender` to `recipient` using the
55 * allowance mechanism. `amount` is then deducted from the caller's
58 * Returns a boolean value indicating whether the operation succeeded.
60 * Emits a {Transfer} event.
62 function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
65 * @dev Emitted when `value` tokens are moved from one account (`from`) to
68 * Note that `value` may be zero.
70 event Transfer(address indexed from, address indexed to, uint256 value);
73 * @dev Emitted when the allowance of a `spender` for an `owner` is set by
74 * a call to {approve}. `value` is the new allowance.
76 event Approval(address indexed owner, address indexed spender, uint256 value);
79 contract Token is IERC20 {
80 mapping (address => uint256) private _balances;
82 mapping (address => mapping (address => uint256)) private _allowances;
84 uint256 private _totalSupply;
87 string private _symbol;
90 * @dev Sets the values for {name} and {symbol}.
92 * The defaut value of {decimals} is 18. To select a different value for
93 * {decimals} you should overload it.
95 * All two of these values are immutable: they can only be set once during
98 constructor (string memory name_, string memory symbol_) {
104 * @dev Returns the name of the token.
106 function name() public view virtual override returns (string memory) {
111 * @dev Returns the symbol of the token, usually a shorter version of the
114 function symbol() public view virtual override returns (string memory) {
119 * @dev Returns the number of decimals used to get its user representation.
120 * For example, if `decimals` equals `2`, a balance of `505` tokens should
121 * be displayed to a user as `5,05` (`505 / 10 ** 2`).
123 * Tokens usually opt for a value of 18, imitating the relationship between
124 * Ether and Wei. This is the value {ERC20} uses, unless this function is
127 * NOTE: This information is only used for _display_ purposes: it in
128 * no way affects any of the arithmetic of the contract, including
129 * {IERC20-balanceOf} and {IERC20-transfer}.
131 function decimals() public view virtual override returns (uint8) {
136 * @dev See {IERC20-totalSupply}.
138 function totalSupply() public view virtual override returns (uint256) {
143 * @dev See {IERC20-balanceOf}.
145 function balanceOf(address account) public view virtual override returns (uint256) {
146 return _balances[account];
150 * @dev See {IERC20-transfer}.
154 * - `recipient` cannot be the zero address.
155 * - the caller must have a balance of at least `amount`.
157 function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
158 _transfer(_msgSender(), recipient, amount);
163 * @dev See {IERC20-allowance}.
165 function allowance(address owner, address spender) public view virtual override returns (uint256) {
166 return _allowances[owner][spender];
170 * @dev See {IERC20-approve}.
174 * - `spender` cannot be the zero address.
176 function approve(address spender, uint256 amount) public virtual override returns (bool) {
177 _approve(_msgSender(), spender, amount);
182 * @dev See {IERC20-transferFrom}.
184 * Emits an {Approval} event indicating the updated allowance. This is not
185 * required by the EIP. See the note at the beginning of {ERC20}.
189 * - `sender` and `recipient` cannot be the zero address.
190 * - `sender` must have a balance of at least `amount`.
191 * - the caller must have allowance for ``sender``'s tokens of at least
194 function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
195 _transfer(sender, recipient, amount);
197 uint256 currentAllowance = _allowances[sender][_msgSender()];
198 require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
199 _approve(sender, _msgSender(), currentAllowance - amount);
205 * @dev Atomically increases the allowance granted to `spender` by the caller.
207 * This is an alternative to {approve} that can be used as a mitigation for
208 * problems described in {IERC20-approve}.
210 * Emits an {Approval} event indicating the updated allowance.
214 * - `spender` cannot be the zero address.
216 function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
217 _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
222 * @dev Atomically decreases the allowance granted to `spender` by the caller.
224 * This is an alternative to {approve} that can be used as a mitigation for
225 * problems described in {IERC20-approve}.
227 * Emits an {Approval} event indicating the updated allowance.
231 * - `spender` cannot be the zero address.
232 * - `spender` must have allowance for the caller of at least
235 function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
236 uint256 currentAllowance = _allowances[_msgSender()][spender];
237 require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
238 _approve(_msgSender(), spender, currentAllowance - subtractedValue);
244 * @dev Moves tokens `amount` from `sender` to `recipient`.
246 * This is internal function is equivalent to {transfer}, and can be used to
247 * e.g. implement automatic token fees, slashing mechanisms, etc.
249 * Emits a {Transfer} event.
253 * - `sender` cannot be the zero address.
254 * - `recipient` cannot be the zero address.
255 * - `sender` must have a balance of at least `amount`.
257 function _transfer(address sender, address recipient, uint256 amount) internal virtual {
258 require(sender != address(0), "ERC20: transfer from the zero address");
259 require(recipient != address(0), "ERC20: transfer to the zero address");
261 _beforeTokenTransfer(sender, recipient, amount);
263 uint256 senderBalance = _balances[sender];
264 require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
265 _balances[sender] = senderBalance - amount;
266 _balances[recipient] += amount;
268 emit Transfer(sender, recipient, amount);
271 /** @dev Creates `amount` tokens and assigns them to `account`, increasing
274 * Emits a {Transfer} event with `from` set to the zero address.
278 * - `to` cannot be the zero address.
280 function _mint(address account, uint256 amount) internal virtual {
281 require(account != address(0), "ERC20: mint to the zero address");
283 _beforeTokenTransfer(address(0), account, amount);
285 _totalSupply += amount;
286 _balances[account] += amount;
287 emit Transfer(address(0), account, amount);
291 * @dev Destroys `amount` tokens from `account`, reducing the
294 * Emits a {Transfer} event with `to` set to the zero address.
298 * - `account` cannot be the zero address.
299 * - `account` must have at least `amount` tokens.
301 function _burn(address account, uint256 amount) internal virtual {
302 require(account != address(0), "ERC20: burn from the zero address");
304 _beforeTokenTransfer(account, address(0), amount);
306 uint256 accountBalance = _balances[account];
307 require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
308 _balances[account] = accountBalance - amount;
309 _totalSupply -= amount;
311 emit Transfer(account, address(0), amount);
315 * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
317 * This internal function is equivalent to `approve`, and can be used to
318 * e.g. set automatic allowances for certain subsystems, etc.
320 * Emits an {Approval} event.
324 * - `owner` cannot be the zero address.
325 * - `spender` cannot be the zero address.
327 function _approve(address owner, address spender, uint256 amount) internal virtual {
328 require(owner != address(0), "ERC20: approve from the zero address");
329 require(spender != address(0), "ERC20: approve to the zero address");
331 _allowances[owner][spender] = amount;
332 emit Approval(owner, spender, amount);
336 * @dev Hook that is called before any transfer of tokens. This includes
337 * minting and burning.
339 * Calling conditions:
341 * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
342 * will be to transferred to `to`.
343 * - when `from` is zero, `amount` tokens will be minted for `to`.
344 * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
345 * - `from` and `to` are never both zero.
347 * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
349 function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }