Skip to content

Commit 9f0bdd9

Browse files
committed
Use Sourcify v2 endpoint for contract lookups
1 parent 197c4d3 commit 9f0bdd9

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

.changeset/giant-owls-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
Use Sourcify v2 endpoint for contract lookups

packages/cli/src/command-helpers/contracts.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ const TEST_SOURCIFY_CONTRACT_INFO = {
111111
startBlock: null,
112112
},
113113
},
114+
// Invalid address (missing 0x)
115+
matic: {
116+
'0000000000000000000000000000000000000000': {
117+
name: null,
118+
startBlock: null,
119+
},
120+
}
114121
};
115122

116123
// Retry helper with configurable number of retries

packages/cli/src/command-helpers/contracts.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)