StandardToken.sol 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. pragma solidity ^0.4.11;
  2. import './BasicToken.sol';
  3. import './ERC20.sol';
  4. /**
  5. * @title Standard ERC20 token
  6. *
  7. * @dev Implemantation of the basic standart 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 BasicToken, ERC20 {
  12. mapping (address => mapping (address => uint)) 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 uint the amout of tokens to be transfered
  18. */
  19. function transferFrom(address _from, address _to, uint _value) onlyPayloadSize(3 * 32) {
  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. // if (_value > _allowance) throw;
  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. }
  28. /**
  29. * @dev Aprove the passed address to spend the specified amount of tokens on beahlf of msg.sender.
  30. * @param _spender The address which will spend the funds.
  31. * @param _value The amount of tokens to be spent.
  32. */
  33. function approve(address _spender, uint _value) {
  34. // To change the approve amount you first have to reduce the addresses`
  35. // allowance to zero by calling `approve(_spender, 0)` if it is not
  36. // already 0 to mitigate the race condition described here:
  37. // https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  38. if ((_value != 0) && (allowed[msg.sender][_spender] != 0)) throw;
  39. allowed[msg.sender][_spender] = _value;
  40. Approval(msg.sender, _spender, _value);
  41. }
  42. /**
  43. * @dev Function to check the amount of tokens than an owner allowed to a spender.
  44. * @param _owner address The address which owns the funds.
  45. * @param _spender address The address which will spend the funds.
  46. * @return A uint specifing the amount of tokens still avaible for the spender.
  47. */
  48. function allowance(address _owner, address _spender) constant returns (uint remaining) {
  49. return allowed[_owner][_spender];
  50. }
  51. }