StandardToken.sol 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. pragma solidity ^0.4.18;
  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(address _from, address _to, uint256 _value) public returns (bool) {
  20. require(_to != address(0));
  21. require(_value <= balances[_from]);
  22. require(_value <= allowed[_from][msg.sender]);
  23. balances[_from] = balances[_from].sub(_value);
  24. balances[_to] = balances[_to].add(_value);
  25. allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
  26. Transfer(_from, _to, _value);
  27. return true;
  28. }
  29. /**
  30. * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
  31. *
  32. * Beware that changing an allowance with this method brings the risk that someone may use both the old
  33. * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
  34. * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
  35. * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  36. * @param _spender The address which will spend the funds.
  37. * @param _value The amount of tokens to be spent.
  38. */
  39. function approve(address _spender, uint256 _value) public returns (bool) {
  40. allowed[msg.sender][_spender] = _value;
  41. Approval(msg.sender, _spender, _value);
  42. return true;
  43. }
  44. /**
  45. * @dev Function to check the amount of tokens that an owner allowed to a spender.
  46. * @param _owner address The address which owns the funds.
  47. * @param _spender address The address which will spend the funds.
  48. * @return A uint256 specifying the amount of tokens still available for the spender.
  49. */
  50. function allowance(address _owner, address _spender) public view returns (uint256) {
  51. return allowed[_owner][_spender];
  52. }
  53. /**
  54. * @dev Increase the amount of tokens that an owner allowed to a spender.
  55. *
  56. * approve should be called when allowed[_spender] == 0. To increment
  57. * allowed value is better to use this function to avoid 2 calls (and wait until
  58. * the first transaction is mined)
  59. * From MonolithDAO Token.sol
  60. * @param _spender The address which will spend the funds.
  61. * @param _addedValue The amount of tokens to increase the allowance by.
  62. */
  63. function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
  64. allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
  65. Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  66. return true;
  67. }
  68. /**
  69. * @dev Decrease the amount of tokens that an owner allowed to a spender.
  70. *
  71. * approve should be called when allowed[_spender] == 0. To decrement
  72. * allowed value is better to use this function to avoid 2 calls (and wait until
  73. * the first transaction is mined)
  74. * From MonolithDAO Token.sol
  75. * @param _spender The address which will spend the funds.
  76. * @param _subtractedValue The amount of tokens to decrease the allowance by.
  77. */
  78. function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
  79. uint oldValue = allowed[msg.sender][_spender];
  80. if (_subtractedValue > oldValue) {
  81. allowed[msg.sender][_spender] = 0;
  82. } else {
  83. allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
  84. }
  85. Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  86. return true;
  87. }
  88. }