@@ -8,15 +8,15 @@ import { getIndexProviderPeerIdFromSmartContract } from './smart-contract-client
8
8
* @param {function } [options.rpcFn]
9
9
* @returns {Promise<string> } The chain head Cid
10
10
*/
11
- async function getChainHead ( { maxAttempts = 5 , rpcFn } = { } ) {
11
+ async function getChainHead ( { maxAttempts = 5 , rpcFn } = { } ) {
12
12
try {
13
13
const res = await retry ( ( ) => ( rpcFn ?? rpc ) ( 'Filecoin.ChainHead' ) , {
14
14
// The maximum amount of attempts until failure.
15
15
maxAttempts,
16
16
// The initial and minimum amount of milliseconds between attempts.
17
17
minTimeout : 5_000 ,
18
18
// How much to backoff after each retry.
19
- multiplier : 1.5
19
+ multiplier : 1.5 ,
20
20
} )
21
21
return res . Cids
22
22
} catch ( err ) {
@@ -35,12 +35,15 @@ async function getChainHead ({ maxAttempts = 5, rpcFn } = {}) {
35
35
* @param {number } [options.maxAttempts]
36
36
* @returns {Promise<string> } Miner's PeerId, e.g. `12D3KooWMsPmAA65yHAHgbxgh7CPkEctJHZMeM3rAvoW8CZKxtpG`
37
37
*/
38
- export async function getIndexProviderPeerId ( minerId , { maxAttempts = 5 , smartContract, rpcFn } = { } ) {
38
+ export async function getIndexProviderPeerId (
39
+ minerId ,
40
+ { maxAttempts = 5 , smartContract, rpcFn } = { } ,
41
+ ) {
39
42
try {
40
- // Make a concurrent request to both sources: FilecoinMinerInfo and smart contract
43
+ // Make a concurrent request to both sources: FilecoinMinerInfo and smart contract
41
44
const [ minerInfoResult , contractResult ] = await Promise . all ( [
42
45
getIndexProviderPeerIdFromFilecoinMinerInfo ( minerId , { maxAttempts, rpcFn } ) ,
43
- getIndexProviderPeerIdFromSmartContract ( minerId , { smartContract } )
46
+ getIndexProviderPeerIdFromSmartContract ( minerId , { smartContract } ) ,
44
47
] )
45
48
// Check contract result first
46
49
if ( contractResult ) {
@@ -55,11 +58,13 @@ export async function getIndexProviderPeerId (minerId, { maxAttempts = 5, smartC
55
58
}
56
59
57
60
// Handle the case where both failed
58
- throw new Error ( `Failed to obtain Miner's Index Provider PeerID.\nSmartContract query result: ${ contractResult } \nStateMinerInfo query result: ${ minerInfoResult } ` )
61
+ throw new Error (
62
+ `Failed to obtain Miner's Index Provider PeerID.\nSmartContract query result: ${ contractResult } \nStateMinerInfo query result: ${ minerInfoResult } ` ,
63
+ )
59
64
} catch ( error ) {
60
65
console . error ( 'Error fetching PeerID:' , error )
61
66
throw Error ( `Error fetching PeerID for miner ${ minerId } .` , {
62
- cause : error
67
+ cause : error ,
63
68
} )
64
69
}
65
70
}
@@ -71,7 +76,10 @@ export async function getIndexProviderPeerId (minerId, { maxAttempts = 5, smartC
71
76
* @param {function } [options.rpcFn]
72
77
* @returns {Promise<string> } Miner's PeerId, e.g. `12D3KooWMsPmAA65yHAHgbxgh7CPkEctJHZMeM3rAvoW8CZKxtpG`
73
78
*/
74
- export async function getIndexProviderPeerIdFromFilecoinMinerInfo ( minerId , { maxAttempts = 5 , rpcFn } = { } ) {
79
+ export async function getIndexProviderPeerIdFromFilecoinMinerInfo (
80
+ minerId ,
81
+ { maxAttempts = 5 , rpcFn } = { } ,
82
+ ) {
75
83
const chainHead = await getChainHead ( { maxAttempts, rpcFn } )
76
84
try {
77
85
const res = await retry ( ( ) => ( rpcFn ?? rpc ) ( 'Filecoin.StateMinerInfo' , minerId , chainHead ) , {
@@ -80,7 +88,7 @@ export async function getIndexProviderPeerIdFromFilecoinMinerInfo (minerId, { ma
80
88
// The initial and minimum amount of milliseconds between attempts.
81
89
minTimeout : 5_000 ,
82
90
// How much to backoff after each retry.
83
- multiplier : 1.5
91
+ multiplier : 1.5 ,
84
92
} )
85
93
return res . PeerId
86
94
} catch ( err ) {
@@ -97,23 +105,23 @@ export async function getIndexProviderPeerIdFromFilecoinMinerInfo (minerId, { ma
97
105
* @param {string } method
98
106
* @param {unknown[] } params
99
107
*/
100
- async function rpc ( method , ...params ) {
108
+ async function rpc ( method , ...params ) {
101
109
const req = new Request ( RPC_URL , {
102
110
method : 'POST' ,
103
111
headers : {
104
112
'content-type' : 'application/json' ,
105
113
accepts : 'application/json' ,
106
- authorization : `Bearer ${ RPC_AUTH } `
114
+ authorization : `Bearer ${ RPC_AUTH } ` ,
107
115
} ,
108
116
body : JSON . stringify ( {
109
117
jsonrpc : '2.0' ,
110
118
id : 1 ,
111
119
method,
112
- params
113
- } )
120
+ params,
121
+ } ) ,
114
122
} )
115
123
const res = await fetch ( req , {
116
- signal : AbortSignal . timeout ( 60_000 )
124
+ signal : AbortSignal . timeout ( 60_000 ) ,
117
125
} )
118
126
119
127
if ( ! res . ok ) {
0 commit comments