Skip to content
This repository was archived by the owner on Jul 20, 2023. It is now read-only.

Commit 2c23066

Browse files
committed
feat(fetch): added contract name to output
BREAKING CHANGE: Output interface of getAbiAt was udpated from the ABI itself (string) to an object containing the name of the contract and its ABI
1 parent 136f914 commit 2c23066

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "etherscan-abi",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "⏬🚀 Fetch the most up-to-date ABI of a verified Smart Contract from Etherscan in seconds!",
55
"main": "./lib/index.js",
66
"bin": "./lib/cli.js",

src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ const fetchAbis = () => {
7777
return Promise.all(
7878
options.contract.map(async (address) => {
7979
try {
80-
const abi = await fetchAbiAt(address, options);
80+
const { name, abi } = await fetchAbiAt(address, options);
8181

82-
const targetPath = path.join(options.target, `${address}.json`);
82+
const targetPath = path.join(options.target, `${name}.json`);
8383
fs.writeFileSync(targetPath, JSON.stringify(abi, null, 2));
8484

8585
console.log(

src/fetch.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,27 @@ import {
66
EIP1967ImplementationNotFound,
77
} from "@openzeppelin/upgrades-core";
88

9-
export interface EtherscanAbiResponse {
9+
interface EtherscanResponse {
1010
status: string;
1111
message: string;
12-
result: string;
12+
}
13+
14+
export interface EtherscanSourceCodeResponse extends EtherscanResponse {
15+
result: {
16+
SourceCode: string;
17+
ABI: string;
18+
ContractName: string;
19+
CompilerVersion: string;
20+
OptimizationUsed: string;
21+
Runs: string;
22+
ConstructorArguments: string;
23+
EVMVersion: string;
24+
Library: string;
25+
LicenseType: string;
26+
Proxy: string;
27+
Implementation: string;
28+
SwarmSource: string;
29+
}[];
1330
}
1431

1532
export const fetchAbiAt = async (
@@ -34,13 +51,20 @@ export const fetchAbiAt = async (
3451
}
3552
}
3653

37-
const res = await axios.get<EtherscanAbiResponse>(
38-
new ethers.providers.EtherscanProvider(network, apiKey).getUrl("contract", {
39-
action: "getabi",
54+
const etherscan = new ethers.providers.EtherscanProvider(network, apiKey);
55+
const res = await axios.get<EtherscanSourceCodeResponse>(
56+
etherscan.getUrl("contract", {
57+
action: "getsourcecode",
4058
address,
4159
})
4260
);
43-
if (res.data.status !== "1") throw new Error(res.data.result);
4461

45-
return JSON.parse(res.data.result);
62+
try {
63+
return {
64+
name: res.data.result[0].ContractName,
65+
abi: JSON.parse(res.data.result[0].ABI),
66+
};
67+
} catch {
68+
throw new Error(res.data.result[0].ABI);
69+
}
4670
};

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { fetchAbiAt } from "./fetch";
2-
export type { EtherscanAbiResponse } from "./fetch";
2+
export type { EtherscanSourceCodeResponse } from "./fetch";

0 commit comments

Comments
 (0)