UNPKG

@berrywallet/core

Version:

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

117 lines (116 loc) 4.55 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const api_1 = require("../../api"); const tracker_client_1 = require("./tracker-client"); const NEW_BLOCK_CHECK_TIMEOUT = 15000; const RECONNECT_TIMEOUT = 30000; const CONNECTION_TIMEOUT = 60000 * 10; class InfuraTrackerProvider extends tracker_client_1.TrackerClient { constructor(networkClient) { super(networkClient); this.enableBlockTracking = false; this.connected = false; this.handleBlockError = (error) => { if (this.blockTrackInterval) { clearInterval(this.blockTrackInterval); this.currentBlockHeight = undefined; this.currentBlockTime = undefined; } this.fireConnectionError(error); if (this.connected) { this.connected = false; this.fireDisconnect(); } setTimeout(() => { this.startBlockTracking(); }, RECONNECT_TIMEOUT); throw error; }; this.startBlockTracking(); } startBlockTracking() { this.enableBlockTracking = true; this.trackLastOrNextBlock(); this.blockTrackInterval = setInterval(() => { this.trackLastOrNextBlock(); }, NEW_BLOCK_CHECK_TIMEOUT); } fireNewBlock(block) { this.currentBlockHeight = block.height; this.currentBlockTime = block.time; const originalResponse = block.original; if (originalResponse) { const { addrs = [], callback = null } = this.addrTxEvents; lodash_1.forEach(originalResponse.transactions, (tx) => { const needCallCallback = (tx.to && this.isAddrTrack(tx.to)) || (tx.from && this.isAddrTrack(tx.from)); if (needCallCallback && callback) { const etherWalletTx = api_1.Infura.toWalletTx(tx, this.networkClient.getCoin(), block.time); this.networkClient.checkAndMapTxReceipt(etherWalletTx).then(callback); } if (this.listenerCount(`tx.${tx.hash}`) > 0) { this.fireTxidConfirmation(api_1.Infura.toWalletTx(tx, this.networkClient.getCoin(), block.time)); } }); } return super.fireNewBlock(block); } getCurrentBlockTime() { return this.currentBlockTime || 0; } ; fireTxidConfirmation(tx) { this.networkClient .checkAndMapTxReceipt(tx) .then(rtx => super.fireTxidConfirmation(rtx)); return true; } activateConnection() { if (!this.connected) { this.connected = true; this.fireConnect(); } } trackLastOrNextBlock() { return __awaiter(this, void 0, void 0, function* () { if (!this.enableBlockTracking) { return; } let blockHeight = 'latest'; if (this.currentBlockHeight && (new Date().getTime() - this.getCurrentBlockTime() < CONNECTION_TIMEOUT)) { blockHeight = this.currentBlockHeight + 1; } try { const block = yield this.networkClient.getBlockByNumber(blockHeight); this.activateConnection(); if (!block) { return; } this.fireNewBlock(block); this.trackLastOrNextBlock(); return block; } catch (error) { this.handleBlockError(error); return; } }); } destruct() { this.enableBlockTracking = false; if (this.blockTrackInterval) { clearInterval(this.blockTrackInterval); } super.destruct(); } } exports.InfuraTrackerProvider = InfuraTrackerProvider;