Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions packages/cli/src/command-helpers/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ const TEST_SOURCIFY_CONTRACT_INFO = {
startBlock: 10_736_242,
},
},
'mainnet-non-verified': {
'0x4a183b7ED67B9E14b3f45Abfb2Cf44ed22c29E54': {
name: null,
startBlock: null,
},
},
optimism: {
'0xc35DADB65012eC5796536bD9864eD8773aBc74C4': {
name: 'BentoBoxV1',
Expand All @@ -111,6 +117,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
45 changes: 20 additions & 25 deletions packages/cli/src/command-helpers/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,37 +163,32 @@ 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 json:
| {
status: string;
files: { name: string; path: string; content: string }[];
}
| { error: string } = await (
await fetch(url).catch(error => {
throw new Error(`Sourcify API is unreachable: ${error}`);
})
).json();
const url = `https://sourcify.dev/server/v2/contract/${chainId}/${address}?fields=abi,compilation,deployment`;
const resp = await fetch(url).catch(error => {
throw new Error(`Sourcify API is unreachable: ${error}`);
});
if (resp.status === 404) throw new Error(`Sourcify API says contract is not verified`);
if (!resp.ok) throw new Error(`Sourcify API returned status ${resp.status}`);
const json: {
abi: any[];
compilation: { name: string };
deployment: { blockNumber: string };
} = await resp.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');

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
4 changes: 2 additions & 2 deletions packages/cli/tests/cli/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe(
'Init',
{
sequential: true,
timeout: 100_000,
timeout: 500_000,
},
() => {
const baseDir = path.join(__dirname, 'init');
Expand All @@ -31,7 +31,7 @@ describe(
path.join('init', 'ethereum', 'from-example'),
{
exitCode: 0,
timeout: 100_000,
timeout: 200_000,
cwd: ethereumBaseDir,
deleteDir: true,
},
Expand Down