mirror of
https://github.com/mempool/mempool.git
synced 2025-01-01 03:04:27 +01:00
Merge pull request #1374 from nymkappa/feature/improve-rpc-calls
Optimize RPC calls
This commit is contained in:
commit
9c60c7ba79
@ -2,7 +2,7 @@ import { IEsploraApi } from './esplora-api.interface';
|
|||||||
|
|
||||||
export interface AbstractBitcoinApi {
|
export interface AbstractBitcoinApi {
|
||||||
$getRawMempool(): Promise<IEsploraApi.Transaction['txid'][]>;
|
$getRawMempool(): Promise<IEsploraApi.Transaction['txid'][]>;
|
||||||
$getRawTransaction(txId: string, skipConversion?: boolean, addPrevout?: boolean): Promise<IEsploraApi.Transaction>;
|
$getRawTransaction(txId: string, skipConversion?: boolean, addPrevout?: boolean, blockHash?: string): Promise<IEsploraApi.Transaction>;
|
||||||
$getBlockHeightTip(): Promise<number>;
|
$getBlockHeightTip(): Promise<number>;
|
||||||
$getTxIdsForBlock(hash: string): Promise<string[]>;
|
$getTxIdsForBlock(hash: string): Promise<string[]>;
|
||||||
$getBlockHash(height: number): Promise<string>;
|
$getBlockHash(height: number): Promise<string>;
|
||||||
|
@ -14,14 +14,14 @@ class BitcoinApi implements AbstractBitcoinApi {
|
|||||||
this.bitcoindClient = bitcoinClient;
|
this.bitcoindClient = bitcoinClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
$getRawTransaction(txId: string, skipConversion = false, addPrevout = false): Promise<IEsploraApi.Transaction> {
|
$getRawTransaction(txId: string, skipConversion = false, addPrevout = false, blockHash?: string): Promise<IEsploraApi.Transaction> {
|
||||||
// If the transaction is in the mempool we already converted and fetched the fee. Only prevouts are missing
|
// If the transaction is in the mempool we already converted and fetched the fee. Only prevouts are missing
|
||||||
const txInMempool = mempool.getMempool()[txId];
|
const txInMempool = mempool.getMempool()[txId];
|
||||||
if (txInMempool && addPrevout) {
|
if (txInMempool && addPrevout) {
|
||||||
return this.$addPrevouts(txInMempool);
|
return this.$addPrevouts(txInMempool);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.bitcoindClient.getRawTransaction(txId, true)
|
return this.bitcoindClient.getRawTransaction(txId, true, blockHash)
|
||||||
.then((transaction: IBitcoinApi.Transaction) => {
|
.then((transaction: IBitcoinApi.Transaction) => {
|
||||||
if (skipConversion) {
|
if (skipConversion) {
|
||||||
transaction.vout.forEach((vout) => {
|
transaction.vout.forEach((vout) => {
|
||||||
|
@ -109,7 +109,7 @@ class Blocks {
|
|||||||
blockExtended.extras.reward = transactions[0].vout.reduce((acc, curr) => acc + curr.value, 0);
|
blockExtended.extras.reward = transactions[0].vout.reduce((acc, curr) => acc + curr.value, 0);
|
||||||
blockExtended.extras.coinbaseTx = transactionUtils.stripCoinbaseTransaction(transactions[0]);
|
blockExtended.extras.coinbaseTx = transactionUtils.stripCoinbaseTransaction(transactions[0]);
|
||||||
|
|
||||||
const coinbaseRaw: IEsploraApi.Transaction = await bitcoinApi.$getRawTransaction(transactions[0].txid, true);
|
const coinbaseRaw: IEsploraApi.Transaction = await bitcoinApi.$getRawTransaction(transactions[0].txid, true, false, block.id);
|
||||||
blockExtended.extras.coinbaseRaw = coinbaseRaw.hex;
|
blockExtended.extras.coinbaseRaw = coinbaseRaw.hex;
|
||||||
|
|
||||||
if (block.height === 0) {
|
if (block.height === 0) {
|
||||||
@ -119,7 +119,9 @@ class Blocks {
|
|||||||
blockExtended.extras.avgFee = 0;
|
blockExtended.extras.avgFee = 0;
|
||||||
blockExtended.extras.avgFeeRate = 0;
|
blockExtended.extras.avgFeeRate = 0;
|
||||||
} else {
|
} else {
|
||||||
const stats = await bitcoinClient.getBlockStats(block.id);
|
const stats = await bitcoinClient.getBlockStats(block.id, [
|
||||||
|
'feerate_percentiles', 'minfeerate', 'maxfeerate', 'totalfee', 'avgfee', 'avgfeerate'
|
||||||
|
]);
|
||||||
blockExtended.extras.medianFee = stats.feerate_percentiles[2]; // 50th percentiles
|
blockExtended.extras.medianFee = stats.feerate_percentiles[2]; // 50th percentiles
|
||||||
blockExtended.extras.feeRange = [stats.minfeerate, stats.feerate_percentiles, stats.maxfeerate].flat();
|
blockExtended.extras.feeRange = [stats.minfeerate, stats.feerate_percentiles, stats.maxfeerate].flat();
|
||||||
blockExtended.extras.totalFees = stats.totalfee;
|
blockExtended.extras.totalFees = stats.totalfee;
|
||||||
|
Loading…
Reference in New Issue
Block a user