ERC20.sol 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "../../GSN/Context.sol";
  4. import "./IERC20.sol";
  5. import "../../math/SafeMath.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 guidelines: functions revert instead
  18. * of returning `false` on failure. This behavior is nonetheless conventional
  19. * and does not conflict with the expectations of ERC20 applications.
  20. *
  21. * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
  22. * This allows applications to reconstruct the allowance for all accounts just
  23. * by listening to said events. Other implementations of the EIP may not emit
  24. * these events, as it isn't required by the specification.
  25. *
  26. * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
  27. * functions have been added to mitigate the well-known issues around setting
  28. * allowances. See {IERC20-approve}.
  29. */
  30. contract ERC20 is Context, IERC20 {
  31. using SafeMath for uint256;
  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. uint8 private _decimals;
  38. /**
  39. * @dev Sets the values for {name} and {symbol}, initializes {decimals} with
  40. * a default value of 18.
  41. *
  42. * To select a different value for {decimals}, use {_setupDecimals}.
  43. *
  44. * All three of these values are immutable: they can only be set once during
  45. * construction.
  46. */
  47. constructor (string memory name, string memory symbol) public {
  48. _name = name;
  49. _symbol = symbol;
  50. _decimals = 18;
  51. }
  52. /**
  53. * @dev Returns the name of the token.
  54. */
  55. function name() public view returns (string memory) {
  56. return _name;
  57. }
  58. /**
  59. * @dev Returns the symbol of the token, usually a shorter version of the
  60. * name.
  61. */
  62. function symbol() public view returns (string memory) {
  63. return _symbol;
  64. }
  65. /**
  66. * @dev Returns the number of decimals used to get its user representation.
  67. * For example, if `decimals` equals `2`, a balance of `505` tokens should
  68. * be displayed to a user as `5,05` (`505 / 10 ** 2`).
  69. *
  70. * Tokens usually opt for a value of 18, imitating the relationship between
  71. * Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
  72. * called.
  73. *
  74. * NOTE: This information is only used for _display_ purposes: it in
  75. * no way affects any of the arithmetic of the contract, including
  76. * {IERC20-balanceOf} and {IERC20-transfer}.
  77. */
  78. function decimals() public view returns (uint8) {
  79. return _decimals;
  80. }
  81. /**
  82. * @dev See {IERC20-totalSupply}.
  83. */
  84. function totalSupply() public view override returns (uint256) {
  85. return _totalSupply;
  86. }
  87. /**
  88. * @dev See {IERC20-balanceOf}.
  89. */
  90. function balanceOf(address account) public view override returns (uint256) {
  91. return _balances[account];
  92. }
  93. /**
  94. * @dev See {IERC20-transfer}.
  95. *
  96. * Requirements:
  97. *
  98. * - `recipient` cannot be the zero address.
  99. * - the caller must have a balance of at least `amount`.
  100. */
  101. function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
  102. _transfer(_msgSender(), recipient, amount);
  103. return true;
  104. }
  105. /**
  106. * @dev See {IERC20-allowance}.
  107. */
  108. function allowance(address owner, address spender) public view virtual override returns (uint256) {
  109. return _allowances[owner][spender];
  110. }
  111. /**
  112. * @dev See {IERC20-approve}.
  113. *
  114. * Requirements:
  115. *
  116. * - `spender` cannot be the zero address.
  117. */
  118. function approve(address spender, uint256 amount) public virtual override returns (bool) {
  119. _approve(_msgSender(), spender, amount);
  120. return true;
  121. }
  122. /**
  123. * @dev See {IERC20-transferFrom}.
  124. *
  125. * Emits an {Approval} event indicating the updated allowance. This is not
  126. * required by the EIP. See the note at the beginning of {ERC20};
  127. *
  128. * Requirements:
  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(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
  135. _transfer(sender, recipient, amount);
  136. _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
  137. return true;
  138. }
  139. /**
  140. * @dev Atomically increases the allowance granted to `spender` by the caller.
  141. *
  142. * This is an alternative to {approve} that can be used as a mitigation for
  143. * problems described in {IERC20-approve}.
  144. *
  145. * Emits an {Approval} event indicating the updated allowance.
  146. *
  147. * Requirements:
  148. *
  149. * - `spender` cannot be the zero address.
  150. */
  151. function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
  152. _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
  153. return true;
  154. }
  155. /**
  156. * @dev Atomically decreases the allowance granted to `spender` by the caller.
  157. *
  158. * This is an alternative to {approve} that can be used as a mitigation for
  159. * problems described in {IERC20-approve}.
  160. *
  161. * Emits an {Approval} event indicating the updated allowance.
  162. *
  163. * Requirements:
  164. *
  165. * - `spender` cannot be the zero address.
  166. * - `spender` must have allowance for the caller of at least
  167. * `subtractedValue`.
  168. */
  169. function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
  170. _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
  171. return true;
  172. }
  173. /**
  174. * @dev Moves tokens `amount` from `sender` to `recipient`.
  175. *
  176. * This is internal function is equivalent to {transfer}, and can be used to
  177. * e.g. implement automatic token fees, slashing mechanisms, etc.
  178. *
  179. * Emits a {Transfer} event.
  180. *
  181. * Requirements:
  182. *
  183. * - `sender` cannot be the zero address.
  184. * - `recipient` cannot be the zero address.
  185. * - `sender` must have a balance of at least `amount`.
  186. */
  187. function _transfer(address sender, address recipient, uint256 amount) internal virtual {
  188. require(sender != address(0), "ERC20: transfer from the zero address");
  189. require(recipient != address(0), "ERC20: transfer to the zero address");
  190. _beforeTokenTransfer(sender, recipient, amount);
  191. _balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");
  192. _balances[recipient] = _balances[recipient].add(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 = _totalSupply.add(amount);
  208. _balances[account] = _balances[account].add(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. _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
  226. _totalSupply = _totalSupply.sub(amount);
  227. emit Transfer(account, address(0), amount);
  228. }
  229. /**
  230. * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
  231. *
  232. * This internal function is equivalent to `approve`, and can be used to
  233. * e.g. set automatic allowances for certain subsystems, etc.
  234. *
  235. * Emits an {Approval} event.
  236. *
  237. * Requirements:
  238. *
  239. * - `owner` cannot be the zero address.
  240. * - `spender` cannot be the zero address.
  241. */
  242. function _approve(address owner, address spender, uint256 amount) internal virtual {
  243. require(owner != address(0), "ERC20: approve from the zero address");
  244. require(spender != address(0), "ERC20: approve to the zero address");
  245. _allowances[owner][spender] = amount;
  246. emit Approval(owner, spender, amount);
  247. }
  248. /**
  249. * @dev Sets {decimals} to a value other than the default one of 18.
  250. *
  251. * WARNING: This function should only be called from the constructor. Most
  252. * applications that interact with token contracts will not expect
  253. * {decimals} to ever change, and may work incorrectly if it does.
  254. */
  255. function _setupDecimals(uint8 decimals_) internal {
  256. _decimals = decimals_;
  257. }
  258. /**
  259. * @dev Hook that is called before any transfer of tokens. This includes
  260. * minting and burning.
  261. *
  262. * Calling conditions:
  263. *
  264. * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
  265. * will be to transferred to `to`.
  266. * - when `from` is zero, `amount` tokens will be minted for `to`.
  267. * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
  268. * - `from` and `to` are never both zero.
  269. *
  270. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  271. */
  272. function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
  273. }