ethereum.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Main Ethereum library
  2. window.eth = {
  3. prototype: Object(),
  4. // Retrieve block
  5. //
  6. // Either supply a number or a string. Type is determent for the lookup method
  7. // string - Retrieves the block by looking up the hash
  8. // number - Retrieves the block by looking up the block number
  9. getBlock: function(numberOrHash, cb) {
  10. var func;
  11. if(typeof numberOrHash == "string") {
  12. func = "getBlockByHash";
  13. } else {
  14. func = "getBlockByNumber";
  15. }
  16. postData({call: func, args: [numberOrHash]}, cb);
  17. },
  18. // Create transaction
  19. //
  20. // Transact between two state objects
  21. transact: function(sec, recipient, value, gas, gasPrice, data, cb) {
  22. postData({call: "transact", args: [sec, recipient, value, gas, gasPrice, data]}, cb);
  23. },
  24. create: function(sec, value, gas, gasPrice, init, body, cb) {
  25. postData({call: "create", args: [sec, value, gas, gasPrice, init, body]}, cb);
  26. },
  27. getStorageAt: function(address, storageAddress, cb) {
  28. postData({call: "getStorage", args: [address, storageAddress]}, cb);
  29. },
  30. getStateKeyVals: function(address, cb){
  31. postData({call: "getStateKeyVals", args: [address]}, cb);
  32. },
  33. getKey: function(cb) {
  34. postData({call: "getKey"}, cb);
  35. },
  36. getTxCountAt: function(address, cb) {
  37. postData({call: "getTxCountAt", args: [address]}, cb);
  38. },
  39. getIsMining: function(cb){
  40. postData({call: "getIsMining"}, cb)
  41. },
  42. getIsListening: function(cb){
  43. postData({call: "getIsListening"}, cb)
  44. },
  45. getCoinBase: function(cb){
  46. postData({call: "getCoinBase"}, cb);
  47. },
  48. getPeerCount: function(cb){
  49. postData({call: "getPeerCount"}, cb);
  50. },
  51. getBalanceAt: function(address, cb) {
  52. postData({call: "getBalance", args: [address]}, cb);
  53. },
  54. getTransactionsFor: function(address, cb) {
  55. postData({call: "getTransactionsFor", args: [address]}, cb);
  56. },
  57. getSecretToAddress: function(sec, cb) {
  58. postData({call: "getSecretToAddress", args: [sec]}, cb);
  59. },
  60. watch: function(address, storageAddrOrCb, cb) {
  61. var ev;
  62. if(cb === undefined) {
  63. cb = storageAddrOrCb;
  64. storageAddrOrCb = "";
  65. ev = "object:"+address;
  66. } else {
  67. ev = "storage:"+address+":"+storageAddrOrCb;
  68. }
  69. eth.on(ev, cb)
  70. postData({call: "watch", args: [address, storageAddrOrCb]});
  71. },
  72. disconnect: function(address, storageAddrOrCb, cb) {
  73. var ev;
  74. if(cb === undefined) {
  75. cb = storageAddrOrCb;
  76. storageAddrOrCb = "";
  77. ev = "object:"+address;
  78. } else {
  79. ev = "storage:"+address+":"+storageAddrOrCb;
  80. }
  81. eth.off(ev, cb)
  82. postData({call: "disconnect", args: [address, storageAddrOrCb]});
  83. },
  84. set: function(props) {
  85. postData({call: "set", args: props});
  86. },
  87. on: function(event, cb) {
  88. if(eth._onCallbacks[event] === undefined) {
  89. eth._onCallbacks[event] = [];
  90. }
  91. eth._onCallbacks[event].push(cb);
  92. return this
  93. },
  94. off: function(event, cb) {
  95. if(eth._onCallbacks[event] !== undefined) {
  96. var callbacks = eth._onCallbacks[event];
  97. for(var i = 0; i < callbacks.length; i++) {
  98. if(callbacks[i] === cb) {
  99. delete callbacks[i];
  100. }
  101. }
  102. }
  103. return this
  104. },
  105. trigger: function(event, data) {
  106. var callbacks = eth._onCallbacks[event];
  107. if(callbacks !== undefined) {
  108. for(var i = 0; i < callbacks.length; i++) {
  109. // Figure out whether the returned data was an array
  110. // array means multiple return arguments (multiple params)
  111. if(data instanceof Array) {
  112. callbacks[i].apply(this, data);
  113. } else {
  114. callbacks[i].call(this, data);
  115. }
  116. }
  117. }
  118. },
  119. }
  120. window.eth._callbacks = {}
  121. window.eth._onCallbacks = {}