This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add index provider library #7
Merged
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b24b508
add library
00729f2
add dependencies
13eb682
revert multiaddr test
f66700e
resolve PR threads
8ddcbdc
fmt
6a1990f
changed import naming
49bbe56
changed import naming
2b56a25
changed default naming
998257f
change test naming
9a6b053
Merge branch 'main' into nhaimerl-add-index-probier-library
c024010
lint
cb0d03a
add abort signal
1f35d65
removed error message
8d747b3
merged with main
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,89 +1,33 @@ | ||
| import { retry } from '../vendor/deno-deps.js' | ||
| import { RPC_URL, RPC_AUTH } from './constants.js' | ||
| import { | ||
| getIndexProviderPeerId, | ||
| MINER_TO_PEERID_CONTRACT_ADDRESS, | ||
| MINER_TO_PEERID_CONTRACT_ABI | ||
| , ethers | ||
| } from '../vendor/deno-deps.js' | ||
|
|
||
| async function getChainHead ({ maxAttempts = 5 } = {}) { | ||
| try { | ||
| const res = await retry(() => rpc('Filecoin.ChainHead'), { | ||
| // The maximum amount of attempts until failure. | ||
| maxAttempts, | ||
| // The initial and minimum amount of milliseconds between attempts. | ||
| minTimeout: 5_000, | ||
| // How much to backoff after each retry. | ||
| multiplier: 1.5 | ||
| }) | ||
| return res.Cids | ||
| } catch (err) { | ||
| if (err.name === 'RetryError' && err.cause) { | ||
| // eslint-disable-next-line no-ex-assign | ||
| err = err.cause | ||
| } | ||
| err.message = `Cannot obtain chain head: ${err.message}` | ||
| throw err | ||
| } | ||
| } | ||
| // Initialize your ethers contract instance | ||
| const fetchRequest = new ethers.FetchRequest(RPC_URL) | ||
| fetchRequest.setHeader('Authorization', `Bearer ${RPC_AUTH}`) | ||
| const provider = new ethers.JsonRpcProvider(fetchRequest) | ||
| const smartContractClient = new ethers.Contract( | ||
| MINER_TO_PEERID_CONTRACT_ADDRESS, | ||
| MINER_TO_PEERID_CONTRACT_ABI, | ||
| provider | ||
| ) | ||
|
|
||
| /** | ||
| * @param {string} minerId A miner actor id, e.g. `f0142637` | ||
| * @param {object} options | ||
| * @param {number} [options.maxAttempts] | ||
| * @returns {Promise<string>} Miner's PeerId, e.g. `12D3KooWMsPmAA65yHAHgbxgh7CPkEctJHZMeM3rAvoW8CZKxtpG` | ||
| * @param {string} minerId - The ID of the miner. | ||
| * @param {object} options - Options for the function. | ||
| * @param {number} options.maxAttempts - The maximum number of attempts to fetch the peer ID. | ||
| * @returns {Promise<string>} The peer ID of the miner. | ||
| */ | ||
| export async function getMinerPeerId (minerId, { maxAttempts = 5 } = {}) { | ||
| const chainHead = await getChainHead({ maxAttempts }) | ||
| try { | ||
| const res = await retry(() => rpc('Filecoin.StateMinerInfo', minerId, chainHead), { | ||
| // The maximum amount of attempts until failure. | ||
| maxAttempts, | ||
| // The initial and minimum amount of milliseconds between attempts. | ||
| minTimeout: 5_000, | ||
| // How much to backoff after each retry. | ||
| multiplier: 1.5 | ||
| }) | ||
| return res.PeerId | ||
| } catch (err) { | ||
| if (err.name === 'RetryError' && err.cause) { | ||
| // eslint-disable-next-line no-ex-assign | ||
| err = err.cause | ||
| } | ||
| err.message = `Cannot obtain miner info for ${minerId}: ${err.message}` | ||
| throw err | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} method | ||
| * @param {unknown[]} params | ||
| */ | ||
| async function rpc (method, ...params) { | ||
| const req = new Request(RPC_URL, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'content-type': 'application/json', | ||
| accepts: 'application/json', | ||
| authorization: `Bearer ${RPC_AUTH}` | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method, | ||
| params | ||
| return ( | ||
| await getIndexProviderPeerId(minerId, smartContractClient, { | ||
| rpcUrl: RPC_URL, | ||
| rpcAuth: RPC_AUTH, | ||
| maxAttempts | ||
| }) | ||
|
||
| }) | ||
| const res = await fetch(req, { | ||
| signal: AbortSignal.timeout(60_000) | ||
| }) | ||
|
|
||
| if (!res.ok) { | ||
| throw new Error(`JSON RPC failed with ${res.code}: ${(await res.text()).trimEnd()}`) | ||
| } | ||
|
|
||
| const body = await res.json() | ||
| if (body.error) { | ||
| const err = new Error(body.error.message) | ||
| err.name = 'FilecoinRpcError' | ||
| err.code = body.code | ||
| throw err | ||
| } | ||
|
|
||
| return body.result | ||
| ).peerId | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.