Skip to content

Commit 6caba38

Browse files
committed
[MNY-115] Dashboard: Fix token creation flow not working for custom chains (#7896)
<!-- ## title your PR with this format: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" If you did not copy the branch name from Linear, paste the issue tag here (format is TEAM-0000): ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. --> <!-- start pr-codex --> --- ## PR-Codex overview This PR introduces a new custom hook `useGetV5DashboardChain` and modifies existing components to utilize this hook for chain management, improving code clarity and reducing redundancy. ### Detailed summary - Added `useGetV5DashboardChain` hook in `v5-adapter.ts`. - Updated `LaunchNFT` component to use `useV5DashboardChain`. - Refactored `CreateTokenAssetPageUI` to use `useV5DashboardChain`. - Refactored `CreateNFTPage` to use `getChain` from `useGetV5DashboardChain`. - Refactored `CreateTokenAssetPage` to use `getChain` from `useGetV5DashboardChain`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - Refactor - Unified chain selection across NFT creation, NFT launch, and token creation flows using a single, consistent resolver. - Modernized internals for better compatibility with the latest chain data. - Bug Fixes - Improved reliability of chain selection during deployments and validations, reducing chances of selecting the wrong network. - More consistent behavior when switching chains in forms and launch steps. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 68a07ed commit 6caba38

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

apps/dashboard/src/@/hooks/chains/v5-adapter.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { useMemo } from "react";
3+
import { useCallback, useMemo } from "react";
44
import type { Chain, ChainMetadata } from "thirdweb/chains";
55
import { useActiveWalletChain } from "thirdweb/react";
66
import { useAllChainsData } from "@/hooks/chains/allChains";
@@ -27,6 +27,17 @@ export function useV5DashboardChain(
2727
}, [chainId, idToChain]);
2828
}
2929

30+
export function useGetV5DashboardChain() {
31+
const { idToChain } = useAllChainsData();
32+
return useCallback(
33+
(chainId: number) => {
34+
// eslint-disable-next-line no-restricted-syntax
35+
return defineDashboardChain(chainId, idToChain.get(chainId));
36+
},
37+
[idToChain],
38+
);
39+
}
40+
3041
/**
3142
* same behavior as v4 `useChain()` but for v5
3243
*/

apps/dashboard/src/app/(app)/team/[team_slug]/[project_slug]/(sidebar)/tokens/create/nft/create-nft-page.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use client";
22
import { useRef } from "react";
33
import {
4-
defineChain,
54
encode,
65
getContract,
76
sendAndConfirmTransaction,
@@ -25,6 +24,7 @@ import { create7702MinimalAccount } from "thirdweb/wallets/smart";
2524
import { revalidatePathAction } from "@/actions/revalidate";
2625
import { reportContractDeployed } from "@/analytics/report";
2726
import type { Team } from "@/api/team/get-team";
27+
import { useGetV5DashboardChain } from "@/hooks/chains/v5-adapter";
2828
import { useAddContractToProject } from "@/hooks/project-contracts";
2929
import type { CreateNFTCollectionAllValues } from "./_common/form";
3030
import { CreateNFTPageUI } from "./create-nft-page-ui";
@@ -41,6 +41,7 @@ export function CreateNFTPage(props: {
4141
const activeAccount = useActiveAccount();
4242
const addContractToProject = useAddContractToProject();
4343
const contractAddressRef = useRef<string | undefined>(undefined);
44+
const getChain = useGetV5DashboardChain();
4445

4546
function getAccount(params: { gasless: boolean }) {
4647
if (!activeAccount) {
@@ -59,9 +60,7 @@ export function CreateNFTPage(props: {
5960
}
6061

6162
function getDeployedContract(params: { chain: string }) {
62-
// eslint-disable-next-line no-restricted-syntax
63-
const chain = defineChain(Number(params.chain));
64-
63+
const chain = getChain(Number(params.chain));
6564
const contractAddress = contractAddressRef.current;
6665

6766
if (!contractAddress) {
@@ -89,8 +88,7 @@ export function CreateNFTPage(props: {
8988
gasless: params.gasless,
9089
});
9190

92-
// eslint-disable-next-line no-restricted-syntax
93-
const chain = defineChain(Number(collectionInfo.chain));
91+
const chain = getChain(Number(collectionInfo.chain));
9492

9593
let contractAddress: string;
9694

apps/dashboard/src/app/(app)/team/[team_slug]/[project_slug]/(sidebar)/tokens/create/nft/launch/launch-nft.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "lucide-react";
88
import Link from "next/link";
99
import { useMemo, useRef, useState } from "react";
10-
import { defineChain, type ThirdwebClient } from "thirdweb";
10+
import type { ThirdwebClient } from "thirdweb";
1111
import {
1212
TokenProvider,
1313
TokenSymbol,
@@ -35,6 +35,7 @@ import {
3535
DialogTitle,
3636
} from "@/components/ui/dialog";
3737
import { Skeleton } from "@/components/ui/skeleton";
38+
import { useV5DashboardChain } from "@/hooks/chains/v5-adapter";
3839
import { parseError } from "@/utils/errorParser";
3940
import { ChainOverview } from "../../_common/chain-overview";
4041
import { StepCard } from "../../_common/step-card";
@@ -308,6 +309,8 @@ export function LaunchNFT(props: {
308309
(nft) => nft.price_amount === props.values.nfts[0]?.price_amount,
309310
);
310311

312+
const chain = useV5DashboardChain(Number(formValues.collectionInfo.chain));
313+
311314
const uniqueAttributes = useMemo(() => {
312315
const attributeNames = new Set<string>();
313316
for (const nft of props.values.nfts) {
@@ -500,8 +503,7 @@ export function LaunchNFT(props: {
500503
{formValues.nfts[0].price_amount}{" "}
501504
<TokenProvider
502505
address={formValues.nfts[0].price_currency}
503-
// eslint-disable-next-line no-restricted-syntax
504-
chain={defineChain(Number(formValues.collectionInfo.chain))}
506+
chain={chain}
505507
client={props.client}
506508
>
507509
<TokenSymbol

apps/dashboard/src/app/(app)/team/[team_slug]/[project_slug]/(sidebar)/tokens/create/token/create-token-page-impl.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use client";
22
import { useRef } from "react";
33
import {
4-
defineChain,
54
getAddress,
65
getContract,
76
NATIVE_TOKEN_ADDRESS,
@@ -33,6 +32,7 @@ import {
3332
DEFAULT_FEE_BPS_NEW,
3433
DEFAULT_FEE_RECIPIENT,
3534
} from "@/constants/addresses";
35+
import { useGetV5DashboardChain } from "@/hooks/chains/v5-adapter";
3636
import { useAddContractToProject } from "@/hooks/project-contracts";
3737
import { pollWithTimeout } from "@/utils/pollWithTimeout";
3838
import { createTokenOnUniversalBridge } from "../_apis/create-token-on-bridge";
@@ -52,6 +52,7 @@ export function CreateTokenAssetPage(props: {
5252
const activeAccount = useActiveAccount();
5353
const addContractToProject = useAddContractToProject();
5454
const contractAddressRef = useRef<string | undefined>(undefined);
55+
const getChain = useGetV5DashboardChain();
5556

5657
function getAccount(gasless: boolean) {
5758
if (!activeAccount) {
@@ -75,8 +76,7 @@ export function CreateTokenAssetPage(props: {
7576
throw new Error("Contract address not set");
7677
}
7778

78-
// eslint-disable-next-line no-restricted-syntax
79-
const chain = defineChain(Number(params.chain));
79+
const chain = getChain(Number(params.chain));
8080

8181
return getContract({
8282
address: contractAddress,
@@ -111,10 +111,11 @@ export function CreateTokenAssetPage(props: {
111111
Number(params.values.supply) * (salePercent / 100),
112112
);
113113

114+
const chain = getChain(Number(params.values.chain));
115+
114116
const contractAddress = await createToken({
115117
account,
116-
// eslint-disable-next-line no-restricted-syntax
117-
chain: defineChain(Number(params.values.chain)),
118+
chain: chain,
118119
client: props.client,
119120
launchConfig:
120121
params.values.saleEnabled && saleAmount !== 0
@@ -245,10 +246,11 @@ export function CreateTokenAssetPage(props: {
245246
{} as Record<string, string>,
246247
);
247248

249+
const chain = getChain(Number(params.values.chain));
250+
248251
const contractAddress = await deployERC20Contract({
249252
account,
250-
// eslint-disable-next-line no-restricted-syntax
251-
chain: defineChain(Number(values.chain)),
253+
chain: chain,
252254
client: props.client,
253255
params: {
254256
description: values.description,

apps/dashboard/src/app/(app)/team/[team_slug]/[project_slug]/(sidebar)/tokens/create/token/create-token-page.client.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { useQuery } from "@tanstack/react-query";
55
import { useState } from "react";
66
import { useForm } from "react-hook-form";
77
import {
8-
defineChain,
98
getAddress,
109
NATIVE_TOKEN_ADDRESS,
1110
type ThirdwebClient,
@@ -18,6 +17,7 @@ import {
1817
import { reportAssetCreationStepConfigured } from "@/analytics/report";
1918
import type { Team } from "@/api/team/get-team";
2019
import { Spinner } from "@/components/ui/Spinner/Spinner";
20+
import { useV5DashboardChain } from "@/hooks/chains/v5-adapter";
2121
import { StepCard } from "../_common/step-card";
2222
import {
2323
type CreateAssetFormValues,
@@ -94,13 +94,15 @@ export function CreateTokenAssetPageUI(props: {
9494
reValidateMode: "onChange",
9595
});
9696

97+
const chain = useV5DashboardChain(Number(tokenInfoForm.watch("chain")));
98+
9799
const isERC20AssetSupportedQuery = useQuery({
98-
queryKey: ["is-erc20-asset-supported", tokenInfoForm.watch("chain")],
100+
queryKey: ["is-erc20-asset-supported", chain],
99101
queryFn: async () => {
100102
try {
101103
const res = await getDeployedContractFactory({
102104
// eslint-disable-next-line no-restricted-syntax
103-
chain: defineChain(Number(tokenInfoForm.watch("chain"))),
105+
chain: chain,
104106
client: props.client,
105107
});
106108
return !!res;
@@ -114,15 +116,14 @@ export function CreateTokenAssetPageUI(props: {
114116
queryFn: async () => {
115117
try {
116118
return await isPoolRouterEnabled({
117-
// eslint-disable-next-line no-restricted-syntax
118-
chain: defineChain(Number(tokenInfoForm.watch("chain"))),
119+
chain: chain,
119120
client: props.client,
120121
});
121122
} catch {
122123
return false;
123124
}
124125
},
125-
queryKey: ["is-asset-router-enabled", tokenInfoForm.watch("chain")],
126+
queryKey: ["is-asset-router-enabled", chain],
126127
});
127128

128129
const defaultSaleMode = isERC20AssetSupportedQuery.data

0 commit comments

Comments
 (0)