diff --git a/.changeset/puny-planes-study.md b/.changeset/puny-planes-study.md new file mode 100644 index 00000000000..7d1ddcf7766 --- /dev/null +++ b/.changeset/puny-planes-study.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Functions to manage extensions on a dynamic contract diff --git a/packages/thirdweb/scripts/generate/abis/dynamic-contracts/IExtensionManager.json b/packages/thirdweb/scripts/generate/abis/dynamic-contracts/IExtensionManager.json new file mode 100644 index 00000000000..8e6684149bc --- /dev/null +++ b/packages/thirdweb/scripts/generate/abis/dynamic-contracts/IExtensionManager.json @@ -0,0 +1,4 @@ +[ + "function addExtension(((string name, string metadataURI, address implementation) metadata, (bytes4 functionSelector, string functionSignature)[] functions) extension)", + "function removeExtension(string extensionName)" +] \ No newline at end of file diff --git a/packages/thirdweb/src/exports/extensions/dynamic-contracts.ts b/packages/thirdweb/src/exports/extensions/dynamic-contracts.ts new file mode 100644 index 00000000000..dadc5787150 --- /dev/null +++ b/packages/thirdweb/src/exports/extensions/dynamic-contracts.ts @@ -0,0 +1,11 @@ +/** + * Write + */ +export { + installPublishedExtension, + type InstallPublishedExtensionOptions, +} from "../../extensions/dynamic-contracts/write/installPublishedExtension.js"; +export { + uninstallExtension, + type UninstallExtensionOptions, +} from "../../extensions/dynamic-contracts/write/uninstallExtension.js"; diff --git a/packages/thirdweb/src/extensions/dynamic-contracts/__generated__/IExtensionManager/write/addExtension.ts b/packages/thirdweb/src/extensions/dynamic-contracts/__generated__/IExtensionManager/write/addExtension.ts new file mode 100644 index 00000000000..76c64fa7c17 --- /dev/null +++ b/packages/thirdweb/src/extensions/dynamic-contracts/__generated__/IExtensionManager/write/addExtension.ts @@ -0,0 +1,193 @@ +import type { AbiParameterToPrimitiveType } from "abitype"; +import type { + BaseTransactionOptions, + WithOverrides, +} from "../../../../../transaction/types.js"; +import { prepareContractCall } from "../../../../../transaction/prepare-contract-call.js"; +import { encodeAbiParameters } from "../../../../../utils/abi/encodeAbiParameters.js"; +import { once } from "../../../../../utils/promise/once.js"; +import { detectMethod } from "../../../../../utils/bytecode/detectExtension.js"; + +/** + * Represents the parameters for the "addExtension" function. + */ +export type AddExtensionParams = WithOverrides<{ + extension: AbiParameterToPrimitiveType<{ + type: "tuple"; + name: "extension"; + components: [ + { + type: "tuple"; + name: "metadata"; + components: [ + { type: "string"; name: "name" }, + { type: "string"; name: "metadataURI" }, + { type: "address"; name: "implementation" }, + ]; + }, + { + type: "tuple[]"; + name: "functions"; + components: [ + { type: "bytes4"; name: "functionSelector" }, + { type: "string"; name: "functionSignature" }, + ]; + }, + ]; + }>; +}>; + +export const FN_SELECTOR = "0xe05688fe" as const; +const FN_INPUTS = [ + { + type: "tuple", + name: "extension", + components: [ + { + type: "tuple", + name: "metadata", + components: [ + { + type: "string", + name: "name", + }, + { + type: "string", + name: "metadataURI", + }, + { + type: "address", + name: "implementation", + }, + ], + }, + { + type: "tuple[]", + name: "functions", + components: [ + { + type: "bytes4", + name: "functionSelector", + }, + { + type: "string", + name: "functionSignature", + }, + ], + }, + ], + }, +] as const; +const FN_OUTPUTS = [] as const; + +/** + * Checks if the `addExtension` method is supported by the given contract. + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. + * @returns A boolean indicating if the `addExtension` method is supported. + * @extension DYNAMIC-CONTRACTS + * @example + * ```ts + * import { isAddExtensionSupported } from "thirdweb/extensions/dynamic-contracts"; + * + * const supported = isAddExtensionSupported(["0x..."]); + * ``` + */ +export function isAddExtensionSupported(availableSelectors: string[]) { + return detectMethod({ + availableSelectors, + method: [FN_SELECTOR, FN_INPUTS, FN_OUTPUTS] as const, + }); +} + +/** + * Encodes the parameters for the "addExtension" function. + * @param options - The options for the addExtension function. + * @returns The encoded ABI parameters. + * @extension DYNAMIC-CONTRACTS + * @example + * ```ts + * import { encodeAddExtensionParams } from "thirdweb/extensions/dynamic-contracts"; + * const result = encodeAddExtensionParams({ + * extension: ..., + * }); + * ``` + */ +export function encodeAddExtensionParams(options: AddExtensionParams) { + return encodeAbiParameters(FN_INPUTS, [options.extension]); +} + +/** + * Encodes the "addExtension" function into a Hex string with its parameters. + * @param options - The options for the addExtension function. + * @returns The encoded hexadecimal string. + * @extension DYNAMIC-CONTRACTS + * @example + * ```ts + * import { encodeAddExtension } from "thirdweb/extensions/dynamic-contracts"; + * const result = encodeAddExtension({ + * extension: ..., + * }); + * ``` + */ +export function encodeAddExtension(options: AddExtensionParams) { + // we do a "manual" concat here to avoid the overhead of the "concatHex" function + // we can do this because we know the specific formats of the values + return (FN_SELECTOR + + encodeAddExtensionParams(options).slice( + 2, + )) as `${typeof FN_SELECTOR}${string}`; +} + +/** + * Prepares a transaction to call the "addExtension" function on the contract. + * @param options - The options for the "addExtension" function. + * @returns A prepared transaction object. + * @extension DYNAMIC-CONTRACTS + * @example + * ```ts + * import { sendTransaction } from "thirdweb"; + * import { addExtension } from "thirdweb/extensions/dynamic-contracts"; + * + * const transaction = addExtension({ + * contract, + * extension: ..., + * overrides: { + * ... + * } + * }); + * + * // Send the transaction + * await sendTransaction({ transaction, account }); + * ``` + */ +export function addExtension( + options: BaseTransactionOptions< + | AddExtensionParams + | { + asyncParams: () => Promise; + } + >, +) { + const asyncOptions = once(async () => { + return "asyncParams" in options ? await options.asyncParams() : options; + }); + + return prepareContractCall({ + contract: options.contract, + method: [FN_SELECTOR, FN_INPUTS, FN_OUTPUTS] as const, + params: async () => { + const resolvedOptions = await asyncOptions(); + return [resolvedOptions.extension] as const; + }, + value: async () => (await asyncOptions()).overrides?.value, + accessList: async () => (await asyncOptions()).overrides?.accessList, + gas: async () => (await asyncOptions()).overrides?.gas, + gasPrice: async () => (await asyncOptions()).overrides?.gasPrice, + maxFeePerGas: async () => (await asyncOptions()).overrides?.maxFeePerGas, + maxPriorityFeePerGas: async () => + (await asyncOptions()).overrides?.maxPriorityFeePerGas, + nonce: async () => (await asyncOptions()).overrides?.nonce, + extraGas: async () => (await asyncOptions()).overrides?.extraGas, + erc20Value: async () => (await asyncOptions()).overrides?.erc20Value, + }); +} diff --git a/packages/thirdweb/src/extensions/dynamic-contracts/__generated__/IExtensionManager/write/removeExtension.ts b/packages/thirdweb/src/extensions/dynamic-contracts/__generated__/IExtensionManager/write/removeExtension.ts new file mode 100644 index 00000000000..5a7ae6749c3 --- /dev/null +++ b/packages/thirdweb/src/extensions/dynamic-contracts/__generated__/IExtensionManager/write/removeExtension.ts @@ -0,0 +1,140 @@ +import type { AbiParameterToPrimitiveType } from "abitype"; +import type { + BaseTransactionOptions, + WithOverrides, +} from "../../../../../transaction/types.js"; +import { prepareContractCall } from "../../../../../transaction/prepare-contract-call.js"; +import { encodeAbiParameters } from "../../../../../utils/abi/encodeAbiParameters.js"; +import { once } from "../../../../../utils/promise/once.js"; +import { detectMethod } from "../../../../../utils/bytecode/detectExtension.js"; + +/** + * Represents the parameters for the "removeExtension" function. + */ +export type RemoveExtensionParams = WithOverrides<{ + extensionName: AbiParameterToPrimitiveType<{ + type: "string"; + name: "extensionName"; + }>; +}>; + +export const FN_SELECTOR = "0xee7d2adf" as const; +const FN_INPUTS = [ + { + type: "string", + name: "extensionName", + }, +] as const; +const FN_OUTPUTS = [] as const; + +/** + * Checks if the `removeExtension` method is supported by the given contract. + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. + * @returns A boolean indicating if the `removeExtension` method is supported. + * @extension DYNAMIC-CONTRACTS + * @example + * ```ts + * import { isRemoveExtensionSupported } from "thirdweb/extensions/dynamic-contracts"; + * + * const supported = isRemoveExtensionSupported(["0x..."]); + * ``` + */ +export function isRemoveExtensionSupported(availableSelectors: string[]) { + return detectMethod({ + availableSelectors, + method: [FN_SELECTOR, FN_INPUTS, FN_OUTPUTS] as const, + }); +} + +/** + * Encodes the parameters for the "removeExtension" function. + * @param options - The options for the removeExtension function. + * @returns The encoded ABI parameters. + * @extension DYNAMIC-CONTRACTS + * @example + * ```ts + * import { encodeRemoveExtensionParams } from "thirdweb/extensions/dynamic-contracts"; + * const result = encodeRemoveExtensionParams({ + * extensionName: ..., + * }); + * ``` + */ +export function encodeRemoveExtensionParams(options: RemoveExtensionParams) { + return encodeAbiParameters(FN_INPUTS, [options.extensionName]); +} + +/** + * Encodes the "removeExtension" function into a Hex string with its parameters. + * @param options - The options for the removeExtension function. + * @returns The encoded hexadecimal string. + * @extension DYNAMIC-CONTRACTS + * @example + * ```ts + * import { encodeRemoveExtension } from "thirdweb/extensions/dynamic-contracts"; + * const result = encodeRemoveExtension({ + * extensionName: ..., + * }); + * ``` + */ +export function encodeRemoveExtension(options: RemoveExtensionParams) { + // we do a "manual" concat here to avoid the overhead of the "concatHex" function + // we can do this because we know the specific formats of the values + return (FN_SELECTOR + + encodeRemoveExtensionParams(options).slice( + 2, + )) as `${typeof FN_SELECTOR}${string}`; +} + +/** + * Prepares a transaction to call the "removeExtension" function on the contract. + * @param options - The options for the "removeExtension" function. + * @returns A prepared transaction object. + * @extension DYNAMIC-CONTRACTS + * @example + * ```ts + * import { sendTransaction } from "thirdweb"; + * import { removeExtension } from "thirdweb/extensions/dynamic-contracts"; + * + * const transaction = removeExtension({ + * contract, + * extensionName: ..., + * overrides: { + * ... + * } + * }); + * + * // Send the transaction + * await sendTransaction({ transaction, account }); + * ``` + */ +export function removeExtension( + options: BaseTransactionOptions< + | RemoveExtensionParams + | { + asyncParams: () => Promise; + } + >, +) { + const asyncOptions = once(async () => { + return "asyncParams" in options ? await options.asyncParams() : options; + }); + + return prepareContractCall({ + contract: options.contract, + method: [FN_SELECTOR, FN_INPUTS, FN_OUTPUTS] as const, + params: async () => { + const resolvedOptions = await asyncOptions(); + return [resolvedOptions.extensionName] as const; + }, + value: async () => (await asyncOptions()).overrides?.value, + accessList: async () => (await asyncOptions()).overrides?.accessList, + gas: async () => (await asyncOptions()).overrides?.gas, + gasPrice: async () => (await asyncOptions()).overrides?.gasPrice, + maxFeePerGas: async () => (await asyncOptions()).overrides?.maxFeePerGas, + maxPriorityFeePerGas: async () => + (await asyncOptions()).overrides?.maxPriorityFeePerGas, + nonce: async () => (await asyncOptions()).overrides?.nonce, + extraGas: async () => (await asyncOptions()).overrides?.extraGas, + erc20Value: async () => (await asyncOptions()).overrides?.erc20Value, + }); +} diff --git a/packages/thirdweb/src/extensions/dynamic-contracts/write/installPublishedExtension.test.ts b/packages/thirdweb/src/extensions/dynamic-contracts/write/installPublishedExtension.test.ts new file mode 100644 index 00000000000..e6d4c0427ed --- /dev/null +++ b/packages/thirdweb/src/extensions/dynamic-contracts/write/installPublishedExtension.test.ts @@ -0,0 +1,61 @@ +import { describe, expect, it } from "vitest"; + +import { readContract } from "src/transaction/read-contract.js"; +import { resolveMethod } from "src/transaction/resolve-method.js"; +import { ANVIL_CHAIN } from "../../../../test/src/chains.js"; +import { TEST_CLIENT } from "../../../../test/src/test-clients.js"; +import { TEST_ACCOUNT_A } from "../../../../test/src/test-wallets.js"; +import { getContract } from "../../../contract/contract.js"; +import { deployCloneFactory } from "../../../contract/deployment/utils/bootstrap.js"; +import { deployPublishedContract } from "../../../extensions/prebuilts/deploy-published.js"; +import { sendTransaction } from "../../../transaction/actions/send-transaction.js"; +import { installPublishedExtension } from "./installPublishedExtension.js"; + +describe.runIf(process.env.TW_SECRET_KEY)("install extension", () => { + it.sequential("should install extension to a dynamic contract", async () => { + await deployCloneFactory({ + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + account: TEST_ACCOUNT_A, + }); + + const deployed = await deployPublishedContract({ + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + account: TEST_ACCOUNT_A, + contractId: "EvolvingNFT", + contractParams: { + name: "Evolving nft", + symbol: "ENFT", + defaultAdmin: TEST_ACCOUNT_A.address, + royaltyBps: 0n, + royaltyRecipient: TEST_ACCOUNT_A.address, + saleRecipient: TEST_ACCOUNT_A.address, + trustedForwarders: [], + contractURI: "", + }, + }); + + const contract = getContract({ + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + address: deployed, + }); + + const transaction = installPublishedExtension({ + account: TEST_ACCOUNT_A, + contract, + extensionName: "DirectListingsLogic", + }); + + await sendTransaction({ transaction, account: TEST_ACCOUNT_A }); + + const extensions = await readContract({ + contract, + method: resolveMethod("getAllExtensions"), + params: [], + }); + + expect(extensions.length).toEqual(4); + }); +}); diff --git a/packages/thirdweb/src/extensions/dynamic-contracts/write/installPublishedExtension.ts b/packages/thirdweb/src/extensions/dynamic-contracts/write/installPublishedExtension.ts new file mode 100644 index 00000000000..f363462261f --- /dev/null +++ b/packages/thirdweb/src/extensions/dynamic-contracts/write/installPublishedExtension.ts @@ -0,0 +1,85 @@ +import { resolveContractAbi } from "../../../contract/actions/resolve-abi.js"; +import type { ThirdwebContract } from "../../../contract/contract.js"; +import { getOrDeployInfraForPublishedContract } from "../../../contract/deployment/utils/bootstrap.js"; +import { + generateExtensionFunctionsFromAbi, + getAllDefaultConstructorParamsForImplementation, +} from "../../../extensions/prebuilts/get-required-transactions.js"; +import type { Account } from "../../../wallets/interfaces/wallet.js"; +import { addExtension } from "../__generated__/IExtensionManager/write/addExtension.js"; + +export type InstallPublishedExtensionOptions = { + account: Account; + contract: ThirdwebContract; + extensionName: string; + publisher?: string; + version?: string; + constructorParams?: Record; +}; + +/** + * Install a published extension on a dynamic contract + * @param options - The options for installing a published extension + * @returns A prepared transaction to send + * @example + * ```ts + * import { installPublishedExtension } from "thirdweb/dynamic-contracts"; + * const transaction = installPublishedExtension({ + * client, + * chain, + * account, + * contract, + * extensionName: "MyExtension", + * publisherAddress: "0x...", + * }); + * await sendTransaction({ transaction, account }); + * ``` + */ +export function installPublishedExtension( + options: InstallPublishedExtensionOptions, +) { + const { + account, + contract, + extensionName, + constructorParams, + publisher, + version, + } = options; + + return addExtension({ + contract, + asyncParams: async () => { + const deployedExtension = await getOrDeployInfraForPublishedContract({ + chain: contract.chain, + client: contract.client, + account, + contractId: extensionName, + constructorParams: + constructorParams || + (await getAllDefaultConstructorParamsForImplementation({ + chain: contract.chain, + client: contract.client, + contractId: extensionName, + })), + publisher, + version, + }); + + const abi = await resolveContractAbi( + deployedExtension.implementationContract, + ); + const functions = generateExtensionFunctionsFromAbi(abi); + return { + extension: { + metadata: { + name: extensionName, + metadataURI: "", + implementation: deployedExtension.implementationContract.address, + }, + functions, + }, + }; + }, + }); +} diff --git a/packages/thirdweb/src/extensions/dynamic-contracts/write/uninstallExtension.test.ts b/packages/thirdweb/src/extensions/dynamic-contracts/write/uninstallExtension.test.ts new file mode 100644 index 00000000000..8949dacb55a --- /dev/null +++ b/packages/thirdweb/src/extensions/dynamic-contracts/write/uninstallExtension.test.ts @@ -0,0 +1,64 @@ +import { describe, expect, it } from "vitest"; + +import { readContract } from "src/transaction/read-contract.js"; +import { resolveMethod } from "src/transaction/resolve-method.js"; +import { ANVIL_CHAIN } from "../../../../test/src/chains.js"; +import { TEST_CLIENT } from "../../../../test/src/test-clients.js"; +import { TEST_ACCOUNT_A } from "../../../../test/src/test-wallets.js"; +import { getContract } from "../../../contract/contract.js"; +import { deployCloneFactory } from "../../../contract/deployment/utils/bootstrap.js"; +import { sendTransaction } from "../../../transaction/actions/send-transaction.js"; +import { deployPublishedContract } from "../../prebuilts/deploy-published.js"; +import { uninstallExtension } from "./uninstallExtension.js"; + +describe.runIf(process.env.TW_SECRET_KEY)("uninstall extension", () => { + it.sequential( + "should uninstall extension from a dynamic contract", + async () => { + await deployCloneFactory({ + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + account: TEST_ACCOUNT_A, + }); + + const deployed = await deployPublishedContract({ + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + account: TEST_ACCOUNT_A, + contractId: "EvolvingNFT", + contractParams: { + name: "Evolving nft", + symbol: "ENFT", + defaultAdmin: TEST_ACCOUNT_A.address, + royaltyBps: 0n, + royaltyRecipient: TEST_ACCOUNT_A.address, + saleRecipient: TEST_ACCOUNT_A.address, + trustedForwarders: [], + contractURI: "", + }, + }); + + const contract = getContract({ + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + address: deployed, + }); + + const transaction = uninstallExtension({ + account: TEST_ACCOUNT_A, + contract, + extensionName: "EvolvingNFTLogic", + }); + + await sendTransaction({ transaction, account: TEST_ACCOUNT_A }); + + const extensions = await readContract({ + contract, + method: resolveMethod("getAllExtensions"), + params: [], + }); + + expect(extensions.length).toEqual(2); + }, + ); +}); diff --git a/packages/thirdweb/src/extensions/dynamic-contracts/write/uninstallExtension.ts b/packages/thirdweb/src/extensions/dynamic-contracts/write/uninstallExtension.ts new file mode 100644 index 00000000000..a5b53dfe4d7 --- /dev/null +++ b/packages/thirdweb/src/extensions/dynamic-contracts/write/uninstallExtension.ts @@ -0,0 +1,35 @@ +import type { ThirdwebContract } from "../../../contract/contract.js"; +import type { Account } from "../../../wallets/interfaces/wallet.js"; +import { removeExtension } from "../__generated__/IExtensionManager/write/removeExtension.js"; + +export type UninstallExtensionOptions = { + account: Account; + contract: ThirdwebContract; + extensionName: string; +}; + +/** + * Uninstall an extension on a dynamic contract + * @param options - The options for uninstalling an extension + * @returns A prepared transaction to send + * @example + * ```ts + * import { uninstallExtension } from "thirdweb/dynamic-contracts"; + * const transaction = uninstallExtension({ + * client, + * chain, + * account, + * contract, + * extensionName: "MyExtension", + * }); + * await sendTransaction({ transaction, account }); + * ``` + */ +export function uninstallExtension(options: UninstallExtensionOptions) { + const { contract, extensionName } = options; + + return removeExtension({ + contract, + extensionName, + }); +} diff --git a/packages/thirdweb/src/extensions/prebuilts/get-required-transactions.ts b/packages/thirdweb/src/extensions/prebuilts/get-required-transactions.ts index 837dc206be2..b626e182765 100644 --- a/packages/thirdweb/src/extensions/prebuilts/get-required-transactions.ts +++ b/packages/thirdweb/src/extensions/prebuilts/get-required-transactions.ts @@ -382,7 +382,7 @@ async function generateExtensionInput(args: { } export function generateExtensionFunctionsFromAbi(abi: Abi): Array<{ - functionSelector: string; + functionSelector: `0x${string}`; functionSignature: string; }> { const functions = abi.filter(