ERC20.sol 11 KB

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