11import { loadDeals } from './deal-observer.js'
22import * as util from 'node:util'
3- import { getMinerPeerId } from './rpc-service/service.js'
43import { PayloadRetrievabilityState } from '@filecoin-station/deal-observer-db/lib/types.js'
4+ import debug from 'debug'
5+ import { GLIF_TOKEN , RPC_URL } from './config.js'
6+ import { ethers } from 'ethers'
7+ import {
8+ getIndexProviderPeerId ,
9+ MINER_TO_PEERID_CONTRACT_ADDRESS , MINER_TO_PEERID_CONTRACT_ABI
10+ // @ts -ignore
11+ } from 'index-provider-peer-id'
12+ import { rpcRequest } from './rpc-service/service.js'
13+ import assert from 'node:assert'
514
615/** @import {Queryable} from '@filecoin-station/deal-observer-db' */
716/** @import { Static } from '@sinclair/typebox' */
@@ -11,16 +20,18 @@ const THREE_DAYS_IN_MILLISECONDS = 1000 * 60 * 60 * 24 * 3
1120
1221/**
1322 *
14- * @param {import('./typings.js').MakeRpcRequest } makeRpcRequest
23+ * @param {import('./typings.js').GetIndexProviderPeerId } getIndexProviderPeerId
1524 * @param {import('./typings.js').MakePayloadCidRequest } makePayloadCidRequest
1625 * @param {Queryable } pgPool
1726 * @param {number } maxDeals
27+ * @param {number } [now] The current timestamp in milliseconds
1828 * @returns {Promise<number> }
1929 */
20- export const resolvePayloadCids = async ( makeRpcRequest , makePayloadCidRequest , pgPool , maxDeals , now = Date . now ( ) ) => {
30+ export const resolvePayloadCids = async ( getIndexProviderPeerId , makePayloadCidRequest , pgPool , maxDeals , now = Date . now ( ) ) => {
2131 let payloadCidsResolved = 0
2232 for ( const deal of await fetchDealsWithUnresolvedPayloadCid ( pgPool , maxDeals , new Date ( now - THREE_DAYS_IN_MILLISECONDS ) ) ) {
23- const minerPeerId = await getMinerPeerId ( deal . miner_id , makeRpcRequest )
33+ const { peerId : minerPeerId , source } = await getIndexProviderPeerId ( deal . miner_id )
34+ debug ( `Using PeerID from ${ source } .` )
2435 const payloadCid = await makePayloadCidRequest ( minerPeerId , deal . piece_cid )
2536 if ( payloadCid ) deal . payload_cid = payloadCid
2637 if ( ! deal . payload_cid ) {
@@ -106,3 +117,31 @@ async function updatePayloadCidInActiveDeal (pgPool, deal, newPayloadRetrievalSt
106117 throw Error ( util . format ( 'Error updating payload of deal: ' , deal ) , { cause : error } )
107118 }
108119}
120+
121+ function getSmartContractClient ( ) {
122+ const fetchRequest = new ethers . FetchRequest ( RPC_URL )
123+ assert ( GLIF_TOKEN , 'GLIF_TOKEN is required' )
124+ fetchRequest . setHeader ( 'Authorization' , `Bearer ${ GLIF_TOKEN } ` )
125+ const provider = new ethers . JsonRpcProvider ( fetchRequest )
126+ return new ethers . Contract (
127+ MINER_TO_PEERID_CONTRACT_ADDRESS ,
128+ MINER_TO_PEERID_CONTRACT_ABI ,
129+ provider
130+ )
131+ }
132+ const defaultSmartContractClient = getSmartContractClient ( )
133+
134+ /**
135+ * @param {number } minerId
136+ * @param {object } [options]
137+ * @param {unknown } options.smartContract
138+ * @param {import('./typings.js').MakeRpcRequest } options.makeRpcRequest
139+ * @returns {Promise<{ peerId: string, source: string }> }
140+ */
141+ export const getPeerId = async ( minerId , { smartContract, makeRpcRequest } = { smartContract : defaultSmartContractClient , makeRpcRequest : rpcRequest } ) => {
142+ return await getIndexProviderPeerId (
143+ `f0${ minerId } ` ,
144+ smartContract ,
145+ { rpcFn : makeRpcRequest }
146+ )
147+ }
0 commit comments