The NEAR Lightweight Block Explorer now includes robust RPC failover functionality with automatic retry logic and intelligent provider management. This feature ensures high availability by automatically switching between multiple RPC endpoints when failures occur.
- Seamlessly switches between RPC providers on network failures
- Maintains service continuity without user intervention
- Smart error detection distinguishes network errors from RPC errors
- 3 retry attempts per provider with exponential backoff
- Backoff timing: 100ms → 300ms → 900ms
- Prevents overwhelming providers with rapid requests
- Fetches latest provider list from GitHub on startup
- Falls back to built-in provider list if fetch fails
- Supports custom RPC endpoints (e.g., localhost:3030)
- Visual provider management interface
- Enable/disable providers by network type
- Test provider connectivity in real-time
- Reorder providers to set priority
- Add custom RPC endpoints
-
Provider Manager (
lib/providerManager.ts)- Manages the list of available RPC providers
- Handles localStorage persistence
- Fetches and parses providers from GitHub
- Tracks provider health status
-
Failover RPC Client (
lib/nearRpcFailover.ts)- Drop-in replacement for the original
NearRpcClient - Implements retry logic with exponential backoff
- Smart error detection (network vs RPC errors)
- Event system for UI notifications
- Drop-in replacement for the original
-
Settings Page (
pages/Settings.tsx)- User interface for provider management
- Provider testing and health monitoring
- Custom endpoint configuration
-
Access Settings
- Navigate to
/settingsor click "Settings" in the navigation menu
- Navigate to
-
Select Providers
- Enable/disable individual providers using checkboxes
- Use "Select All" buttons for bulk operations by network type
- Drag providers or use ↑↓ buttons to set priority
-
Add Custom Endpoints
- Enter a name and URL for your custom RPC endpoint
- Common example:
Localhost:3030→http://localhost:3030 - Click "Add Provider" to save
-
Test Providers
- Click "Test" button next to any provider
- View response time and health status
- Green badge = healthy, Red badge = failed
-
Refresh Providers
- Click "Refresh from GitHub" to fetch latest provider list
- Preserves your enabled/disabled selections
The failover client is a drop-in replacement:
import { nearRpc } from '@/lib/nearRpcFailover';
// All methods work the same as before
const status = await nearRpc.getStatus();
const block = await nearRpc.getBlock(12345);
const latestBlock = await nearRpc.getLatestBlock();import { nearRpc } from '@/lib/nearRpcFailover';
const unsubscribe = nearRpc.onFailoverEvent((event) => {
switch (event.type) {
case 'provider-switch':
console.log(`Switched to ${event.providerUrl}`);
break;
case 'retry':
console.log(`Retrying... attempt ${event.attempt}`);
break;
case 'error':
console.error(`Provider error: ${event.error}`);
break;
case 'success':
console.log(`Request succeeded via ${event.providerUrl}`);
break;
}
});
// Clean up when done
unsubscribe();import { nearRpc } from '@/lib/nearRpcFailover';
// Get current provider info
const { provider, health } = nearRpc.getCurrentProviderInfo();
console.log(`Using: ${provider?.name} (${provider?.url})`);
// Manually select a provider
nearRpc.selectProvider('mainnet-fastnear');import { providerManager } from '@/lib/providerManager';
// Get all providers
const allProviders = providerManager.getAllProviders();
// Get enabled providers only
const enabled = providerManager.getEnabledProviders();
// Filter by network
const mainnetProviders = providerManager.getProvidersByNetwork('mainnet');
// Add custom provider
const newProvider = providerManager.addCustomProvider(
'My Local Node',
'http://localhost:3030'
);
// Toggle provider
providerManager.toggleProvider('mainnet-fastnear', true);
// Enable all in network
providerManager.enableAllInNetwork('testnet');
// Test a provider
const health = await providerManager.testProvider('mainnet-near-official');
console.log(`Health: ${health.isHealthy}, Response: ${health.responseTime}ms`);Failover is triggered only for network-related errors:
- Connection timeout
- Network unreachable
- DNS resolution failures
- HTTP 502, 503, 504 errors
- CORS errors
RPC-level errors do not trigger failover:
- Transaction not found
- Invalid parameters
- Account doesn't exist
- Block not found
These are valid responses from the server and indicate the query itself was the issue, not the provider.
For a request with 3 enabled providers:
- Provider A - Attempt 1 → Failed (network error)
- Provider A - Attempt 2 (after 100ms) → Failed
- Provider A - Attempt 3 (after 300ms) → Failed
- Provider B - Attempt 1 → Failed
- Provider B - Attempt 2 (after 100ms) → Failed
- Provider B - Attempt 3 (after 300ms) → Failed
- Provider C - Attempt 1 → Success! ✓
Total time: ~2.7 seconds (worst case with 3 providers)
- NEAR Official (
https://rpc.mainnet.near.org) - FastNEAR (
https://free.rpc.fastnear.com) - Default enabled - Pagoda (
https://rpc.mainnet.pagoda.co) - Aurora (
https://mainnet.aurora.dev) - Lava Network (
https://near.lava.build)
- NEAR Testnet Official (
https://rpc.testnet.near.org) - Default enabled - FastNEAR Testnet (
https://test.rpc.fastnear.com) - Pagoda Testnet (
https://rpc.testnet.pagoda.co)
- Localhost:3030 (
http://localhost:3030) - Default enabled
Providers are automatically fetched from:
https://raw.githubusercontent.com/near/docs/master/docs/api/rpc/providers.md
The system parses the markdown file to extract:
- Provider names
- RPC URLs
- Network classifications (mainnet/testnet)
Configuration is persisted in localStorage:
near_rpc_providers- Base provider listnear_rpc_custom_providers- User-added custom providersnear_rpc_enabled_providers- List of enabled provider IDs
To reset all settings:
- Go to Settings page
- Click "Reset to Defaults"
- Or clear localStorage and refresh
Symptom: Error message "All RPC providers failed"
Solutions:
- Check your internet connection
- Enable more providers in Settings
- Test each provider to identify issues
- Add a custom localhost provider if running a local node
Symptom: Requests taking a long time
Solutions:
- Reorder providers to prioritize faster ones
- Test providers and disable slow/unhealthy ones
- Reduce number of enabled providers
- Use a local node for development
Symptom: Expected provider missing from list
Solutions:
- Click "Refresh from GitHub" to update provider list
- Check if it's a custom provider that needs to be manually added
- Verify the provider exists in the NEAR docs
Symptom: Settings reset after page reload
Solutions:
- Check if browser localStorage is enabled
- Verify you're not in private/incognito mode
- Check browser console for localStorage errors
Potential improvements for future versions:
- Provider response time tracking and auto-optimization
- Weighted failover based on historical reliability
- Circuit breaker pattern for persistently failing providers
- Provider discovery via DNS or other protocols
- Rate limit detection and handling
- Geographic provider selection
- Provider benchmarking tools
This implementation is inspired by the official NEAR RPC failover example: https://github.com/near-examples/near-api-examples/blob/main/javascript/examples/rpc-failover.js
For issues or questions:
- Check the NEAR Docs
- Review provider status at individual provider websites
- Test providers using the Settings page
- Check browser console for detailed error messages