The network switching feature is now fully integrated. Users can switch between testnet and mainnet from the UI header without reloading the page.
- Click the network badge in the top-right header (yellow for testnet, green for mainnet)
- Select desired network from dropdown
- If switching to mainnet, confirm the warning dialog
- Network switches instantly, balance updates automatically
import { useNetwork } from './context/NetworkContext'
function MyComponent() {
const { network, switchNetwork, rpcUrl, horizonUrl, networkPassphrase } = useNetwork()
// network: 'testnet' | 'mainnet'
// switchNetwork(network): void
// rpcUrl: string (Soroban RPC URL)
// horizonUrl: string (Horizon API URL)
// networkPassphrase: string (for transaction signing)
}import { useStellarContext } from './context/StellarContext'
function MyComponent() {
const { stellarService } = useStellarContext()
// stellarService is automatically network-aware
// It's recreated whenever network changes
const result = await stellarService.deployToken({...})
}import { walletService } from './services/wallet'
import { useNetwork } from './context/NetworkContext'
function MyComponent() {
const { network } = useNetwork()
// Pass network to wallet service methods
const balance = await walletService.getBalance(address, network)
const signedXdr = await walletService.signTransaction(xdr, network)
}- Network selection is stored in localStorage under key
stellarforge_network - Persists across page reloads
- Falls back to
VITE_NETWORKenv var if not set
StellarServiceis recreated when network changes (via StellarContext)- This ensures all RPC calls use the correct endpoint
- Wallet balance is refreshed automatically
- Network mismatch errors from Freighter are caught and displayed
- User is prompted to switch Freighter to the correct network
- Balance fetch failures are logged but don't break the app
# Start dev server
npm run dev
# In browser:
1. Connect wallet
2. Note current balance
3. Click network badge
4. Switch to different network
5. Verify balance updates
6. Try creating/minting tokens
7. Verify transactions use correct network// In browser console
localStorage.getItem('stellarforge_network') // 'testnet' or 'mainnet'Solution: Check that WalletContext has useNetwork() hook and balance refresh effect
Solution: Ensure walletService.signTransaction() receives correct network parameter
Solution: Verify StellarService is being recreated (check React DevTools Profiler)
src/services/stellar.ts- Network-aware RPC callssrc/services/wallet.ts- Network parameter in signing/balancesrc/context/StellarContext.tsx- Service recreation on network changesrc/context/WalletContext.tsx- Balance refresh on network changesrc/components/NetworkSwitcher.tsx- Enhanced UI with testnet badgesrc/App.tsx- Added missing imports
No new environment variables needed. Existing VITE_NETWORK is used as fallback.
- StellarService recreation is lightweight (just creates new instance)
- RPC Server instances are created on-demand (not cached)
- Balance refresh is debounced by React's effect cleanup
- No unnecessary re-renders due to proper dependency arrays
- Mainnet switching requires explicit user confirmation
- Network selection is client-side only (no server calls)
- Freighter handles actual network validation
- All transactions are signed by user's wallet