ERC20.sol 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. pragma solidity ^0.5.0;
  2. import "./IERC20.sol";
  3. import "../../math/SafeMath.sol";
  4. /**
  5. * @title Standard ERC20 token
  6. *
  7. * @dev Implementation of the basic standard token.
  8. * https://eips.ethereum.org/EIPS/eip-20
  9. * Originally based on code by FirstBlood:
  10. * https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
  11. *
  12. * This implementation emits additional Approval events, allowing applications to reconstruct the allowance status for
  13. * all accounts just by listening to said events. Note that this isn't required by the specification, and other
  14. * compliant implementations may not do it.
  15. */
  16. contract ERC20 is IERC20 {
  17. using SafeMath for uint256;
  18. mapping (address => uint256) private _balances;
  19. mapping (address => mapping (address => uint256)) private _allowances;
  20. uint256 private _totalSupply;
  21. /**
  22. * @dev Total number of tokens in existence.
  23. */
  24. function totalSupply() public view returns (uint256) {
  25. return _totalSupply;
  26. }
  27. /**
  28. * @dev Gets the balance of the specified address.
  29. * @param owner The address to query the balance of.
  30. * @return A uint256 representing the amount owned by the passed address.
  31. */
  32. function balanceOf(address owner) public view returns (uint256) {
  33. return _balances[owner];
  34. }
  35. /**
  36. * @dev Function to check the amount of tokens that an owner allowed to a spender.
  37. * @param owner address The address which owns the funds.
  38. * @param spender address The address which will spend the funds.
  39. * @return A uint256 specifying the amount of tokens still available for the spender.
  40. */
  41. function allowance(address owner, address spender) public view returns (uint256) {
  42. return _allowances[owner][spender];
  43. }
  44. /**
  45. * @dev Transfer token to a specified address.
  46. * @param to The address to transfer to.
  47. * @param value The amount to be transferred.
  48. */
  49. function transfer(address to, uint256 value) public returns (bool) {
  50. _transfer(msg.sender, to, value);
  51. return true;
  52. }
  53. /**
  54. * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
  55. * Beware that changing an allowance with this method brings the risk that someone may use both the old
  56. * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
  57. * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
  58. * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  59. * @param spender The address which will spend the funds.
  60. * @param value The amount of tokens to be spent.
  61. */
  62. function approve(address spender, uint256 value) public returns (bool) {
  63. _approve(msg.sender, spender, value);
  64. return true;
  65. }
  66. /**
  67. * @dev Transfer tokens from one address to another.
  68. * Note that while this function emits an Approval event, this is not required as per the specification,
  69. * and other compliant implementations may not emit the event.
  70. * @param from address The address which you want to send tokens from
  71. * @param to address The address which you want to transfer to
  72. * @param value uint256 the amount of tokens to be transferred
  73. */
  74. function transferFrom(address from, address to, uint256 value) public returns (bool) {
  75. _transfer(from, to, value);
  76. _approve(from, msg.sender, _allowances[from][msg.sender].sub(value));
  77. return true;
  78. }
  79. /**
  80. * @dev Increase the amount of tokens that an owner allowed to a spender.
  81. * approve should be called when _allowances[msg.sender][spender] == 0. To increment
  82. * allowed value is better to use this function to avoid 2 calls (and wait until
  83. * the first transaction is mined)
  84. * From MonolithDAO Token.sol
  85. * Emits an Approval event.
  86. * @param spender The address which will spend the funds.
  87. * @param addedValue The amount of tokens to increase the allowance by.
  88. */
  89. function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
  90. _approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue));
  91. return true;
  92. }
  93. /**
  94. * @dev Decrease the amount of tokens that an owner allowed to a spender.
  95. * approve should be called when _allowances[msg.sender][spender] == 0. To decrement
  96. * allowed value is better to use this function to avoid 2 calls (and wait until
  97. * the first transaction is mined)
  98. * From MonolithDAO Token.sol
  99. * Emits an Approval event.
  100. * @param spender The address which will spend the funds.
  101. * @param subtractedValue The amount of tokens to decrease the allowance by.
  102. */
  103. function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
  104. _approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue));
  105. return true;
  106. }
  107. /**
  108. * @dev Transfer token for a specified addresses.
  109. * @param from The address to transfer from.
  110. * @param to The address to transfer to.
  111. * @param value The amount to be transferred.
  112. */
  113. function _transfer(address from, address to, uint256 value) internal {
  114. require(to != address(0), "ERC20: transfer to the zero address");
  115. _balances[from] = _balances[from].sub(value);
  116. _balances[to] = _balances[to].add(value);
  117. emit Transfer(from, to, value);
  118. }
  119. /**
  120. * @dev Internal function that mints an amount of the token and assigns it to
  121. * an account. This encapsulates the modification of balances such that the
  122. * proper events are emitted.
  123. * @param account The account that will receive the created tokens.
  124. * @param value The amount that will be created.
  125. */
  126. function _mint(address account, uint256 value) internal {
  127. require(account != address(0), "ERC20: mint to the zero address");
  128. _totalSupply = _totalSupply.add(value);
  129. _balances[account] = _balances[account].add(value);
  130. emit Transfer(address(0), account, value);
  131. }
  132. /**
  133. * @dev Internal function that burns an amount of the token of a given
  134. * account.
  135. * @param account The account whose tokens will be burnt.
  136. * @param value The amount that will be burnt.
  137. */
  138. function _burn(address account, uint256 value) internal {
  139. require(account != address(0), "ERC20: burn from the zero address");
  140. _totalSupply = _totalSupply.sub(value);
  141. _balances[account] = _balances[account].sub(value);
  142. emit Transfer(account, address(0), value);
  143. }
  144. /**
  145. * @dev Approve an address to spend another addresses' tokens.
  146. * @param owner The address that owns the tokens.
  147. * @param spender The address that will spend the tokens.
  148. * @param value The number of tokens that can be spent.
  149. */
  150. function _approve(address owner, address spender, uint256 value) internal {
  151. require(owner != address(0), "ERC20: approve from the zero address");
  152. require(spender != address(0), "ERC20: approve to the zero address");
  153. _allowances[owner][spender] = value;
  154. emit Approval(owner, spender, value);
  155. }
  156. /**
  157. * @dev Internal function that burns an amount of the token of a given
  158. * account, deducting from the sender's allowance for said account. Uses the
  159. * internal burn function.
  160. * Emits an Approval event (reflecting the reduced allowance).
  161. * @param account The account whose tokens will be burnt.
  162. * @param value The amount that will be burnt.
  163. */
  164. function _burnFrom(address account, uint256 value) internal {
  165. _burn(account, value);
  166. _approve(account, msg.sender, _allowances[account][msg.sender].sub(value));
  167. }
  168. }