ERC20.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.3.0) (token/ERC20/ERC20.sol)
  3. pragma solidity ^0.8.20;
  4. import {IERC20} from "./IERC20.sol";
  5. import {IERC20Metadata} from "./extensions/IERC20Metadata.sol";
  6. import {Context} from "../../utils/Context.sol";
  7. import {IERC20Errors} from "../../interfaces/draft-IERC6093.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. *
  14. * TIP: For a detailed writeup see our guide
  15. * https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
  16. * to implement supply mechanisms].
  17. *
  18. * The default value of {decimals} is 18. To change this, you should override
  19. * this function so it returns a different value.
  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 ERC-20
  24. * applications.
  25. */
  26. abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
  27. mapping(address account => uint256) private _balances;
  28. mapping(address account => mapping(address spender => uint256)) private _allowances;
  29. uint256 private _totalSupply;
  30. string private _name;
  31. string private _symbol;
  32. /**
  33. * @dev Sets the values for {name} and {symbol}.
  34. *
  35. * Both values are immutable: they can only be set once during construction.
  36. */
  37. constructor(string memory name_, string memory symbol_) {
  38. _name = name_;
  39. _symbol = symbol_;
  40. }
  41. /**
  42. * @dev Returns the name of the token.
  43. */
  44. function name() public view virtual returns (string memory) {
  45. return _name;
  46. }
  47. /**
  48. * @dev Returns the symbol of the token, usually a shorter version of the
  49. * name.
  50. */
  51. function symbol() public view virtual returns (string memory) {
  52. return _symbol;
  53. }
  54. /**
  55. * @dev Returns the number of decimals used to get its user representation.
  56. * For example, if `decimals` equals `2`, a balance of `505` tokens should
  57. * be displayed to a user as `5.05` (`505 / 10 ** 2`).
  58. *
  59. * Tokens usually opt for a value of 18, imitating the relationship between
  60. * Ether and Wei. This is the default value returned by this function, unless
  61. * it's overridden.
  62. *
  63. * NOTE: This information is only used for _display_ purposes: it in
  64. * no way affects any of the arithmetic of the contract, including
  65. * {IERC20-balanceOf} and {IERC20-transfer}.
  66. */
  67. function decimals() public view virtual returns (uint8) {
  68. return 18;
  69. }
  70. /**
  71. * @dev See {IERC20-totalSupply}.
  72. */
  73. function totalSupply() public view virtual returns (uint256) {
  74. return _totalSupply;
  75. }
  76. /**
  77. * @dev See {IERC20-balanceOf}.
  78. */
  79. function balanceOf(address account) public view virtual returns (uint256) {
  80. return _balances[account];
  81. }
  82. /**
  83. * @dev See {IERC20-transfer}.
  84. *
  85. * Requirements:
  86. *
  87. * - `to` cannot be the zero address.
  88. * - the caller must have a balance of at least `value`.
  89. */
  90. function transfer(address to, uint256 value) public virtual returns (bool) {
  91. address owner = _msgSender();
  92. _transfer(owner, to, value);
  93. return true;
  94. }
  95. /**
  96. * @dev See {IERC20-allowance}.
  97. */
  98. function allowance(address owner, address spender) public view virtual returns (uint256) {
  99. return _allowances[owner][spender];
  100. }
  101. /**
  102. * @dev See {IERC20-approve}.
  103. *
  104. * NOTE: If `value` is the maximum `uint256`, the allowance is not updated on
  105. * `transferFrom`. This is semantically equivalent to an infinite approval.
  106. *
  107. * Requirements:
  108. *
  109. * - `spender` cannot be the zero address.
  110. */
  111. function approve(address spender, uint256 value) public virtual returns (bool) {
  112. address owner = _msgSender();
  113. _approve(owner, spender, value);
  114. return true;
  115. }
  116. /**
  117. * @dev See {IERC20-transferFrom}.
  118. *
  119. * Skips emitting an {Approval} event indicating an allowance update. This is not
  120. * required by the ERC. See {xref-ERC20-_approve-address-address-uint256-bool-}[_approve].
  121. *
  122. * NOTE: Does not update the allowance if the current allowance
  123. * is the maximum `uint256`.
  124. *
  125. * Requirements:
  126. *
  127. * - `from` and `to` cannot be the zero address.
  128. * - `from` must have a balance of at least `value`.
  129. * - the caller must have allowance for ``from``'s tokens of at least
  130. * `value`.
  131. */
  132. function transferFrom(address from, address to, uint256 value) public virtual returns (bool) {
  133. address spender = _msgSender();
  134. _spendAllowance(from, spender, value);
  135. _transfer(from, to, value);
  136. return true;
  137. }
  138. /**
  139. * @dev Moves a `value` amount of tokens from `from` to `to`.
  140. *
  141. * This internal function is equivalent to {transfer}, and can be used to
  142. * e.g. implement automatic token fees, slashing mechanisms, etc.
  143. *
  144. * Emits a {Transfer} event.
  145. *
  146. * NOTE: This function is not virtual, {_update} should be overridden instead.
  147. */
  148. function _transfer(address from, address to, uint256 value) internal {
  149. if (from == address(0)) {
  150. revert ERC20InvalidSender(address(0));
  151. }
  152. if (to == address(0)) {
  153. revert ERC20InvalidReceiver(address(0));
  154. }
  155. _update(from, to, value);
  156. }
  157. /**
  158. * @dev Transfers a `value` amount of tokens from `from` to `to`, or alternatively mints (or burns) if `from`
  159. * (or `to`) is the zero address. All customizations to transfers, mints, and burns should be done by overriding
  160. * this function.
  161. *
  162. * Emits a {Transfer} event.
  163. */
  164. function _update(address from, address to, uint256 value) internal virtual {
  165. if (from == address(0)) {
  166. // Overflow check required: The rest of the code assumes that totalSupply never overflows
  167. _totalSupply += value;
  168. } else {
  169. uint256 fromBalance = _balances[from];
  170. if (fromBalance < value) {
  171. revert ERC20InsufficientBalance(from, fromBalance, value);
  172. }
  173. unchecked {
  174. // Overflow not possible: value <= fromBalance <= totalSupply.
  175. _balances[from] = fromBalance - value;
  176. }
  177. }
  178. if (to == address(0)) {
  179. unchecked {
  180. // Overflow not possible: value <= totalSupply or value <= fromBalance <= totalSupply.
  181. _totalSupply -= value;
  182. }
  183. } else {
  184. unchecked {
  185. // Overflow not possible: balance + value is at most totalSupply, which we know fits into a uint256.
  186. _balances[to] += value;
  187. }
  188. }
  189. emit Transfer(from, to, value);
  190. }
  191. /**
  192. * @dev Creates a `value` amount of tokens and assigns them to `account`, by transferring it from address(0).
  193. * Relies on the `_update` mechanism
  194. *
  195. * Emits a {Transfer} event with `from` set to the zero address.
  196. *
  197. * NOTE: This function is not virtual, {_update} should be overridden instead.
  198. */
  199. function _mint(address account, uint256 value) internal {
  200. if (account == address(0)) {
  201. revert ERC20InvalidReceiver(address(0));
  202. }
  203. _update(address(0), account, value);
  204. }
  205. /**
  206. * @dev Destroys a `value` amount of tokens from `account`, lowering the total supply.
  207. * Relies on the `_update` mechanism.
  208. *
  209. * Emits a {Transfer} event with `to` set to the zero address.
  210. *
  211. * NOTE: This function is not virtual, {_update} should be overridden instead
  212. */
  213. function _burn(address account, uint256 value) internal {
  214. if (account == address(0)) {
  215. revert ERC20InvalidSender(address(0));
  216. }
  217. _update(account, address(0), value);
  218. }
  219. /**
  220. * @dev Sets `value` as the allowance of `spender` over the `owner`'s tokens.
  221. *
  222. * This internal function is equivalent to `approve`, and can be used to
  223. * e.g. set automatic allowances for certain subsystems, etc.
  224. *
  225. * Emits an {Approval} event.
  226. *
  227. * Requirements:
  228. *
  229. * - `owner` cannot be the zero address.
  230. * - `spender` cannot be the zero address.
  231. *
  232. * Overrides to this logic should be done to the variant with an additional `bool emitEvent` argument.
  233. */
  234. function _approve(address owner, address spender, uint256 value) internal {
  235. _approve(owner, spender, value, true);
  236. }
  237. /**
  238. * @dev Variant of {_approve} with an optional flag to enable or disable the {Approval} event.
  239. *
  240. * By default (when calling {_approve}) the flag is set to true. On the other hand, approval changes made by
  241. * `_spendAllowance` during the `transferFrom` operation set the flag to false. This saves gas by not emitting any
  242. * `Approval` event during `transferFrom` operations.
  243. *
  244. * Anyone who wishes to continue emitting `Approval` events on the`transferFrom` operation can force the flag to
  245. * true using the following override:
  246. *
  247. * ```solidity
  248. * function _approve(address owner, address spender, uint256 value, bool) internal virtual override {
  249. * super._approve(owner, spender, value, true);
  250. * }
  251. * ```
  252. *
  253. * Requirements are the same as {_approve}.
  254. */
  255. function _approve(address owner, address spender, uint256 value, bool emitEvent) internal virtual {
  256. if (owner == address(0)) {
  257. revert ERC20InvalidApprover(address(0));
  258. }
  259. if (spender == address(0)) {
  260. revert ERC20InvalidSpender(address(0));
  261. }
  262. _allowances[owner][spender] = value;
  263. if (emitEvent) {
  264. emit Approval(owner, spender, value);
  265. }
  266. }
  267. /**
  268. * @dev Updates `owner`'s allowance for `spender` based on spent `value`.
  269. *
  270. * Does not update the allowance value in case of infinite allowance.
  271. * Revert if not enough allowance is available.
  272. *
  273. * Does not emit an {Approval} event.
  274. */
  275. function _spendAllowance(address owner, address spender, uint256 value) internal virtual {
  276. uint256 currentAllowance = allowance(owner, spender);
  277. if (currentAllowance < type(uint256).max) {
  278. if (currentAllowance < value) {
  279. revert ERC20InsufficientAllowance(spender, currentAllowance, value);
  280. }
  281. unchecked {
  282. _approve(owner, spender, currentAllowance - value, false);
  283. }
  284. }
  285. }
  286. }