Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions etc/sdk.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,12 @@ export class FetchError extends Error {
// @internal (undocumented)
export function fetchPreDeployMetadata(publishMetadataUri: string, storage: IStorage): Promise<PreDeployMetadataFetched>;

// Warning: (ae-forgotten-export) The symbol "ContractSource" needs to be exported by the entry point index.d.ts
// Warning: (ae-internal-missing-underscore) The name "fetchSourceFilesFromMetadata" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal (undocumented)
export function fetchSourceFilesFromMetadata(publishedMetadata: PublishedMetadata, storage: IStorage): Promise<ContractSource[]>;

// Warning: (ae-internal-missing-underscore) The name "FileNameMissingError" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal (undocumented)
Expand Down
37 changes: 35 additions & 2 deletions src/common/feature-detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AbiFunction,
AbiSchema,
AbiTypeSchema,
ContractSource,
PreDeployMetadata,
PreDeployMetadataFetched,
PublishedMetadata,
Expand Down Expand Up @@ -262,14 +263,46 @@ async function fetchContractMetadata(
const metadata = await storage.get(compilerMetadataUri);
const abi = AbiSchema.parse(metadata.output.abi);
const compilationTarget = metadata.settings.compilationTarget;
const keys = Object.keys(compilationTarget);
const name = compilationTarget[keys[0]];
const targets = Object.keys(compilationTarget);
const name = compilationTarget[targets[0]];
return {
name,
abi,
metadata,
};
}

/**
* @internal
* @param publishedMetadata
* @param storage
*/
export async function fetchSourceFilesFromMetadata(
publishedMetadata: PublishedMetadata,
storage: IStorage,
): Promise<ContractSource[]> {
return await Promise.all(
Object.entries(publishedMetadata.metadata.sources).map(
async ([path, info]) => {
const urls = (info as any).urls as string[];
const ipfsLink = urls.find((url) => url.includes("ipfs"));
if (ipfsLink) {
const ipfsHash = ipfsLink.split("ipfs/")[1];
return {
filename: path,
source: await storage.getRaw(`ipfs://${ipfsHash}`),
};
} else {
return {
filename: path,
source: "Could not find source for this contract",
};
}
},
),
);
}

/**
* @internal
* @param publishMetadataUri
Expand Down
13 changes: 13 additions & 0 deletions src/core/classes/contract-publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import {
extractFunctions,
fetchContractMetadataFromAddress,
fetchPreDeployMetadata,
fetchSourceFilesFromMetadata,
} from "../../common/feature-detection";
import {
AbiFunction,
ContractParam,
ContractSource,
PublishedContract,
PublishedContractSchema,
} from "../../schema/contracts/custom";
Expand Down Expand Up @@ -99,6 +101,17 @@ export class ContractPublisher extends RPCConnectionHandler {
);
}

/**
* @internal
* @param address
*/
public async fetchContractSourcesFromAddress(
address: string,
): Promise<ContractSource[]> {
const metadata = await this.fetchContractMetadataFromAddress(address);
return await fetchSourceFilesFromMetadata(metadata, this.storage);
}

/**
* @interface
* @param publisherAddress
Expand Down
2 changes: 1 addition & 1 deletion src/core/classes/contract-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export class ContractWrapper<
fn
];
if (!func) {
throw new Error("invalid function");
throw new Error(`invalid function: "${fn.toString()}"`);
}
try {
return await func(...args, callOverrides);
Expand Down
5 changes: 5 additions & 0 deletions src/schema/contracts/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ export type AbiFunction = {
signature: string;
stateMutability: string;
};
export type ContractSource = {
filename: string;
source: string;
};
export type PublishedMetadata = {
name: string;
abi: z.infer<typeof AbiSchema>;
metadata: Record<string, any>;
};