@@ -23,7 +23,11 @@ import EthereumABI from '../protocols/ethereum/abi.js';
2323import  Protocol ,  {  ProtocolName  }  from  '../protocols/index.js' ; 
2424import  {  abiEvents  }  from  '../scaffold/schema.js' ; 
2525import  Schema  from  '../schema.js' ; 
26- import  {  createIpfsClient ,  loadSubgraphSchemaFromIPFS  }  from  '../utils.js' ; 
26+ import  { 
27+   createIpfsClient , 
28+   loadSubgraphSchemaFromIPFS , 
29+   validateSubgraphNetworkMatch , 
30+ }  from  '../utils.js' ; 
2731import  {  validateContract  }  from  '../validation/index.js' ; 
2832import  AddCommand  from  './add.js' ; 
2933
@@ -515,7 +519,7 @@ async function processInitForm(
515519              value : 'contract' , 
516520            } , 
517521            {  message : 'Substreams' ,  name : 'substreams' ,  value : 'substreams'  } , 
518-             //  { message: 'Subgraph', name: 'subgraph', value: 'subgraph' },
522+             {  message : 'Subgraph' ,  name : 'subgraph' ,  value : 'subgraph'  } , 
519523          ] . filter ( ( {  name } )  =>  name ) , 
520524        } ) ; 
521525
@@ -596,9 +600,17 @@ async function processInitForm(
596600        isSubstreams  || 
597601        ( ! protocolInstance . hasContract ( )  &&  ! isComposedSubgraph ) , 
598602      initial : initContract , 
599-       validate : async  ( value : string )  =>  { 
603+       validate : async  ( value : string ) :  Promise < string   |   boolean >  =>  { 
600604        if  ( isComposedSubgraph )  { 
601-           return  value . startsWith ( 'Qm' )  ? true  : 'Subgraph deployment ID must start with Qm' ; 
605+           if  ( ! ipfsNode )  { 
606+             return  true ;  // Skip validation if no IPFS node is available 
607+           } 
608+           const  ipfs  =  createIpfsClient ( ipfsNode ) ; 
609+           const  {  valid,  error }  =  await  validateSubgraphNetworkMatch ( ipfs ,  value ,  network . id ) ; 
610+           if  ( ! valid )  { 
611+             return  error  ||  'Invalid subgraph network match' ; 
612+           } 
613+           return  true ; 
602614        } 
603615        if  ( initFromExample  !==  undefined  ||  ! protocolInstance . hasContract ( ) )  { 
604616          return  true ; 
@@ -731,7 +743,7 @@ async function processInitForm(
731743        isSubstreams  || 
732744        ! ! initAbiPath  || 
733745        isComposedSubgraph , 
734-       validate : async  ( value : string )  =>  { 
746+       validate : async  ( value : string ) :  Promise < string   |   boolean >  =>  { 
735747        if  ( 
736748          initFromExample  || 
737749          abiFromApi  || 
@@ -822,6 +834,22 @@ async function processInitForm(
822834
823835    await  promptManager . executeInteractive ( ) ; 
824836
837+     // If loading from IPFS, validate network matches 
838+     if  ( ipfsNode  &&  subgraphName . startsWith ( 'Qm' ) )  { 
839+       const  ipfs  =  createIpfsClient ( ipfsNode ) ; 
840+       try  { 
841+         const  {  valid,  error }  =  await  validateSubgraphNetworkMatch ( ipfs ,  subgraphName ,  network . id ) ; 
842+         if  ( ! valid )  { 
843+           throw  new  Error ( error  ||  'Invalid subgraph network match' ) ; 
844+         } 
845+       }  catch  ( e )  { 
846+         if  ( e  instanceof  Error )  { 
847+           print . error ( `Failed to validate subgraph network: ${ e . message }  ` ) ; 
848+         } 
849+         throw  e ; 
850+       } 
851+     } 
852+ 
825853    return  { 
826854      abi : ( abiFromApi  ||  abiFromFile ) ! , 
827855      protocolInstance, 
@@ -1188,8 +1216,9 @@ async function initSubgraphFromContract(
11881216  } 
11891217
11901218  if  ( 
1191-     ! protocolInstance . isComposedSubgraph ( )  && 
1219+     ! isComposedSubgraph  && 
11921220    protocolInstance . hasABIs ( )  && 
1221+     abi  &&  // Add check for abi existence 
11931222    ( abiEvents ( abi ) . size  ===  0  || 
11941223      // @ts -expect-error TODO: the abiEvents result is expected to be a List, how's it an array? 
11951224      abiEvents ( abi ) . length  ===  0 ) 
@@ -1204,6 +1233,12 @@ async function initSubgraphFromContract(
12041233    `Failed to create subgraph scaffold` , 
12051234    `Warnings while creating subgraph scaffold` , 
12061235    async  spinner  =>  { 
1236+       initDebugger ( 'Generating scaffold with ABI:' ,  abi ) ; 
1237+       initDebugger ( 'ABI data:' ,  abi ?. data ) ; 
1238+       if  ( abi )  { 
1239+         initDebugger ( 'ABI events:' ,  abiEvents ( abi ) ) ; 
1240+       } 
1241+ 
12071242      const  scaffold  =  await  generateScaffold ( 
12081243        { 
12091244          protocolInstance, 
0 commit comments