@@ -163,37 +163,35 @@ export class ContractService {
163163      if  ( ! network . caip2Id . startsWith ( 'eip155' ) ) 
164164        throw  new  Error ( `Invalid chainId, Sourcify API only supports EVM chains` ) ; 
165165
166+       if  ( ! address . startsWith ( '0x' )  ||  address . length  !=  42 ) 
167+         throw  new  Error ( `Invalid address, must start with 0x prefix and be 20 bytes long` ) ; 
168+ 
166169      const  chainId  =  network . caip2Id . split ( ':' ) [ 1 ] ; 
167-       const  url  =  `https://sourcify.dev/server/files/any /${ chainId } ${ address }  ; 
170+       const  url  =  `https://sourcify.dev/server/v2/contract /${ chainId } ${ address } ?fields=abi,compilation,deployment ` ; 
168171      const  json :
169172        |  { 
170-             status : string ; 
171-             files : {  name : string ;  path : string ;  content : string  } [ ] ; 
173+             abi : any [ ] ; 
174+             compilation : {  name : string  } ; 
175+             deployment : {  blockNumber : string  } ; 
172176          } 
173-         |  {  error : string  }  =  await  ( 
177+         |  {  customCode : string ;   message :  string ;   errorId :  string ;  }  =  await  ( 
174178        await  fetch ( url ) . catch ( error  =>  { 
175179          throw  new  Error ( `Sourcify API is unreachable: ${ error }  ) ; 
176180        } ) 
177181      ) . json ( ) ; 
178182
179183      if  ( json )  { 
180-         if  ( 'error'  in  json )  throw  new  Error ( `Sourcify API error: ${ json . error }  ) ; 
181- 
182-         let  metadata : any  =  json . files . find ( e  =>  e . name  ===  'metadata.json' ) ?. content ; 
183-         if  ( ! metadata )  throw  new  Error ( 'Contract is missing metadata' ) ; 
184+         if  ( 'errorId'  in  json )  throw  new  Error ( `Sourcify API error: [${ json . customCode } ${ json . message }  ) ; 
184185
185-         const  tx_hash  =  json . files . find ( e  =>  e . name  ===  'creator-tx-hash.txt' ) ?. content ; 
186-         if  ( ! tx_hash )  throw  new  Error ( 'Contract is missing tx creation hash' ) ; 
186+         const  abi  =  json . abi ; 
187+         const  contractName  =  json . compilation . name ; 
188+         const  blockNumber  =  json . deployment . blockNumber ; 
187189
188-         const  tx  =  await  this . fetchTransactionByHash ( networkId ,  tx_hash ) ; 
189-         if  ( ! tx ?. blockNumber ) 
190-           throw  new  Error ( `Can't fetch blockNumber from tx: ${ JSON . stringify ( tx ) }  ) ; 
190+         if  ( ! abi  ||  ! contractName  ||  ! blockNumber )  throw  new  Error ( 'Contract is missing metadata' ) ; 
191191
192-         metadata  =  JSON . parse ( metadata ) ; 
193-         const  contractName  =  Object . values ( metadata . settings . compilationTarget ) [ 0 ]  as  string ; 
194192        return  { 
195-           abi : new  ABICtor ( contractName ,  undefined ,  immutable . fromJS ( metadata . output . abi ) )  as  ABI , 
196-           startBlock : Number ( tx . blockNumber ) . toString ( ) , 
193+           abi : new  ABICtor ( contractName ,  undefined ,  immutable . fromJS ( abi ) )  as  ABI , 
194+           startBlock : Number ( blockNumber ) . toString ( ) , 
197195          name : contractName , 
198196        } ; 
199197      } 
0 commit comments