Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding AnyABI loader #156

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 66 additions & 5 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import * as errors from "./errors.js";
export type ContractResult = {
abi: any[];
name: string | null;
evmVersion: string;
compilerVersion: string;
runs: number;

ok: boolean; // False if no result is found
evmVersion?: string;
compilerVersion?: string;
runs?: number;


/**
* getSources returns the imports -> source code mapping for the contract, if available.
Expand Down Expand Up @@ -425,7 +425,6 @@ export interface SourcifyContractMetadata {
version: number;
}


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

Expand Down Expand Up @@ -602,6 +601,68 @@ export type BlockscoutContractResult = {
has_methods_write_proxy?: boolean;
};


function isAnyABINotFound(error: any): boolean {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting to think we need an ABILoader.isNotFound(error:any): boolean that we can inherit/override, but maybe premature?

return (
error.message === "Failed to fetch" ||
error.message === "ABI not found" ||
error.status === 404
);
}

// https://anyabi.xyz/
export class AnyABILoader implements ABILoader {
readonly name = "AnyABILoader";

chainId?: number;

constructor(config?: { chainId?: number }) {
this.chainId = config?.chainId ?? 1;
}

async #fetchAnyABI(address: string): Promise<ContractResult> {
const url = "https://anyabi.xyz/api/get-abi/" + this.chainId + "/" + address;
try {
const r = await fetchJSON(url);
const { abi, name }: { abi: any[]; name: string } = r;

return {
abi: abi,
name: name,
ok: true,
loader: this,
loaderResult: r,
};
} catch (err: any) {
if (!isAnyABINotFound(err)) return emptyContractResult;
throw new AnyABILoaderError("AnyABILoader load contract error: " + err.message, {
context: { url },
cause: err,
});
}
}

async getContract(address: string): Promise<ContractResult> {
{
const r = await this.#fetchAnyABI(address);
if (r.ok) return r;
}

return emptyContractResult;
}

async loadABI(address: string): Promise<any[]> {
{
const r = await this.#fetchAnyABI(address);
if (r.ok) return r.abi;
}

return [];
}
}

export class AnyABILoaderError extends errors.LoaderError { };

export interface SignatureLookup {
loadFunctions(selector: string): Promise<string[]>;
loadEvents(hash: string): Promise<string[]>;
Expand Down
Loading