Skip to content

Commit db4801f

Browse files
committed
Extracted getBlockTxIdsByTransactionHash so pmt-builder and pmt-witness-builder reuse that method
1 parent 987ae3f commit db4801f

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

tool/pmt-builder-utils.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,20 @@ const getTransactionWithRetry = async (transactionsClient, txId, retries = 0) =>
9191
}
9292
};
9393

94-
module.exports = { getWtxids, sleep, getTransactionWithRetry, REQUEST_DELAY_MS };
94+
/**
95+
* Fetches the transaction IDs (txids) of all transactions in the same block as the given transaction.
96+
* @param {Object} blocksClient - The blocks client instance used to interact with the blockchain.
97+
* @param {Object} transactionsClient - The transactions client instance used to interact with the blockchain.
98+
* @param {string} txHash - The transaction hash for which to find the block's transaction IDs.
99+
* @returns {Promise<string[]>} - A promise that resolves to an array of transaction IDs in the same block.
100+
*/
101+
102+
const getBlockTxidsByTransactionHash = async (blocksClient, transactionsClient, txHash) => {
103+
const transaction = await transactionsClient.getTx({ txid: txHash });
104+
const blockHash = transaction.status.block_hash;
105+
const blockTxids = await blocksClient.getBlockTxids({ hash: blockHash });
106+
107+
return blockTxids;
108+
};
109+
110+
module.exports = { getWtxids, sleep, getTransactionWithRetry, getBlockTxidsByTransactionHash, REQUEST_DELAY_MS };

tool/pmt-builder.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
const mempoolJS = require("@mempool/mempool.js");
22
const pmtBuilder = require("../index");
3+
const { getBlockTxidsByTransactionHash } = require("./pmt-builder-utils");
34

45
const getPmtInformation = async (network, txHash) => {
5-
66
const { bitcoin: { blocks, transactions } } = mempoolJS({
77
hostname: 'mempool.space',
88
network // 'testnet' | 'mainnet'
99
});
1010

11-
const transaction = await transactions.getTx({ txid: txHash });
12-
const blockHash = transaction.status.block_hash;
13-
14-
const blockTxids = await blocks.getBlockTxids({ hash: blockHash });
15-
11+
const blockTxids = await getBlockTxidsByTransactionHash(blocks, transactions, txHash);
1612
const resultPmt = pmtBuilder.buildPMT(blockTxids, txHash);
1713

1814
return resultPmt;
19-
2015
};
2116

2217
(async () => {

tool/pmt-witness-builder.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
const mempoolJS = require("@mempool/mempool.js");
22
const pmtBuilder = require("../index");
3-
const { getWtxids } = require("./pmt-builder-utils");
3+
const { getWtxids, getBlockTxidsByTransactionHash } = require("./pmt-builder-utils");
44

55
const getPmtInformationWithWitness = async (network, txHash) => {
6-
const bitcoin = mempoolJS({
6+
const { bitcoin: { blocks, transactions } } = mempoolJS({
77
hostname: 'mempool.space',
88
network // 'testnet' | 'mainnet'
99
});
1010

11-
const transactionsClient = bitcoin.bitcoin.transactions;
12-
const blocksClient = bitcoin.bitcoin.blocks;
13-
14-
const transaction = await transactionsClient.getTx({ txid: txHash });
15-
16-
const blockHash = transaction.status.block_hash;
17-
const blockTxIds = await blocksClient.getBlockTxids({ hash: blockHash });
18-
19-
const { blockWtxids, targetWtxid } = await getWtxids(transactionsClient, blockTxIds, txHash);
11+
const blockTxIds = await getBlockTxidsByTransactionHash(blocks, transactions, txHash);
12+
const { blockWtxids, targetWtxid } = await getWtxids(transactions, blockTxIds, txHash);
2013
const resultPmt = pmtBuilder.buildPMT(blockWtxids, targetWtxid);
14+
2115
return resultPmt;
2216
};
2317

0 commit comments

Comments
 (0)