@@ -11,7 +11,6 @@ import {
1111 NodeEnvironments ,
1212} from '@/types/environments' ;
1313import { roundTokenAmount } from '@/utils/formatters' ;
14- import { multiaddr } from '@multiformats/multiaddr' ;
1514import axios from 'axios' ;
1615import { useSearchParams } from 'next/navigation' ;
1716import { createContext , ReactNode , useCallback , useContext , useEffect , useState } from 'react' ;
@@ -36,7 +35,7 @@ type RunJobContextType = {
3635 environment : ComputeEnvironment ;
3736 freeCompute : boolean ;
3837 maxJobDurationSeconds : number ;
39- multiaddrsOrPeerId : MultiaddrsOrPeerId ;
38+ multiaddrsOrPeerId : string [ ] | string ;
4039 onError ?: ( error : unknown ) => void ;
4140 onSuccess ?: ( cost : number , minLockSeconds : number ) => void ;
4241 resources : { id : string ; amount : number } [ ] ;
@@ -73,7 +72,7 @@ const RunJobContext = createContext<RunJobContextType | undefined>(undefined);
7372
7473export const RunJobProvider = ( { children } : { children : ReactNode } ) => {
7574 const { provider } = useOceanAccount ( ) ;
76- const { initializeCompute, node : p2pNode } = useP2P ( ) ;
75+ const { initializeCompute, isReady : p2pIsReady } = useP2P ( ) ;
7776
7877 const searchParams = useSearchParams ( ) ;
7978 const [ estimatedTotalCost , setEstimatedTotalCost ] = useState < number | null > ( null ) ;
@@ -134,8 +133,10 @@ export const RunJobProvider = ({ children }: { children: ReactNode }) => {
134133 setSelectedEnv ( environment ) ;
135134 setFreeCompute ( freeCompute ) ;
136135 setNodeInfo ( nodeInfo ) ;
137- const multiaddrs = new Set ( nodeInfo ?. multiaddrs ?. filter ( Boolean ) . filter ( multiaddr ) ) ;
138- setMultiaddrsOrPeerId ( multiaddrs . size > 0 ? Array . from ( multiaddrs ) : nodeInfo ?. id ) ;
136+
137+ const addrs = ( nodeInfo . multiaddrs ?? [ ] ) . map ( ( a ) => ( a . includes ( '/p2p/' ) ? a : `${ a } /p2p/${ nodeInfo . id } ` ) ) ;
138+ setMultiaddrsOrPeerId ( addrs . length > 0 ? addrs : nodeInfo . id ) ;
139+
139140 if ( resources ) {
140141 setSelectedResources ( resources ) ;
141142 }
@@ -169,7 +170,7 @@ export const RunJobProvider = ({ children }: { children: ReactNode }) => {
169170 environment : ComputeEnvironment ;
170171 freeCompute : boolean ;
171172 maxJobDurationSeconds : number ;
172- multiaddrsOrPeerId : MultiaddrsOrPeerId ;
173+ multiaddrsOrPeerId : string [ ] | string ;
173174 onError ?: ( error : unknown ) => void ;
174175 onSuccess ?: ( cost : number , minLockSeconds : number ) => void ;
175176 resources : { id : string ; amount : number } [ ] ;
@@ -179,7 +180,7 @@ export const RunJobProvider = ({ children }: { children: ReactNode }) => {
179180 setEstimatedTotalCost ( 0 ) ;
180181 return ;
181182 }
182- if ( ! provider || ! p2pNode ) {
183+ if ( ! provider || ! p2pIsReady ) {
183184 return ;
184185 }
185186 try {
@@ -190,8 +191,7 @@ export const RunJobProvider = ({ children }: { children: ReactNode }) => {
190191 multiaddrsOrPeerId ,
191192 environment . consumerAddress ,
192193 resources ,
193- CHAIN_ID ,
194- provider
194+ CHAIN_ID
195195 ) ;
196196 setEstimatedTotalCost ( roundTokenAmount ( Number ( cost ) , tokenAddress , 'up' ) ) ;
197197 setMinLockSeconds ( minLockSeconds ) ;
@@ -201,7 +201,7 @@ export const RunJobProvider = ({ children }: { children: ReactNode }) => {
201201 console . error ( 'Failed to fetch estimated cost:' , error ) ;
202202 }
203203 } ,
204- [ initializeCompute , p2pNode , provider ]
204+ [ initializeCompute , p2pIsReady , provider ]
205205 ) ;
206206
207207 /**
@@ -241,7 +241,6 @@ export const RunJobProvider = ({ children }: { children: ReactNode }) => {
241241 ) ;
242242 const foundEnv = foundNode ?. computeEnvironments . environments . find ( ( env ) => env . id === queryEnv ) ;
243243 if ( foundNode && foundEnv ) {
244- const multiaddrs = new Set ( foundNode ?. multiaddrs ?. filter ( Boolean ) . filter ( multiaddr ) ) ;
245244 const queryFree = searchParams . get ( 'free' ) === 'true' ;
246245 const qJobDuration = searchParams . get ( 'maxJobDuration' ) ;
247246 const queryGpusArray = searchParams . getAll ( 'gpus[]' ) ;
@@ -290,6 +289,10 @@ export const RunJobProvider = ({ children }: { children: ReactNode }) => {
290289 nodeInfo : foundNode ,
291290 resources,
292291 } ) ;
292+ const foundAddrs = ( foundNode . multiaddrs ?? [ ] ) . map ( ( a ) =>
293+ a . includes ( '/p2p/' ) ? a : `${ a } /p2p/${ foundNode . id } `
294+ ) ;
295+ const resolvedNodeUri = foundAddrs . length > 0 ? foundAddrs : foundNode . id ;
293296 if ( ! queryFree ) {
294297 const queryToken = searchParams . get ( 'token' ) ;
295298 if ( queryToken ) {
@@ -298,7 +301,7 @@ export const RunJobProvider = ({ children }: { children: ReactNode }) => {
298301 environment : foundEnv ,
299302 freeCompute : queryFree ,
300303 maxJobDurationSeconds : resources . maxJobDurationSeconds ,
301- multiaddrsOrPeerId : multiaddrs . size > 0 ? Array . from ( multiaddrs ) : foundNode . id ,
304+ multiaddrsOrPeerId : resolvedNodeUri ,
302305 resources : [
303306 ...( resources . cpuId && resources . cpuCores ? [ { id : resources . cpuId , amount : resources . cpuCores } ] : [ ] ) ,
304307 ...( resources . ramId && resources . ram ? [ { id : resources . ramId , amount : resources . ram } ] : [ ] ) ,
@@ -332,13 +335,13 @@ export const RunJobProvider = ({ children }: { children: ReactNode }) => {
332335 const queryFree = searchParams . get ( 'free' ) === 'true' ;
333336 // For paid compute, also wait for p2p node and provider in order to fetch the cost
334337 // For free compute, they are not needed
335- if ( queryFree || ( p2pNode && provider ) ) {
338+ if ( queryFree || ( p2pIsReady && provider ) ) {
336339 setHydrateFromUrlStarted ( true ) ;
337340 hydrateContextFromQueryParams ( ) ;
338341 }
339342 }
340343 }
341- } , [ hydrateContextFromQueryParams , hydrateFromUrlStarted , p2pNode , provider , searchParams ] ) ;
344+ } , [ hydrateContextFromQueryParams , hydrateFromUrlStarted , p2pIsReady , provider , searchParams ] ) ;
342345
343346 return (
344347 < RunJobContext . Provider
0 commit comments