Skip to content

Commit

Permalink
Use Sourcify v2 endpoint for contract lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
0237h committed Feb 7, 2025
1 parent 197c4d3 commit 9f0bdd9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-owls-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': patch
---

Use Sourcify v2 endpoint for contract lookups
7 changes: 7 additions & 0 deletions packages/cli/src/command-helpers/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ const TEST_SOURCIFY_CONTRACT_INFO = {
startBlock: null,
},
},
// Invalid address (missing 0x)
matic: {
'0000000000000000000000000000000000000000': {
name: null,
startBlock: null,
},
}
};

// Retry helper with configurable number of retries
Expand Down
32 changes: 15 additions & 17 deletions packages/cli/src/command-helpers/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,37 +163,35 @@ export class ContractService {
if (!network.caip2Id.startsWith('eip155'))
throw new Error(`Invalid chainId, Sourcify API only supports EVM chains`);

if (!address.startsWith('0x') || address.length != 42)
throw new Error(`Invalid address, must start with 0x prefix and be 20 bytes long`);

const chainId = network.caip2Id.split(':')[1];
const url = `https://sourcify.dev/server/files/any/${chainId}/${address}`;
const url = `https://sourcify.dev/server/v2/contract/${chainId}/${address}?fields=abi,compilation,deployment`;
const json:
| {
status: string;
files: { name: string; path: string; content: string }[];
abi: any[];
compilation: { name: string };
deployment: { blockNumber: string };
}
| { error: string } = await (
| { customCode: string; message: string; errorId: string; } = await (
await fetch(url).catch(error => {
throw new Error(`Sourcify API is unreachable: ${error}`);
})
).json();

if (json) {
if ('error' in json) throw new Error(`Sourcify API error: ${json.error}`);

let metadata: any = json.files.find(e => e.name === 'metadata.json')?.content;
if (!metadata) throw new Error('Contract is missing metadata');
if ('errorId' in json) throw new Error(`Sourcify API error: [${json.customCode}] ${json.message}`);

const tx_hash = json.files.find(e => e.name === 'creator-tx-hash.txt')?.content;
if (!tx_hash) throw new Error('Contract is missing tx creation hash');
const abi = json.abi;
const contractName = json.compilation.name;
const blockNumber = json.deployment.blockNumber;

const tx = await this.fetchTransactionByHash(networkId, tx_hash);
if (!tx?.blockNumber)
throw new Error(`Can't fetch blockNumber from tx: ${JSON.stringify(tx)}`);
if (!abi || !contractName || !blockNumber) throw new Error('Contract is missing metadata');

metadata = JSON.parse(metadata);
const contractName = Object.values(metadata.settings.compilationTarget)[0] as string;
return {
abi: new ABICtor(contractName, undefined, immutable.fromJS(metadata.output.abi)) as ABI,
startBlock: Number(tx.blockNumber).toString(),
abi: new ABICtor(contractName, undefined, immutable.fromJS(abi)) as ABI,
startBlock: Number(blockNumber).toString(),
name: contractName,
};
}
Expand Down

0 comments on commit 9f0bdd9

Please sign in to comment.