Add block/:hash/tx/:txid/summary endpoint
This commit is contained in:
parent
f08fa034cc
commit
b6aeb5661f
@ -42,6 +42,7 @@ class BitcoinRoutes {
|
|||||||
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/:height', this.getBlocks.bind(this))
|
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/:height', this.getBlocks.bind(this))
|
||||||
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash', this.getBlock)
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash', this.getBlock)
|
||||||
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/summary', this.getStrippedBlockTransactions)
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/summary', this.getStrippedBlockTransactions)
|
||||||
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/tx/:txid/summary', this.getStrippedBlockTransaction)
|
||||||
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/audit-summary', this.getBlockAuditSummary)
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/audit-summary', this.getBlockAuditSummary)
|
||||||
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/tx/:txid/audit', this.$getBlockTxAuditSummary)
|
.get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/tx/:txid/audit', this.$getBlockTxAuditSummary)
|
||||||
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/tip/height', this.getBlockTipHeight)
|
.get(config.MEMPOOL.API_URL_PREFIX + 'blocks/tip/height', this.getBlockTipHeight)
|
||||||
@ -321,6 +322,20 @@ class BitcoinRoutes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async getStrippedBlockTransaction(req: Request, res: Response) {
|
||||||
|
try {
|
||||||
|
const transaction = await blocks.$getSingleTxFromSummary(req.params.hash, req.params.txid);
|
||||||
|
if (!transaction) {
|
||||||
|
handleError(req, res, 404, `transaction not found in summary`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 3600 * 24 * 30).toUTCString());
|
||||||
|
res.json(transaction);
|
||||||
|
} catch (e) {
|
||||||
|
res.status(500).send(e instanceof Error ? e.message : e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async getBlock(req: Request, res: Response) {
|
private async getBlock(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const block = await blocks.$getBlock(req.params.hash);
|
const block = await blocks.$getBlock(req.params.hash);
|
||||||
|
@ -1224,6 +1224,11 @@ class Blocks {
|
|||||||
return summary.transactions;
|
return summary.transactions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async $getSingleTxFromSummary(hash: string, txid: string): Promise<TransactionClassified | null> {
|
||||||
|
const txs = await this.$getStrippedBlockTransactions(hash);
|
||||||
|
return txs.find(tx => tx.txid === txid) || null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get 15 blocks
|
* Get 15 blocks
|
||||||
*
|
*
|
||||||
|
@ -18,6 +18,7 @@ export class ApiService {
|
|||||||
private apiBasePath: string; // network path is /testnet, etc. or '' for mainnet
|
private apiBasePath: string; // network path is /testnet, etc. or '' for mainnet
|
||||||
|
|
||||||
private requestCache = new Map<string, { subject: BehaviorSubject<any>, expiry: number }>;
|
private requestCache = new Map<string, { subject: BehaviorSubject<any>, expiry: number }>;
|
||||||
|
public blockSummaryLoaded: { [hash: string]: boolean } = {};
|
||||||
public blockAuditLoaded: { [hash: string]: boolean } = {};
|
public blockAuditLoaded: { [hash: string]: boolean } = {};
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@ -318,9 +319,14 @@ export class ApiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getStrippedBlockTransactions$(hash: string): Observable<TransactionStripped[]> {
|
getStrippedBlockTransactions$(hash: string): Observable<TransactionStripped[]> {
|
||||||
|
this.setBlockSummaryLoaded(hash);
|
||||||
return this.httpClient.get<TransactionStripped[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/block/' + hash + '/summary');
|
return this.httpClient.get<TransactionStripped[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/block/' + hash + '/summary');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getStrippedBlockTransaction$(hash: string, txid: string): Observable<TransactionStripped> {
|
||||||
|
return this.httpClient.get<TransactionStripped>(this.apiBaseUrl + this.apiBasePath + '/api/v1/block/' + hash + '/tx/' + txid + '/summary');
|
||||||
|
}
|
||||||
|
|
||||||
getDifficultyAdjustments$(interval: string | undefined): Observable<any> {
|
getDifficultyAdjustments$(interval: string | undefined): Observable<any> {
|
||||||
return this.httpClient.get<any[]>(
|
return this.httpClient.get<any[]>(
|
||||||
this.apiBaseUrl + this.apiBasePath + `/api/v1/mining/difficulty-adjustments` +
|
this.apiBaseUrl + this.apiBasePath + `/api/v1/mining/difficulty-adjustments` +
|
||||||
@ -567,4 +573,12 @@ export class ApiService {
|
|||||||
getBlockAuditLoaded(hash) {
|
getBlockAuditLoaded(hash) {
|
||||||
return this.blockAuditLoaded[hash];
|
return this.blockAuditLoaded[hash];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async setBlockSummaryLoaded(hash: string) {
|
||||||
|
this.blockSummaryLoaded[hash] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
getBlockSummaryLoaded(hash) {
|
||||||
|
return this.blockSummaryLoaded[hash];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user