StandardToken.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/issues/20
  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) balances;
  14. mapping (address => mapping (address => uint256)) internal allowed;
  15. uint256 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. }