Token.sol 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. mapping( address => uint ) _balances;
  7. mapping( address => mapping( address => uint ) ) _approvals;
  8. uint _supply;
  9. function Token( uint initial_balance ) {
  10. _balances[msg.sender] = initial_balance;
  11. _supply = initial_balance;
  12. }
  13. function totalSupply() constant returns (uint supply) {
  14. return _supply;
  15. }
  16. function balanceOf( address who ) constant returns (uint value) {
  17. return _balances[who];
  18. }
  19. function transfer( address to, uint value) returns (bool ok) {
  20. if( _balances[msg.sender] < value ) {
  21. throw;
  22. }
  23. if( !safeToAdd(_balances[to], value) ) {
  24. throw;
  25. }
  26. _balances[msg.sender] -= value;
  27. _balances[to] += value;
  28. Transfer( msg.sender, to, value );
  29. return true;
  30. }
  31. function transferFrom( address from, address to, uint value) returns (bool ok) {
  32. // if you don't have enough balance, throw
  33. if( _balances[from] < value ) {
  34. throw;
  35. }
  36. // if you don't have approval, throw
  37. if( _approvals[from][msg.sender] < value ) {
  38. throw;
  39. }
  40. if( !safeToAdd(_balances[to], value) ) {
  41. throw;
  42. }
  43. // transfer and return true
  44. _approvals[from][msg.sender] -= value;
  45. _balances[from] -= value;
  46. _balances[to] += value;
  47. Transfer( from, to, value );
  48. return true;
  49. }
  50. function approve(address spender, uint value) returns (bool ok) {
  51. _approvals[msg.sender][spender] = value;
  52. Approval( msg.sender, spender, value );
  53. return true;
  54. }
  55. function allowance(address owner, address spender) constant returns (uint _allowance) {
  56. return _approvals[owner][spender];
  57. }
  58. function safeToAdd(uint a, uint b) internal returns (bool) {
  59. return (a + b >= a);
  60. }
  61. }