Overview
Switching chains allows your application to interact with different blockchain networks without requiring users to manually change networks. This is particularly useful for:- Multi-chain dApps: Support operations across multiple networks
- Network optimization: Move to cheaper or faster chains for specific operations
- Feature availability: Access chain-specific protocols and features
- User experience: Seamlessly switch networks based on app context
React SDK
Basic Usage
To switch the network of an embedded wallet, use theswitchChain method from the useSwitchChain hook:
- Using useSwitchChain
- Using Wallet Method
Chain ID Formats
TheswitchChain method accepts chain IDs in two formats:
Using decimal numbers is recommended for better readability and consistency across your codebase.
Supported Chains
MoonKey supports switching to any EVM-compatible chain that has been configured in your app. Common networks include:Learn how to configure additional networks in the Configuring EVM Networks guide.
Complete Examples
Multi-Chain Network Selector
Network Switch with Confirmation
Error Handling
Handle common errors when switching chains:Getting Current Chain
To get the current chain ID of the wallet:Best Practices
Configure chains before switching
Configure chains before switching
Ensure chains are properly configured in your
MoonKeyProvider:Provide clear user feedback
Provide clear user feedback
Always inform users when switching networks:
Handle chain-specific features gracefully
Handle chain-specific features gracefully
Check chain compatibility before enabling features:
Use descriptive chain names
Use descriptive chain names
Display human-readable names instead of chain IDs:
Validate chain before operations
Validate chain before operations
Ensure correct network before executing transactions:
Handle network switch failures gracefully
Handle network switch failures gracefully
Provide fallback options when switching fails:
Troubleshooting
Chain not configured error
Chain not configured error
If you get a “chain not configured” error, add the chain to your configuration:See Configuring EVM Networks for more details.
Switch not taking effect
Switch not taking effect
If the network doesn’t seem to switch:
- Check wallet state: Ensure wallet is connected
- Wait for promise: Make sure to await the
switchChaincall - Verify chain support: Check if the chain is configured
- Check for errors: Look for error messages in console
Multiple rapid switches
Multiple rapid switches
Avoid switching chains multiple times in quick succession:
Hex vs decimal chain IDs
Hex vs decimal chain IDs
If switching with hex strings isn’t working, use decimal numbers:
Related Documentation
Configure EVM Networks
Add and configure custom EVM networks
Send Transaction
Send transactions on different chains
Get User Wallets
Access wallet information and chain details
Viem Chains
Explore available chain configurations
Next Steps
- Learn how to configure custom EVM networks
- Understand multi-chain wallet management
- Explore chain-specific RPC configurations
- Implement cross-chain transaction flows