Token.sol 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Source: https://github.com/nexusdev/erc20
  2. // Flat file implementation of `dappsys/token/base.sol::DSTokenBase`
  3. // Everything throws instead of returning false on failure.
  4. import 'ERC20.sol';
  5. contract Token is ERC20
  6. {
  7. mapping( address => uint ) _balances;
  8. mapping( address => mapping( address => uint ) ) _approvals;
  9. uint _supply;
  10. function Token( uint initial_balance ) {
  11. _balances[msg.sender] = initial_balance;
  12. _supply = initial_balance;
  13. }
  14. function totalSupply() constant returns (uint supply) {
  15. return _supply;
  16. }
  17. function balanceOf( address who ) constant returns (uint value) {
  18. return _balances[who];
  19. }
  20. function transfer( address to, uint value) returns (bool ok) {
  21. if( _balances[msg.sender] < value ) {
  22. throw;
  23. }
  24. if( !safeToAdd(_balances[to], value) ) {
  25. throw;
  26. }
  27. _balances[msg.sender] -= value;
  28. _balances[to] += value;
  29. Transfer( msg.sender, to, value );
  30. return true;
  31. }
  32. function transferFrom( address from, address to, uint value) returns (bool ok) {
  33. // if you don't have enough balance, throw
  34. if( _balances[from] < value ) {
  35. throw;
  36. }
  37. // if you don't have approval, throw
  38. if( _approvals[from][msg.sender] < value ) {
  39. throw;
  40. }
  41. if( !safeToAdd(_balances[to], value) ) {
  42. throw;
  43. }
  44. // transfer and return true
  45. _approvals[from][msg.sender] -= value;
  46. _balances[from] -= value;
  47. _balances[to] += value;
  48. Transfer( from, to, value );
  49. return true;
  50. }
  51. function approve(address spender, uint value) returns (bool ok) {
  52. _approvals[msg.sender][spender] = value;
  53. Approval( msg.sender, spender, value );
  54. return true;
  55. }
  56. function allowance(address owner, address spender) constant returns (uint _allowance) {
  57. return _approvals[owner][spender];
  58. }
  59. function safeToAdd(uint a, uint b) internal returns (bool) {
  60. return (a + b >= a);
  61. }
  62. }