|
1 | | -import { retry } from '../vendor/deno-deps.js' |
2 | 1 | import { RPC_URL, RPC_AUTH } from './constants.js' |
| 2 | +import { |
| 3 | + getIndexProviderPeerId as getPeerId, |
| 4 | + MINER_TO_PEERID_CONTRACT_ADDRESS, |
| 5 | + MINER_TO_PEERID_CONTRACT_ABI |
| 6 | + , ethers |
| 7 | +} from '../vendor/deno-deps.js' |
3 | 8 |
|
4 | | -async function getChainHead ({ maxAttempts = 5 } = {}) { |
5 | | - try { |
6 | | - const res = await retry(() => rpc('Filecoin.ChainHead'), { |
7 | | - // The maximum amount of attempts until failure. |
8 | | - maxAttempts, |
9 | | - // The initial and minimum amount of milliseconds between attempts. |
10 | | - minTimeout: 5_000, |
11 | | - // How much to backoff after each retry. |
12 | | - multiplier: 1.5 |
13 | | - }) |
14 | | - return res.Cids |
15 | | - } catch (err) { |
16 | | - if (err.name === 'RetryError' && err.cause) { |
17 | | - // eslint-disable-next-line no-ex-assign |
18 | | - err = err.cause |
19 | | - } |
20 | | - err.message = `Cannot obtain chain head: ${err.message}` |
21 | | - throw err |
22 | | - } |
23 | | -} |
| 9 | +// Initialize your ethers contract instance |
| 10 | +const fetchRequest = new ethers.FetchRequest(RPC_URL) |
| 11 | +fetchRequest.setHeader('Authorization', `Bearer ${RPC_AUTH}`) |
| 12 | +const provider = new ethers.JsonRpcProvider(fetchRequest) |
| 13 | +const smartContractClient = new ethers.Contract( |
| 14 | + MINER_TO_PEERID_CONTRACT_ADDRESS, |
| 15 | + MINER_TO_PEERID_CONTRACT_ABI, |
| 16 | + provider |
| 17 | +) |
24 | 18 |
|
25 | 19 | /** |
26 | | - * @param {string} minerId A miner actor id, e.g. `f0142637` |
27 | | - * @param {object} options |
28 | | - * @param {number} [options.maxAttempts] |
29 | | - * @returns {Promise<string>} Miner's PeerId, e.g. `12D3KooWMsPmAA65yHAHgbxgh7CPkEctJHZMeM3rAvoW8CZKxtpG` |
| 20 | + * @param {string} minerId - The ID of the miner. |
| 21 | + * @param {object} options - Options for the function. |
| 22 | + * @param {number} options.maxAttempts - The maximum number of attempts to fetch the peer ID. |
| 23 | + * @returns {Promise<string>} The peer ID of the miner. |
30 | 24 | */ |
31 | | -export async function getMinerPeerId (minerId, { maxAttempts = 5 } = {}) { |
32 | | - const chainHead = await getChainHead({ maxAttempts }) |
| 25 | +export async function getIndexProviderPeerId (minerId, { maxAttempts = 5 } = {}) { |
33 | 26 | try { |
34 | | - const res = await retry(() => rpc('Filecoin.StateMinerInfo', minerId, chainHead), { |
35 | | - // The maximum amount of attempts until failure. |
| 27 | + const { peerId, source } = await getPeerId(minerId, smartContractClient, { |
| 28 | + rpcUrl: RPC_URL, |
| 29 | + rpcAuth: RPC_AUTH, |
36 | 30 | maxAttempts, |
37 | | - // The initial and minimum amount of milliseconds between attempts. |
38 | | - minTimeout: 5_000, |
39 | | - // How much to backoff after each retry. |
40 | | - multiplier: 1.5 |
| 31 | + signal: AbortSignal.timeout(60_000) |
41 | 32 | }) |
42 | | - return res.PeerId |
| 33 | + console.log(`Peer ID fetched from ${source}.`) |
| 34 | + return peerId |
43 | 35 | } catch (err) { |
44 | | - if (err.name === 'RetryError' && err.cause) { |
45 | | - // eslint-disable-next-line no-ex-assign |
46 | | - err = err.cause |
47 | | - } |
48 | | - err.message = `Cannot obtain miner info for ${minerId}: ${err.message}` |
49 | | - throw err |
50 | | - } |
51 | | -} |
52 | | - |
53 | | -/** |
54 | | - * @param {string} method |
55 | | - * @param {unknown[]} params |
56 | | - */ |
57 | | -async function rpc (method, ...params) { |
58 | | - const req = new Request(RPC_URL, { |
59 | | - method: 'POST', |
60 | | - headers: { |
61 | | - 'content-type': 'application/json', |
62 | | - accepts: 'application/json', |
63 | | - authorization: `Bearer ${RPC_AUTH}` |
64 | | - }, |
65 | | - body: JSON.stringify({ |
66 | | - jsonrpc: '2.0', |
67 | | - id: 1, |
68 | | - method, |
69 | | - params |
70 | | - }) |
71 | | - }) |
72 | | - const res = await fetch(req, { |
73 | | - signal: AbortSignal.timeout(60_000) |
74 | | - }) |
75 | | - |
76 | | - if (!res.ok) { |
77 | | - throw new Error(`JSON RPC failed with ${res.code}: ${(await res.text()).trimEnd()}`) |
78 | | - } |
79 | | - |
80 | | - const body = await res.json() |
81 | | - if (body.error) { |
82 | | - const err = new Error(body.error.message) |
83 | | - err.name = 'FilecoinRpcError' |
84 | | - err.code = body.code |
| 36 | + console.error(err) |
85 | 37 | throw err |
86 | 38 | } |
87 | | - |
88 | | - return body.result |
89 | 39 | } |
0 commit comments