feat: Helius primary + Alchemy fallback for trade execution

- Helius HTTPS: Primary RPC for Drift SDK initialization and subscriptions
- Alchemy HTTPS (10K CU/s): Fallback RPC for transaction confirmations
- Added getFallbackConnection() method to DriftService
- openPosition() and closePosition() now use Alchemy for tx confirmations
- accountSubscribe errors are non-fatal warnings (SDK falls back gracefully)
- System fully operational: Drift initialized, Position Manager ready
- Trade execution will use high-throughput Alchemy for confirmations
This commit is contained in:
mindesbunister
2025-11-14 16:51:14 +01:00
parent 1cf5c9aba1
commit 6445a135a8
3 changed files with 35 additions and 23 deletions

26
.env
View File

@@ -31,26 +31,16 @@ API_SECRET_KEY=2a344f0149442c857fb56c038c0c7d1b113883b830bec792c76f1e0efa15d6bb
# Solana RPC URL (Required for blockchain access) # Solana RPC URL (Required for blockchain access)
# #
# CRITICAL: Primary RPC for normal trading operations # PRIMARY: Helius WebSocket RPC (supports accountSubscribe for Drift SDK)
# Current: Alchemy (300M compute units/month free tier, excellent stability) SOLANA_RPC_URL=https://mainnet.helius-rpc.com/?api-key=5e236449-f936-4af7-ae38-f15e2f1a3757
SOLANA_RPC_URL=https://solana-mainnet.g.alchemy.com/v2/5A0iA5UYpsmP9gkuezYeg
# Fallback RPC URL (Optional but HIGHLY recommended) # Fallback RPC URL (used for trade execution - higher throughput)
# # Helius: startup/subscriptions, Alchemy: trade execution
# SMART STRATEGY (DISABLED - Helius API key issue): SOLANA_FALLBACK_RPC_URL=https://solana-mainnet.g.alchemy.com/v2/5A0iA5UYpsmP9gkuezYeg
# 1. Bot STARTS with fallback (Helius) - handles startup burst better (10 req/sec sustained)
# 2. After init, SWITCHES to primary (Alchemy) - more stable for trading operations
# 3. On 429 errors, falls back to Helius temporarily, then returns to Alchemy
#
# TEMPORARILY DISABLED: Set to empty until Helius API key fixed
# SOLANA_FALLBACK_RPC_URL=https://mainnet.helius-rpc.com/?api-key=dcca4bf0-0b91-4f6a-8d12-1c5a4c1c6e5b
SOLANA_FALLBACK_RPC_URL=
# RPC Provider Comparison (as of Nov 2025): # Alternative RPC providers:
# ✅ Alchemy: 300M CU/month, excellent for trading operations (PRIMARY) # QuickNode: https://solana-mainnet.quiknode.pro/YOUR_ENDPOINT/
# ✅ Helius: 10 req/sec sustained, perfect for startup bursts (STARTUP + FALLBACK) # Public (not recommended): https://api.mainnet-beta.solana.com
# QuickNode: Paid plans, very reliable
# Ankr/Public: Unreliable, not recommended
# ================================ # ================================
# REQUIRED - PYTH NETWORK (Price Feeds) # REQUIRED - PYTH NETWORK (Price Feeds)

View File

@@ -470,6 +470,13 @@ export class DriftService {
return this.connection return this.connection
} }
/**
* Get fallback connection (Alchemy for trade execution)
*/
getFallbackConnection(): Connection | null {
return this.fallbackConnection || null
}
/** /**
* Get user instance * Get user instance
*/ */

View File

@@ -146,8 +146,16 @@ export async function openPosition(
console.log(`📝 Transaction submitted: ${txSig}`) console.log(`📝 Transaction submitted: ${txSig}`)
// CRITICAL: Confirm transaction actually executed on-chain // CRITICAL: Confirm transaction actually executed on-chain
console.log('⏳ Confirming transaction on-chain...') // Use Alchemy (fallback RPC) for confirmation - higher throughput
const connection = driftService.getConnection() console.log('⏳ Confirming transaction on-chain (using Alchemy for high throughput)...')
const alchemyConnection = driftService.getFallbackConnection()
const connection = alchemyConnection || driftService.getConnection()
if (alchemyConnection) {
console.log('✅ Using Alchemy RPC for transaction confirmation')
} else {
console.log('⚠️ No fallback RPC available, using primary RPC')
}
try { try {
const confirmation = await connection.confirmTransaction(txSig, 'confirmed') const confirmation = await connection.confirmTransaction(txSig, 'confirmed')
@@ -557,9 +565,16 @@ export async function closePosition(
console.log(`✅ Close order placed! Transaction: ${txSig}`) console.log(`✅ Close order placed! Transaction: ${txSig}`)
// CRITICAL: Confirm transaction on-chain to prevent phantom closes // CRITICAL: Confirm transaction on-chain to prevent phantom closes
// BUT: Use timeout to prevent API hangs during network congestion // Use Alchemy (fallback RPC) for confirmation - higher throughput
console.log('⏳ Confirming transaction on-chain (30s timeout)...') console.log('⏳ Confirming transaction on-chain (using Alchemy, 30s timeout)...')
const connection = driftService.getConnection() const alchemyConnection = driftService.getFallbackConnection()
const connection = alchemyConnection || driftService.getConnection()
if (alchemyConnection) {
console.log('✅ Using Alchemy RPC for transaction confirmation')
} else {
console.log('⚠️ No fallback RPC available, using primary RPC')
}
try { try {
const confirmationPromise = connection.confirmTransaction(txSig, 'confirmed') const confirmationPromise = connection.confirmTransaction(txSig, 'confirmed')