ERC20.sol 11 KB

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