Skip to content

Commit 23d6a9c

Browse files
committed
Replace Etherscan ABI lookups with Sourcify
Etherscan requires an API key for ABI lookup and other operations. Sourcify (https://sourcify.dev) is an open-source decentralized alternative.
1 parent 946f9f0 commit 23d6a9c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ export const loadAbiFromSourcify = async (
1111
ABICtor: typeof ABI,
1212
network: string,
1313
address: string,
14+
): Promise<ABI> =>
15+
await withSpinner(
16+
`Fetching ABI from Sourcify`,
17+
`Failed to fetch ABI from Sourcify`,
18+
`Warnings while fetching ABI from Sourcify`,
19+
async () => {
20+
const chainId = await getSourcifyChainId(network);
21+
const result = await fetch(`https://repo.sourcify.dev/contracts/full_match/${chainId}/${address}/metadata.json`);
22+
const json = await result.json();
23+
24+
// Etherscan returns a JSON object that has a `status`, a `message` and
25+
// a `result` field. The `status` is '0' in case of errors and '1' in
26+
// case of success
27+
if (result.ok) {
28+
return new ABICtor('Contract', undefined, immutable.fromJS(json.output.abi));
29+
}
30+
throw new Error('ABI not found, try loading it from a local file');
31+
},
32+
);
33+
34+
export const loadAbiFromEtherscan = async (
35+
ABICtor: typeof ABI,
36+
network: string,
37+
address: string,
1438
): Promise<ABI> =>
1539
await withSpinner(
1640
`Fetching ABI from Sourcify`,
@@ -202,8 +226,10 @@ const getSourcifyChainId = async (network: string) => {
202226
// Can fail if network name doesn't follow https://chainlist.org name convention
203227
const match = json.find((e: any) => e.name.toLowerCase().includes(network.replace('-', ' ')));
204228

205-
if (match) return match.chainId;
206-
throw new Error(`Could not find chain id for "${network}"`);
229+
if (match)
230+
return match.chainId;
231+
else
232+
throw new Error(`Could not find chain id for "${network}"`);
207233
};
208234

209235
const getEtherscanLikeAPIUrl = (network: string) => {

packages/cli/src/commands/init.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,9 @@ async function processInitForm(
707707

708708
// Try loading the ABI from Sourcify, if none was provided
709709
if (protocolInstance.hasABIs() && !initAbi) {
710-
abiFromSourcify = await retryWithPrompt(() => loadAbiFromSourcify(ABI, network, value));
710+
abiFromSourcify = await retryWithPrompt(() =>
711+
loadAbiFromSourcify(ABI, network, value),
712+
);
711713
}
712714
// If startBlock is not set, try to load it.
713715
if (!initStartBlock) {

0 commit comments

Comments
 (0)