ERC20.sol 10 KB

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