What you’ll build
AConnectWallet component with three states: not connected, connected to the wrong network, and connected to BOT Chain testnet.
Prerequisites
- A React app with wagmi set up. See Set up wagmi.
- A browser wallet such as MetaMask.
Steps
1
Write the component
src/ConnectWallet.tsx
useConnectionreplacesuseAccount, which is deprecated in wagmi 3.useConnectorslists the connectors from your config. Withinjected()you get one button per detected browser wallet.- Action hooks return a mutation. Call
.mutate(...), and read.isPendingand.errorfrom the same object. chainIdfromuseConnectionis the wallet’s chain, which can be one your app doesn’t support. That’s how you detect the wrong network.
2
Render it
src/App.tsx
Verify it worked
- Run
npm run devand open the app. You see a Connect button for your wallet. - Click it and approve in the wallet. If the wallet is on another network, you see Wrong network.
- Click Switch network. The wallet asks to switch, or to add BOT Chain Testnet if it doesn’t know it yet. Approve it.
- The component shows your address.
Troubleshooting
Switching fails with an unrecognized chain error
Switching fails with an unrecognized chain error
Some wallets can’t add chains from a dapp. Add BOT Chain Testnet by hand using the values in Connect a wallet, then try again.
User rejected the request
User rejected the request
The user closed or rejected the wallet popup. Nothing changed; they can click the button again.
Next steps
Read contract state
Show the connected user’s balances.
Send transactions
Write to a contract and wait for the receipt.