ERC20.sol 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.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. * Requirements:
  114. *
  115. * - `spender` cannot be the zero address.
  116. */
  117. function approve(address spender, uint256 amount) public virtual override returns (bool) {
  118. _approve(_msgSender(), spender, amount);
  119. return true;
  120. }
  121. /**
  122. * @dev See {IERC20-transferFrom}.
  123. *
  124. * Emits an {Approval} event indicating the updated allowance. This is not
  125. * required by the EIP. See the note at the beginning of {ERC20}.
  126. *
  127. * Requirements:
  128. *
  129. * - `sender` and `recipient` cannot be the zero address.
  130. * - `sender` must have a balance of at least `amount`.
  131. * - the caller must have allowance for ``sender``'s tokens of at least
  132. * `amount`.
  133. */
  134. function transferFrom(
  135. address sender,
  136. address recipient,
  137. uint256 amount
  138. ) public virtual override returns (bool) {
  139. _transfer(sender, recipient, amount);
  140. uint256 currentAllowance = _allowances[sender][_msgSender()];
  141. require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
  142. unchecked {
  143. _approve(sender, _msgSender(), currentAllowance - amount);
  144. }
  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. _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
  161. return true;
  162. }
  163. /**
  164. * @dev Atomically decreases the allowance granted to `spender` by the caller.
  165. *
  166. * This is an alternative to {approve} that can be used as a mitigation for
  167. * problems described in {IERC20-approve}.
  168. *
  169. * Emits an {Approval} event indicating the updated allowance.
  170. *
  171. * Requirements:
  172. *
  173. * - `spender` cannot be the zero address.
  174. * - `spender` must have allowance for the caller of at least
  175. * `subtractedValue`.
  176. */
  177. function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
  178. uint256 currentAllowance = _allowances[_msgSender()][spender];
  179. require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
  180. unchecked {
  181. _approve(_msgSender(), spender, currentAllowance - subtractedValue);
  182. }
  183. return true;
  184. }
  185. /**
  186. * @dev Moves `amount` of tokens from `sender` to `recipient`.
  187. *
  188. * This internal function is equivalent to {transfer}, and can be used to
  189. * e.g. implement automatic token fees, slashing mechanisms, etc.
  190. *
  191. * Emits a {Transfer} event.
  192. *
  193. * Requirements:
  194. *
  195. * - `sender` cannot be the zero address.
  196. * - `recipient` cannot be the zero address.
  197. * - `sender` must have a balance of at least `amount`.
  198. */
  199. function _transfer(
  200. address sender,
  201. address recipient,
  202. uint256 amount
  203. ) internal virtual {
  204. require(sender != address(0), "ERC20: transfer from the zero address");
  205. require(recipient != address(0), "ERC20: transfer to the zero address");
  206. _beforeTokenTransfer(sender, recipient, amount);
  207. uint256 senderBalance = _balances[sender];
  208. require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
  209. unchecked {
  210. _balances[sender] = senderBalance - amount;
  211. }
  212. _balances[recipient] += amount;
  213. emit Transfer(sender, recipient, amount);
  214. _afterTokenTransfer(sender, recipient, amount);
  215. }
  216. /** @dev Creates `amount` tokens and assigns them to `account`, increasing
  217. * the total supply.
  218. *
  219. * Emits a {Transfer} event with `from` set to the zero address.
  220. *
  221. * Requirements:
  222. *
  223. * - `account` cannot be the zero address.
  224. */
  225. function _mint(address account, uint256 amount) internal virtual {
  226. require(account != address(0), "ERC20: mint to the zero address");
  227. _beforeTokenTransfer(address(0), account, amount);
  228. _totalSupply += amount;
  229. _balances[account] += amount;
  230. emit Transfer(address(0), account, amount);
  231. _afterTokenTransfer(address(0), account, amount);
  232. }
  233. /**
  234. * @dev Destroys `amount` tokens from `account`, reducing the
  235. * total supply.
  236. *
  237. * Emits a {Transfer} event with `to` set to the zero address.
  238. *
  239. * Requirements:
  240. *
  241. * - `account` cannot be the zero address.
  242. * - `account` must have at least `amount` tokens.
  243. */
  244. function _burn(address account, uint256 amount) internal virtual {
  245. require(account != address(0), "ERC20: burn from the zero address");
  246. _beforeTokenTransfer(account, address(0), amount);
  247. uint256 accountBalance = _balances[account];
  248. require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
  249. unchecked {
  250. _balances[account] = accountBalance - amount;
  251. }
  252. _totalSupply -= amount;
  253. emit Transfer(account, address(0), amount);
  254. _afterTokenTransfer(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(
  270. address owner,
  271. address spender,
  272. uint256 amount
  273. ) internal virtual {
  274. require(owner != address(0), "ERC20: approve from the zero address");
  275. require(spender != address(0), "ERC20: approve to the zero address");
  276. _allowances[owner][spender] = amount;
  277. emit Approval(owner, spender, amount);
  278. }
  279. /**
  280. * @dev Hook that is called before any transfer of tokens. This includes
  281. * minting and burning.
  282. *
  283. * Calling conditions:
  284. *
  285. * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  286. * will be transferred to `to`.
  287. * - when `from` is zero, `amount` tokens will be minted for `to`.
  288. * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
  289. * - `from` and `to` are never both zero.
  290. *
  291. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  292. */
  293. function _beforeTokenTransfer(
  294. address from,
  295. address to,
  296. uint256 amount
  297. ) internal virtual {}
  298. /**
  299. * @dev Hook that is called after any transfer of tokens. This includes
  300. * minting and burning.
  301. *
  302. * Calling conditions:
  303. *
  304. * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  305. * has been transferred to `to`.
  306. * - when `from` is zero, `amount` tokens have been minted for `to`.
  307. * - when `to` is zero, `amount` of ``from``'s tokens have been burned.
  308. * - `from` and `to` are never both zero.
  309. *
  310. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  311. */
  312. function _afterTokenTransfer(
  313. address from,
  314. address to,
  315. uint256 amount
  316. ) internal virtual {}
  317. }