UNPKG

@tatumio/mempool-tracker

Version:

Mempool Tracker Extension

70 lines 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MempoolTracker = void 0; const tatum_1 = require("@tatumio/tatum"); class MempoolTracker extends tatum_1.TatumSdkExtension { constructor(tatumSdkContainer, options = { intervalMs: 10000 }) { super(tatumSdkContainer); this.options = options; this.seenTxIds = new Set(); this.newTxPool = []; this.mempoolSize = 0; this.supportedNetworks = [ tatum_1.Network.BITCOIN, tatum_1.Network.BITCOIN_TESTNET, tatum_1.Network.LITECOIN, tatum_1.Network.LITECOIN_TESTNET, tatum_1.Network.DOGECOIN, tatum_1.Network.DOGECOIN_TESTNET, ]; this.utxoRpc = this.tatumSdkContainer.getRpc(); } async getNewMempoolTransactions() { const newMemPoolTransactions = this.newTxPool; this.newTxPool = []; return newMemPoolTransactions; } async startTracking() { await this.fetchNewTransactions(); this.timeout = setInterval(async () => { await this.fetchNewTransactions(); }, this.options.intervalMs); } async stopTracking() { clearInterval(this.timeout); } async destroy() { await this.stopTracking(); } async fetchNewTransactions() { const infoResponse = await this.utxoRpc.getMempoolInfo(); if (!infoResponse.result) { throw new Error(`Failed to retrieve mempool info - ${infoResponse.error}`); } if (infoResponse.result.size === this.mempoolSize) { return; } this.mempoolSize = infoResponse.result.size; const response = await this.utxoRpc.getRawMemPool(); if (!response.result) { throw new Error(`Failed to retrieve mempool transactions - ${response.error}`); } const currentTxIds = response.result; if (this.seenTxIds.size === 0) { this.seenTxIds = new Set(currentTxIds); return; } const newTxIds = currentTxIds.filter((txId) => !this.seenTxIds.has(txId)); if (this.seenTxIds.size > currentTxIds.length * 2) { this.seenTxIds = new Set(currentTxIds); } else { for (const newTxId of newTxIds) { this.seenTxIds.add(newTxId); } } this.newTxPool.push(...newTxIds); } } exports.MempoolTracker = MempoolTracker; //# sourceMappingURL=extension.js.map