@@ -3,6 +3,8 @@ import { Cardano, Serialization, UtxoByAddressesArgs, UtxoProvider } from '@card
3
3
import type { Responses } from '@blockfrost/blockfrost-js' ;
4
4
5
5
export class BlockfrostUtxoProvider extends BlockfrostProvider implements UtxoProvider {
6
+ private readonly cache : Map < string , Cardano . Tx > = new Map ( ) ;
7
+
6
8
protected async fetchUtxos ( addr : Cardano . PaymentAddress , paginationQueryString : string ) : Promise < Cardano . Utxo [ ] > {
7
9
const queryString = `addresses/${ addr . toString ( ) } /utxos?${ paginationQueryString } ` ;
8
10
const utxos = await this . request < Responses [ 'address_utxo_content' ] > ( queryString ) ;
@@ -27,7 +29,11 @@ export class BlockfrostUtxoProvider extends BlockfrostProvider implements UtxoPr
27
29
} ) ;
28
30
}
29
31
protected async fetchDetailsFromCBOR ( hash : string ) {
30
- return this . fetchCBOR ( hash )
32
+ if ( this . cache . has ( hash ) ) {
33
+ return this . cache . get ( hash ) ;
34
+ }
35
+
36
+ const result = await this . fetchCBOR ( hash )
31
37
. then ( ( cbor ) => {
32
38
const tx = Serialization . Transaction . fromCbor ( Serialization . TxCBOR ( cbor ) ) . toCore ( ) ;
33
39
this . logger . info ( 'Fetched details from CBOR for tx' , hash ) ;
@@ -37,7 +43,15 @@ export class BlockfrostUtxoProvider extends BlockfrostProvider implements UtxoPr
37
43
this . logger . warn ( 'Failed to fetch details from CBOR for tx' , hash , error ) ;
38
44
return null ;
39
45
} ) ;
46
+
47
+ if ( ! result ) {
48
+ return null ;
49
+ }
50
+
51
+ this . cache . set ( hash , result ) ;
52
+ return result ;
40
53
}
54
+
41
55
public async utxoByAddresses ( { addresses } : UtxoByAddressesArgs ) : Promise < Cardano . Utxo [ ] > {
42
56
try {
43
57
const utxoResults = await Promise . all (
0 commit comments