ERC20Upgradeable.sol 13 KB

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