Token.sol 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. pragma solidity ^0.4.0;
  2. // Source: https://github.com/nexusdev/erc20
  3. // Flat file implementation of `dappsys/token/base.sol::DSTokenBase`
  4. // Everything throws instead of returning false on failure.
  5. import './ERC20.sol';
  6. contract Token is ERC20 {
  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. }