@@ -4,7 +4,11 @@ import { getThirdwebClient } from "@/constants/thirdweb.server";
44import { ContractPublishForm } from "components/contract-components/contract-publish-form" ;
55import { revalidatePath } from "next/cache" ;
66import { redirect } from "next/navigation" ;
7- import { fetchDeployMetadata } from "thirdweb/contract" ;
7+ import {
8+ type FetchDeployMetadataResult ,
9+ fetchDeployMetadata ,
10+ } from "thirdweb/contract" ;
11+ import { getPublishedContractsWithPublisherMapping } from "../../../published-contract/[publisher]/[contract_id]/utils/getPublishedContractsWithPublisherMapping" ;
812
913type DirectDeployPageProps = {
1014 params : {
@@ -20,10 +24,13 @@ export default async function PublishContractPage(
2024 ? decodedPublishUri
2125 : `ipfs://${ decodedPublishUri } ` ;
2226
23- const publishMetadata = await fetchDeployMetadata ( {
24- uri : publishUri ,
25- client : getThirdwebClient ( ) ,
26- } ) ;
27+ const publishMetadataFromUri : FetchDeployMetadataResult =
28+ await fetchDeployMetadata ( {
29+ uri : publishUri ,
30+ client : getThirdwebClient ( ) ,
31+ } ) ;
32+
33+ let publishMetadata = publishMetadataFromUri ;
2734
2835 const pathname = `/contracts/publish/${ props . params . publish_uri } ` ;
2936
@@ -32,6 +39,29 @@ export default async function PublishContractPage(
3239 redirect ( `/login?next=${ encodeURIComponent ( pathname ) } ` ) ;
3340 }
3441
42+ // Deploying the next version of a contract scenario:
43+ // check if this is a pre-deployed metadata ( doesn't have a version )
44+ // If that's the case:
45+ // - get the publish metadata with name+publisher address
46+ // - merge the two objects with publishMetadataFromUri taking higher precedence
47+ if ( ! publishMetadataFromUri . version ) {
48+ const publishedContractVersions =
49+ await getPublishedContractsWithPublisherMapping ( {
50+ publisher : address ,
51+ contract_id : publishMetadataFromUri . name ,
52+ } ) ;
53+
54+ const publishedContract = publishedContractVersions [ 0 ] ;
55+
56+ if ( publishedContract ) {
57+ publishMetadata = {
58+ ...publishedContract ,
59+ ...publishMetadataFromUri ,
60+ version : publishedContract . version ,
61+ } ;
62+ }
63+ }
64+
3565 const token = getJWTCookie ( address ) ;
3666 if ( ! token ) {
3767 redirect ( `/login?next=${ encodeURIComponent ( pathname ) } ` ) ;
0 commit comments