ERC20.sol 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
  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 _allowed;
  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 An 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 _allowed[owner][spender];
  43. }
  44. /**
  45. * @dev Transfer token for 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. require(spender != address(0));
  64. _allowed[msg.sender][spender] = value;
  65. emit Approval(msg.sender, spender, value);
  66. return true;
  67. }
  68. /**
  69. * @dev Transfer tokens from one address to another.
  70. * Note that while this function emits an Approval event, this is not required as per the specification,
  71. * and other compliant implementations may not emit the event.
  72. * @param from address The address which you want to send tokens from
  73. * @param to address The address which you want to transfer to
  74. * @param value uint256 the amount of tokens to be transferred
  75. */
  76. function transferFrom(address from, address to, uint256 value) public returns (bool) {
  77. _allowed[from][msg.sender] = _allowed[from][msg.sender].sub(value);
  78. _transfer(from, to, value);
  79. emit Approval(from, msg.sender, _allowed[from][msg.sender]);
  80. return true;
  81. }
  82. /**
  83. * @dev Increase the amount of tokens that an owner allowed to a spender.
  84. * approve should be called when allowed_[_spender] == 0. To increment
  85. * allowed value is better to use this function to avoid 2 calls (and wait until
  86. * the first transaction is mined)
  87. * From MonolithDAO Token.sol
  88. * Emits an Approval event.
  89. * @param spender The address which will spend the funds.
  90. * @param addedValue The amount of tokens to increase the allowance by.
  91. */
  92. function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
  93. require(spender != address(0));
  94. _allowed[msg.sender][spender] = _allowed[msg.sender][spender].add(addedValue);
  95. emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
  96. return true;
  97. }
  98. /**
  99. * @dev Decrease the amount of tokens that an owner allowed to a spender.
  100. * approve should be called when allowed_[_spender] == 0. To decrement
  101. * allowed value is better to use this function to avoid 2 calls (and wait until
  102. * the first transaction is mined)
  103. * From MonolithDAO Token.sol
  104. * Emits an Approval event.
  105. * @param spender The address which will spend the funds.
  106. * @param subtractedValue The amount of tokens to decrease the allowance by.
  107. */
  108. function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
  109. require(spender != address(0));
  110. _allowed[msg.sender][spender] = _allowed[msg.sender][spender].sub(subtractedValue);
  111. emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
  112. return true;
  113. }
  114. /**
  115. * @dev Transfer token for a specified addresses
  116. * @param from The address to transfer from.
  117. * @param to The address to transfer to.
  118. * @param value The amount to be transferred.
  119. */
  120. function _transfer(address from, address to, uint256 value) internal {
  121. require(to != address(0));
  122. _balances[from] = _balances[from].sub(value);
  123. _balances[to] = _balances[to].add(value);
  124. emit Transfer(from, to, value);
  125. }
  126. /**
  127. * @dev Internal function that mints an amount of the token and assigns it to
  128. * an account. This encapsulates the modification of balances such that the
  129. * proper events are emitted.
  130. * @param account The account that will receive the created tokens.
  131. * @param value The amount that will be created.
  132. */
  133. function _mint(address account, uint256 value) internal {
  134. require(account != address(0));
  135. _totalSupply = _totalSupply.add(value);
  136. _balances[account] = _balances[account].add(value);
  137. emit Transfer(address(0), account, value);
  138. }
  139. /**
  140. * @dev Internal function that burns an amount of the token of a given
  141. * account.
  142. * @param account The account whose tokens will be burnt.
  143. * @param value The amount that will be burnt.
  144. */
  145. function _burn(address account, uint256 value) internal {
  146. require(account != address(0));
  147. _totalSupply = _totalSupply.sub(value);
  148. _balances[account] = _balances[account].sub(value);
  149. emit Transfer(account, address(0), value);
  150. }
  151. /**
  152. * @dev Internal function that burns an amount of the token of a given
  153. * account, deducting from the sender's allowance for said account. Uses the
  154. * internal burn function.
  155. * Emits an Approval event (reflecting the reduced allowance).
  156. * @param account The account whose tokens will be burnt.
  157. * @param value The amount that will be burnt.
  158. */
  159. function _burnFrom(address account, uint256 value) internal {
  160. _allowed[account][msg.sender] = _allowed[account][msg.sender].sub(value);
  161. _burn(account, value);
  162. emit Approval(account, msg.sender, _allowed[account][msg.sender]);
  163. }
  164. }