From 32283cd0557b8183b2cf670c5bd0f838b98316db Mon Sep 17 00:00:00 2001 From: ttang Date: Fri, 2 Feb 2024 21:06:50 +0800 Subject: [PATCH] fix: switch network --- .../social/farcaster/signupv2/AddAccountKey.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/u3/src/components/social/farcaster/signupv2/AddAccountKey.tsx b/apps/u3/src/components/social/farcaster/signupv2/AddAccountKey.tsx index 71b9e733..85fa2e71 100644 --- a/apps/u3/src/components/social/farcaster/signupv2/AddAccountKey.tsx +++ b/apps/u3/src/components/social/farcaster/signupv2/AddAccountKey.tsx @@ -5,11 +5,17 @@ import { optimism } from 'viem/chains'; import { toast } from 'react-toastify'; import { NobleEd25519Signer, ViemWalletEip712Signer } from '@farcaster/hub-web'; import { useConnectModal } from '@rainbow-me/rainbowkit'; -import { useAccount, usePublicClient, useWalletClient } from 'wagmi'; +import { + useAccount, + useNetwork, + usePublicClient, + useWalletClient, +} from 'wagmi'; import { waitForTransaction, writeContract, prepareWriteContract, + switchNetwork, } from '@wagmi/core'; import * as ed25519 from '@noble/ed25519'; @@ -37,6 +43,7 @@ export default function AddAccountKey({ const { address } = useAccount(); const account = useAccount(); const wallet = useWalletClient(); + const network = useNetwork(); const publicClient = usePublicClient({ chainId: optimism.id, @@ -59,6 +66,9 @@ export default function AddAccountKey({ setSigner(ed25519Signer); return; } + if (network.chain?.id !== optimism.id) { + await switchNetwork({ chainId: optimism.id }); + } // const privateKey = ed25519.utils.randomPrivateKey(); // const publicKey = toHex(ed25519.getPublicKey(privateKey)); const privateKey = ed25519.utils.randomPrivateKey(); @@ -118,7 +128,7 @@ export default function AddAccountKey({ console.error(error); toast.error(error.message); } - }, [fid, account, publicClient]); + }, [fid, account, publicClient, network]); return (