1
1
// eslint-disable-next-line jsdoc/check-param-names
2
2
import * as Crypto from '@cardano-sdk/crypto' ;
3
- import { BlockfrostProvider , BlockfrostProviderDependencies } from '../../util/BlockfrostProvider/BlockfrostProvider' ;
3
+
4
4
import {
5
+ BlockfrostClient ,
6
+ BlockfrostProvider ,
5
7
BlockfrostToCore ,
6
8
BlockfrostTransactionContent ,
7
9
blockfrostMetadataToTxMetadata ,
8
- blockfrostToProviderError ,
9
- fetchByAddressSequentially ,
10
+ fetchSequentially ,
10
11
isBlockfrostNotFoundError
11
- } from '../../util ' ;
12
+ } from '../blockfrost ' ;
12
13
import {
13
14
BlocksByIdsArgs ,
14
15
Cardano ,
@@ -22,22 +23,19 @@ import {
22
23
TransactionsByIdsArgs ,
23
24
createSlotEpochCalc
24
25
} from '@cardano-sdk/core' ;
25
- import { DB_MAX_SAFE_INTEGER } from '../DbSyncChainHistory/queries' ;
26
- import { Responses } from '@blockfrost/blockfrost-js' ;
27
- import { Schemas } from '@blockfrost/blockfrost-js/lib/types/open-api' ;
26
+ import { Logger } from 'ts-log' ;
28
27
import omit from 'lodash/omit.js' ;
28
+ import type { Responses } from '@blockfrost/blockfrost-js' ;
29
+ import type { Schemas } from '@blockfrost/blockfrost-js/lib/types/open-api' ;
29
30
30
31
type WithCertIndex < T > = T & { cert_index : number } ;
31
-
32
- export interface BlockfrostChainHistoryProviderDependencies extends BlockfrostProviderDependencies {
33
- networkInfoProvider : NetworkInfoProvider ;
34
- }
32
+ export const DB_MAX_SAFE_INTEGER = 2_147_483_647 ;
35
33
36
34
export class BlockfrostChainHistoryProvider extends BlockfrostProvider implements ChainHistoryProvider {
37
35
private networkInfoProvider : NetworkInfoProvider ;
38
36
39
- constructor ( { logger , blockfrost , networkInfoProvider } : BlockfrostChainHistoryProviderDependencies ) {
40
- super ( { blockfrost , logger } ) ;
37
+ constructor ( client : BlockfrostClient , networkInfoProvider : NetworkInfoProvider , logger : Logger ) {
38
+ super ( client , logger ) ;
41
39
this . networkInfoProvider = networkInfoProvider ;
42
40
}
43
41
@@ -46,7 +44,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
46
44
redeemer_count
47
45
} : Responses [ 'tx_content' ] ) : Promise < Cardano . Redeemer [ ] | undefined > {
48
46
if ( ! redeemer_count ) return ;
49
- return this . blockfrost . txsRedeemers ( hash ) . then ( ( response ) =>
47
+ return this . request < Responses [ 'tx_content_redeemers' ] > ( `txs/ ${ hash } /redeemers` ) . then ( ( response ) =>
50
48
response . map (
51
49
( { purpose, script_hash, unit_mem, unit_steps, tx_index } ) : Cardano . Redeemer => ( {
52
50
data : Buffer . from ( script_hash ) ,
@@ -79,7 +77,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
79
77
hash
80
78
} : Responses [ 'tx_content' ] ) : Promise < Cardano . Withdrawal [ ] | undefined > {
81
79
if ( ! withdrawal_count ) return ;
82
- return this . blockfrost . txsWithdrawals ( hash ) . then ( ( response ) =>
80
+ return this . request < Responses [ 'tx_content_withdrawals' ] > ( `txs/ ${ hash } /withdrawals` ) . then ( ( response ) =>
83
81
response . map (
84
82
( { address, amount } ) : Cardano . Withdrawal => ( {
85
83
quantity : BigInt ( amount ) ,
@@ -107,7 +105,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
107
105
}
108
106
109
107
protected async fetchPoolRetireCerts ( hash : string ) : Promise < WithCertIndex < Cardano . PoolRetirementCertificate > [ ] > {
110
- return this . blockfrost . txsPoolRetires ( hash ) . then ( ( response ) =>
108
+ return this . request < Responses [ 'tx_content_pool_retires' ] > ( `txs/ ${ hash } /pool_retires` ) . then ( ( response ) =>
111
109
response . map ( ( { pool_id, retiring_epoch, cert_index } ) => ( {
112
110
__typename : Cardano . CertificateType . PoolRetirement ,
113
111
cert_index,
@@ -118,7 +116,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
118
116
}
119
117
120
118
protected async fetchPoolUpdateCerts ( hash : string ) : Promise < WithCertIndex < Cardano . PoolRegistrationCertificate > [ ] > {
121
- return this . blockfrost . txsPoolUpdates ( hash ) . then ( ( response ) =>
119
+ return this . request < Responses [ 'tx_content_pool_certs' ] > ( `txs/ ${ hash } /pool_updates` ) . then ( ( response ) =>
122
120
response . map ( ( { pool_id, cert_index, fixed_cost, margin_cost, pledge, reward_account, vrf_key } ) => ( {
123
121
__typename : Cardano . CertificateType . PoolRegistration ,
124
122
cert_index,
@@ -138,10 +136,9 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
138
136
}
139
137
140
138
async fetchCBOR ( hash : string ) : Promise < string > {
141
- return this . blockfrost
142
- . instance < Schemas [ 'script_cbor' ] > ( `txs/${ hash } /cbor` )
139
+ return this . request < Responses [ 'tx_content_cbor' ] > ( `txs/${ hash } /cbor` )
143
140
. then ( ( response ) => {
144
- if ( response . body . cbor ) return response . body . cbor ;
141
+ if ( response ) return response . cbor ;
145
142
throw new Error ( 'CBOR is null' ) ;
146
143
} )
147
144
. catch ( ( _error ) => {
@@ -163,7 +160,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
163
160
}
164
161
165
162
protected async fetchMirCerts ( hash : string ) : Promise < WithCertIndex < Cardano . MirCertificate > [ ] > {
166
- return this . blockfrost . txsMirs ( hash ) . then ( ( response ) =>
163
+ return this . request < Responses [ 'tx_content_mirs' ] > ( `txs/ ${ hash } /mirs` ) . then ( ( response ) =>
167
164
response . map ( ( { address, amount, cert_index, pot } ) => ( {
168
165
__typename : Cardano . CertificateType . MIR ,
169
166
cert_index,
@@ -176,7 +173,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
176
173
}
177
174
178
175
protected async fetchStakeCerts ( hash : string ) : Promise < WithCertIndex < Cardano . StakeAddressCertificate > [ ] > {
179
- return this . blockfrost . txsStakes ( hash ) . then ( ( response ) =>
176
+ return this . request < Responses [ 'tx_content_stake_addr' ] > ( `txs/ ${ hash } /stakes` ) . then ( ( response ) =>
180
177
response . map ( ( { address, cert_index, registration } ) => ( {
181
178
__typename : registration
182
179
? Cardano . CertificateType . StakeRegistration
@@ -191,7 +188,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
191
188
}
192
189
193
190
protected async fetchDelegationCerts ( hash : string ) : Promise < WithCertIndex < Cardano . StakeDelegationCertificate > [ ] > {
194
- return this . blockfrost . txsDelegations ( hash ) . then ( ( response ) =>
191
+ return this . request < Responses [ 'tx_content_delegations' ] > ( `txs/ ${ hash } /delegations` ) . then ( ( response ) =>
195
192
response . map ( ( { address, pool_id, cert_index } ) => ( {
196
193
__typename : Cardano . CertificateType . StakeDelegation ,
197
194
cert_index,
@@ -230,8 +227,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
230
227
231
228
protected async fetchJsonMetadataAsAuxiliaryData ( txHash : string ) : Promise < Cardano . AuxiliaryData | undefined > {
232
229
const UNDEFINED = undefined ;
233
- return this . blockfrost
234
- . txsMetadata ( txHash )
230
+ return this . request < Responses [ 'tx_content_metadata' ] > ( `txs/${ txHash } /metadata` )
235
231
. then ( ( m ) => {
236
232
const metadata = blockfrostMetadataToTxMetadata ( m ) ;
237
233
return metadata && metadata . size > 0
@@ -257,7 +253,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
257
253
}
258
254
259
255
protected async fetchEpochParameters ( epochNo : Cardano . EpochNo ) : Promise < Schemas [ 'epoch_param_content' ] > {
260
- return await this . blockfrost . epochsParameters ( epochNo ) ;
256
+ return await this . request < Responses [ 'epoch_param_content' ] > ( `epochs/ ${ epochNo } /parameters` ) ;
261
257
}
262
258
263
259
protected async processCertificates (
@@ -375,7 +371,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
375
371
const txFromCBOR = await this . fetchDetailsFromCBOR ( id ) ;
376
372
if ( ! txFromCBOR ) return ;
377
373
378
- const utxos : Schemas [ 'tx_content_utxo' ] = ( await this . blockfrost . txsUtxos ( id ) ) as Schemas [ 'tx_content_utxo' ] ;
374
+ const utxos = await this . request < Responses [ 'tx_content_utxo' ] > ( `txs/ ${ id } /utxos` ) ;
379
375
380
376
// We can't use txFromCBOR.body.inputs since it misses HydratedTxIn.address
381
377
const { inputs, outputs, collaterals } = this . transactionUtxos ( utxos , txFromCBOR ) ;
@@ -418,11 +414,11 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
418
414
419
415
protected async fetchTransaction ( txId : Cardano . TransactionId ) : Promise < Cardano . HydratedTx > {
420
416
try {
421
- const txContent = await this . blockfrost . txs ( txId . toString ( ) ) ;
417
+ const txContent = await this . request < Responses [ 'tx_content' ] > ( `txs/ ${ txId . toString ( ) } ` ) ;
422
418
423
419
return ( await this . transactionDetailsUsingCBOR ( txContent ) ) ?? ( await this . transactionDetailsUsingAPIs ( txContent ) ) ;
424
420
} catch ( error ) {
425
- throw blockfrostToProviderError ( error ) ;
421
+ throw this . toProviderError ( error ) ;
426
422
}
427
423
}
428
424
@@ -444,7 +440,9 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
444
440
445
441
public async blocksByHashes ( { ids } : BlocksByIdsArgs ) : Promise < Cardano . ExtendedBlockInfo [ ] > {
446
442
try {
447
- const responses = await Promise . all ( ids . map ( ( id ) => this . blockfrost . blocks ( id . toString ( ) ) ) ) ;
443
+ const responses = await Promise . all (
444
+ ids . map ( ( id ) => this . request < Responses [ 'block_content' ] > ( `blocks/${ id . toString ( ) } ` ) )
445
+ ) ;
448
446
return responses . map ( ( response ) => {
449
447
if ( ! response . epoch || ! response . epoch_slot || ! response . height || ! response . slot || ! response . block_vrf ) {
450
448
throw new ProviderError ( ProviderFailure . Unknown , null , 'Queried unsupported block' ) ;
@@ -470,15 +468,15 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
470
468
} ;
471
469
} ) ;
472
470
} catch ( error ) {
473
- throw blockfrostToProviderError ( error ) ;
471
+ throw this . toProviderError ( error ) ;
474
472
}
475
473
}
476
474
477
475
public async transactionsByHashes ( { ids } : TransactionsByIdsArgs ) : Promise < Cardano . HydratedTx [ ] > {
478
476
try {
479
477
return Promise . all ( ids . map ( ( id ) => this . fetchTransaction ( id ) ) ) ;
480
478
} catch ( error ) {
481
- throw blockfrostToProviderError ( error ) ;
479
+ throw this . toProviderError ( error ) ;
482
480
}
483
481
}
484
482
@@ -495,21 +493,18 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
495
493
496
494
const addressTransactions = await Promise . all (
497
495
addresses . map ( async ( address ) =>
498
- fetchByAddressSequentially <
499
- { tx_hash : string ; tx_index : number ; block_height : number } ,
500
- BlockfrostTransactionContent
501
- > ( {
502
- address,
496
+ fetchSequentially < { tx_hash : string ; tx_index : number ; block_height : number } , BlockfrostTransactionContent > ( {
503
497
haveEnoughItems : blockRange ?. lowerBound
504
498
? ( transactions ) =>
505
499
transactions . length > 0 &&
506
500
transactions [ transactions . length - 1 ] . block_height < blockRange ! . lowerBound !
507
501
: undefined ,
508
- request : ( addr : Cardano . PaymentAddress , paginationOptions ) =>
509
- this . blockfrost . addressesTransactions ( addr . toString ( ) , paginationOptions , {
510
- from : blockRange ?. lowerBound ? blockRange ?. lowerBound . toString ( ) : undefined ,
511
- to : blockRange ?. upperBound ? blockRange ?. upperBound . toString ( ) : undefined
512
- } )
502
+ request : ( paginationQueryString ) => {
503
+ let queryString = `addresses/${ address } /transactions?${ paginationQueryString } ` ;
504
+ if ( blockRange ?. lowerBound ) queryString += `&from=${ blockRange . lowerBound . toString ( ) } ` ;
505
+ if ( blockRange ?. upperBound ) queryString += `&to=${ blockRange . upperBound . toString ( ) } ` ;
506
+ return this . request < Responses [ 'address_transactions_content' ] > ( queryString ) ;
507
+ }
513
508
} )
514
509
)
515
510
) ;
@@ -526,7 +521,7 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
526
521
527
522
return { pageResults, totalResultCount : allTransactions . length } ;
528
523
} catch ( error ) {
529
- throw blockfrostToProviderError ( error ) ;
524
+ throw this . toProviderError ( error ) ;
530
525
}
531
526
}
532
527
@@ -581,6 +576,6 @@ export class BlockfrostChainHistoryProvider extends BlockfrostProvider implement
581
576
}
582
577
583
578
private fetchUtxos ( id : Cardano . TransactionId ) : Promise < Schemas [ 'tx_content_utxo' ] > {
584
- return this . blockfrost . txsUtxos ( id ) ;
579
+ return this . request < Responses [ 'tx_content_utxo' ] > ( `txs/ ${ id } /utxos` ) ;
585
580
}
586
581
}
0 commit comments