Standard_Token.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*Most, basic default, standardised Token contract.
  2. Allows the creation of a token with a finite issued amount to the creator.
  3. This can't be changed.
  4. Based on standardised APIs & slightly extended. https://github.com/ethereum/wiki/wiki/Standardized_Contract_APIs
  5. adds AddressApproval & AddressApprovalOnce events
  6. approve & approveOnce works on premise that approved always takes precedence.
  7. adds unapprove to basic coin interface.*/
  8. contract Coin {
  9. function sendCoin(uint _value, address _to) returns (bool _success) {}
  10. function sendCoinFrom(address _from, uint _value, address _to) returns (bool _success) {}
  11. function coinBalance() constant returns (uint _r) {}
  12. function coinBalanceOf(address _addr) constant returns (uint _r) {}
  13. function approve(address _addr) {}
  14. function approveOnce(address _addr, uint256 _maxValue) {}
  15. function unapprove(address _addr) {}
  16. function isApproved(address _proxy) constant returns (bool _r) {}
  17. function isApprovedFor(address _target, address _proxy) constant returns (bool _r) {}
  18. }
  19. contract Standard_Token is Coin {
  20. function Standard_Token(uint _initialAmount) {
  21. balances[msg.sender] = _initialAmount;
  22. }
  23. event CoinTransfer(address indexed from, address indexed to, uint256 value);
  24. event AddressApproval(address indexed from, address indexed to, bool result);
  25. event AddressApprovalOnce(address indexed from, address indexed to, uint256 value);
  26. function sendCoin(uint _value, address _to) returns (bool _success) {
  27. if (balances[msg.sender] >= _value) {
  28. balances[msg.sender] -= _value;
  29. balances[_to] += _value;
  30. CoinTransfer(msg.sender, _to, _value);
  31. return true;
  32. } else { return false; }
  33. }
  34. function sendCoinFrom(address _from, uint _value, address _to) returns (bool _success) {
  35. if (balances[_from] >= _value) {
  36. bool transfer = false;
  37. if(approved[_from][msg.sender]) {
  38. transfer = true;
  39. } else {
  40. if(_value < approved_once[_from][msg.sender]) {
  41. transfer = true;
  42. approved_once[_from][msg.sender] = 0; //reset
  43. }
  44. }
  45. if(transfer == true) {
  46. balances[_from] -= _value;
  47. balances[_to] += _value;
  48. CoinTransfer(_from, _to, _value);
  49. return true;
  50. } else { return false; }
  51. }
  52. }
  53. function coinBalance() constant returns (uint _r) {
  54. return balances[msg.sender];
  55. }
  56. function coinBalanceOf(address _addr) constant returns (uint _r) {
  57. return balances[_addr];
  58. }
  59. function approve(address _addr) {
  60. approved[msg.sender][_addr] = true;
  61. AddressApproval(msg.sender, _addr, true);
  62. }
  63. function approveOnce(address _addr, uint256 _maxValue) {
  64. approved_once[msg.sender][_addr] = _maxValue;
  65. AddressApprovalOnce(msg.sender, _addr, _maxValue);
  66. }
  67. function unapprove(address _addr) {
  68. approved[msg.sender][_addr] = false;
  69. approved_once[msg.sender][_addr] = 0;
  70. AddressApproval(msg.sender, _addr, false);
  71. AddressApprovalOnce(msg.sender, _addr, 0);
  72. }
  73. function isApproved(address _proxy) constant returns (bool _r) {
  74. if(approved[msg.sender][_proxy] == true || approved_once[msg.sender][_proxy] > 0) {
  75. return true;
  76. }
  77. }
  78. function isApprovedFor(address _target, address _proxy) constant returns (bool _r) {
  79. if(approved[_target][_proxy] == true || approved_once[_target][_proxy] > 0) {
  80. return true;
  81. }
  82. }
  83. mapping (address => uint) public balances;
  84. mapping (address => mapping (address => bool)) public approved;
  85. mapping (address => mapping (address => uint256)) public approved_once;
  86. }