Skip to content

Commit 3d0a582

Browse files
authored
Catch wallet rejection error (#3334)
* don't throw when user rejects wallet connection * catch when user rejects wallet connection
1 parent 6603424 commit 3d0a582

File tree

1 file changed

+22
-6
lines changed
  • wormhole-connect/src/utils/wallet

1 file changed

+22
-6
lines changed

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

+22-6
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
@@ -136,16 +150,18 @@ export const connectLastUsedWallet = async (
136150
dispatch: Dispatch<any>,
137151
) => {
138152
const chainConfig = config.chains[chain!]!;
139-
const lastUsedWallet = localStorage.getItem(
140-
`wormhole-connect:wallet:${chainConfig.context}`,
141-
);
153+
const localStorageKey = `wormhole-connect:wallet:${chainConfig.context}`;
154+
const lastUsedWallet = localStorage.getItem(localStorageKey);
142155

143156
// if the last used wallet is not WalletConnect, try to connect to it
144157
if (lastUsedWallet && lastUsedWallet !== 'WalletConnect') {
145158
const options = await getWalletOptions(chainConfig);
146159
const wallet = options.find((w) => w.name === lastUsedWallet);
147160
if (wallet) {
148-
await connectWallet(type, chain, wallet, dispatch);
161+
const connected = await connectWallet(type, chain, wallet, dispatch);
162+
if (!connected) {
163+
localStorage.removeItem(localStorageKey);
164+
}
149165
}
150166
}
151167
};

0 commit comments

Comments
 (0)