Skip to content

Commit

Permalink
fix(wallets): cache walletConnectClient
Browse files Browse the repository at this point in the history
  • Loading branch information
gregfromstl committed Jun 16, 2024
1 parent 6b67cb6 commit 48c5a18
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export function WalletConnectReceiverScreen(props: {
setWalletConnectClient(wcClient);
})
.catch(() => {
console.log("Failed to establish WalletConnect connection");
setErrorConnecting("Failed to establish WalletConnect connection");
});
}, [activeWallet, props.client, props.closeModal, errorConnecting]);
Expand Down
20 changes: 19 additions & 1 deletion packages/thirdweb/src/wallets/wallet-connect/receiver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ export type CreateWalletConnectSessionOptions = {
onConnect?: (session: WalletConnectSession) => void;
};

let walletConnectClientCache = new WeakMap<
ThirdwebClient,
WalletConnectClient
>();

/*
* @internal
*/
export const clearWalletConnectClientCache = () => {
walletConnectClientCache = new WeakMap<ThirdwebClient, WalletConnectClient>();
};

/**
* Creates a new WalletConnect client for interacting with another application.
* @param options - The options to use to create the WalletConnect client.
Expand Down Expand Up @@ -115,6 +127,10 @@ export async function createWalletConnectClient(
): Promise<WalletConnectClient> {
const { wallet, requestHandlers, onConnect, onDisconnect } = options;

if (walletConnectClientCache.has(options.client)) {
return walletConnectClientCache.get(options.client) as WalletConnectClient;
}

Check warning on line 132 in packages/thirdweb/src/wallets/wallet-connect/receiver/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/receiver/index.ts#L131-L132

Added lines #L131 - L132 were not covered by tests

initializeSessionStore({ clientId: options.client.clientId });

const defaults = getDefaultAppMetadata();
Expand Down Expand Up @@ -188,6 +204,8 @@ export async function createWalletConnectClient(
return result;
};

walletConnectClientCache.set(options.client, walletConnectClient);

return walletConnectClient;
}

Expand Down Expand Up @@ -258,7 +276,7 @@ export async function disconnectWalletConnectSession(options: {
topic: options.session.topic,
reason: {
code: 6000,
message: "Replaced by new session",
message: "Disconnected",
},
});
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TEST_IN_APP_WALLET_A } from "../../../../test/src/test-wallets.js";
import { getDefaultAppMetadata } from "../../utils/defaultDappMetadata.js";
import { DEFAULT_PROJECT_ID } from "../constants.js";
import {
clearWalletConnectClientCache,
createWalletConnectClient,
createWalletConnectSession,
disconnectWalletConnectSession,
Expand Down Expand Up @@ -64,6 +65,10 @@ beforeEach(() => {
});

describe("createWalletConnectClient", () => {
beforeEach(() => {
clearWalletConnectClientCache();
});

it("creates a client with provided metadata", async () => {
const client = await createWalletConnectClient({
projectId: "test",
Expand Down

0 comments on commit 48c5a18

Please sign in to comment.