ERC20.sol 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. pragma solidity ^0.5.0;
  2. import "./IERC20.sol";
  3. import "../../math/SafeMath.sol";
  4. /**
  5. * @dev Implementation of the {IERC20} interface.
  6. *
  7. * This implementation is agnostic to the way tokens are created. This means
  8. * that a supply mechanism has to be added in a derived contract using {_mint}.
  9. * For a generic mechanism see {ERC20Mintable}.
  10. *
  11. * TIP: For a detailed writeup see our guide
  12. * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
  13. * to implement supply mechanisms].
  14. *
  15. * We have followed general OpenZeppelin guidelines: functions revert instead
  16. * of returning `false` on failure. This behavior is nonetheless conventional
  17. * and does not conflict with the expectations of ERC20 applications.
  18. *
  19. * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
  20. * This allows applications to reconstruct the allowance for all accounts just
  21. * by listening to said events. Other implementations of the EIP may not emit
  22. * these events, as it isn't required by the specification.
  23. *
  24. * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
  25. * functions have been added to mitigate the well-known issues around setting
  26. * allowances. See {IERC20-approve}.
  27. */
  28. contract ERC20 is IERC20 {
  29. using SafeMath for uint256;
  30. mapping (address => uint256) private _balances;
  31. mapping (address => mapping (address => uint256)) private _allowances;
  32. uint256 private _totalSupply;
  33. /**
  34. * @dev See {IERC20-totalSupply}.
  35. */
  36. function totalSupply() public view returns (uint256) {
  37. return _totalSupply;
  38. }
  39. /**
  40. * @dev See {IERC20-balanceOf}.
  41. */
  42. function balanceOf(address account) public view returns (uint256) {
  43. return _balances[account];
  44. }
  45. /**
  46. * @dev See {IERC20-transfer}.
  47. *
  48. * Requirements:
  49. *
  50. * - `recipient` cannot be the zero address.
  51. * - the caller must have a balance of at least `amount`.
  52. */
  53. function transfer(address recipient, uint256 amount) public returns (bool) {
  54. _transfer(msg.sender, recipient, amount);
  55. return true;
  56. }
  57. /**
  58. * @dev See {IERC20-allowance}.
  59. */
  60. function allowance(address owner, address spender) public view returns (uint256) {
  61. return _allowances[owner][spender];
  62. }
  63. /**
  64. * @dev See {IERC20-approve}.
  65. *
  66. * Requirements:
  67. *
  68. * - `spender` cannot be the zero address.
  69. */
  70. function approve(address spender, uint256 value) public returns (bool) {
  71. _approve(msg.sender, spender, value);
  72. return true;
  73. }
  74. /**
  75. * @dev See {IERC20-transferFrom}.
  76. *
  77. * Emits an {Approval} event indicating the updated allowance. This is not
  78. * required by the EIP. See the note at the beginning of {ERC20};
  79. *
  80. * Requirements:
  81. * - `sender` and `recipient` cannot be the zero address.
  82. * - `sender` must have a balance of at least `value`.
  83. * - the caller must have allowance for `sender`'s tokens of at least
  84. * `amount`.
  85. */
  86. function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) {
  87. _transfer(sender, recipient, amount);
  88. _approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount));
  89. return true;
  90. }
  91. /**
  92. * @dev Atomically increases the allowance granted to `spender` by the caller.
  93. *
  94. * This is an alternative to {approve} that can be used as a mitigation for
  95. * problems described in {IERC20-approve}.
  96. *
  97. * Emits an {Approval} event indicating the updated allowance.
  98. *
  99. * Requirements:
  100. *
  101. * - `spender` cannot be the zero address.
  102. */
  103. function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
  104. _approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue));
  105. return true;
  106. }
  107. /**
  108. * @dev Atomically decreases the allowance granted to `spender` by the caller.
  109. *
  110. * This is an alternative to {approve} that can be used as a mitigation for
  111. * problems described in {IERC20-approve}.
  112. *
  113. * Emits an {Approval} event indicating the updated allowance.
  114. *
  115. * Requirements:
  116. *
  117. * - `spender` cannot be the zero address.
  118. * - `spender` must have allowance for the caller of at least
  119. * `subtractedValue`.
  120. */
  121. function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
  122. _approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue));
  123. return true;
  124. }
  125. /**
  126. * @dev Moves tokens `amount` from `sender` to `recipient`.
  127. *
  128. * This is internal function is equivalent to {transfer}, and can be used to
  129. * e.g. implement automatic token fees, slashing mechanisms, etc.
  130. *
  131. * Emits a {Transfer} event.
  132. *
  133. * Requirements:
  134. *
  135. * - `sender` cannot be the zero address.
  136. * - `recipient` cannot be the zero address.
  137. * - `sender` must have a balance of at least `amount`.
  138. */
  139. function _transfer(address sender, address recipient, uint256 amount) internal {
  140. require(sender != address(0), "ERC20: transfer from the zero address");
  141. require(recipient != address(0), "ERC20: transfer to the zero address");
  142. _balances[sender] = _balances[sender].sub(amount);
  143. _balances[recipient] = _balances[recipient].add(amount);
  144. emit Transfer(sender, recipient, amount);
  145. }
  146. /** @dev Creates `amount` tokens and assigns them to `account`, increasing
  147. * the total supply.
  148. *
  149. * Emits a {Transfer} event with `from` set to the zero address.
  150. *
  151. * Requirements
  152. *
  153. * - `to` cannot be the zero address.
  154. */
  155. function _mint(address account, uint256 amount) internal {
  156. require(account != address(0), "ERC20: mint to the zero address");
  157. _totalSupply = _totalSupply.add(amount);
  158. _balances[account] = _balances[account].add(amount);
  159. emit Transfer(address(0), account, amount);
  160. }
  161. /**
  162. * @dev Destroys `amount` tokens from `account`, reducing the
  163. * total supply.
  164. *
  165. * Emits a {Transfer} event with `to` set to the zero address.
  166. *
  167. * Requirements
  168. *
  169. * - `account` cannot be the zero address.
  170. * - `account` must have at least `amount` tokens.
  171. */
  172. function _burn(address account, uint256 value) internal {
  173. require(account != address(0), "ERC20: burn from the zero address");
  174. _totalSupply = _totalSupply.sub(value);
  175. _balances[account] = _balances[account].sub(value);
  176. emit Transfer(account, address(0), value);
  177. }
  178. /**
  179. * @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens.
  180. *
  181. * This is internal function is equivalent to `approve`, and can be used to
  182. * e.g. set automatic allowances for certain subsystems, etc.
  183. *
  184. * Emits an {Approval} event.
  185. *
  186. * Requirements:
  187. *
  188. * - `owner` cannot be the zero address.
  189. * - `spender` cannot be the zero address.
  190. */
  191. function _approve(address owner, address spender, uint256 value) internal {
  192. require(owner != address(0), "ERC20: approve from the zero address");
  193. require(spender != address(0), "ERC20: approve to the zero address");
  194. _allowances[owner][spender] = value;
  195. emit Approval(owner, spender, value);
  196. }
  197. /**
  198. * @dev Destoys `amount` tokens from `account`.`amount` is then deducted
  199. * from the caller's allowance.
  200. *
  201. * See {_burn} and {_approve}.
  202. */
  203. function _burnFrom(address account, uint256 amount) internal {
  204. _burn(account, amount);
  205. _approve(account, msg.sender, _allowances[account][msg.sender].sub(amount));
  206. }
  207. }