github.com/igggame/nebulas-go@v2.1.0+incompatible/cmd/console/library/neb-light.js (about) 1 require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ 2 3 "use strict"; 4 5 var utils = require('./utils/utils.js'); 6 7 /** 8 * Admin API constructor. 9 * Class encapsulate methods for admin APIs commands. 10 * API documentation: {@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md}. 11 * @constructor 12 * 13 * @param {Neb} neb - Instance of Neb library. 14 * 15 * @example 16 * var admin = new Admin( new Neb() ); 17 * // or just 18 * var admin = new Neb().admin; 19 */ 20 var Admin = function (neb) { 21 this._setRequest(neb._request); 22 }; 23 24 /** 25 * @private 26 * @param {Request} request - transport wrapper. 27 */ 28 Admin.prototype._setRequest = function (request) { 29 this._request = request; 30 this._path = '/admin'; 31 }; 32 33 /** 34 * Method get info about nodes in Nebulas Network. 35 * 36 * @param {Function} [callback] - Without callback return data synchronous. 37 * 38 * @return [nodeInfoObject]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#nodeinfo} 39 * 40 * @example 41 * var admin = new Neb().admin; 42 * //sync 43 * var info = admin.nodeInfo(); 44 * //async 45 * admin.nodeInfo(function(info) { 46 * //code 47 * }); 48 */ 49 Admin.prototype.nodeInfo = function () { 50 var options = utils.argumentsToObject(['callback'], arguments); 51 return this._sendRequest("get", "/nodeinfo", null, options.callback); 52 }; 53 54 /** 55 * Method get list of available addresses. 56 * 57 * @param {Function} [callback] - Without callback return data synchronous. 58 * 59 * @return [accountsList]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#accounts} 60 * 61 * @example 62 * var admin = new Neb().admin; 63 * //sync 64 * var accounts = admin.accounts(); 65 * //async 66 * admin.accounts(function(accounts) { 67 * //code 68 * }); 69 */ 70 Admin.prototype.accounts = function () { 71 var options = utils.argumentsToObject(['callback'], arguments); 72 return this._sendRequest("get", "/accounts", null, options.callback); 73 }; 74 75 /** 76 * Method create a new account in Nebulas network with provided passphrase. 77 * {@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#newaccount} 78 * 79 * @param {String} passphrase 80 * @param {Function} [callback] - Without callback return data synchronous. 81 * 82 * @return [address]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#newaccount} 83 * 84 * @example 85 * var admin = new Neb().admin; 86 * //sync 87 * var address = admin.newAccount("passphrase"); 88 * //async 89 * admin.newAccount("passphrase", function(address) { 90 * //code 91 * }); 92 */ 93 Admin.prototype.newAccount = function () { 94 var options = utils.argumentsToObject(['passphrase', 'callback'], arguments); 95 var params = { "passphrase": options.passphrase }; 96 return this._sendRequest("post", "/account/new", params, options.callback); 97 }; 98 99 /** 100 * Method unlock account with provided passphrase. 101 * After the default unlock time, the account will be locked. 102 * {@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#unlockaccount} 103 * 104 * @param {String} address 105 * @param {String} passphrase 106 * @param {Function} [callback] - Without callback return data synchronous. 107 * 108 * @return [address]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#unlockaccount} 109 * 110 * @example 111 * var admin = new Neb().admin; 112 * //sync 113 * var isUnLocked = admin.unlockAccount("8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf", "passphrase"); 114 * //async 115 * admin.unlockAccount("8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf", "passphrase", function(isUnLocked) { 116 * //code 117 * }); 118 */ 119 Admin.prototype.unlockAccount = function () { 120 var options = utils.argumentsToObject(['address', 'passphrase', 'callback'], arguments); 121 var params = { 122 "address": options.address, 123 "passphrase": options.passphrase 124 }; 125 return this._sendRequest("post", "/account/unlock", params, options.callback); 126 }; 127 128 /** 129 * Method lock account. 130 * {@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#lockaccount} 131 * 132 * @param {String} address 133 * @param {Function} [callback] - Without callback return data synchronous. 134 * 135 * @return [address]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#lockaccount} 136 * 137 * @example 138 * var admin = new Neb().admin; 139 * //sync 140 * var isLocked = admin.lockAccount("8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf"); 141 * //async 142 * admin.lockAccount("8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf", function(isLocked) { 143 * //code 144 * }); 145 */ 146 Admin.prototype.lockAccount = function () { 147 var options = utils.argumentsToObject(['address', 'callback'], arguments); 148 var params = { "address": options.address }; 149 return this._sendRequest("post", "/account/lock", params, options.callback); 150 }; 151 152 /** 153 * Method wrap transaction sending functionality. 154 * For more information about parameters, follow this link: 155 * {@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#sendtransaction} 156 * 157 * @param {String} from 158 * @param {String} to 159 * @param {Number|Sting} value 160 * @param {Number} nonce 161 * @param {Number|String} gasPrice 162 * @param {Number|String} gasLimit 163 * @param {Object} [contract] 164 * @param {String} [binary] 165 * @param {Function} [callback] - Without callback return data synchronous. 166 * 167 * @return [Transcation hash and contract address]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#sendtransaction} 168 * 169 * @example 170 * var admin = new Neb().admin; 171 * //sync 172 * var tx = admin.sendTransaction( 173 * "n1QsosVXKxiV3B4iDWNmxfN4VqpHn2TeUcn", 174 * "n1Lf5VcZQnzBc69iANxLTBqmojCeMFKowoM", 175 * "10", 176 * 1, 177 * "1000000", 178 * "2000000" 179 * ); 180 * //async 181 * admin.sendTransaction( 182 * "n1QsosVXKxiV3B4iDWNmxfN4VqpHn2TeUcn", 183 * "n1Lf5VcZQnzBc69iANxLTBqmojCeMFKowoM", 184 * "10", 185 * 1, 186 * "1000000", 187 * "2000000", 188 * null, null, 189 * function(tx) { 190 * //code 191 * } 192 * ); 193 */ 194 Admin.prototype.sendTransaction = function () { 195 var options = utils.argumentsToObject(['from', 'to', 'value', 'nonce', 'gasPrice', 'gasLimit', 'contract', 'binary', 'callback'], arguments); 196 var params = { 197 "from": options.from, 198 "to": options.to, 199 "value": utils.toString(options.value), 200 "nonce": options.nonce, 201 "gasPrice": utils.toString(options.gasPrice), 202 "gasLimit": utils.toString(options.gasLimit), 203 "contract": options.contract, 204 "binary": options.binary 205 }; 206 return this._sendRequest("post", "/transaction", params, options.callback); 207 }; 208 209 /** 210 * Method sign hash. 211 * {@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#signhash} 212 * 213 * @param {String} address 214 * @param {string} string of hash bytes with base64 encode. 215 * @param {uint32} alg 216 * @param {Function} [callback] - Without callback return data synchronous. 217 * 218 * @return [data]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#signhash} 219 * 220 * @example 221 * var admin = new Neb().admin; 222 * //sync 223 * var data = admin.SignHash("8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf", "OGQ5NjllZWY2ZWNhZDNjMjlhM2E2MjkyODBlNjg2Y2YwYzNmNWQ1YTg2YWZmM2NhMTIwMjBjOTIzYWRjNmM5Mg==", 1); 224 * //async 225 * admin.SignHash("8a209cec02cbeab7e2f74ad969d2dfe8dd24416aa65589bf", "OGQ5NjllZWY2ZWNhZDNjMjlhM2E2MjkyODBlNjg2Y2YwYzNmNWQ1YTg2YWZmM2NhMTIwMjBjOTIzYWRjNmM5Mg==", 1, function(isLocked) { 226 * //code 227 * }); 228 */ 229 Admin.prototype.signHash = function () { 230 var options = utils.argumentsToObject(['address', 'hash', 'alg', 'callback'], arguments); 231 var params = { 232 "address": options.address, 233 "hash": options.hash, 234 "alg": options.alg 235 }; 236 return this._sendRequest("post", "/sign/hash", params, options.callback); 237 }; 238 239 /** 240 * Method sign transaction with passphrase. 241 * The transaction's from addrees must be unlock before sign call. 242 * {@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#signtransactionwithpassphrase} 243 * 244 * @param {String} from 245 * @param {String} to 246 * @param {Number|Sting} value 247 * @param {Number} nonce 248 * @param {Number|String} gasPrice 249 * @param {Number|String} gasLimit 250 * @param {Object} [contract] 251 * @param {String} [binary] 252 * @param {String} passphrase 253 * @param {Function} [callback] - Without callback return data synchronous. 254 * 255 * @return [data]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#signtransactionwithpassphrase} 256 * 257 * @example 258 * var admin = new Neb().admin; 259 * //sync 260 * var data = admin.signTransactionWithPassphrase( 261 * "22ac3a9a2b1c31b7a9084e46eae16e761f83f02324092b09", 262 * "5bed67f99cb3319e0c6f6a03548be3c8c52a8364464f886f", 263 * "10", 264 * 1, 265 * "1000000", 266 * "2000000" 267 * null, null, 268 * "passphrase" 269 * ); 270 * //async 271 * admin.signTransactionWithPassphrase( 272 * "22ac3a9a2b1c31b7a9084e46eae16e761f83f02324092b09", 273 * "5bed67f99cb3319e0c6f6a03548be3c8c52a8364464f886f", 274 * "10", 275 * 1, 276 * "1000000", 277 * "2000000", 278 * null, null, 279 * "passphrase" 280 * function(data) { 281 * //code 282 * } 283 * ); 284 */ 285 Admin.prototype.signTransactionWithPassphrase = function () { 286 var options = utils.argumentsToObject(['from', 'to', 'value', 'nonce', 'gasPrice', 'gasLimit', 'contract', 'binary', 'passphrase', 'callback'], arguments); 287 var tx = { 288 "from": options.from, 289 "to": options.to, 290 "value": utils.toString(options.value), 291 "nonce": options.nonce, 292 "gasPrice": utils.toString(options.gasPrice), 293 "gasLimit": utils.toString(options.gasLimit), 294 "contract": options.contract, 295 "binary": options.binary 296 }; 297 var params = { 298 "transaction": tx, 299 "passphrase": options.passphrase 300 }; 301 return this._sendRequest("post", "/sign", params, options.callback); 302 }; 303 304 /** 305 * Method send transaction with passphrase. 306 * {@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#sendtransactionwithpassphrase} 307 * 308 * @param {String} from 309 * @param {String} to 310 * @param {Number|Sting} value 311 * @param {Number} nonce 312 * @param {Number|String} gasPrice 313 * @param {Number|String} gasLimit 314 * @param {Object} [contract] 315 * @param {String} [binary] 316 * @param {String} [passphrase] 317 * @param {Function} [callback] - Without callback return data synchronous. 318 * 319 * @return [data]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#sendtransactionwithpassphrase} 320 * 321 * @example 322 * var admin = new Neb().admin; 323 * //sync 324 * var data = admin.sendTransactionWithPassphrase( 325 * "22ac3a9a2b1c31b7a9084e46eae16e761f83f02324092b09", 326 * "5bed67f99cb3319e0c6f6a03548be3c8c52a8364464f886f", 327 * "10", 328 * 1, 329 * "1000000", 330 * "2000000", 331 * null, null, 332 * "passphrase" 333 * ); 334 * //async 335 * admin.sendTransactionWithPassphrase 336 * "22ac3a9a2b1c31b7a9084e46eae16e761f83f02324092b09", 337 * "5bed67f99cb3319e0c6f6a03548be3c8c52a8364464f886f", 338 * "10", 339 * 1, 340 * "1000000", 341 * "2000000", 342 * null, null, 343 * "passphrase", 344 * function(data) { 345 * //code 346 * } 347 * ); 348 */ 349 Admin.prototype.sendTransactionWithPassphrase = function () { 350 var options = utils.argumentsToObject(['from', 'to', 'value', 'nonce', 'gasPrice', 'gasLimit', 'contract', 'binary', 'passphrase', 'callback'], arguments); 351 var tx = { 352 "from": options.from, 353 "to": options.to, 354 "value": utils.toString(options.value), 355 "nonce": options.nonce, 356 "gasPrice": utils.toString(options.gasPrice), 357 "gasLimit": utils.toString(options.gasLimit), 358 "contract": options.contract, 359 "binary": options.binary 360 }; 361 var params = { 362 "transaction": tx, 363 "passphrase": options.passphrase 364 }; 365 return this._sendRequest("post", "/transactionWithPassphrase", params, options.callback); 366 }; 367 368 /** 369 * Method start listen provided port. {@link https://github.com/nebulasio/go-nebulas/blob/1bd9bc9c9c6ca4fa0d515b620aa096f7e1c45088/neblet/neblet.go#L159}<br> 370 * TODO: Add parameter to wiki documentation. 371 * 372 * @param {String} [callback] - Listen port. 373 * @param {Function} [callback] - Without callback return data synchronous. 374 * 375 * @return [isListenStrted]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#stopmining} 376 * 377 * @example 378 * var admin = new Neb().admin; 379 * //sync 380 * var isListenStrted = admin.startPprof('8080'); 381 * //async 382 * admin.startPprof('8080', function(isListenStrted) { 383 * //code 384 * }); 385 */ 386 Admin.prototype.startPprof = function () { 387 var options = utils.argumentsToObject(['listen', 'callback'], arguments); 388 var params = { "listen": options.listen }; 389 return this._sendRequest("post", "/pprof", params, options.callback); 390 }; 391 392 /** 393 * Method get config of node in Nebulas Network. 394 * 395 * @param {Function} [callback] - Without callback return data synchronous. 396 * 397 * @return [config]{@link https://github.com/nebulasio/wiki/blob/master/rpc_admin.md#getConfig} 398 * 399 * @example 400 * var admin = new Neb().admin; 401 * //sync 402 * var info = admin.getConfig(); 403 * //async 404 * admin.getConfig(function(info) { 405 * //code 406 * }); 407 */ 408 Admin.prototype.getConfig = function () { 409 var options = utils.argumentsToObject(['callback'], arguments); 410 return this._sendRequest("get", "/getConfig", null, options.callback); 411 }; 412 413 Admin.prototype._sendRequest = function (method, api, params, callback) { 414 var action = this._path + api; 415 if (typeof callback === "function") { 416 return this._request.asyncRequest(method, action, params, callback); 417 } else { 418 return this._request.request(method, action, params); 419 } 420 }; 421 422 module.exports = Admin; 423 424 },{"./utils/utils.js":4}],2:[function(require,module,exports){ 425 426 "use strict"; 427 428 var utils = require('./utils/utils.js'); 429 430 /** 431 * User API constructor. 432 * Class encapsulate methods for building distributed applications and services. 433 * API documentation: {@link https://github.com/nebulasio/wiki/blob/master/rpc.md}. 434 * @constructor 435 * 436 * @param {Neb} neb - Instance of Neb library. 437 * 438 * @example 439 * var api = new API ( new Neb() ); 440 * // or just 441 * var api = new Neb().api; 442 */ 443 var API = function (neb) { 444 this._setRequest(neb._request); 445 }; 446 447 /** 448 * @private 449 * @param {Request} request - transport wrapper. 450 */ 451 API.prototype._setRequest = function (request) { 452 this._request = request; 453 this._path = '/user'; 454 }; 455 456 /** 457 * Method get state of Nebulas Network. 458 * 459 * @param {Function} [callback] - Without callback return data synchronous. 460 * 461 * @return [NebStateObject]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#getnebstate} 462 * 463 * @example 464 * var api = new Neb().api; 465 * //sync 466 * var state = api.getNebState(); 467 * //async 468 * api.getNebState(function(state) { 469 * //code 470 * }); 471 */ 472 API.prototype.getNebState = function () { 473 var options = utils.argumentsToObject(['callback'], arguments); 474 return this._sendRequest("get", "/nebstate", null, options.callback); 475 }; 476 477 /** 478 * Method get latest irreversible block of Nebulas Network. 479 * 480 * @param {Function} [callback] - Without callback return data synchronous. 481 * 482 * @return [NebStateObject]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#latestirreversibleblock} 483 * 484 * @example 485 * var api = new Neb().api; 486 * //sync 487 * var state = api.latestIrreversibleBlock(); 488 * //async 489 * api.latestIrreversibleBlock(function(state) { 490 * //code 491 * }); 492 */ 493 API.prototype.latestIrreversibleBlock = function () { 494 var options = utils.argumentsToObject(['callback'], arguments); 495 return this._sendRequest("get", "/lib", null, options.callback); 496 }; 497 498 /** 499 * Method return the state of the account. Balance and nonce. 500 * For more information about parameters, follow this link: 501 * {@link https://github.com/nebulasio/wiki/blob/master/rpc.md#getaccountstate} 502 * 503 * @param {String} address 504 * @param {String} height 505 * @param {Function} [callback] - Without callback return data synchronous. 506 * 507 * @return [accaountStateObject]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#getaccountstate} 508 * 509 * @example 510 * var api = new Neb().api; 511 * //sync 512 * var state = api.getAccountState("n1QsosVXKxiV3B4iDWNmxfN4VqpHn2TeUcn"); 513 * //async 514 * api.getAccountState("n1QsosVXKxiV3B4iDWNmxfN4VqpHn2TeUcn", function(state) { 515 * //code 516 * }); 517 */ 518 API.prototype.getAccountState = function () { 519 var options = utils.argumentsToObject(['address', 'height', 'callback'], arguments); 520 var params = { "address": options.address, "height": options.height }; 521 return this._sendRequest("post", "/accountstate", params, options.callback); 522 }; 523 524 /** 525 * Method wrap smart contract call functionality. 526 * For more information about parameters, follow this link: 527 * {@link https://github.com/nebulasio/wiki/blob/master/rpc.md#call} 528 * 529 * @param {String} from 530 * @param {String} to 531 * @param {Number|Sting} value 532 * @param {Number} nonce 533 * @param {Number|String} gasPrice 534 * @param {Number|String} gasLimit 535 * @param {Object} contract 536 * @param {Function} [callback] - Without callback return data synchronous. 537 * 538 * @return [Transcation hash]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#call} 539 * 540 * @example 541 * var api = new Neb().api; 542 * //sync 543 * var tx = api.call( 544 * "n1QsosVXKxiV3B4iDWNmxfN4VqpHn2TeUcn", 545 * "n1Lf5VcZQnzBc69iANxLTBqmojCeMFKowoM", 546 * "0", 547 * 3, 548 * "1000000", 549 * "2000000", 550 * "contract":{"function":"save","args":"[0]"} 551 * ); 552 * //async 553 * api.call( 554 * 100, 555 * "n1QsosVXKxiV3B4iDWNmxfN4VqpHn2TeUcn", 556 * "n1Lf5VcZQnzBc69iANxLTBqmojCeMFKowoM", 557 * "0", 558 * 3, 559 * "1000000", 560 * "2000000", 561 * "contract":{"function":"save","args":"[0]"}, 562 * function(tx) { 563 * //code 564 * } 565 * ); 566 */ 567 API.prototype.call = function () { 568 var options = utils.argumentsToObject(['from', 'to', 'value', 'nonce', 'gasPrice', 'gasLimit', 'contract', 'callback'], arguments); 569 var params = { 570 "from": options.from, 571 "to": options.to, 572 "value": utils.toString(options.value), 573 "nonce": options.nonce, 574 "gasPrice": utils.toString(options.gasPrice), 575 "gasLimit": utils.toString(options.gasLimit), 576 "contract": options.contract 577 }; 578 return this._sendRequest("post", "/call", params, options.callback); 579 }; 580 581 /** 582 * Method wrap submit the signed transaction. 583 * For more information about parameters, follow this link: 584 * {@link https://github.com/nebulasio/wiki/blob/master/rpc.md#sendrawtransaction} 585 * 586 * @param {Object} data 587 * @param {Function} [callback] - Without callback return data synchronous. 588 * 589 * @return [Transcation hash]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#sendrawtransaction} 590 * 591 * @example 592 * var api = new Neb().api; 593 * var tx = new Transaction(ChainID, from, to, transferValue, nonce, gasPrice, gasLimit); 594 * tx.signTransaction(); 595 * //sync 596 * var hash = api.sendRawTransaction( tx.toProtoString() ); 597 * //async 598 * api.sendRawTransaction( tx.toProtoString(), function(hash) { 599 * //code 600 * }); 601 */ 602 API.prototype.sendRawTransaction = function () { 603 var options = utils.argumentsToObject(['data', 'callback'], arguments); 604 var params = { "data": options.data }; 605 return this._sendRequest("post", "/rawtransaction", params, options.callback); 606 }; 607 608 /** 609 * Get block header info by the block hash. 610 * For more information about parameters, follow this link: 611 * {@link https://github.com/nebulasio/wiki/blob/master/rpc.md#getblockbyhash} 612 * 613 * @param {String} hash 614 * @param {Boolean} fullTransaction 615 * @param {Function} [callback] - Without callback return data synchronous. 616 * 617 * @return [Block]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#getblockbyhash} 618 * 619 * @example 620 * var api = new Neb().api; 621 * //sync 622 * var block = api.getBlockByHash("00000658397a90df6459b8e7e63ad3f4ce8f0a40b8803ff2f29c611b2e0190b8", true); 623 * //async 624 * api.getBlockByHash("00000658397a90df6459b8e7e63ad3f4ce8f0a40b8803ff2f29c611b2e0190b8", true, function(block) { 625 * //code 626 * }); 627 */ 628 API.prototype.getBlockByHash = function () { 629 var options = utils.argumentsToObject(['hash', 'fullTransaction', 'callback'], arguments); 630 var params = { "hash": options.hash, "fullTransaction": options.fullTransaction }; 631 return this._sendRequest("post", "/getBlockByHash", params, options.callback); 632 }; 633 634 /** 635 * Get block header info by the block height. 636 * For more information about parameters, follow this link: 637 * {@link https://github.com/nebulasio/wiki/blob/master/rpc.md#getblockbyheight} 638 * 639 * @param {Number} height 640 * @param {Boolean} fullTransaction 641 * @param {Function} [callback] - Without callback return data synchronous. 642 * 643 * @return [Block]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#getblockbyheight} 644 * 645 * @example 646 * var api = new Neb().api; 647 * //sync 648 * var block = api.getBlockByHeight(2, true); 649 * //async 650 * api.getBlockByHeight(2, true, function(block) { 651 * //code 652 * }); 653 */ 654 API.prototype.getBlockByHeight = function () { 655 var options = utils.argumentsToObject(['height', 'fullTransaction', 'callback'], arguments); 656 var params = { "height": options.height, "fullTransaction": options.fullTransaction }; 657 return this._sendRequest("post", "/getBlockByHeight", params, options.callback); 658 }; 659 660 /** 661 * Get transactionReceipt info by tansaction hash. 662 * For more information about parameters, follow this link: 663 * {@link https://github.com/nebulasio/wiki/blob/master/rpc.md#gettransactionreceipt} 664 * 665 * @param {String} hash 666 * @param {Function} [callback] - Without callback return data synchronous. 667 * 668 * @return [TransactionReceipt]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#gettransactionreceipt} 669 * 670 * @example 671 * var api = new Neb().api; 672 * //sync 673 * var receipt = api.getTransactionReceipt("cc7133643a9ae90ec9fa222871b85349ccb6f04452b835851280285ed72b008c"); 674 * //async 675 * api.getTransactionReceipt("cc7133643a9ae90ec9fa222871b85349ccb6f04452b835851280285ed72b008c", function(receipt) { 676 * //code 677 * }); 678 */ 679 API.prototype.getTransactionReceipt = function () { 680 var options = utils.argumentsToObject(['hash', 'callback'], arguments); 681 var params = { "hash": options.hash }; 682 return this._sendRequest("post", "/getTransactionReceipt", params, options.callback); 683 }; 684 685 /** 686 * Return the subscribed events of transaction & block. 687 * For more information about parameters, follow this link: 688 * {@link https://github.com/nebulasio/wiki/blob/master/rpc.md#subscribe} 689 * 690 * @param {Array|String} topic 691 * @param {Function} [callback] - Without callback return data synchronous. 692 * 693 * @return [eventData]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#subscribe} 694 * 695 * @example 696 * var api = new Neb().api; 697 * //sync 698 * var eventData = api.subscribe(["chain.linkBlock", "chain.pendingTransaction"]); 699 * //async 700 * api.subscribe(["chain.linkBlock", "chain.pendingTransaction"], function(eventData) { 701 * //code 702 * }); 703 */ 704 API.prototype.subscribe = function () { 705 var options = utils.argumentsToObject(['topic', 'callback'], arguments); 706 var params = { "topic": options.topic }; 707 return this._sendRequest("post", "/subscribe", params, options.callback); 708 }; 709 710 /** 711 * Return current gasPrice. 712 * 713 * @param {Function} [callback] - Without callback return data synchronous. 714 * 715 * @return [Gas Price]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#getgasprice} 716 * 717 * @example 718 * var api = new Neb().api; 719 * //sync 720 * var gasPrice = api.gasPrice(); 721 * //async 722 * api.gasPrice(function(gasPrice) { 723 * //code 724 * }); 725 */ 726 API.prototype.gasPrice = function () { 727 var options = utils.argumentsToObject(['callback'], arguments); 728 return this._sendRequest("get", "/getGasPrice", null, options.callback); 729 }; 730 731 /** 732 * Return the estimate gas of transaction. 733 * For more information about parameters, follow this link: 734 * {@link https://github.com/nebulasio/wiki/blob/master/rpc.md#estimategas} 735 * 736 * @param {String} from 737 * @param {String} to 738 * @param {Number|Sting} value 739 * @param {Number} nonce 740 * @param {Number|String} gasPrice 741 * @param {Number|String} gasLimit 742 * @param {Object} [contract] 743 * @param {String} [binary] 744 * @param {Function} [callback] - Without callback return data synchronous. 745 * 746 * @return [Gas]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#estimategas} 747 * 748 * @example 749 * var api = new Neb().api; 750 * //sync 751 * var gas = api.estimateGas( 752 * "n1QsosVXKxiV3B4iDWNmxfN4VqpHn2TeUcn", 753 * "n1Lf5VcZQnzBc69iANxLTBqmojCeMFKowoM", 754 * "10", 755 * 1, 756 * "1000000", 757 * "2000000" 758 * ); 759 * //async 760 * api.estimateGas( 761 * "n1QsosVXKxiV3B4iDWNmxfN4VqpHn2TeUcn", 762 * "n1Lf5VcZQnzBc69iANxLTBqmojCeMFKowoM", 763 * "10", 764 * 1, 765 * "1000000", 766 * "2000000", 767 * null, null, 768 * function(gas) { 769 * //code 770 * } 771 * ); 772 */ 773 API.prototype.estimateGas = function () { 774 var options = utils.argumentsToObject(['from', 'to', 'value', 'nonce', 'gasPrice', 'gasLimit', 'contract', 'binary', 'callback'], arguments); 775 var params = { 776 "from": options.from, 777 "to": options.to, 778 "value": utils.toString(options.value), 779 "nonce": options.nonce, 780 "gasPrice": utils.toString(options.gasPrice), 781 "gasLimit": utils.toString(options.gasLimit), 782 "contract": options.contract, 783 "binary": options.binary 784 }; 785 return this._sendRequest("post", "/estimateGas", params, options.callback); 786 }; 787 788 /** 789 * Return the events list of transaction. 790 * For more information about parameters, follow this link: 791 * {@link https://github.com/nebulasio/wiki/blob/master/rpc.md#geteventsbyhash} 792 * 793 * @param {String} hash 794 * @param {Function} [callback] - Without callback return data synchronous. 795 * 796 * @return [Events]{@link https://github.com/nebulasio/wiki/blob/master/rpc.md#geteventsbyhash} 797 * 798 * @example 799 * var api = new Neb().api; 800 * //sync 801 * var events = api.getEventsByHash("ec239d532249f84f158ef8ec9262e1d3d439709ebf4dd5f7c1036b26c6fe8073"); 802 * //async 803 * api.getEventsByHash("ec239d532249f84f158ef8ec9262e1d3d439709ebf4dd5f7c1036b26c6fe8073", function(events) { 804 * //code 805 * }); 806 */ 807 API.prototype.getEventsByHash = function () { 808 var options = utils.argumentsToObject(['hash', 'callback'], arguments); 809 var params = { "hash": options.hash }; 810 return this._sendRequest("post", "/getEventsByHash", params, options.callback); 811 }; 812 813 /** 814 * Method getter for dpos dynasty.{@link https://github.com/nebulasio/go-nebulas/blob/0c3439f9cedc539f64f64dd400878d2318cb215f/rpc/api_service.go#L596}<br> 815 * TODO: Add parameter to wiki documentation. 816 * 817 * @param {Function} [callback] - Without callback return data synchronous. 818 * 819 * @return [delegatees] 820 * 821 * @example 822 * var api = new Neb().api; 823 * //sync 824 * var delegatees = api.getDynasty(); 825 * //async 826 * api.getDynasty(function(delegatees) { 827 * //code 828 * }); 829 */ 830 API.prototype.getDynasty = function (height, callback) { 831 var params = { "height": height }; 832 return this._sendRequest("post", "/dynasty", params, callback); 833 }; 834 835 API.prototype._sendRequest = function (method, api, params, callback) { 836 var action = this._path + api; 837 if (typeof callback === "function") { 838 return this._request.asyncRequest(method, action, params, callback); 839 } else { 840 return this._request.request(method, action, params); 841 } 842 }; 843 844 module.exports = API; 845 846 },{"./utils/utils.js":4}],3:[function(require,module,exports){ 847 848 "use strict"; 849 850 var BigNumber = require('bignumber.js'); 851 var utils = require('./utils.js'); 852 853 var unitMap = { 854 'none': '0', 855 'None': '0', 856 'wei': '1', 857 'Wei': '1', 858 'kwei': '1000', 859 'Kwei': '1000', 860 'mwei': '1000000', 861 'Mwei': '1000000', 862 'gwei': '1000000000', 863 'Gwei': '1000000000', 864 'nas': '1000000000000000000', 865 'NAS': '1000000000000000000' 866 }; 867 868 var unitValue = function (unit) { 869 unit = unit ? unit.toLowerCase() : 'nas'; 870 var unitValue = unitMap[unit]; 871 if (unitValue === undefined) { 872 throw new Error('The unit undefined, please use the following units:' + JSON.stringify(unitMap, null, 2)); 873 } 874 return new BigNumber(unitValue, 10); 875 }; 876 877 var toBasic = function (number, unit) { 878 return utils.toBigNumber(number).times(unitValue(unit)); 879 }; 880 881 var fromBasic = function (number, unit) { 882 return utils.toBigNumber(number).dividedBy(unitValue(unit)); 883 }; 884 885 var nasToBasic = function (number) { 886 return utils.toBigNumber(number).times(unitValue("nas")); 887 }; 888 889 module.exports = { 890 toBasic: toBasic, 891 fromBasic: fromBasic, 892 nasToBasic: nasToBasic 893 }; 894 895 },{"./utils.js":4,"bignumber.js":"bignumber.js"}],4:[function(require,module,exports){ 896 897 "use strict"; 898 899 var BigNumber = require('bignumber.js'); 900 901 var isNull = function (v) { 902 return v === null || typeof v === "undefined"; 903 }; 904 905 var isBrowser = function () { 906 return typeof window !== "undefined"; 907 }; 908 909 var isBigNumber = function (obj) { 910 return obj instanceof BigNumber || obj && obj.constructor && obj.constructor.name === 'BigNumber'; 911 }; 912 913 var isString = function (obj) { 914 return typeof obj === 'string' && obj.constructor === String; 915 }; 916 917 var isObject = function (obj) { 918 return obj !== null && typeof obj === 'object'; 919 }; 920 921 var isFunction = function (object) { 922 return typeof object === 'function'; 923 }; 924 925 var isNumber = function (object) { 926 return typeof object === 'number'; 927 }; 928 929 var toBigNumber = function (number) { 930 number = number || 0; 931 if (isBigNumber(number)) { 932 return number; 933 } 934 if (isString(number) && number.indexOf('0x') === 0) { 935 return new BigNumber(number.replace('0x', ''), 16); 936 } 937 return new BigNumber(number.toString(10), 10); 938 }; 939 940 var toString = function (obj) { 941 if (isString(obj)) { 942 return obj; 943 } else if (isBigNumber(obj)) { 944 return obj.toString(10); 945 } else if (isObject(obj)) { 946 return JSON.stringify(obj); 947 } else { 948 return obj + ""; 949 } 950 }; 951 952 // Transform Array-like arguments object to common array. 953 var argumentsToArray = function (args) { 954 var len = args.length, 955 resultArray = new Array(len); 956 957 for (var i = 0; i < len; i += 1) { 958 resultArray[i] = args[i]; 959 } 960 return resultArray; 961 }; 962 963 // Create object based on provided arrays 964 var zipArraysToObject = function (keysArr, valuesArr) { 965 var resultObject = {}; 966 967 for (var i = 0; i < keysArr.length; i += 1) { 968 resultObject[keysArr[i]] = valuesArr[i]; 969 } 970 return resultObject; 971 }; 972 973 // Function what make overall view for arguments. 974 // If arguments was provided separated by commas like "func(arg1 ,arg2)" we create 975 // ArgumentsObject and write keys from argsNames and value from args. 976 // in case wheare we provide args in object like "func({arg1: value})" 977 // we just return that object 978 var argumentsToObject = function (keys, args) { 979 var ArgumentsObject = {}; 980 981 args = argumentsToArray(args); 982 if (isObject(args[0])) { 983 ArgumentsObject = args[0]; 984 } else { 985 ArgumentsObject = zipArraysToObject(keys, args); 986 } 987 988 return ArgumentsObject; 989 }; 990 991 module.exports = { 992 isNull: isNull, 993 isBrowser: isBrowser, 994 isBigNumber: isBigNumber, 995 isString: isString, 996 isObject: isObject, 997 isFunction: isFunction, 998 isNumber: isNumber, 999 toBigNumber: toBigNumber, 1000 toString: toString, 1001 argumentsToObject: argumentsToObject, 1002 zipArraysToObject: zipArraysToObject 1003 }; 1004 1005 },{"bignumber.js":"bignumber.js"}],5:[function(require,module,exports){ 1006 1007 },{}],"bignumber.js":[function(require,module,exports){ 1008 'use strict'; 1009 1010 module.exports = BigNumber; // jshint ignore:line 1011 1012 },{}],"neb":[function(require,module,exports){ 1013 1014 "use strict"; 1015 1016 var API = require("./api.js"); 1017 var Admin = require("./admin.js"); 1018 1019 var Unit = require("./utils/unit.js"); 1020 1021 /** 1022 * Neb API library constructor. 1023 * @constructor 1024 * @param {Request} request - transport wrapper. 1025 */ 1026 var Neb = function (request) { 1027 if (request) { 1028 this._request = request; 1029 } 1030 1031 this.api = new API(this); 1032 this.admin = new Admin(this); 1033 }; 1034 1035 Neb.prototype.setRequest = function (request) { 1036 this._request = request; 1037 this.api._setRequest(request); 1038 this.admin._setRequest(request); 1039 }; 1040 1041 Neb.prototype.toBasic = Unit.toBasic; 1042 Neb.prototype.fromBasic = Unit.fromBasic; 1043 Neb.prototype.nasToBasic = Unit.nasToBasic; 1044 1045 module.exports = Neb; 1046 1047 },{"./admin.js":1,"./api.js":2,"./utils/unit.js":3}]},{},[]) 1048 //# sourceMappingURL=neb-light.js.map