diff --git a/lib/state/wallet-connect.ts b/lib/state/wallet-connect.ts index 2eed3fa6d..a4da81844 100644 --- a/lib/state/wallet-connect.ts +++ b/lib/state/wallet-connect.ts @@ -8,7 +8,7 @@ import { import { WalletConnectSigner } from "lib/util/wallet-connect-signer"; import { ZTG_CHAIN_ID } from "lib/constants"; -const WC_PROJECT_ID = process.env.NEXT_PUBLIC_WC_PROJECT_ID; +const WC_PROJECT_ID = process.env.NEXT_PUBLIC_WC_PROJECT_ID ?? ""; const DOMAIN_URL = "https://app.zeitgeist.pm/"; const walletConnectParams = { @@ -34,16 +34,12 @@ const chains = Object.values(requiredNamespaces) .map((namespace) => namespace.chains) .flat(); -if (!WC_PROJECT_ID) { - throw new Error("Missing WalletConnect project ID"); -} - -const modal = new WalletConnectModal({ - projectId: WC_PROJECT_ID, - chains, -}); +let provider; +let modal; -const provider = await UniversalProvider.init(walletConnectParams); +const setProvider = async () => { + provider = await UniversalProvider.init(walletConnectParams); +}; export class WalletConnect implements Wallet { extensionName = "walletconnect"; @@ -65,6 +61,15 @@ export class WalletConnect implements Wallet { onModalOpen?: () => void; onModalClose?: () => void; } = {}) { + if (!WC_PROJECT_ID) return; + + setProvider(); + + modal = new WalletConnectModal({ + projectId: WC_PROJECT_ID, + chains, + }); + modal.subscribeModal((state) => { state.open ? onModalOpen?.() : onModalClose?.(); }); diff --git a/lib/state/wallet.tsx b/lib/state/wallet.tsx index 47ab1d73f..20f693bef 100644 --- a/lib/state/wallet.tsx +++ b/lib/state/wallet.tsx @@ -26,6 +26,7 @@ import { web3AuthWalletInstance } from "./util/web3auth-config"; import { WalletConnect } from "./wallet-connect"; const DAPP_NAME = "zeitgeist"; +const WC_PROJECT_ID = process.env.NEXT_PUBLIC_WC_PROJECT_ID; export type UseWallet = WalletState & { /** @@ -222,7 +223,7 @@ export const supportedWallets = [ new PolkadotjsWallet(), new SubWallet(), new TalismanWallet(), - new WalletConnect(), + ...(WC_PROJECT_ID ? [new WalletConnect()] : []), web3AuthWalletInstance, ];