StandardToken.sol 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. pragma solidity ^0.4.23;
  2. import "./BasicToken.sol";
  3. import "./ERC20.sol";
  4. /**
  5. * @title Standard ERC20 token
  6. *
  7. * @dev Implementation of the basic standard token.
  8. * @dev https://github.com/ethereum/EIPs/issues/20
  9. * @dev Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
  10. */
  11. contract StandardToken is ERC20, BasicToken {
  12. mapping (address => mapping (address => uint256)) internal allowed;
  13. /**
  14. * @dev Transfer tokens from one address to another
  15. * @param _from address The address which you want to send tokens from
  16. * @param _to address The address which you want to transfer to
  17. * @param _value uint256 the amount of tokens to be transferred
  18. */
  19. function transferFrom(
  20. address _from,
  21. address _to,
  22. uint256 _value
  23. )
  24. public
  25. returns (bool)
  26. {
  27. require(_to != address(0));
  28. require(_value <= balances[_from]);
  29. require(_value <= allowed[_from][msg.sender]);
  30. balances[_from] = balances[_from].sub(_value);
  31. balances[_to] = balances[_to].add(_value);
  32. allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
  33. emit Transfer(_from, _to, _value);
  34. return true;
  35. }
  36. /**
  37. * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
  38. *
  39. * Beware that changing an allowance with this method brings the risk that someone may use both the old
  40. * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
  41. * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
  42. * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  43. * @param _spender The address which will spend the funds.
  44. * @param _value The amount of tokens to be spent.
  45. */
  46. function approve(address _spender, uint256 _value) public returns (bool) {
  47. allowed[msg.sender][_spender] = _value;
  48. emit Approval(msg.sender, _spender, _value);
  49. return true;
  50. }
  51. /**
  52. * @dev Function to check the amount of tokens that an owner allowed to a spender.
  53. * @param _owner address The address which owns the funds.
  54. * @param _spender address The address which will spend the funds.
  55. * @return A uint256 specifying the amount of tokens still available for the spender.
  56. */
  57. function allowance(
  58. address _owner,
  59. address _spender
  60. )
  61. public
  62. view
  63. returns (uint256)
  64. {
  65. return allowed[_owner][_spender];
  66. }
  67. /**
  68. * @dev Increase the amount of tokens that an owner allowed to a spender.
  69. *
  70. * approve should be called when allowed[_spender] == 0. To increment
  71. * allowed value is better to use this function to avoid 2 calls (and wait until
  72. * the first transaction is mined)
  73. * From MonolithDAO Token.sol
  74. * @param _spender The address which will spend the funds.
  75. * @param _addedValue The amount of tokens to increase the allowance by.
  76. */
  77. function increaseApproval(
  78. address _spender,
  79. uint _addedValue
  80. )
  81. public
  82. returns (bool)
  83. {
  84. allowed[msg.sender][_spender] = (
  85. allowed[msg.sender][_spender].add(_addedValue));
  86. emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  87. return true;
  88. }
  89. /**
  90. * @dev Decrease the amount of tokens that an owner allowed to a spender.
  91. *
  92. * approve should be called when allowed[_spender] == 0. To decrement
  93. * allowed value is better to use this function to avoid 2 calls (and wait until
  94. * the first transaction is mined)
  95. * From MonolithDAO Token.sol
  96. * @param _spender The address which will spend the funds.
  97. * @param _subtractedValue The amount of tokens to decrease the allowance by.
  98. */
  99. function decreaseApproval(
  100. address _spender,
  101. uint _subtractedValue
  102. )
  103. public
  104. returns (bool)
  105. {
  106. uint oldValue = allowed[msg.sender][_spender];
  107. if (_subtractedValue > oldValue) {
  108. allowed[msg.sender][_spender] = 0;
  109. } else {
  110. allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
  111. }
  112. emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  113. return true;
  114. }
  115. }