@@ -34,6 +34,7 @@ type BlockfrostTx = Pick<Responses['address_transactions_content'][0], 'block_he
34
34
const compareTx = ( a : BlockfrostTx , b : BlockfrostTx ) => a . block_height - b . block_height || a . tx_index - b . tx_index ;
35
35
36
36
export class BlockfrostChainHistoryProvider extends BlockfrostProvider implements ChainHistoryProvider {
37
+ private readonly cache : Map < string , Cardano . HydratedTx > = new Map ( ) ;
37
38
private networkInfoProvider : NetworkInfoProvider ;
38
39
39
40
constructor ( client : BlockfrostClient , networkInfoProvider : NetworkInfoProvider , logger : Logger ) {
@@ -475,11 +476,31 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
475
476
}
476
477
477
478
public async transactionsByHashes ( { ids } : TransactionsByIdsArgs ) : Promise < Cardano . HydratedTx [ ] > {
478
- try {
479
- return Promise . all ( ids . map ( ( id ) => this . fetchTransaction ( id ) ) ) ;
480
- } catch ( error ) {
481
- throw this . toProviderError ( error ) ;
479
+ const cachedTransactions : Cardano . HydratedTx [ ] = [ ] ;
480
+ const idsToFetch : Cardano . TransactionId [ ] = [ ] ;
481
+
482
+ for ( const id of ids ) {
483
+ if ( this . cache . has ( id ) ) {
484
+ cachedTransactions . push ( this . cache . get ( id ) ! ) ;
485
+ } else {
486
+ idsToFetch . push ( id ) ;
487
+ }
488
+ }
489
+
490
+ let fetchedTransactions : Cardano . HydratedTx [ ] = [ ] ;
491
+ if ( idsToFetch . length > 0 ) {
492
+ try {
493
+ fetchedTransactions = await Promise . all ( idsToFetch . map ( ( id ) => this . fetchTransaction ( id ) ) ) ;
494
+
495
+ for ( const tx of fetchedTransactions ) {
496
+ this . cache . set ( tx . id , tx ) ;
497
+ }
498
+ } catch ( error ) {
499
+ throw this . toProviderError ( error ) ;
500
+ }
482
501
}
502
+
503
+ return [ ...cachedTransactions , ...fetchedTransactions ] ;
483
504
}
484
505
485
506
// eslint-disable-next-line sonarjs/cognitive-complexity
0 commit comments