|
| 1 | +import { createAppKit } from "@reown/appkit/vue"; |
1 | 2 | import { WagmiAdapter } from "@reown/appkit-adapter-wagmi"; |
2 | 3 |
|
| 4 | +// Track if AppKit has been initialized |
| 5 | +let appKitInitialized = false; |
| 6 | +let wagmiAdapterInstance: WagmiAdapter | null = null; |
| 7 | + |
3 | 8 | export const useAppKit = () => { |
4 | 9 | const runtimeConfig = useRuntimeConfig(); |
5 | 10 | const { defaultChain } = useClientStore(); |
6 | 11 |
|
7 | 12 | const projectId = runtimeConfig.public.appKitProjectId; |
| 13 | + const origin = typeof window !== "undefined" ? window.location.origin : "https://auth.zksync.dev"; |
| 14 | + |
8 | 15 | const metadata = { |
9 | 16 | name: "ZKsync SSO Auth Server", |
10 | 17 | description: "ZKsync SSO Auth Server", |
11 | | - url: window.location.origin, |
12 | | - icons: [new URL("/icon-512.png", window.location.origin).toString()], |
| 18 | + url: origin, |
| 19 | + icons: [`${origin}/icon-512.png`], |
13 | 20 | }; |
14 | 21 |
|
15 | | - const wagmiAdapter = new WagmiAdapter({ |
16 | | - networks: [defaultChain], |
17 | | - projectId, |
18 | | - }); |
| 22 | + // Create plain chain object to avoid Viem Proxy issues |
| 23 | + const plainChain = { |
| 24 | + id: defaultChain.id, |
| 25 | + name: defaultChain.name, |
| 26 | + nativeCurrency: { |
| 27 | + name: defaultChain.nativeCurrency.name, |
| 28 | + symbol: defaultChain.nativeCurrency.symbol, |
| 29 | + decimals: defaultChain.nativeCurrency.decimals, |
| 30 | + }, |
| 31 | + rpcUrls: { |
| 32 | + default: { |
| 33 | + http: [...defaultChain.rpcUrls.default.http], |
| 34 | + }, |
| 35 | + }, |
| 36 | + blockExplorers: defaultChain.blockExplorers |
| 37 | + ? { |
| 38 | + default: { |
| 39 | + name: defaultChain.blockExplorers.default.name, |
| 40 | + url: defaultChain.blockExplorers.default.url, |
| 41 | + }, |
| 42 | + } |
| 43 | + : undefined, |
| 44 | + }; |
| 45 | + |
| 46 | + // Lazy initialization - only create AppKit when first used |
| 47 | + if (!appKitInitialized && typeof window !== "undefined") { |
| 48 | + try { |
| 49 | + wagmiAdapterInstance = new WagmiAdapter({ |
| 50 | + networks: [plainChain], |
| 51 | + projectId, |
| 52 | + }); |
| 53 | + |
| 54 | + createAppKit({ |
| 55 | + adapters: [wagmiAdapterInstance], |
| 56 | + networks: [plainChain], |
| 57 | + projectId, |
| 58 | + metadata, |
| 59 | + }); |
| 60 | + |
| 61 | + appKitInitialized = true; |
| 62 | + } catch (error) { |
| 63 | + console.warn("Failed to initialize AppKit:", error); |
| 64 | + } |
| 65 | + } |
19 | 66 |
|
20 | | - const wagmiConfig = wagmiAdapter.wagmiConfig; |
| 67 | + const wagmiConfig = wagmiAdapterInstance?.wagmiConfig; |
21 | 68 |
|
22 | 69 | return { |
23 | 70 | metadata, |
24 | 71 | projectId, |
25 | | - wagmiAdapter, |
| 72 | + wagmiAdapter: wagmiAdapterInstance, |
26 | 73 | wagmiConfig, |
27 | 74 | }; |
28 | 75 | }; |
0 commit comments