From 15abccaa594de2405944124fcca823f08f394a3e Mon Sep 17 00:00:00 2001 From: robhyrk Date: Wed, 10 Apr 2024 12:39:19 -0400 Subject: [PATCH 1/2] fix: check if wallet connect ID exists before instantiating WC, modal, and provider --- lib/state/wallet-connect.ts | 23 ++++++++++++++--------- lib/state/wallet.tsx | 3 ++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/state/wallet-connect.ts b/lib/state/wallet-connect.ts index 2eed3fa6d..824fed1bc 100644 --- a/lib/state/wallet-connect.ts +++ b/lib/state/wallet-connect.ts @@ -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, ]; From 0377bfe8efce516810ad089eeef00dd37612601f Mon Sep 17 00:00:00 2001 From: robhyrk Date: Tue, 23 Apr 2024 12:07:24 -0400 Subject: [PATCH 2/2] fix: type error --- lib/state/wallet-connect.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/state/wallet-connect.ts b/lib/state/wallet-connect.ts index 824fed1bc..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 = {