@@ -24,6 +24,7 @@ import {
24
24
import { Logger } from 'ts-log' ;
25
25
import omit from 'lodash/omit.js' ;
26
26
import uniq from 'lodash/uniq.js' ;
27
+ import type { Cache } from '@cardano-sdk/util' ;
27
28
import type { Responses } from '@blockfrost/blockfrost-js' ;
28
29
import type { Schemas } from '@blockfrost/blockfrost-js/lib/types/open-api' ;
29
30
@@ -33,12 +34,20 @@ export const DB_MAX_SAFE_INTEGER = 2_147_483_647;
33
34
type BlockfrostTx = Pick < Responses [ 'address_transactions_content' ] [ 0 ] , 'block_height' | 'tx_index' > ;
34
35
const compareTx = ( a : BlockfrostTx , b : BlockfrostTx ) => a . block_height - b . block_height || a . tx_index - b . tx_index ;
35
36
37
+ type BlockfrostChainHistoryProviderDependencies = {
38
+ cache : Cache < Cardano . HydratedTx > ;
39
+ client : BlockfrostClient ;
40
+ networkInfoProvider : NetworkInfoProvider ;
41
+ logger : Logger ;
42
+ } ;
43
+
36
44
export class BlockfrostChainHistoryProvider extends BlockfrostProvider implements ChainHistoryProvider {
37
- private readonly cache : Map < string , Cardano . HydratedTx > = new Map ( ) ;
45
+ private readonly cache : Cache < Cardano . HydratedTx > ;
38
46
private networkInfoProvider : NetworkInfoProvider ;
39
47
40
- constructor ( client : BlockfrostClient , networkInfoProvider : NetworkInfoProvider , logger : Logger ) {
48
+ constructor ( { cache , client , networkInfoProvider , logger } : BlockfrostChainHistoryProviderDependencies ) {
41
49
super ( client , logger ) ;
50
+ this . cache = cache ;
42
51
this . networkInfoProvider = networkInfoProvider ;
43
52
}
44
53
@@ -478,12 +487,12 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
478
487
public async transactionsByHashes ( { ids } : TransactionsByIdsArgs ) : Promise < Cardano . HydratedTx [ ] > {
479
488
return Promise . all (
480
489
ids . map ( async ( id ) => {
481
- if ( this . cache . has ( id ) ) {
482
- return this . cache . get ( id ) ! ;
483
- }
490
+ const cached = await this . cache . get ( id ) ;
491
+ if ( cached ) return cached ;
492
+
484
493
try {
485
494
const fetchedTransaction = await this . fetchTransaction ( id ) ;
486
- this . cache . set ( id , fetchedTransaction ) ;
495
+ void this . cache . set ( id , fetchedTransaction ) ;
487
496
return fetchedTransaction ;
488
497
} catch ( error ) {
489
498
throw this . toProviderError ( error ) ;
0 commit comments