StandardToken.sol 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. pragma solidity ^0.4.8;
  2. import './BasicToken.sol';
  3. import './ERC20.sol';
  4. /**
  5. * Standard ERC20 token
  6. *
  7. * https://github.com/ethereum/EIPs/issues/20
  8. * Based on code by FirstBlood:
  9. * 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. function transferFrom(address _from, address _to, uint _value) onlyPayloadSize(3 * 32) {
  14. var _allowance = allowed[_from][msg.sender];
  15. // Check is not needed because sub(_allowance, _value) will already throw if this condition is not met
  16. // if (_value > _allowance) throw;
  17. balances[_to] = balances[_to].add(_value);
  18. balances[_from] = balances[_from].sub(_value);
  19. allowed[_from][msg.sender] = _allowance.sub(_value);
  20. Transfer(_from, _to, _value);
  21. }
  22. function approve(address _spender, uint _value) {
  23. // To change the approve amount you first have to reduce the addresses`
  24. // allowance to zero by calling `approve(_spender, 0)` if it is not
  25. // already 0 to mitigate the race condition described here:
  26. // https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  27. if ((_value != 0) && (allowed[msg.sender][_spender] != 0)) throw;
  28. allowed[msg.sender][_spender] = _value;
  29. Approval(msg.sender, _spender, _value);
  30. }
  31. function allowance(address _owner, address _spender) constant returns (uint remaining) {
  32. return allowed[_owner][_spender];
  33. }
  34. }