ERC20.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/ERC20.sol)
  3. pragma solidity ^0.8.19;
  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. *
  13. * TIP: For a detailed writeup see our guide
  14. * https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
  15. * to implement supply mechanisms].
  16. *
  17. * The default value of {decimals} is 18. To change this, you should override
  18. * this function so it returns a different value.
  19. *
  20. * We have followed general OpenZeppelin Contracts guidelines: functions revert
  21. * instead returning `false` on failure. This behavior is nonetheless
  22. * conventional and does not conflict with the expectations of ERC20
  23. * applications.
  24. *
  25. * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
  26. * This allows applications to reconstruct the allowance for all accounts just
  27. * by listening to said events. Other implementations of the EIP may not emit
  28. * these events, as it isn't required by the specification.
  29. *
  30. * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
  31. * functions have been added to mitigate the well-known issues around setting
  32. * allowances. See {IERC20-approve}.
  33. */
  34. contract ERC20 is Context, IERC20, IERC20Metadata {
  35. mapping(address => uint256) private _balances;
  36. mapping(address => mapping(address => uint256)) private _allowances;
  37. uint256 private _totalSupply;
  38. string private _name;
  39. string private _symbol;
  40. /**
  41. * @dev Sets the values for {name} and {symbol}.
  42. *
  43. * All two of these values are immutable: they can only be set once during
  44. * construction.
  45. */
  46. constructor(string memory name_, string memory symbol_) {
  47. _name = name_;
  48. _symbol = symbol_;
  49. }
  50. /**
  51. * @dev Returns the name of the token.
  52. */
  53. function name() public view virtual override returns (string memory) {
  54. return _name;
  55. }
  56. /**
  57. * @dev Returns the symbol of the token, usually a shorter version of the
  58. * name.
  59. */
  60. function symbol() public view virtual override returns (string memory) {
  61. return _symbol;
  62. }
  63. /**
  64. * @dev Returns the number of decimals used to get its user representation.
  65. * For example, if `decimals` equals `2`, a balance of `505` tokens should
  66. * be displayed to a user as `5.05` (`505 / 10 ** 2`).
  67. *
  68. * Tokens usually opt for a value of 18, imitating the relationship between
  69. * Ether and Wei. This is the default value returned by this function, unless
  70. * it's overridden.
  71. *
  72. * NOTE: This information is only used for _display_ purposes: it in
  73. * no way affects any of the arithmetic of the contract, including
  74. * {IERC20-balanceOf} and {IERC20-transfer}.
  75. */
  76. function decimals() public view virtual override returns (uint8) {
  77. return 18;
  78. }
  79. /**
  80. * @dev See {IERC20-totalSupply}.
  81. */
  82. function totalSupply() public view virtual override returns (uint256) {
  83. return _totalSupply;
  84. }
  85. /**
  86. * @dev See {IERC20-balanceOf}.
  87. */
  88. function balanceOf(address account) public view virtual override returns (uint256) {
  89. return _balances[account];
  90. }
  91. /**
  92. * @dev See {IERC20-transfer}.
  93. *
  94. * Requirements:
  95. *
  96. * - `to` cannot be the zero address.
  97. * - the caller must have a balance of at least `amount`.
  98. */
  99. function transfer(address to, uint256 amount) public virtual override returns (bool) {
  100. address owner = _msgSender();
  101. _transfer(owner, to, 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. address owner = _msgSender();
  122. _approve(owner, spender, amount);
  123. return true;
  124. }
  125. /**
  126. * @dev See {IERC20-transferFrom}.
  127. *
  128. * Emits an {Approval} event indicating the updated allowance. This is not
  129. * required by the EIP. See the note at the beginning of {ERC20}.
  130. *
  131. * NOTE: Does not update the allowance if the current allowance
  132. * is the maximum `uint256`.
  133. *
  134. * Requirements:
  135. *
  136. * - `from` and `to` cannot be the zero address.
  137. * - `from` must have a balance of at least `amount`.
  138. * - the caller must have allowance for ``from``'s tokens of at least
  139. * `amount`.
  140. */
  141. function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) {
  142. address spender = _msgSender();
  143. _spendAllowance(from, spender, amount);
  144. _transfer(from, to, amount);
  145. return true;
  146. }
  147. /**
  148. * @dev Atomically increases the allowance granted to `spender` by the caller.
  149. *
  150. * This is an alternative to {approve} that can be used as a mitigation for
  151. * problems described in {IERC20-approve}.
  152. *
  153. * Emits an {Approval} event indicating the updated allowance.
  154. *
  155. * Requirements:
  156. *
  157. * - `spender` cannot be the zero address.
  158. */
  159. function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
  160. address owner = _msgSender();
  161. _approve(owner, spender, allowance(owner, spender) + addedValue);
  162. return true;
  163. }
  164. /**
  165. * @dev Atomically decreases the allowance granted to `spender` by the caller.
  166. *
  167. * This is an alternative to {approve} that can be used as a mitigation for
  168. * problems described in {IERC20-approve}.
  169. *
  170. * Emits an {Approval} event indicating the updated allowance.
  171. *
  172. * Requirements:
  173. *
  174. * - `spender` cannot be the zero address.
  175. * - `spender` must have allowance for the caller of at least
  176. * `subtractedValue`.
  177. */
  178. function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
  179. address owner = _msgSender();
  180. uint256 currentAllowance = allowance(owner, spender);
  181. require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
  182. unchecked {
  183. _approve(owner, spender, currentAllowance - subtractedValue);
  184. }
  185. return true;
  186. }
  187. /**
  188. * @dev Moves `amount` of tokens from `from` to `to`.
  189. *
  190. * This internal function is equivalent to {transfer}, and can be used to
  191. * e.g. implement automatic token fees, slashing mechanisms, etc.
  192. *
  193. * Emits a {Transfer} event.
  194. *
  195. * NOTE: This function is not virtual, {_update} should be overridden instead.
  196. */
  197. function _transfer(address from, address to, uint256 amount) internal {
  198. require(from != address(0), "ERC20: transfer from the zero address");
  199. require(to != address(0), "ERC20: transfer to the zero address");
  200. _update(from, to, amount);
  201. }
  202. /**
  203. * @dev Transfers `amount` of tokens from `from` to `to`, or alternatively mints (or burns) if `from` (or `to`) is
  204. * the zero address. All customizations to transfers, mints, and burns should be done by overriding this function.
  205. *
  206. * Emits a {Transfer} event.
  207. */
  208. function _update(address from, address to, uint256 amount) internal virtual {
  209. if (from == address(0)) {
  210. _totalSupply += amount;
  211. } else {
  212. uint256 fromBalance = _balances[from];
  213. require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
  214. unchecked {
  215. // Overflow not possible: amount <= fromBalance <= totalSupply.
  216. _balances[from] = fromBalance - amount;
  217. }
  218. }
  219. if (to == address(0)) {
  220. unchecked {
  221. // Overflow not possible: amount <= totalSupply or amount <= fromBalance <= totalSupply.
  222. _totalSupply -= amount;
  223. }
  224. } else {
  225. unchecked {
  226. // Overflow not possible: balance + amount is at most totalSupply, which we know fits into a uint256.
  227. _balances[to] += amount;
  228. }
  229. }
  230. emit Transfer(from, to, amount);
  231. }
  232. /**
  233. * @dev Creates `amount` tokens and assigns them to `account`, by transferring it from address(0).
  234. * Relies on the `_update` mechanism
  235. *
  236. * Emits a {Transfer} event with `from` set to the zero address.
  237. *
  238. * NOTE: This function is not virtual, {_update} should be overridden instead.
  239. */
  240. function _mint(address account, uint256 amount) internal {
  241. require(account != address(0), "ERC20: mint to the zero address");
  242. _update(address(0), account, amount);
  243. }
  244. /**
  245. * @dev Destroys `amount` tokens from `account`, by transferring it to address(0).
  246. * Relies on the `_update` mechanism.
  247. *
  248. * Emits a {Transfer} event with `to` set to the zero address.
  249. *
  250. * NOTE: This function is not virtual, {_update} should be overridden instead
  251. */
  252. function _burn(address account, uint256 amount) internal {
  253. require(account != address(0), "ERC20: burn from the zero address");
  254. _update(account, address(0), amount);
  255. }
  256. /**
  257. * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
  258. *
  259. * This internal function is equivalent to `approve`, and can be used to
  260. * e.g. set automatic allowances for certain subsystems, etc.
  261. *
  262. * Emits an {Approval} event.
  263. *
  264. * Requirements:
  265. *
  266. * - `owner` cannot be the zero address.
  267. * - `spender` cannot be the zero address.
  268. */
  269. function _approve(address owner, address spender, uint256 amount) internal virtual {
  270. require(owner != address(0), "ERC20: approve from the zero address");
  271. require(spender != address(0), "ERC20: approve to the zero address");
  272. _allowances[owner][spender] = amount;
  273. emit Approval(owner, spender, amount);
  274. }
  275. /**
  276. * @dev Updates `owner` s allowance for `spender` based on spent `amount`.
  277. *
  278. * Does not update the allowance amount in case of infinite allowance.
  279. * Revert if not enough allowance is available.
  280. *
  281. * Might emit an {Approval} event.
  282. */
  283. function _spendAllowance(address owner, address spender, uint256 amount) internal virtual {
  284. uint256 currentAllowance = allowance(owner, spender);
  285. if (currentAllowance != type(uint256).max) {
  286. require(currentAllowance >= amount, "ERC20: insufficient allowance");
  287. unchecked {
  288. _approve(owner, spender, currentAllowance - amount);
  289. }
  290. }
  291. }
  292. }