Skip to content

Commit f3f8fdd

Browse files
committed
chore: update to use chainIds
1 parent ccf83b4 commit f3f8fdd

File tree

4 files changed

+65
-62
lines changed

4 files changed

+65
-62
lines changed

apps/dashboard/src/app/team/[team_slug]/(team)/~/ecosystem/[slug]/(active)/configuration/components/client/auth-options-form.client.tsx

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"use client";
2+
import { MultiNetworkSelector } from "@/components/blocks/NetworkSelectors";
23
import { SettingsCard } from "@/components/blocks/SettingsCard";
34
import { Button } from "@/components/ui/button";
45
import { Checkbox } from "@/components/ui/checkbox";
@@ -23,25 +24,17 @@ import { Skeleton } from "@/components/ui/skeleton";
2324
import { Switch } from "@/components/ui/switch";
2425
import { cn } from "@/lib/utils";
2526
import { zodResolver } from "@hookform/resolvers/zod";
26-
import { NetworkSelectorButton } from "components/selects/NetworkSelectorButton";
2727
import { PlusIcon } from "lucide-react";
2828
import { useFieldArray, useForm } from "react-hook-form";
2929
import { toast } from "sonner";
3030
import { isAddress } from "thirdweb";
31-
import {
32-
defineChain,
33-
getCachedChainIfExists,
34-
} from "thirdweb/dist/types/chains/utils";
35-
import {
36-
useActiveWalletChain,
37-
useSwitchActiveWalletChain,
38-
} from "thirdweb/react";
3931
import { getSocialIcon } from "thirdweb/wallets/in-app";
4032
import {
4133
DEFAULT_ACCOUNT_FACTORY_V0_6,
4234
DEFAULT_ACCOUNT_FACTORY_V0_7,
4335
} from "thirdweb/wallets/smart";
4436
import invariant from "tiny-invariant";
37+
import { FormErrorMessage } from "tw-components";
4538
import { z } from "zod";
4639
import { type Ecosystem, authOptions } from "../../../../../types";
4740
import { useUpdateEcosystem } from "../../hooks/use-update-ecosystem";
@@ -53,6 +46,7 @@ type AuthOptionsFormData = {
5346
customHeaders: { key: string; value: string }[];
5447
useSmartAccount: boolean;
5548
sponsorGas: boolean;
49+
chainIds: number[];
5650
accountFactoryType: "v0.6" | "v0.7" | "custom";
5751
customAccountFactoryAddress: string;
5852
};
@@ -66,6 +60,7 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
6660
customHeaders: ecosystem.customAuthOptions?.authEndpoint?.headers || [],
6761
useSmartAccount: !!ecosystem.smartAccountOptions,
6862
sponsorGas: ecosystem.smartAccountOptions?.sponsorGas || false,
63+
chainIds: ecosystem.smartAccountOptions?.chainIds ?? [],
6964
accountFactoryType:
7065
ecosystem.smartAccountOptions?.accountFactoryAddress ===
7166
DEFAULT_ACCOUNT_FACTORY_V0_7
@@ -93,6 +88,7 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
9388
.optional(),
9489
useSmartAccount: z.boolean(),
9590
sponsorGas: z.boolean(),
91+
chainIds: z.array(z.number()),
9692
accountFactoryType: z.enum(["v0.6", "v0.7", "custom"]),
9793
customAccountFactoryAddress: z.string().optional(),
9894
})
@@ -101,7 +97,8 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
10197
if (
10298
data.useSmartAccount &&
10399
data.customAccountFactoryAddress &&
104-
!isAddress(data.customAccountFactoryAddress)
100+
(!isAddress(data.customAccountFactoryAddress) ||
101+
!data.customAccountFactoryAddress)
105102
) {
106103
return false;
107104
}
@@ -112,6 +109,18 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
112109
path: ["customAccountFactoryAddress"],
113110
},
114111
)
112+
.refine(
113+
(data) => {
114+
if (data.useSmartAccount && data.chainIds.length === 0) {
115+
return false;
116+
}
117+
return true;
118+
},
119+
{
120+
message: "Please select at least one chain ID",
121+
path: ["chainIds"],
122+
},
123+
)
115124
.refine(
116125
(data) => {
117126
if (data.useCustomAuth && !data.customAuthEndpoint) {
@@ -142,17 +151,6 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
142151
},
143152
});
144153

145-
const switchChain = useSwitchActiveWalletChain();
146-
const currentChain = useActiveWalletChain();
147-
if (
148-
ecosystem.smartAccountOptions?.defaultChainId &&
149-
currentChain &&
150-
currentChain.id !== ecosystem.smartAccountOptions?.defaultChainId
151-
) {
152-
const chainId = ecosystem.smartAccountOptions?.defaultChainId;
153-
switchChain(getCachedChainIfExists(chainId) ?? defineChain(chainId));
154-
}
155-
156154
const onSubmit = (data: AuthOptionsFormData) => {
157155
let customAuthOptions: Ecosystem["customAuthOptions"] | null = null;
158156
if (data.useCustomAuth && data.customAuthEndpoint) {
@@ -182,22 +180,12 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
182180
accountFactoryAddress = DEFAULT_ACCOUNT_FACTORY_V0_7;
183181
break;
184182
case "custom":
185-
if (!data.customAccountFactoryAddress) {
186-
toast.error("Please enter a custom account factory address");
187-
return;
188-
}
189183
accountFactoryAddress = data.customAccountFactoryAddress;
190184
break;
191185
}
192186

193-
if (!currentChain?.id) {
194-
toast.error("Please select a default chain ID");
195-
return;
196-
}
197187
smartAccountOptions = {
198-
defaultChainId: currentChain?.id,
199-
chainIds: [], // TODO: remove once backend is updated
200-
188+
chainIds: data.chainIds,
201189
sponsorGas: data.sponsorGas,
202190
accountFactoryAddress,
203191
};
@@ -455,25 +443,32 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
455443
)}
456444
/>
457445

458-
<FormItem>
459-
<FormLabel>Default Chain ID</FormLabel>
460-
<FormControl>
461-
<NetworkSelectorButton />
462-
</FormControl>
463-
<FormDescription>
464-
This will be the chain ID the smart account will be
465-
initialized to on your{" "}
466-
<a
467-
href={`https://${ecosystem.slug}.ecosystem.thirdweb.com`}
468-
className="text-link-foreground"
469-
target="_blank"
470-
rel="noreferrer"
471-
>
472-
ecosystem page
473-
</a>
474-
.
475-
</FormDescription>
476-
</FormItem>
446+
<FormField
447+
control={form.control}
448+
name="chainIds"
449+
render={({ field }) => (
450+
<FormItem>
451+
<FormLabel>Allowed Chain Ids</FormLabel>
452+
<div className="flex flex-col">
453+
<MultiNetworkSelector
454+
selectedChainIds={field.value}
455+
onChange={field.onChange}
456+
/>
457+
</div>
458+
<FormDescription>
459+
Choose a default account factory or select custom to enter
460+
your own address
461+
</FormDescription>
462+
<FormErrorMessage>
463+
{
464+
form.getFieldState("chainIds", form.formState).error
465+
?.message
466+
}
467+
</FormErrorMessage>
468+
</FormItem>
469+
)}
470+
/>
471+
477472
<FormField
478473
control={form.control}
479474
name="accountFactoryType"

apps/dashboard/src/app/team/[team_slug]/(team)/~/ecosystem/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ export type Ecosystem = {
3636
};
3737
} | null;
3838
smartAccountOptions?: {
39-
defaultChainId: number;
40-
chainIds: number[]; // TODO: Remove this once we update the backend to stop requiring this
39+
chainIds: number[];
4140
sponsorGas: boolean;
4241
accountFactoryAddress: string;
4342
} | null;

packages/thirdweb/src/wallets/ecosystem/get-ecosystem-wallet-auth-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type EcosystemOptions = {
1313
};
1414

1515
type SmartAccountOptions = {
16-
defaultChainId: number;
16+
chainIds: number[];
1717
sponsorGas: boolean;
1818
accountFactoryAddress: string;
1919
};

packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { trackConnect } from "../../../../analytics/track/connect.js";
22
import type { Chain } from "../../../../chains/types.js";
33
import {
4-
defineChain,
4+
getCachedChain,
55
getCachedChainIfExists,
66
} from "../../../../chains/utils.js";
77
import type { ThirdwebClient } from "../../../../client/client.js";
@@ -77,17 +77,26 @@ export function createInAppWallet(args: {
7777
const ecosystemOptions = await getEcosystemInfo(ecosystem.id);
7878
const smartAccountOptions = ecosystemOptions?.smartAccountOptions;
7979
if (smartAccountOptions) {
80+
const defaultChainId =
81+
ecosystemOptions.smartAccountOptions.chainIds[0];
82+
if (!defaultChainId) {
83+
throw new Error(
84+
"No default chain ID found for ecosystem smart accounts. Please reach out to the ecosystem owner.",
85+
);
86+
}
8087
const preferredChain =
81-
options.chain ??
82-
getCachedChainIfExists(
83-
ecosystemOptions.smartAccountOptions.defaultChainId,
84-
) ??
85-
defineChain(ecosystemOptions.smartAccountOptions.defaultChainId);
86-
if (!preferredChain) {
88+
options.chain ?? getCachedChain(defaultChainId);
89+
90+
if (
91+
!ecosystemOptions.smartAccountOptions.chainIds.includes(
92+
preferredChain?.id,
93+
)
94+
) {
8795
throw new Error(
88-
"Chain is required for ecosystem smart accounts, pass it via connect() or via UI components",
96+
`Chain ID ${preferredChain.id} is not supported for ecosystem smart accounts, pass it via connect() or via UI components`,
8997
);
9098
}
99+
91100
createOptions = {
92101
...createOptions,
93102
smartAccount: {

0 commit comments

Comments
 (0)