UNPKG

insight-explorer

Version:

Easily retreive information about transactions and addresses from any insight api explorer

381 lines (326 loc) 9.42 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios); var _socket = require('socket.io-client'); var _socket2 = _interopRequireDefault(_socket); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } module.exports = function () { function Insight(url) { var _this = this; var useWebSockets = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; _classCallCheck(this, Insight); this.url = url; this.api = _axios2.default.create({ baseURL: this.url }); this.socketReady = false; this.socketSubscribedToInv = false; if (useWebSockets) { var urlNoTrail = this.url.split("/"); urlNoTrail.pop(); urlNoTrail = urlNoTrail.join("/"); this.socket = (0, _socket2.default)(urlNoTrail); this.socket.on("connect", function () { _this.socketReady = true; }); } } _createClass(Insight, [{ key: 'getBlock', value: async function getBlock(hash) { try { var response = await this.api.get("/block/" + hash); } catch (e) { throw this.createErrorString("getBlock", e); } return response.data; } }, { key: 'getBlockIndex', value: async function getBlockIndex(height) { try { var response = await this.api.get("/block-index/" + height); } catch (e) { throw this.createErrorString("getBlockIndex", e); } return response.data; } }, { key: 'getRawBlock', value: async function getRawBlock(block) { try { var response = await this.api.get("/rawblock/" + block); } catch (e) { throw this.createErrorString("getRawBlock", e); } return response.data; } }, { key: 'getBlockSummary', value: async function getBlockSummary(limit, blockDate) { var reqURL = "/blocks"; var addedQuestionMark = false; if (limit && limit !== "") { reqURL += "?limit=" + limit; addedQuestionMark = true; } if (blockDate && blockDate !== "") { if (addedQuestionMark) { reqURL += "&"; } else { reqURL += "?"; addedQuestionMark = true; } reqURL += "blockDate=" + blockDate; } try { var response = await this.api.get(reqURL); } catch (e) { throw this.createErrorString("getBlockSummary", e); } return response.data; } }, { key: 'getTransaction', value: async function getTransaction(txid) { try { var response = await this.api.get("/tx/" + txid); } catch (e) { throw this.createErrorString("getTransaction", e); } return response.data; } }, { key: 'getRawTransaction', value: async function getRawTransaction(txid) { try { var response = await this.api.get("/rawtx/" + txid); } catch (e) { throw this.createErrorString("getRawTransaction", e); } return response.data; } }, { key: 'getAddress', value: async function getAddress(address, options) { var reqURL = "/addr/" + address; var addedQuestionMark = false; if (options) { if (options.noTxList) { reqURL += "?noTxList=" + options.noTxList; addedQuestionMark = true; } if (options.from) { if (addedQuestionMark) { reqURL += "&"; } else { reqURL += "?"; addedQuestionMark = true; } reqURL += "from=" + options.from; } if (options.to) { if (addedQuestionMark) { reqURL += "&"; } else { reqURL += "?"; addedQuestionMark = true; } reqURL += "to=" + options.to; } } try { var response = await this.api.get(reqURL); } catch (e) { throw this.createErrorString("getAddress", e); } return response.data; } }, { key: 'getAddressProperties', value: async function getAddressProperties(address, property) { try { var response = await this.api.get("/addr/" + address + "/" + property); } catch (e) { throw this.createErrorString("getAddressProperties", e); } return response.data; } }, { key: 'getAddressUtxo', value: async function getAddressUtxo(address) { try { var response = await this.api.get("/addr/" + address + "/utxo"); } catch (e) { throw this.createErrorString("getAddressUtxo", e); } return response.data; } }, { key: 'getAddressesUtxo', value: async function getAddressesUtxo(addresses) { try { var response = await this.api.post("/addrs/utxo", { addrs: addresses.join() }); } catch (e) { throw this.createErrorString("getAddressesUtxo", e); } return response.data; } }, { key: 'getTransactionsForBlock', value: async function getTransactionsForBlock(hash) { try { var response = await this.api.get("/txs/?block=" + hash); } catch (e) { throw this.createErrorString("getTransactionsForBlock", e); } return response.data; } }, { key: 'getTransactionsForAddress', value: async function getTransactionsForAddress(address) { try { var response = await this.api.get("/txs/?address=" + address); } catch (e) { throw this.createErrorString("getTransactionsForAddress", e); } return response.data; } }, { key: 'getTransactionsForAddresses', value: async function getTransactionsForAddresses(addresses, options) { var opts = options || {}; opts.addrs = addresses.join(); try { var response = await this.api.post("/addrs/txs", opts); } catch (e) { throw this.createErrorString("getTransactionsForAddresses", e); } return response.data; } }, { key: 'broadcastRawTransaction', value: async function broadcastRawTransaction(rawtx, options) { var opts = options || {}; opts.rawtx = rawtx; try { var response = await this.api.post("/tx/send", opts); } catch (e) { throw this.createErrorString("broadcastRawTransaction", e); } return response.data; } }, { key: 'getSync', value: async function getSync() { try { var response = await this.api.get("/sync"); } catch (e) { throw this.createErrorString("getSync", e); } return response.data; } }, { key: 'getPeer', value: async function getPeer() { try { var response = await this.api.get("/peer"); } catch (e) { throw this.createErrorString("getPeer", e); } return response.data; } }, { key: 'getStatus', value: async function getStatus(query) { try { var response = await this.api.get("/status?q=" + query); } catch (e) { throw this.createErrorString("getStatus", e); } return response.data; } }, { key: 'getExchangeRate', value: async function getExchangeRate() { try { var response = await this.api.get("/currency"); } catch (e) { throw this.createErrorString("getExchangeRate", e); } return response.data; } }, { key: 'estimateFee', value: async function estimateFee(nbBlocks) { var reqURL = "/utils/estimatefee"; if (nbBlocks && nbBlocks !== "") reqURL += "?nbBlocks=" + nbBlocks; try { var response = await this.api.get(reqURL); } catch (e) { throw this.createErrorString("estimateFee", e); } return response.data; } }, { key: 'createErrorString', value: function createErrorString(functionName, error) { var extraErrorText = ""; if (error && error.response) { if (error.response.status) extraErrorText += error.response.status + " "; if (error.response.statusText) extraErrorText += error.response.statusText + " | "; if (error.response.data) extraErrorText += error.response.data; } else { extraErrorText = error.toString(); } return new Error("Unable to " + functionName + ": " + extraErrorText); } }, { key: 'onAddressUpdate', value: function onAddressUpdate(address, subscriberMethod) { var _this2 = this; if (this.socketReady) { this.socket.on(address, subscriberMethod); } else { setTimeout(function () { _this2.onAddressUpdate(address, subscriberMethod); }, 100); } } }, { key: 'onTX', value: function onTX(subscriberMethod) { var _this3 = this; if (this.socketReady) { this.socket.on('tx', subscriberMethod); if (!this.socketSubscribedToInv) { this.socket.emit("subscribe", "inv"); this.socketSubscribedToInv = true; } } else { setTimeout(function () { _this3.onTX(subscriberMethod); }, 100); } } }, { key: 'onBlock', value: function onBlock(subscriberMethod) { var _this4 = this; if (this.socketReady) { this.socket.on('block', subscriberMethod); if (!this.socketSubscribedToInv) { this.socket.emit("subscribe", "inv"); this.socketSubscribedToInv = true; } } else { setTimeout(function () { _this4.onBlock(subscriberMethod); }, 100); } } }]); return Insight; }();