ERC20.sol 12 KB

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