StandardToken.sol 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. pragma solidity ^0.4.11;
  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)) 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 amout of tokens to be transfered
  18. */
  19. function transferFrom(address _from, address _to, uint256 _value) returns (bool) {
  20. var _allowance = allowed[_from][msg.sender];
  21. // Check is not needed because sub(_allowance, _value) will already throw if this condition is not met
  22. // require (_value <= _allowance);
  23. balances[_to] = balances[_to].add(_value);
  24. balances[_from] = balances[_from].sub(_value);
  25. allowed[_from][msg.sender] = _allowance.sub(_value);
  26. Transfer(_from, _to, _value);
  27. return true;
  28. }
  29. /**
  30. * @dev Aprove the passed address to spend the specified amount of tokens on behalf of msg.sender.
  31. * @param _spender The address which will spend the funds.
  32. * @param _value The amount of tokens to be spent.
  33. */
  34. function approve(address _spender, uint256 _value) returns (bool) {
  35. // To change the approve amount you first have to reduce the addresses`
  36. // allowance to zero by calling `approve(_spender, 0)` if it is not
  37. // already 0 to mitigate the race condition described here:
  38. // https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  39. require((_value == 0) || (allowed[msg.sender][_spender] == 0));
  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 specifing the amount of tokens still avaible for the spender.
  49. */
  50. function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
  51. return allowed[_owner][_spender];
  52. }
  53. }