ERC20.sol 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../../utils/Context.sol";
  4. import "./IERC20.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. require(_allowances[sender][_msgSender()] >= amount, "ERC20: transfer amount exceeds allowance");
  134. _approve(sender, _msgSender(), _allowances[sender][_msgSender()] - amount);
  135. return true;
  136. }
  137. /**
  138. * @dev Atomically increases the allowance granted to `spender` by the caller.
  139. *
  140. * This is an alternative to {approve} that can be used as a mitigation for
  141. * problems described in {IERC20-approve}.
  142. *
  143. * Emits an {Approval} event indicating the updated allowance.
  144. *
  145. * Requirements:
  146. *
  147. * - `spender` cannot be the zero address.
  148. */
  149. function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
  150. _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
  151. return true;
  152. }
  153. /**
  154. * @dev Atomically decreases the allowance granted to `spender` by the caller.
  155. *
  156. * This is an alternative to {approve} that can be used as a mitigation for
  157. * problems described in {IERC20-approve}.
  158. *
  159. * Emits an {Approval} event indicating the updated allowance.
  160. *
  161. * Requirements:
  162. *
  163. * - `spender` cannot be the zero address.
  164. * - `spender` must have allowance for the caller of at least
  165. * `subtractedValue`.
  166. */
  167. function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
  168. require(_allowances[_msgSender()][spender] >= subtractedValue, "ERC20: decreased allowance below zero");
  169. _approve(_msgSender(), spender, _allowances[_msgSender()][spender] - subtractedValue);
  170. return true;
  171. }
  172. /**
  173. * @dev Moves tokens `amount` from `sender` to `recipient`.
  174. *
  175. * This is internal function is equivalent to {transfer}, and can be used to
  176. * e.g. implement automatic token fees, slashing mechanisms, etc.
  177. *
  178. * Emits a {Transfer} event.
  179. *
  180. * Requirements:
  181. *
  182. * - `sender` cannot be the zero address.
  183. * - `recipient` cannot be the zero address.
  184. * - `sender` must have a balance of at least `amount`.
  185. */
  186. function _transfer(address sender, address recipient, uint256 amount) internal virtual {
  187. require(sender != address(0), "ERC20: transfer from the zero address");
  188. require(recipient != address(0), "ERC20: transfer to the zero address");
  189. _beforeTokenTransfer(sender, recipient, amount);
  190. require(_balances[sender] >= amount, "ERC20: transfer amount exceeds balance");
  191. _balances[sender] -= amount;
  192. _balances[recipient] += amount;
  193. emit Transfer(sender, recipient, amount);
  194. }
  195. /** @dev Creates `amount` tokens and assigns them to `account`, increasing
  196. * the total supply.
  197. *
  198. * Emits a {Transfer} event with `from` set to the zero address.
  199. *
  200. * Requirements:
  201. *
  202. * - `to` cannot be the zero address.
  203. */
  204. function _mint(address account, uint256 amount) internal virtual {
  205. require(account != address(0), "ERC20: mint to the zero address");
  206. _beforeTokenTransfer(address(0), account, amount);
  207. _totalSupply += amount;
  208. _balances[account] += amount;
  209. emit Transfer(address(0), account, amount);
  210. }
  211. /**
  212. * @dev Destroys `amount` tokens from `account`, reducing the
  213. * total supply.
  214. *
  215. * Emits a {Transfer} event with `to` set to the zero address.
  216. *
  217. * Requirements:
  218. *
  219. * - `account` cannot be the zero address.
  220. * - `account` must have at least `amount` tokens.
  221. */
  222. function _burn(address account, uint256 amount) internal virtual {
  223. require(account != address(0), "ERC20: burn from the zero address");
  224. _beforeTokenTransfer(account, address(0), amount);
  225. require(_balances[account] >= amount, "ERC20: burn amount exceeds balance");
  226. _balances[account] -= amount;
  227. _totalSupply -= amount;
  228. emit Transfer(account, address(0), amount);
  229. }
  230. /**
  231. * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
  232. *
  233. * This internal function is equivalent to `approve`, and can be used to
  234. * e.g. set automatic allowances for certain subsystems, etc.
  235. *
  236. * Emits an {Approval} event.
  237. *
  238. * Requirements:
  239. *
  240. * - `owner` cannot be the zero address.
  241. * - `spender` cannot be the zero address.
  242. */
  243. function _approve(address owner, address spender, uint256 amount) internal virtual {
  244. require(owner != address(0), "ERC20: approve from the zero address");
  245. require(spender != address(0), "ERC20: approve to the zero address");
  246. _allowances[owner][spender] = amount;
  247. emit Approval(owner, spender, amount);
  248. }
  249. /**
  250. * @dev Hook that is called before any transfer of tokens. This includes
  251. * minting and burning.
  252. *
  253. * Calling conditions:
  254. *
  255. * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  256. * will be to transferred to `to`.
  257. * - when `from` is zero, `amount` tokens will be minted for `to`.
  258. * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
  259. * - `from` and `to` are never both zero.
  260. *
  261. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  262. */
  263. function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
  264. }