StandardToken.sol 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. pragma solidity ^0.4.24;
  2. import "./ERC20.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. * Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
  10. */
  11. contract StandardToken is ERC20 {
  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. allowed[msg.sender][_spender] = _value;
  70. emit Approval(msg.sender, _spender, _value);
  71. return true;
  72. }
  73. /**
  74. * @dev Transfer tokens from one address to another
  75. * @param _from address The address which you want to send tokens from
  76. * @param _to address The address which you want to transfer to
  77. * @param _value uint256 the amount of tokens to be transferred
  78. */
  79. function transferFrom(
  80. address _from,
  81. address _to,
  82. uint256 _value
  83. )
  84. public
  85. returns (bool)
  86. {
  87. require(_value <= balances[_from]);
  88. require(_value <= allowed[_from][msg.sender]);
  89. require(_to != address(0));
  90. balances[_from] = balances[_from].sub(_value);
  91. balances[_to] = balances[_to].add(_value);
  92. allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
  93. emit Transfer(_from, _to, _value);
  94. return true;
  95. }
  96. /**
  97. * @dev Increase the amount of tokens that an owner allowed to a spender.
  98. * approve should be called when allowed[_spender] == 0. To increment
  99. * allowed value is better to use this function to avoid 2 calls (and wait until
  100. * the first transaction is mined)
  101. * From MonolithDAO Token.sol
  102. * @param _spender The address which will spend the funds.
  103. * @param _addedValue The amount of tokens to increase the allowance by.
  104. */
  105. function increaseApproval(
  106. address _spender,
  107. uint256 _addedValue
  108. )
  109. public
  110. returns (bool)
  111. {
  112. allowed[msg.sender][_spender] = (
  113. allowed[msg.sender][_spender].add(_addedValue));
  114. emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  115. return true;
  116. }
  117. /**
  118. * @dev Decrease the amount of tokens that an owner allowed to a spender.
  119. * approve should be called when allowed[_spender] == 0. To decrement
  120. * allowed value is better to use this function to avoid 2 calls (and wait until
  121. * the first transaction is mined)
  122. * From MonolithDAO Token.sol
  123. * @param _spender The address which will spend the funds.
  124. * @param _subtractedValue The amount of tokens to decrease the allowance by.
  125. */
  126. function decreaseApproval(
  127. address _spender,
  128. uint256 _subtractedValue
  129. )
  130. public
  131. returns (bool)
  132. {
  133. uint256 oldValue = allowed[msg.sender][_spender];
  134. if (_subtractedValue >= oldValue) {
  135. allowed[msg.sender][_spender] = 0;
  136. } else {
  137. allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
  138. }
  139. emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  140. return true;
  141. }
  142. /**
  143. * @dev Internal function that mints an amount of the token and assigns it to
  144. * an account. This encapsulates the modification of balances such that the
  145. * proper events are emitted.
  146. * @param _account The account that will receive the created tokens.
  147. * @param _amount The amount that will be created.
  148. */
  149. function _mint(address _account, uint256 _amount) internal {
  150. require(_account != 0);
  151. totalSupply_ = totalSupply_.add(_amount);
  152. balances[_account] = balances[_account].add(_amount);
  153. emit Transfer(address(0), _account, _amount);
  154. }
  155. /**
  156. * @dev Internal function that burns an amount of the token of a given
  157. * account.
  158. * @param _account The account whose tokens will be burnt.
  159. * @param _amount The amount that will be burnt.
  160. */
  161. function _burn(address _account, uint256 _amount) internal {
  162. require(_account != 0);
  163. require(_amount <= balances[_account]);
  164. totalSupply_ = totalSupply_.sub(_amount);
  165. balances[_account] = balances[_account].sub(_amount);
  166. emit Transfer(_account, address(0), _amount);
  167. }
  168. /**
  169. * @dev Internal function that burns an amount of the token of a given
  170. * account, deducting from the sender's allowance for said account. Uses the
  171. * internal _burn function.
  172. * @param _account The account whose tokens will be burnt.
  173. * @param _amount The amount that will be burnt.
  174. */
  175. function _burnFrom(address _account, uint256 _amount) internal {
  176. require(_amount <= allowed[_account][msg.sender]);
  177. // Should https://github.com/OpenZeppelin/zeppelin-solidity/issues/707 be accepted,
  178. // this function needs to emit an event with the updated approval.
  179. allowed[_account][msg.sender] = allowed[_account][msg.sender].sub(_amount);
  180. _burn(_account, _amount);
  181. }
  182. }