feat: Hybrid RPC fallback system (Alchemy → Helius)

- Automatic fallback after 2 consecutive rate limits
- Primary: Alchemy (300M CU/month, stable for normal ops)
- Fallback: Helius (10 req/sec, backup for startup bursts)
- Reduced startup validation: 6h window, 5 trades (was 24h, 20 trades)
- Multi-position safety check (prevents order cancellation conflicts)
- Rate limit-aware retry logic with exponential backoff

Implementation:
- lib/drift/client.ts: Added fallbackConnection, switchToFallbackRpc()
- .env: SOLANA_FALLBACK_RPC_URL configuration
- lib/startup/init-position-manager.ts: Reduced validation scope
- lib/trading/position-manager.ts: Multi-position order protection

Tested: System switched to fallback on startup, Position Manager active
Result: 1 active trade being monitored after automatic RPC switch
This commit is contained in:
mindesbunister
2025-11-14 15:28:07 +01:00
parent d5183514bc
commit 7ff78ee0bd
5 changed files with 193 additions and 50 deletions

20
.env
View File

@@ -31,16 +31,20 @@ API_SECRET_KEY=2a344f0149442c857fb56c038c0c7d1b113883b830bec792c76f1e0efa15d6bb
# Solana RPC URL (Required for blockchain access)
#
# RECOMMENDED: Helius (best performance, free tier available)
# Get free API key at: https://helius.dev
# CRITICAL: Primary RPC for all trading operations
# Current: Alchemy (300M compute units/month free tier)
SOLANA_RPC_URL=https://solana-mainnet.g.alchemy.com/v2/5A0iA5UYpsmP9gkuezYeg
# Alternative RPC providers (if not using Helius):
#
# QuickNode: https://solana-mainnet.quiknode.pro/YOUR_ENDPOINT/
# Alchemy: https://solana-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_KEY
# Ankr: https://rpc.ankr.com/solana
# Public (not recommended): https://api.mainnet-beta.solana.com
# Fallback RPC URL (Optional but HIGHLY recommended)
# Automatically switches to fallback after 2 consecutive rate limits
# Use a different provider than primary for best redundancy
SOLANA_FALLBACK_RPC_URL=https://mainnet.helius-rpc.com/v1/?api-key=dcca4bf0-0b91-4f6a-8d12-1c5a4c1c6e5b
# RPC Provider Comparison (as of Nov 2025):
# ✅ Alchemy: 300M CU/month, excellent for primary (CURRENT PRIMARY)
# ⚠️ Helius: 10 req/sec sustained (free tier), good for fallback only
# QuickNode: Paid plans, very reliable
# Ankr/Public: Unreliable, not recommended
# ================================
# REQUIRED - PYTH NETWORK (Price Feeds)