StandardToken.sol 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. pragma solidity ^0.4.24;
  2. import "./BasicToken.sol";
  3. import "./ERC20.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, 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. * Beware that changing an allowance with this method brings the risk that someone may use both the old
  39. * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
  40. * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
  41. * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  42. * @param _spender The address which will spend the funds.
  43. * @param _value The amount of tokens to be spent.
  44. */
  45. function approve(address _spender, uint256 _value) public returns (bool) {
  46. allowed[msg.sender][_spender] = _value;
  47. emit Approval(msg.sender, _spender, _value);
  48. return true;
  49. }
  50. /**
  51. * @dev Function to check the amount of tokens that an owner allowed to a spender.
  52. * @param _owner address The address which owns the funds.
  53. * @param _spender address The address which will spend the funds.
  54. * @return A uint256 specifying the amount of tokens still available for the spender.
  55. */
  56. function allowance(
  57. address _owner,
  58. address _spender
  59. )
  60. public
  61. view
  62. returns (uint256)
  63. {
  64. return allowed[_owner][_spender];
  65. }
  66. /**
  67. * @dev Increase the amount of tokens that an owner allowed to a spender.
  68. * approve should be called when allowed[_spender] == 0. To increment
  69. * allowed value is better to use this function to avoid 2 calls (and wait until
  70. * the first transaction is mined)
  71. * From MonolithDAO Token.sol
  72. * @param _spender The address which will spend the funds.
  73. * @param _addedValue The amount of tokens to increase the allowance by.
  74. */
  75. function increaseApproval(
  76. address _spender,
  77. uint256 _addedValue
  78. )
  79. public
  80. returns (bool)
  81. {
  82. allowed[msg.sender][_spender] = (
  83. allowed[msg.sender][_spender].add(_addedValue));
  84. emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  85. return true;
  86. }
  87. /**
  88. * @dev Decrease the amount of tokens that an owner allowed to a spender.
  89. * approve should be called when allowed[_spender] == 0. To decrement
  90. * allowed value is better to use this function to avoid 2 calls (and wait until
  91. * the first transaction is mined)
  92. * From MonolithDAO Token.sol
  93. * @param _spender The address which will spend the funds.
  94. * @param _subtractedValue The amount of tokens to decrease the allowance by.
  95. */
  96. function decreaseApproval(
  97. address _spender,
  98. uint256 _subtractedValue
  99. )
  100. public
  101. returns (bool)
  102. {
  103. uint256 oldValue = allowed[msg.sender][_spender];
  104. if (_subtractedValue > oldValue) {
  105. allowed[msg.sender][_spender] = 0;
  106. } else {
  107. allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
  108. }
  109. emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  110. return true;
  111. }
  112. }