Skip to content

Commit

Permalink
fix: wagmi can't switch chain
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed May 23, 2023
1 parent 64e0592 commit 2a708c2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/widget/src/hooks/useRouteExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const WalletProvider: FC<PropsWithChildren> = ({ 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) {
Expand All @@ -99,12 +100,13 @@ export const WalletProvider: FC<PropsWithChildren> = ({ 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(
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/src/providers/WalletProvider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface WalletContextProps {
addChain(chainId: number): Promise<boolean>;
addToken(chainId: number, token: StaticToken): Promise<void>;
disconnect(wallet?: Wallet): void;
switchChain(chainId: number): Promise<boolean>;
switchChain(chainId: number): Promise<Signer | undefined>;
connect(wallet?: Wallet | undefined): Promise<void>;
}

Expand Down

0 comments on commit 2a708c2

Please sign in to comment.