@@ -163,37 +163,32 @@ export class ContractService {
163
163
if ( ! network . caip2Id . startsWith ( 'eip155' ) )
164
164
throw new Error ( `Invalid chainId, Sourcify API only supports EVM chains` ) ;
165
165
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
+
166
169
const chainId = network . caip2Id . split ( ':' ) [ 1 ] ;
167
- const url = `https://sourcify.dev/server/files/any /${ chainId } /${ address } ` ;
168
- const json :
169
- | {
170
- status : string ;
171
- files : { name : string ; path : string ; content : string } [ ] ;
172
- }
173
- | { error : string } = await (
174
- await fetch ( url ) . catch ( error => {
175
- throw new Error ( `Sourcify API is unreachable: ${ error } ` ) ;
176
- } )
177
- ) . json ( ) ;
170
+ const url = `https://sourcify.dev/server/v2/contract /${ chainId } /${ address } ?fields=abi,compilation,deployment ` ;
171
+ const resp = await fetch ( url ) . catch ( error => {
172
+ throw new Error ( `Sourcify API is unreachable: ${ error } ` ) ;
173
+ } ) ;
174
+ if ( resp . status === 404 ) throw new Error ( `Sourcify API says contract is not verified` ) ;
175
+ if ( ! resp . ok ) throw new Error ( `Sourcify API returned status ${ resp . status } ` ) ;
176
+ const json : {
177
+ abi : any [ ] ;
178
+ compilation : { name : string } ;
179
+ deployment : { blockNumber : string } ;
180
+ } = await resp . json ( ) ;
178
181
179
182
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
-
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' ) ;
183
+ const abi = json . abi ;
184
+ const contractName = json . compilation ?. name ;
185
+ const blockNumber = json . deployment ?. blockNumber ;
187
186
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 ) } ` ) ;
187
+ if ( ! abi || ! contractName || ! blockNumber ) throw new Error ( 'Contract is missing metadata' ) ;
191
188
192
- metadata = JSON . parse ( metadata ) ;
193
- const contractName = Object . values ( metadata . settings . compilationTarget ) [ 0 ] as string ;
194
189
return {
195
- abi : new ABICtor ( contractName , undefined , immutable . fromJS ( metadata . output . abi ) ) as ABI ,
196
- startBlock : Number ( tx . blockNumber ) . toString ( ) ,
190
+ abi : new ABICtor ( contractName , undefined , immutable . fromJS ( abi ) ) as ABI ,
191
+ startBlock : Number ( blockNumber ) . toString ( ) ,
197
192
name : contractName ,
198
193
} ;
199
194
}
0 commit comments