UNPKG

@bsv/wallet-toolbox-mobile

Version:
68 lines 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LiveIngestorWhatsOnChainPoll = void 0; const utilityHelpers_1 = require("../../../../utility/utilityHelpers"); const LiveIngestorBase_1 = require("./LiveIngestorBase"); const WhatsOnChainServices_1 = require("./WhatsOnChainServices"); /** * Reports new headers by polling periodically. */ class LiveIngestorWhatsOnChainPoll extends LiveIngestorBase_1.LiveIngestorBase { static createLiveIngestorWhatsOnChainOptions(chain) { const options = { ...WhatsOnChainServices_1.WhatsOnChainServices.createWhatsOnChainServicesOptions(chain), ...LiveIngestorBase_1.LiveIngestorBase.createLiveIngestorBaseOptions(chain), idleWait: 100000 }; return options; } constructor(options) { super(options); this.done = false; this.idleWait = options.idleWait || 100000; this.woc = new WhatsOnChainServices_1.WhatsOnChainServices(options); } async getHeaderByHash(hash) { const header = await this.woc.getHeaderByHash(hash); return header; } async startListening(liveHeaders) { this.done = false; let lastHeaders = []; for (; !this.done;) { const headers = await this.woc.getHeaders(); const newHeaders = headers.filter(h => !lastHeaders.some(lh => lh.hash === h.hash)); for (const h of newHeaders) { const bits = typeof h.bits === 'string' ? parseInt(h.bits, 16) : h.bits; if (!h.previousblockhash) { h.previousblockhash = '0000000000000000000000000000000000000000000000000000000000000000'; // genesis } const bh = { height: h.height, hash: h.hash, version: h.version, previousHash: h.previousblockhash, merkleRoot: h.merkleroot, time: h.time, bits, nonce: h.nonce }; liveHeaders.unshift(bh); } lastHeaders = headers; for (let sec = 0; sec < 60 && !this.done; sec++) { // Only wait a second at a time so we notice `done` sooner... await (0, utilityHelpers_1.wait)(1000); } } console.log(`LiveIngestorWhatsOnChainPoll stopped`); } stopListening() { this.done = true; } async shutdown() { this.stopListening(); } } exports.LiveIngestorWhatsOnChainPoll = LiveIngestorWhatsOnChainPoll; //# sourceMappingURL=LiveIngestorWhatsOnChainPoll.js.map