|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC20/ERC20.sol) |
| 3 | + |
| 4 | +pragma solidity ^0.8.0; |
| 5 | + |
| 6 | +import "./IERC20.sol"; |
| 7 | +import "./extensions/IERC20Metadata.sol"; |
| 8 | +import "../../utils/Context.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @dev Implementation of the {IERC20} interface. |
| 12 | + * |
| 13 | + * This implementation is agnostic to the way tokens are created. This means |
| 14 | + * that a supply mechanism has to be added in a derived contract using {_mint}. |
| 15 | + * For a generic mechanism see {ERC20PresetMinterPauser}. |
| 16 | + * |
| 17 | + * TIP: For a detailed writeup see our guide |
| 18 | + * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How |
| 19 | + * to implement supply mechanisms]. |
| 20 | + * |
| 21 | + * We have followed general OpenZeppelin Contracts guidelines: functions revert |
| 22 | + * instead returning `false` on failure. This behavior is nonetheless |
| 23 | + * conventional and does not conflict with the expectations of ERC20 |
| 24 | + * applications. |
| 25 | + * |
| 26 | + * Additionally, an {Approval} event is emitted on calls to {transferFrom}. |
| 27 | + * This allows applications to reconstruct the allowance for all accounts just |
| 28 | + * by listening to said events. Other implementations of the EIP may not emit |
| 29 | + * these events, as it isn't required by the specification. |
| 30 | + * |
| 31 | + * Finally, the non-standard {decreaseAllowance} and {increaseAllowance} |
| 32 | + * functions have been added to mitigate the well-known issues around setting |
| 33 | + * allowances. See {IERC20-approve}. |
| 34 | + */ |
| 35 | +contract ERC20 is Context, IERC20, IERC20Metadata { |
| 36 | + mapping(address => uint256) private _balances; |
| 37 | + |
| 38 | + mapping(address => mapping(address => uint256)) private _allowances; |
| 39 | + |
| 40 | + uint256 private _totalSupply; |
| 41 | + |
| 42 | + string private _name; |
| 43 | + string private _symbol; |
| 44 | + |
| 45 | + /** |
| 46 | + * @dev Sets the values for {name} and {symbol}. |
| 47 | + * |
| 48 | + * The default value of {decimals} is 18. To select a different value for |
| 49 | + * {decimals} you should overload it. |
| 50 | + * |
| 51 | + * All two of these values are immutable: they can only be set once during |
| 52 | + * construction. |
| 53 | + */ |
| 54 | + constructor(string memory name_, string memory symbol_) { |
| 55 | + _name = name_; |
| 56 | + _symbol = symbol_; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @dev Returns the name of the token. |
| 61 | + */ |
| 62 | + function name() public view virtual override returns (string memory) { |
| 63 | + return _name; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @dev Returns the symbol of the token, usually a shorter version of the |
| 68 | + * name. |
| 69 | + */ |
| 70 | + function symbol() public view virtual override returns (string memory) { |
| 71 | + return _symbol; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @dev Returns the number of decimals used to get its user representation. |
| 76 | + * For example, if `decimals` equals `2`, a balance of `505` tokens should |
| 77 | + * be displayed to a user as `5.05` (`505 / 10 ** 2`). |
| 78 | + * |
| 79 | + * Tokens usually opt for a value of 18, imitating the relationship between |
| 80 | + * Ether and Wei. This is the value {ERC20} uses, unless this function is |
| 81 | + * overridden; |
| 82 | + * |
| 83 | + * NOTE: This information is only used for _display_ purposes: it in |
| 84 | + * no way affects any of the arithmetic of the contract, including |
| 85 | + * {IERC20-balanceOf} and {IERC20-transfer}. |
| 86 | + */ |
| 87 | + function decimals() public view virtual override returns (uint8) { |
| 88 | + return 18; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @dev See {IERC20-totalSupply}. |
| 93 | + */ |
| 94 | + function totalSupply() public view virtual override returns (uint256) { |
| 95 | + return _totalSupply; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @dev See {IERC20-balanceOf}. |
| 100 | + */ |
| 101 | + function balanceOf(address account) public view virtual override returns (uint256) { |
| 102 | + return _balances[account]; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @dev See {IERC20-transfer}. |
| 107 | + * |
| 108 | + * Requirements: |
| 109 | + * |
| 110 | + * - `to` cannot be the zero address. |
| 111 | + * - the caller must have a balance of at least `amount`. |
| 112 | + */ |
| 113 | + function transfer(address to, uint256 amount) public virtual override returns (bool) { |
| 114 | + address owner = _msgSender(); |
| 115 | + _transfer(owner, to, amount); |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @dev See {IERC20-allowance}. |
| 121 | + */ |
| 122 | + function allowance(address owner, address spender) public view virtual override returns (uint256) { |
| 123 | + return _allowances[owner][spender]; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @dev See {IERC20-approve}. |
| 128 | + * |
| 129 | + * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on |
| 130 | + * `transferFrom`. This is semantically equivalent to an infinite approval. |
| 131 | + * |
| 132 | + * Requirements: |
| 133 | + * |
| 134 | + * - `spender` cannot be the zero address. |
| 135 | + */ |
| 136 | + function approve(address spender, uint256 amount) public virtual override returns (bool) { |
| 137 | + address owner = _msgSender(); |
| 138 | + _approve(owner, spender, amount); |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @dev See {IERC20-transferFrom}. |
| 144 | + * |
| 145 | + * Emits an {Approval} event indicating the updated allowance. This is not |
| 146 | + * required by the EIP. See the note at the beginning of {ERC20}. |
| 147 | + * |
| 148 | + * NOTE: Does not update the allowance if the current allowance |
| 149 | + * is the maximum `uint256`. |
| 150 | + * |
| 151 | + * Requirements: |
| 152 | + * |
| 153 | + * - `from` and `to` cannot be the zero address. |
| 154 | + * - `from` must have a balance of at least `amount`. |
| 155 | + * - the caller must have allowance for ``from``'s tokens of at least |
| 156 | + * `amount`. |
| 157 | + */ |
| 158 | + function transferFrom( |
| 159 | + address from, |
| 160 | + address to, |
| 161 | + uint256 amount |
| 162 | + ) public virtual override returns (bool) { |
| 163 | + address spender = _msgSender(); |
| 164 | + _spendAllowance(from, spender, amount); |
| 165 | + _transfer(from, to, amount); |
| 166 | + return true; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @dev Atomically increases the allowance granted to `spender` by the caller. |
| 171 | + * |
| 172 | + * This is an alternative to {approve} that can be used as a mitigation for |
| 173 | + * problems described in {IERC20-approve}. |
| 174 | + * |
| 175 | + * Emits an {Approval} event indicating the updated allowance. |
| 176 | + * |
| 177 | + * Requirements: |
| 178 | + * |
| 179 | + * - `spender` cannot be the zero address. |
| 180 | + */ |
| 181 | + function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { |
| 182 | + address owner = _msgSender(); |
| 183 | + _approve(owner, spender, allowance(owner, spender) + addedValue); |
| 184 | + return true; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * @dev Atomically decreases the allowance granted to `spender` by the caller. |
| 189 | + * |
| 190 | + * This is an alternative to {approve} that can be used as a mitigation for |
| 191 | + * problems described in {IERC20-approve}. |
| 192 | + * |
| 193 | + * Emits an {Approval} event indicating the updated allowance. |
| 194 | + * |
| 195 | + * Requirements: |
| 196 | + * |
| 197 | + * - `spender` cannot be the zero address. |
| 198 | + * - `spender` must have allowance for the caller of at least |
| 199 | + * `subtractedValue`. |
| 200 | + */ |
| 201 | + function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) { |
| 202 | + address owner = _msgSender(); |
| 203 | + uint256 currentAllowance = allowance(owner, spender); |
| 204 | + require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero"); |
| 205 | + unchecked { |
| 206 | + _approve(owner, spender, currentAllowance - subtractedValue); |
| 207 | + } |
| 208 | + |
| 209 | + return true; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * @dev Moves `amount` of tokens from `from` to `to`. |
| 214 | + * |
| 215 | + * This internal function is equivalent to {transfer}, and can be used to |
| 216 | + * e.g. implement automatic token fees, slashing mechanisms, etc. |
| 217 | + * |
| 218 | + * Emits a {Transfer} event. |
| 219 | + * |
| 220 | + * Requirements: |
| 221 | + * |
| 222 | + * - `from` cannot be the zero address. |
| 223 | + * - `to` cannot be the zero address. |
| 224 | + * - `from` must have a balance of at least `amount`. |
| 225 | + */ |
| 226 | + function _transfer( |
| 227 | + address from, |
| 228 | + address to, |
| 229 | + uint256 amount |
| 230 | + ) internal virtual { |
| 231 | + require(from != address(0), "ERC20: transfer from the zero address"); |
| 232 | + require(to != address(0), "ERC20: transfer to the zero address"); |
| 233 | + |
| 234 | + _beforeTokenTransfer(from, to, amount); |
| 235 | + |
| 236 | + uint256 fromBalance = _balances[from]; |
| 237 | + require(fromBalance >= amount, "ERC20: transfer amount exceeds balance"); |
| 238 | + unchecked { |
| 239 | + _balances[from] = fromBalance - amount; |
| 240 | + } |
| 241 | + _balances[to] += amount; |
| 242 | + |
| 243 | + emit Transfer(from, to, amount); |
| 244 | + |
| 245 | + _afterTokenTransfer(from, to, amount); |
| 246 | + } |
| 247 | + |
| 248 | + /** @dev Creates `amount` tokens and assigns them to `account`, increasing |
| 249 | + * the total supply. |
| 250 | + * |
| 251 | + * Emits a {Transfer} event with `from` set to the zero address. |
| 252 | + * |
| 253 | + * Requirements: |
| 254 | + * |
| 255 | + * - `account` cannot be the zero address. |
| 256 | + */ |
| 257 | + function _mint(address account, uint256 amount) internal virtual { |
| 258 | + require(account != address(0), "ERC20: mint to the zero address"); |
| 259 | + |
| 260 | + _beforeTokenTransfer(address(0), account, amount); |
| 261 | + |
| 262 | + _totalSupply += amount; |
| 263 | + _balances[account] += amount; |
| 264 | + emit Transfer(address(0), account, amount); |
| 265 | + |
| 266 | + _afterTokenTransfer(address(0), account, amount); |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * @dev Destroys `amount` tokens from `account`, reducing the |
| 271 | + * total supply. |
| 272 | + * |
| 273 | + * Emits a {Transfer} event with `to` set to the zero address. |
| 274 | + * |
| 275 | + * Requirements: |
| 276 | + * |
| 277 | + * - `account` cannot be the zero address. |
| 278 | + * - `account` must have at least `amount` tokens. |
| 279 | + */ |
| 280 | + function _burn(address account, uint256 amount) internal virtual { |
| 281 | + require(account != address(0), "ERC20: burn from the zero address"); |
| 282 | + |
| 283 | + _beforeTokenTransfer(account, address(0), amount); |
| 284 | + |
| 285 | + uint256 accountBalance = _balances[account]; |
| 286 | + require(accountBalance >= amount, "ERC20: burn amount exceeds balance"); |
| 287 | + unchecked { |
| 288 | + _balances[account] = accountBalance - amount; |
| 289 | + } |
| 290 | + _totalSupply -= amount; |
| 291 | + |
| 292 | + emit Transfer(account, address(0), amount); |
| 293 | + |
| 294 | + _afterTokenTransfer(account, address(0), amount); |
| 295 | + } |
| 296 | + |
| 297 | + /** |
| 298 | + * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens. |
| 299 | + * |
| 300 | + * This internal function is equivalent to `approve`, and can be used to |
| 301 | + * e.g. set automatic allowances for certain subsystems, etc. |
| 302 | + * |
| 303 | + * Emits an {Approval} event. |
| 304 | + * |
| 305 | + * Requirements: |
| 306 | + * |
| 307 | + * - `owner` cannot be the zero address. |
| 308 | + * - `spender` cannot be the zero address. |
| 309 | + */ |
| 310 | + function _approve( |
| 311 | + address owner, |
| 312 | + address spender, |
| 313 | + uint256 amount |
| 314 | + ) internal virtual { |
| 315 | + require(owner != address(0), "ERC20: approve from the zero address"); |
| 316 | + require(spender != address(0), "ERC20: approve to the zero address"); |
| 317 | + |
| 318 | + _allowances[owner][spender] = amount; |
| 319 | + emit Approval(owner, spender, amount); |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * @dev Updates `owner` s allowance for `spender` based on spent `amount`. |
| 324 | + * |
| 325 | + * Does not update the allowance amount in case of infinite allowance. |
| 326 | + * Revert if not enough allowance is available. |
| 327 | + * |
| 328 | + * Might emit an {Approval} event. |
| 329 | + */ |
| 330 | + function _spendAllowance( |
| 331 | + address owner, |
| 332 | + address spender, |
| 333 | + uint256 amount |
| 334 | + ) internal virtual { |
| 335 | + uint256 currentAllowance = allowance(owner, spender); |
| 336 | + if (currentAllowance != type(uint256).max) { |
| 337 | + require(currentAllowance >= amount, "ERC20: insufficient allowance"); |
| 338 | + unchecked { |
| 339 | + _approve(owner, spender, currentAllowance - amount); |
| 340 | + } |
| 341 | + } |
| 342 | + } |
| 343 | + |
| 344 | + /** |
| 345 | + * @dev Hook that is called before any transfer of tokens. This includes |
| 346 | + * minting and burning. |
| 347 | + * |
| 348 | + * Calling conditions: |
| 349 | + * |
| 350 | + * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens |
| 351 | + * will be transferred to `to`. |
| 352 | + * - when `from` is zero, `amount` tokens will be minted for `to`. |
| 353 | + * - when `to` is zero, `amount` of ``from``'s tokens will be burned. |
| 354 | + * - `from` and `to` are never both zero. |
| 355 | + * |
| 356 | + * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. |
| 357 | + */ |
| 358 | + function _beforeTokenTransfer( |
| 359 | + address from, |
| 360 | + address to, |
| 361 | + uint256 amount |
| 362 | + ) internal virtual {} |
| 363 | + |
| 364 | + /** |
| 365 | + * @dev Hook that is called after any transfer of tokens. This includes |
| 366 | + * minting and burning. |
| 367 | + * |
| 368 | + * Calling conditions: |
| 369 | + * |
| 370 | + * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens |
| 371 | + * has been transferred to `to`. |
| 372 | + * - when `from` is zero, `amount` tokens have been minted for `to`. |
| 373 | + * - when `to` is zero, `amount` of ``from``'s tokens have been burned. |
| 374 | + * - `from` and `to` are never both zero. |
| 375 | + * |
| 376 | + * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. |
| 377 | + */ |
| 378 | + function _afterTokenTransfer( |
| 379 | + address from, |
| 380 | + address to, |
| 381 | + uint256 amount |
| 382 | + ) internal virtual {} |
| 383 | +} |
0 commit comments