UNPKG

@berrywallet/core

Version:

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

107 lines (106 loc) 3.98 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 __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 lodash_1 = require("lodash"); const _1 = require("../"); const Networking = __importStar(require("./")); class NetworkProvider { constructor(coin) { this.coin = coin; this.clientList = []; this.currentClientIndex = 0; this.catchError = (method, client) => { return (error) => { this.debug(`ERROR in ${method}`, error.message, typeof client, client.getOptions()); throw error; }; }; this.debug = _1.Debug.create('NetworkProvider:' + this.coin.getUnit()); const clientOptions = Networking.Adapter.getNetworkAdapters(coin); if (clientOptions.length < 1) { throw new Error(`No providers for ${coin.getUnit()}`); } lodash_1.forEach(clientOptions, (props, indx) => { const client = Networking.createClient(this.coin, props); this.clientList.push({ client: client, banned: false, options: props.options, }); }); } getClientCount() { return this.clientList.length; } rotateClient(depth = 0) { if (depth > 0 && depth > this.getClientCount()) { throw new Error('All clients are wrong..!'); } this.currentClientIndex++; if (this.currentClientIndex >= this.getClientCount()) { this.currentClientIndex = 0; } return this.clientList[this.currentClientIndex].client; } getAddressTxs(address) { const client = this.getClient(0); return client .getAddressTxs(address) .catch(this.catchError('getAddressTxs', client)); } getBulkAddrTxs(addrs) { const client = this.getClient(0); return client.getBulkAddrsTxs(addrs).catch(this.catchError('getBulkAddrTxs', client)); } getTx(txid) { const client = this.getClient(0); return client.getTx(txid).catch(this.catchError('getTx', client)); } getTracker() { return this.clientList[0].client.getTracker(); } onNewBlock(callback) { this.getTracker().onBlock(callback); } onTransactionConfirm(txid, callback) { this.getTracker().onTransactionConfirm(txid, callback); } onAddrsTx(addrs, callback) { this.getTracker().onAddrsTx(addrs, callback); } getClient(index = 0) { return this.clientList[index].client; } broadCastTransaction(transaction) { return this.getClient(0).broadCastTransaction(transaction); } getLastBlock() { return __awaiter(this, void 0, void 0, function* () { throw new Error('Need implement block!'); }); } destruct() { for (let i in this.clientList) { if (this.clientList[i].client) { this.clientList[i].client.destruct(); } delete this.clientList[i]; } this.clientList = []; } } exports.NetworkProvider = NetworkProvider;