Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch wallet rejection error #3334

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions wormhole-connect/src/utils/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@
walletConnection[type] = wallet;
};

// Returns false if the wallet connection was rejected by the user
export const connectWallet = async (
type: TransferWallet,
chain: Chain,
walletInfo: WalletData,
dispatch: Dispatch<any>,
) => {
): Promise<boolean> => {
const { wallet, name } = walletInfo;

setWalletConnection(type, wallet);
Expand All @@ -74,7 +75,18 @@
}

const { chainId, context } = chainConfig;
await wallet.connect({ chainId });

try {
await wallet.connect({ chainId });
} catch (e: any) {
if (e.message && e.message.toLowerCase().includes('rejected')) {
console.info('User rejected wallet connection');
// If user doesn't want to connect to this wallet, this is not an error we need to throw
return false;
} else {
throw e;
}
}

config.triggerEvent({
type: 'wallet.connect',
Expand Down Expand Up @@ -126,6 +138,8 @@
if (name !== ReadOnlyWallet.NAME) {
localStorage.setItem(`wormhole-connect:wallet:${context}`, name);
}

return true;
};

// Checks localStorage for previously used wallet for this chain
Expand All @@ -136,16 +150,18 @@
dispatch: Dispatch<any>,
) => {
const chainConfig = config.chains[chain!]!;
const lastUsedWallet = localStorage.getItem(
`wormhole-connect:wallet:${chainConfig.context}`,
);
const localStorageKey = `wormhole-connect:wallet:${chainConfig.context}`;
const lastUsedWallet = localStorage.getItem(localStorageKey);

// if the last used wallet is not WalletConnect, try to connect to it
if (lastUsedWallet && lastUsedWallet !== 'WalletConnect') {
const options = await getWalletOptions(chainConfig);
const wallet = options.find((w) => w.name === lastUsedWallet);
if (wallet) {
await connectWallet(type, chain, wallet, dispatch);
const connected = await connectWallet(type, chain, wallet, dispatch);
if (!connected) {
localStorage.removeItem(localStorageKey);
}
}
}
};
Expand All @@ -161,7 +177,7 @@
connectLastUsedWallet(TransferWallet.SENDING, fromChain, dispatch);
if (toChain)
connectLastUsedWallet(TransferWallet.RECEIVING, toChain, dispatch);
}, [fromChain, toChain]);

Check warning on line 180 in wormhole-connect/src/utils/wallet/index.ts

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array
};

export const getWalletConnection = (type: TransferWallet) => {
Expand Down
Loading