v5 deploy sdk changes (#4497)

This commit is contained in:
jnsdls
2024-09-10 00:33:26 +00:00
parent bb9ce26d6b
commit 8119db08b3
10 changed files with 87 additions and 70 deletions
@@ -3,7 +3,7 @@ import type { Abi } from "abitype";
/**
* @contract
*/
export type PublishedMetadata = {
export type CompilerMetadata = {
name: string;
abi: Abi;
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix later by updating this type to match the specs here: https://docs.soliditylang.org/en/latest/metadata.html
@@ -27,7 +27,7 @@ export type PublishedMetadata = {
* @internal
*/
// biome-ignore lint/suspicious/noExplicitAny: TODO: fix later
export function formatCompilerMetadata(metadata: any): PublishedMetadata {
export function formatCompilerMetadata(metadata: any): CompilerMetadata {
const compilationTarget = metadata.settings.compilationTarget;
const targets = Object.keys(compilationTarget);
const name = compilationTarget[targets[0] as keyof typeof compilationTarget];
@@ -15,7 +15,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("fetchPublishedContract", () => {
client: TEST_CLIENT,
});
expect(result.extendedMetadata?.version).toEqual("1.0.2");
expect(result?.version).toEqual("1.0.2");
});
it("fetches a specific version when specified", async () => {
@@ -26,7 +26,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("fetchPublishedContract", () => {
version: "1.0.1",
});
expect(result.extendedMetadata?.version).toEqual("1.0.1");
expect(result?.version).toEqual("1.0.1");
});
it("throws an error when an invalid version is specified", async () => {
@@ -46,7 +46,7 @@ export async function getOrDeployInfraForPublishedContract(
client,
account,
});
const { compilerMetadata } = await fetchPublishedContractMetadata({
const compilerMetadata = await fetchPublishedContractMetadata({
client,
contractId: `${contractId}_ZkSync`, // different contract id for zkSync
publisher,
@@ -1,10 +1,10 @@
import type { ThirdwebClient } from "../../client/client.js";
import { download } from "../../storage/download.js";
import type { PublishedMetadata } from "../actions/compiler-metadata.js";
import type { CompilerMetadata } from "../actions/compiler-metadata.js";
type FetchSourceFilesFromMetadataOptions = {
client: ThirdwebClient;
publishedMetadata: PublishedMetadata;
publishedMetadata: CompilerMetadata;
};
type ContractSource = {
@@ -28,6 +28,12 @@ export {
fetchDeployBytecodeFromPublishedContractMetadata,
} from "../contract/deployment/publisher.js";
// contract metadata
export {
fetchDeployMetadata,
type FetchDeployMetadataResult,
} from "../utils/any-evm/deploy-metadata.js";
// deployment - TODO: these end up looking more like extensions -> should they be?
export {
prepareDirectDeployTransaction,
+2
View File
@@ -22,6 +22,8 @@ export {
export {
deployPublishedContract,
type DeployPublishedContractOptions,
deployContractfromDeployMetadata,
type DeployContractfromDeployMetadataOptions,
} from "../extensions/prebuilts/deploy-published.js";
export { prepareDirectDeployTransaction } from "../contract/deployment/deploy-with-abi.js";
@@ -9,6 +9,7 @@ import { simulateTransaction } from "../../transaction/actions/simulate.js";
import { prepareContractCall } from "../../transaction/prepare-contract-call.js";
import { resolveMethod } from "../../transaction/resolve-method.js";
import type { CompilerMetadata } from "../../utils/any-evm/deploy-metadata.js";
import type { FetchDeployMetadataResult } from "../../utils/any-evm/deploy-metadata.js";
import { isZkSyncChain } from "../../utils/any-evm/zksync/isZkSyncChain.js";
import type { Account } from "../../wallets/interfaces/wallet.js";
@@ -58,21 +59,56 @@ export async function deployPublishedContract(
version,
implementationConstructorParams,
} = options;
const { compilerMetadata, extendedMetadata } =
await fetchPublishedContractMetadata({
client,
contractId: isZkSyncChain(chain) ? `${contractId}_ZkSync` : contractId,
publisher,
version,
});
const deployMetadata = await fetchPublishedContractMetadata({
client,
contractId: isZkSyncChain(chain) ? `${contractId}_ZkSync` : contractId,
publisher,
version,
});
switch (extendedMetadata?.deployType) {
return deployContractfromDeployMetadata({
account,
chain,
deployMetadata,
client,
contractParams,
implementationConstructorParams,
});
}
/**
* @internal
*/
export type DeployContractfromDeployMetadataOptions = {
client: ThirdwebClient;
chain: Chain;
account: Account;
deployMetadata: FetchDeployMetadataResult;
contractParams: unknown[];
implementationConstructorParams?: unknown[];
};
/**
* @internal
*/
export async function deployContractfromDeployMetadata(
options: DeployContractfromDeployMetadataOptions,
) {
const {
client,
account,
chain,
contractParams,
deployMetadata,
implementationConstructorParams,
} = options;
switch (deployMetadata?.deployType) {
case "standard": {
return directDeploy({
account,
client,
chain,
compilerMetadata,
compilerMetadata: deployMetadata,
contractParams,
});
}
@@ -89,19 +125,21 @@ export async function deployPublishedContract(
chain,
client,
account,
contractId,
contractId: deployMetadata.name,
constructorParams: implementationConstructorParams || [],
publisher,
publisher: deployMetadata.publisher,
});
const initializeFunction = compilerMetadata.abi.find(
const initializeFunction = deployMetadata.abi.find(
(i) =>
i.type === "function" &&
i.name ===
(extendedMetadata.factoryDeploymentData
(deployMetadata.factoryDeploymentData
?.implementationInitializerFunction || "initialize"),
) as AbiFunction;
if (!initializeFunction) {
throw new Error(`Could not find initialize function for ${contractId}`);
throw new Error(
`Could not find initialize function for ${deployMetadata.name}`,
);
}
const initializeTransaction = prepareContractCall({
contract: getContract({
@@ -109,7 +147,10 @@ export async function deployPublishedContract(
chain,
address: implementationContract.address,
}),
method: initializeFunction,
method: resolveMethod(
deployMetadata.factoryDeploymentData
?.implementationInitializerFunction || "initialize",
),
params: contractParams,
});
@@ -122,14 +163,14 @@ export async function deployPublishedContract(
});
}
case "customFactory": {
if (!extendedMetadata?.factoryDeploymentData?.customFactoryInput) {
if (!deployMetadata?.factoryDeploymentData?.customFactoryInput) {
throw new Error("No custom factory info found");
}
const factoryAddress =
extendedMetadata?.factoryDeploymentData?.customFactoryInput
deployMetadata?.factoryDeploymentData?.customFactoryInput
?.customFactoryAddresses?.[chain.id];
const factoryFunction =
extendedMetadata.factoryDeploymentData?.customFactoryInput
deployMetadata.factoryDeploymentData?.customFactoryInput
?.factoryFunction;
if (!factoryAddress || !factoryFunction) {
throw new Error(`No factory address found on chain ${chain.id}`);
@@ -161,15 +202,13 @@ export async function deployPublishedContract(
account,
client,
chain,
compilerMetadata,
compilerMetadata: deployMetadata,
contractParams,
});
}
default:
// If a deployType was specified but we don't support it, throw an error
throw new Error(
`Unsupported deploy type: ${extendedMetadata?.deployType}`,
);
throw new Error(`Unsupported deploy type: ${deployMetadata?.deployType}`);
}
}
@@ -20,10 +20,6 @@ import type { FetchDeployMetadataResult } from "./deploy-metadata.js";
* ```ts
* import { computePublishedContractAddress } from "thirdweb/deploys";
*
* const contractMetadata = await fetchPublishedContractMetadata({
* client,
* chain,
* });
* const address = await computePublishedContractAddress({
* client,
* chain,
@@ -46,14 +46,13 @@ export async function computeDeploymentInfoFromMetadata(args: {
salt?: string;
}) {
const { client, chain, contractMetadata, constructorParams, salt } = args;
const { compilerMetadata } = contractMetadata;
const create2FactoryAddress = await computeCreate2FactoryAddress({
client,
chain,
});
const bytecode = compilerMetadata.bytecode;
const bytecode = contractMetadata.bytecode;
const constructorAbi =
(compilerMetadata.abi.find(
(contractMetadata.abi.find(
(abi) => abi.type === "constructor",
) as AbiConstructor) || [];
const encodedArgs = encodeAbiParameters(
@@ -10,10 +10,8 @@ export type FetchDeployMetadataOptions = {
client: ThirdwebClient;
};
export type FetchDeployMetadataResult = {
compilerMetadata: CompilerMetadata;
extendedMetadata: ExtendedMetadata | undefined;
};
export type FetchDeployMetadataResult = Partial<ExtendedMetadata> &
CompilerMetadata;
/**
* Fetches the deployment metadata.
@@ -24,31 +22,6 @@ export type FetchDeployMetadataResult = {
export async function fetchDeployMetadata(
options: FetchDeployMetadataOptions,
): Promise<FetchDeployMetadataResult> {
const [compilerMetadata, extendedMetadata] = await Promise.all([
fetchCompilerMetadata(options),
fetchExtendedMetadata(options).catch(() => undefined),
]);
return { compilerMetadata, extendedMetadata };
}
// helpers
/**
* Fetches the published metadata.
* @param options - The options for fetching the published metadata.
* @internal
*/
async function fetchExtendedMetadata(
options: FetchDeployMetadataOptions,
): Promise<ExtendedMetadata> {
return download({
uri: options.uri,
client: options.client,
}).then((r) => r.json());
}
async function fetchCompilerMetadata(
options: FetchDeployMetadataOptions,
): Promise<CompilerMetadata> {
const rawMeta: RawCompilerMetadata = await download({
uri: options.uri,
client: options.client,
@@ -144,11 +117,13 @@ export type ExtendedMetadata = {
publisherAddress: string;
}[]
| undefined;
defaultModules?: {
moduleName: string;
moduleVersion: string;
publisherAddress: string;
}[];
defaultModules?:
| Array<{
moduleName: string;
moduleVersion: string;
publisherAddress: string;
}>
| undefined;
publisher?: string | undefined;
audit?: string | undefined;
logo?: string | undefined;