Skip to content

Commit 477ee26

Browse files
committed
Use Sourcify for Contract name and remove replaced Etherscan methods
1 parent ada165c commit 477ee26

File tree

2 files changed

+12
-40
lines changed

2 files changed

+12
-40
lines changed

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

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,15 @@ export const loadAbiFromSourcify = async (
1717
`Failed to fetch ABI from Sourcify`,
1818
`Warnings while fetching ABI from Sourcify`,
1919
async () => {
20-
const chainId = await getSourcifyChainId(network);
21-
const result = await fetch(
22-
`https://repo.sourcify.dev/contracts/full_match/${chainId}/${address}/metadata.json`,
23-
);
24-
const json = await result.json();
20+
const json = await fetchMetadataFromSourcify(network, address);
2521

26-
if (result.ok) {
22+
if (json) {
2723
return new ABICtor('Contract', undefined, immutable.fromJS(json.output.abi));
2824
}
2925
throw new Error('ABI not found, try loading it from a local file');
3026
},
3127
);
3228

33-
export const loadAbiFromEtherscan = async (
34-
ABICtor: typeof ABI,
35-
network: string,
36-
address: string,
37-
): Promise<ABI> =>
38-
await withSpinner(
39-
`Fetching ABI from Etherscan`,
40-
`Failed to fetch ABI from Etherscan`,
41-
`Warnings while fetching ABI from Etherscan`,
42-
async () => {
43-
const scanApiUrl = getEtherscanLikeAPIUrl(network);
44-
const result = await fetch(`${scanApiUrl}?module=contract&action=getabi&address=${address}`);
45-
const json = await result.json();
46-
47-
// Etherscan returns a JSON object that has a `status`, a `message` and
48-
// a `result` field. The `status` is '0' in case of errors and '1' in
49-
// case of success
50-
if (json.status === '1') {
51-
return new ABICtor('Contract', undefined, immutable.fromJS(JSON.parse(json.result)));
52-
}
53-
throw new Error('ABI not found, try loading it from a local file');
54-
},
55-
);
56-
5729
export const loadStartBlockForContract = async (
5830
network: string,
5931
address: string,
@@ -74,7 +46,7 @@ export const loadContractNameForAddress = async (
7446
await withSpinner(
7547
`Fetching Contract Name`,
7648
`Failed to fetch Contract Name`,
77-
`Warnings while fetching contract name from Etherscan`,
49+
`Warnings while fetching contract name from Sourcify`,
7850
async () => {
7951
return getContractNameForAddress(network, address);
8052
},
@@ -147,28 +119,28 @@ export const fetchTransactionByHashFromRPC = async (
147119
}
148120
};
149121

150-
export const fetchSourceCodeFromEtherscan = async (
122+
export const fetchMetadataFromSourcify = async (
151123
network: string,
152124
address: string,
153125
): Promise<any> => {
154-
const scanApiUrl = getEtherscanLikeAPIUrl(network);
126+
const chainId = await getSourcifyChainId(network);
155127
const result = await fetch(
156-
`${scanApiUrl}?module=contract&action=getsourcecode&address=${address}`,
128+
`https://repo.sourcify.dev/contracts/full_match/${chainId}/${address}/metadata.json`,
157129
);
158130
const json = await result.json();
159-
if (json.status === '1') {
131+
if (result.ok) {
160132
return json;
161133
}
162-
throw new Error('Failed to fetch contract source code');
134+
throw new Error('Failed to fetch metadata for address');
163135
};
164136

165137
export const getContractNameForAddress = async (
166138
network: string,
167139
address: string,
168140
): Promise<string> => {
169141
try {
170-
const contractSourceCode = await fetchSourceCodeFromEtherscan(network, address);
171-
const contractName = contractSourceCode.result[0].ContractName;
142+
const json = await fetchMetadataFromSourcify(network, address);
143+
const contractName = Object.values(json.settings.compilationTarget)[0] as string;
172144
logger('Successfully getContractNameForAddress. contractName: %s', contractName);
173145
return contractName;
174146
} catch (error) {

packages/cli/src/commands/add.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Args, Command, Flags } from '@oclif/core';
44
import { CLIError } from '@oclif/core/lib/errors';
55
import {
66
loadAbiFromBlockScout,
7-
loadAbiFromEtherscan,
7+
loadAbiFromSourcify,
88
loadContractNameForAddress,
99
loadStartBlockForContract,
1010
} from '../command-helpers/abi';
@@ -96,7 +96,7 @@ export default class AddCommand extends Command {
9696
} else if (network === 'poa-core') {
9797
ethabi = await loadAbiFromBlockScout(EthereumABI, network, address);
9898
} else {
99-
ethabi = await loadAbiFromEtherscan(EthereumABI, network, address);
99+
ethabi = await loadAbiFromSourcify(EthereumABI, network, address);
100100
}
101101

102102
try {

0 commit comments

Comments
 (0)