ERC20.sol 6.7 KB

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