Skip to content

Commit

Permalink
Adding EtherscanV2
Browse files Browse the repository at this point in the history
  • Loading branch information
k-thornton committed Nov 22, 2024
1 parent 1a8c899 commit 3d030e4
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,14 @@ export class MultiABILoaderError extends errors.LoaderError { };


export class EtherscanABILoader implements ABILoader {
readonly name = "EtherscanABILoader";

apiKey?: string;
readonly name: string = "EtherscanABILoader";
baseURL: string;

constructor(config?: { apiKey?: string, baseURL?: string }) {
if (config === undefined) config = {};
this.apiKey = config.apiKey;
this.baseURL = config.baseURL || "https://api.etherscan.io/api";
const url = new URL(config.baseURL || "https://api.etherscan.io/api");
config.apiKey && url.searchParams.set("apikey", config.apiKey);
this.baseURL = url.toString();
}


Expand All @@ -189,11 +188,18 @@ export class EtherscanABILoader implements ABILoader {
}

async getContract(address: string): Promise<ContractResult> {
let url = this.baseURL + '?module=contract&action=getsourcecode&address=' + address;
if (this.apiKey) url += "&apikey=" + this.apiKey;
const url = new URL(this.baseURL)
const params = {
module: "contract",
action: "getsourcecode",
address: address,
};

// Using .set() to overwrite any default values that may be present in baseURL
Object.entries(params).forEach(([key, value]) => url.searchParams.set(key, value));

try {
const r = await fetchJSON(url);
const r = await fetchJSON(url.toString());
if (r.status === "0") {
if (r.result === "Contract source code not verified") return emptyContractResult;
throw new Error(r.result); // This gets wrapped below
Expand Down Expand Up @@ -236,11 +242,18 @@ export class EtherscanABILoader implements ABILoader {
}

async loadABI(address: string): Promise<any[]> {
let url = this.baseURL + '?module=contract&action=getabi&address=' + address;
if (this.apiKey) url += "&apikey=" + this.apiKey;
const url = new URL(this.baseURL)
const params = {
module: "contract",
action: "getabi",
address: address,
};

// Using .set() to overwrite any default values that may be present in baseURL
Object.entries(params).forEach(([key, value]) => url.searchParams.set(key, value));

try {
const r = await fetchJSON(url);
const r = await fetchJSON(url.toString());

if (r.status === "0") {
if (r.result === "Contract source code not verified") return [];
Expand Down Expand Up @@ -277,6 +290,14 @@ export type EtherscanContractResult = {
SwarmSource: string;
}

export class EtherscanV2ABILoader extends EtherscanABILoader {
readonly name: string = "EtherscanV2ABILoader";
constructor(config: { apiKey: string, chainId?: number }) {
// chainId is a required parameter in v2, as is an API key
super({ apiKey: config.apiKey, baseURL: `https://api.etherscan.io/v2/api?chainid=${config?.chainId ?? 1}` });
}
}


function isSourcifyNotFound(error: any): boolean {
return (
Expand Down

0 comments on commit 3d030e4

Please sign in to comment.