Skip to content

Commit

Permalink
Merge pull request #128 from yohamta/125-contract-result-types
Browse files Browse the repository at this point in the history
loaders: Enhance `ContractResult.loaderResult` type
  • Loading branch information
shazow authored Sep 25, 2024
2 parents eb34b74 + 478d838 commit 1055953
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type ContractResult = {
*
* @experimental
*/
loaderResult?: any;
loaderResult?: EtherscanContractResult | SourcifyContractMetadata | any;
}

/**
Expand Down Expand Up @@ -199,7 +199,7 @@ export class EtherscanABILoader implements ABILoader {
throw new Error(r.result); // This gets wrapped below
}

const result = r.result[0];
const result = r.result[0] as EtherscanContractResult;
return {
abi: JSON.parse(result.ABI),
name: result.ContractName,
Expand Down Expand Up @@ -255,6 +255,23 @@ export class EtherscanABILoader implements ABILoader {

export class EtherscanABILoaderError extends errors.LoaderError { };

/// Etherscan Contract Source API response
export type EtherscanContractResult = {
SourceCode: string;
ABI: string;
ContractName: string;
CompilerVersion: string;
OptimizationUsed: number;
Runs: number;
ConstructorArguments: string;
EVMVersion: string;
Library: string;
LicenseType: string;
Proxy: "1" | "0";
Implementation: string;
SwarmSource: string;
}


function isSourcifyNotFound(error: any): boolean {
return (
Expand Down Expand Up @@ -288,7 +305,7 @@ export class SourcifyABILoader implements ABILoader {
if (metadata === undefined) throw new SourcifyABILoaderError("metadata.json not found");

// Note: Sometimes metadata.json contains sources, but not always. So we can't rely on just the metadata.json
const m = JSON.parse(metadata.content);
const m = JSON.parse(metadata.content) as SourcifyContractMetadata;

// Sourcify includes a title from the Natspec comments
let name = m.output.devdoc?.title;
Expand Down Expand Up @@ -380,6 +397,28 @@ export class SourcifyABILoader implements ABILoader {

export class SourcifyABILoaderError extends errors.LoaderError { };

/// Sourcify Contract metadata from API response
export interface SourcifyContractMetadata {
compiler: {
version: string;
};
language: string;
output: {
abi: any[];
devdoc?: any;
userdoc?: any;
};
settings: {
compilationTarget: Record<string, string>;
evmVersion: string;
libraries: Record<string, string>;
metadata: Record<string, string>;
optimizer: any;
remappings: string[];
};
sources: Record<string, any>;
version: number;
}

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

0 comments on commit 1055953

Please sign in to comment.