ERC20.sol 7.4 KB

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