This document describes the implementation of runtime network switching for the Stellar Forge dApp, allowing users to toggle between testnet and mainnet without reloading the page.
- Modified
StellarServiceclass to accept and store network as a constructor parameter - Added
setNetwork(network)method to update the active network - Updated all network-dependent functions to accept network parameter:
getNetworkConfig(network)getNetworkPassphrase(network)getRpcServer(network)getRpcUrl(network)
- Updated transaction functions to pass network through the call chain:
simulateAndSubmit(server, tx, network)buildTxBuilder(server, sourceAddress, network)callView(..., network)rpcCall(..., network)buildFeeBumpTransaction(..., network)submitFeeBumpTransaction(..., network)
- Modified
signTransaction(xdr, network)to accept network parameter - Modified
getBalance(address, network)to accept network parameter - Both methods now use the provided network to determine correct Horizon/Freighter configuration
- Imported
NETWORK_CONFIGSfor direct network config access
- Added
useNetwork()hook to access current network from NetworkContext - Changed
useMemodependency array from[]to[network] - StellarService is now recreated whenever network changes
- This ensures all contract calls use the correct RPC endpoint
- Added
useNetwork()hook to access current network - Added
useEffectto refresh wallet balance when network changes fetchBalance()now passes network towalletService.getBalance()- Balance automatically updates when user switches networks
- Added testnet emoji badge (🧪) to visually distinguish testnet
- Improved UI with better visual feedback
- Maintained mainnet confirmation dialog for safety
- Added LABELS constant for network display names
- Dropdown shows current network with checkmark
- Added missing imports:
StellarProvider,Routes,Route,Navigate,useState - Added
showFriendbotBannerstate for testnet banner - Properly structured provider hierarchy
ErrorBoundary
→ NetworkProvider (manages current network state + localStorage)
→ StellarProvider (recreates services on network change)
→ WalletProvider (refreshes balance on network change)
→ ToastProvider
→ TosProvider
→ AppContent
- User clicks NetworkSwitcher dropdown
- Selects new network (with mainnet confirmation)
NetworkContext.switchNetwork()updates state + localStorageStellarContextdetects network change via dependency array- New
StellarServiceinstance created with new network WalletContextdetects network change- Balance is refreshed using new network's Horizon URL
- All subsequent contract calls use new RPC endpoint
✅ Users can switch between testnet and mainnet from the UI without reloading
- NetworkSwitcher component in header allows instant switching
- State persists in localStorage
- No page reload required
✅ The correct Horizon and Soroban RPC URLs are used after switching
- StellarService recreated with new network
- WalletService methods accept network parameter
- All RPC calls use network-specific endpoints
✅ A 'TESTNET' badge is visible when on testnet
- NetworkSwitcher shows 🧪 emoji + "Testnet" label
- Yellow background color for testnet
- Green background color for mainnet
✅ A confirmation dialog is shown before switching to mainnet
- MainnetConfirmationModal appears when selecting mainnet
- Warning message about real funds
- User must explicitly confirm
- Switch from testnet to mainnet - confirmation dialog appears
- Switch from mainnet to testnet - no confirmation needed
- After switching, balance updates correctly
- Contract calls use correct RPC endpoint
- Network preference persists after page reload
- Testnet badge visible when on testnet
- Mainnet badge visible when on mainnet
- Wallet remains connected after network switch
- Freighter network mismatch error handled gracefully
- Add network indicator in transaction status
- Show network-specific contract addresses
- Add network-specific fee estimates
- Implement network health status indicator
- Add network-specific documentation links