UNPKG

@berrywallet/core

Version:

Berrywallet main Core for work with common cryptocurrencies like Bitcoin, Ethereum, Dash, Litecoin

145 lines (144 loc) 5.83 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; } var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; } Object.defineProperty(exports, "__esModule", { value: true }); const bignumber_js_1 = __importDefault(require("bignumber.js")); const bottleneck_1 = __importDefault(require("bottleneck")); const lodash_1 = require("lodash"); const axios_1 = __importDefault(require("axios")); const Coin = __importStar(require("../../coin")); const api_1 = require("../api"); const network_client_1 = require("./network-client"); const tracker_1 = require("./tracker"); class InsightNetworkClient extends network_client_1.NetworkClient { constructor(coin, options) { if (false === (coin instanceof Coin.BIPGenericCoin)) { throw new Error('Insight network for BIP Coin only'); } super(coin, options); this.client = axios_1.default.create({ baseURL: this.getApiUrl(), timeout: 10000, }); this.limiter = new bottleneck_1.default(1, 500); } getTx(txid) { return __awaiter(this, void 0, void 0, function* () { const data = yield this.sendRequest(`/tx/${txid}`); const tx = data; return tx ? api_1.Insight.toWalletTx(tx, this.coin) : undefined; }); } getFeesPerKB() { return __awaiter(this, void 0, void 0, function* () { const resolveFeePerByte = (data, index, defaultFeeProp) => { if (data[index] > 0) { return new bignumber_js_1.default(data[index]).div(1024).decimalPlaces(8); } return this.coin[defaultFeeProp]; }; try { const data = yield this.sendRequest('/utils/estimatefee?nbBlocks=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16'); return { low: resolveFeePerByte(data, 12, 'lowFeePerByte'), standard: resolveFeePerByte(data, 5, 'defaultFeePerByte'), high: resolveFeePerByte(data, 1, 'highFeePerByte'), }; } catch (error) { return { low: this.coin.lowFeePerByte, standard: this.coin.defaultFeePerByte, high: this.coin.highFeePerByte, }; } }); } getBlock(blockHash) { return __awaiter(this, void 0, void 0, function* () { const block = yield this.sendRequest(`/block/${blockHash}`); return { hash: block.hash, height: block.height, time: block.time * 1000, txids: block.tx, original: block, }; }); } broadCastTransaction(transaction) { return __awaiter(this, void 0, void 0, function* () { const data = yield this.sendRequest('/tx/send', { rawtx: transaction.toBuffer().toString('hex'), }); return data.txid; }); } getAddressTxs(address) { return this.pureGetAddrsTxs([address], 0, 50); } getBulkAddrsTxs(addrs) { return this.pureGetAddrsTxs(addrs, 0, 50); } getTracker() { if (!this.trackerClient) { this.trackerClient = new tracker_1.InsightTrackerProvider(this); } return this.trackerClient; } destruct() { if (this.trackerClient) { this.trackerClient.destruct(); delete this.trackerClient; } this.limiter.stopAll(); delete this.client; } pureGetAddrsTxs(addrs, from = 0, limit = 50) { return __awaiter(this, void 0, void 0, function* () { if (!addrs.length) { throw new Error('There is no addresses to request!'); } const data = yield this .sendRequest(`/addrs/${addrs.join(',')}/txs?from=${from}&to=${from + limit}`); const rawTxs = data.items; const txList = []; const extractTxCallback = (tx) => { txList.push(api_1.Insight.toWalletTx(tx, this.coin)); }; lodash_1.forEach(lodash_1.orderBy(rawTxs, 'blockheight', 'asc'), extractTxCallback); return txList; }); } wrapperLimiter(cb, priority = 5) { return this.limiter.schedulePriority(priority, cb); } sendRequest(url, postParams = null) { const requestResolver = () => __awaiter(this, void 0, void 0, function* () { const response = yield this.client.request({ url: url, method: postParams ? 'POST' : 'GET', data: postParams ? postParams : null, }); return response.data; }); return this.wrapperLimiter(requestResolver); } } exports.InsightNetworkClient = InsightNetworkClient;