Skip to content

Commit a6adbcd

Browse files
committedMar 3, 2025
catch when user rejects wallet connection
1 parent 3423d2c commit a6adbcd

File tree

1 file changed

+25
-17
lines changed
  • wormhole-connect/src/utils/wallet

1 file changed

+25
-17
lines changed
 

‎wormhole-connect/src/utils/wallet/index.ts

+25-17
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ export const setWalletConnection = (type: TransferWallet, wallet: Wallet) => {
5858
walletConnection[type] = wallet;
5959
};
6060

61+
// Returns false if the wallet connection was rejected by the user
6162
export const connectWallet = async (
6263
type: TransferWallet,
6364
chain: Chain,
6465
walletInfo: WalletData,
6566
dispatch: Dispatch<any>,
66-
) => {
67+
): Promise<boolean> => {
6768
const { wallet, name } = walletInfo;
6869

6970
setWalletConnection(type, wallet);
@@ -74,7 +75,18 @@ export const connectWallet = async (
7475
}
7576

7677
const { chainId, context } = chainConfig;
77-
await wallet.connect({ chainId });
78+
79+
try {
80+
await wallet.connect({ chainId });
81+
} catch (e: any) {
82+
if (e.message && e.message.toLowerCase().includes('rejected')) {
83+
console.info('User rejected wallet connection');
84+
// If user doesn't want to connect to this wallet, this is not an error we need to throw
85+
return false;
86+
} else {
87+
throw e;
88+
}
89+
}
7890

7991
config.triggerEvent({
8092
type: 'wallet.connect',
@@ -126,6 +138,8 @@ export const connectWallet = async (
126138
if (name !== ReadOnlyWallet.NAME) {
127139
localStorage.setItem(`wormhole-connect:wallet:${context}`, name);
128140
}
141+
142+
return true;
129143
};
130144

131145
// Checks localStorage for previously used wallet for this chain
@@ -135,26 +149,20 @@ export const connectLastUsedWallet = async (
135149
chain: Chain,
136150
dispatch: Dispatch<any>,
137151
) => {
138-
const localStorageKey = `wormhole-connect:wallet:${chainConfig.context}`;
139152
const chainConfig = config.chains[chain!]!;
153+
const localStorageKey = `wormhole-connect:wallet:${chainConfig.context}`;
140154
const lastUsedWallet = localStorage.getItem(localStorageKey);
141155

142-
try {
143-
// if the last used wallet is not WalletConnect, try to connect to it
144-
if (lastUsedWallet && lastUsedWallet !== 'WalletConnect') {
145-
const options = await getWalletOptions(chainConfig);
146-
const wallet = options.find((w) => w.name === lastUsedWallet);
147-
if (wallet) {
148-
await connectWallet(type, chain, wallet, dispatch);
156+
// if the last used wallet is not WalletConnect, try to connect to it
157+
if (lastUsedWallet && lastUsedWallet !== 'WalletConnect') {
158+
const options = await getWalletOptions(chainConfig);
159+
const wallet = options.find((w) => w.name === lastUsedWallet);
160+
if (wallet) {
161+
const connected = await connectWallet(type, chain, wallet, dispatch);
162+
if (!connected) {
163+
localStorage.removeItem(localStorageKey);
149164
}
150165
}
151-
} catch (e: any) {
152-
if (e.message && e.message.includes('UserRejectedRequestError')) {
153-
// If user doesn't want to connect to this wallet, remove it from localStorage
154-
localStorage.removeItem(localStorageKey);
155-
} else {
156-
throw e;
157-
}
158166
}
159167
};
160168

0 commit comments

Comments
 (0)