From 3e8c074011b645ff38587a4d62ac4e0d3aa718e3 Mon Sep 17 00:00:00 2001 From: MananTank Date: Wed, 25 Sep 2024 21:34:25 +0000 Subject: [PATCH] Fix Publish page not prepopulated with latest published version metadata (#4806) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem solved Short description of the bug fixed or feature added --- ## PR-Codex overview This PR focuses on enhancing the `PublishContractPage` by adding logic to handle cases where published contract metadata lacks a version. It fetches existing contracts associated with a publisher and merges their data with the fetched metadata. ### Detailed summary - Imported `getPublishedContractsWithPublisherMapping` utility. - Renamed variable `publishMetadata` to `publishMetadataFromUri`. - Added logic to check if `publishMetadataFromUri` lacks a version. - Fetched existing published contracts for the publisher. - Merged existing contract data with `publishMetadataFromUri` if applicable. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- .../contracts/publish/[publish_uri]/page.tsx | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/apps/dashboard/src/app/(dashboard)/contracts/publish/[publish_uri]/page.tsx b/apps/dashboard/src/app/(dashboard)/contracts/publish/[publish_uri]/page.tsx index 567c8ab8caf..0b6e0686e1b 100644 --- a/apps/dashboard/src/app/(dashboard)/contracts/publish/[publish_uri]/page.tsx +++ b/apps/dashboard/src/app/(dashboard)/contracts/publish/[publish_uri]/page.tsx @@ -5,6 +5,7 @@ import { ContractPublishForm } from "components/contract-components/contract-pub import { revalidatePath } from "next/cache"; import { redirect } from "next/navigation"; import { fetchDeployMetadata } from "thirdweb/contract"; +import { getPublishedContractsWithPublisherMapping } from "../../../published-contract/[publisher]/[contract_id]/utils/getPublishedContractsWithPublisherMapping"; type DirectDeployPageProps = { params: { @@ -20,11 +21,13 @@ export default async function PublishContractPage( ? decodedPublishUri : `ipfs://${decodedPublishUri}`; - const publishMetadata = await fetchDeployMetadata({ + const publishMetadataFromUri = await fetchDeployMetadata({ uri: publishUri, client: getThirdwebClient(), }); + let publishMetadata = publishMetadataFromUri; + const pathname = `/contracts/publish/${props.params.publish_uri}`; const address = getActiveAccountCookie(); @@ -32,6 +35,29 @@ export default async function PublishContractPage( redirect(`/login?next=${encodeURIComponent(pathname)}`); } + // Deploying the next version of a contract scenario: + // check if this is a pre-deployed metadata ( doesn't have a version ) + // If that's the case: + // - get the publish metadata with name+publisher address + // - merge the two objects with publishMetadataFromUri taking higher precedence + if (!publishMetadataFromUri.version) { + const publishedContractVersions = + await getPublishedContractsWithPublisherMapping({ + publisher: address, + contract_id: publishMetadataFromUri.name, + }); + + const publishedContract = publishedContractVersions[0]; + + if (publishedContract) { + publishMetadata = { + ...publishedContract, + ...publishMetadataFromUri, + version: publishedContract.version, + }; + } + } + const token = getJWTCookie(address); if (!token) { redirect(`/login?next=${encodeURIComponent(pathname)}`);