From 2a708c2683abe268936a79bdc071becaeedcf6f4 Mon Sep 17 00:00:00 2001 From: Eugene Chybisov Date: Tue, 23 May 2023 13:00:26 +0200 Subject: [PATCH] fix: wagmi can't switch chain --- packages/widget/src/hooks/useRouteExecution.ts | 5 +++-- .../src/providers/WalletProvider/WalletProvider.tsx | 8 +++++--- packages/widget/src/providers/WalletProvider/types.ts | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/widget/src/hooks/useRouteExecution.ts b/packages/widget/src/hooks/useRouteExecution.ts index 51f84cbcb..9e15fa9e9 100644 --- a/packages/widget/src/hooks/useRouteExecution.ts +++ b/packages/widget/src/hooks/useRouteExecution.ts @@ -75,10 +75,11 @@ export const useRouteExecution = ({ } const currentChainId = await account.signer.getChainId(); if (currentChainId !== requiredChainId) { - const switched = await switchChain(requiredChainId); - if (!switched) { + const signer = await switchChain(requiredChainId); + if (!signer) { throw new Error('Chain was not switched.'); } + return signer; } return account.signer; }; diff --git a/packages/widget/src/providers/WalletProvider/WalletProvider.tsx b/packages/widget/src/providers/WalletProvider/WalletProvider.tsx index b7e3e8188..3cbeb3288 100644 --- a/packages/widget/src/providers/WalletProvider/WalletProvider.tsx +++ b/packages/widget/src/providers/WalletProvider/WalletProvider.tsx @@ -89,6 +89,7 @@ export const WalletProvider: FC = ({ children }) => { const signer = await walletManagement.switchChain(chainId); const account = await extractAccountFromSigner(signer); setAccount(account); + return signer; } else if (!currentWallet) { const provider = account.signer?.provider as Web3Provider; if (!provider) { @@ -99,12 +100,13 @@ export const WalletProvider: FC = ({ children }) => { await currentWallet?.switchChain(chainId); handleWalletUpdate(currentWallet); } - return true; + // TODO: this will fail if it's not created with ethers 'any' network, replace with the new signer when possible + return account.signer; } catch { - return false; + return account.signer; } }, - [account.signer?.provider, currentWallet, walletManagement], + [account.signer, currentWallet, walletManagement], ); const addChain = useCallback( diff --git a/packages/widget/src/providers/WalletProvider/types.ts b/packages/widget/src/providers/WalletProvider/types.ts index 2c9b22173..1ec1a8d84 100644 --- a/packages/widget/src/providers/WalletProvider/types.ts +++ b/packages/widget/src/providers/WalletProvider/types.ts @@ -7,7 +7,7 @@ export interface WalletContextProps { addChain(chainId: number): Promise; addToken(chainId: number, token: StaticToken): Promise; disconnect(wallet?: Wallet): void; - switchChain(chainId: number): Promise; + switchChain(chainId: number): Promise; connect(wallet?: Wallet | undefined): Promise; }