solana-pipkit 2.2.0

Pragmatic Rust utilities for Solana development - safety, speed, analytics, and DeFi
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//! High-performance RPC client with connection pooling and parallel requests.
//!
//! This module provides an optimized RPC client designed for trading agents
//! that require minimal latency and maximum reliability.
//!
//! # Features
//! - Connection pooling across multiple endpoints
//! - Automatic failover and retry logic
//! - Latency-based load balancing
//! - Parallel account fetching
//! - Support for premium RPC providers (Helius, Triton, QuickNode)
//!
//! # Example
//! ```rust,no_run
//! use solana_pipkit::speed::{FastRpcClient, LoadBalanceStrategy};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let endpoints = vec![
//!         "https://api.mainnet-beta.solana.com".to_string(),
//!         "https://solana-api.projectserum.com".to_string(),
//!     ];
//!
//!     let mut client = FastRpcClient::new(endpoints);
//!     client.set_strategy(LoadBalanceStrategy::LeastLatency);
//!
//!     // Benchmark and sort by latency
//!     let health = client.benchmark_endpoints().await;
//!     println!("Fastest endpoint: {} ({}ms)", health[0].url, health[0].latency_ms);
//!
//!     // Fast slot fetch
//!     let slot = client.get_slot_fast().await?;
//!     println!("Current slot: {}", slot);
//!
//!     Ok(())
//! }
//! ```

use crate::{Result, ToolkitError};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::{
    account::Account,
    clock::Slot,
    commitment_config::CommitmentConfig,
    pubkey::Pubkey,
    signature::Signature,
    transaction::Transaction,
};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Load balancing strategy for RPC endpoint selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LoadBalanceStrategy {
    /// Round-robin across all endpoints.
    #[default]
    RoundRobin,
    /// Select endpoint with lowest average latency.
    LeastLatency,
    /// Random endpoint selection.
    Random,
    /// Weighted random based on endpoint weights.
    Weighted,
}

/// RPC endpoint with performance tracking.
pub struct RpcEndpoint {
    /// Endpoint URL.
    pub url: String,
    /// Weight for weighted load balancing (1-100).
    pub weight: u8,
    /// Average latency in milliseconds (rolling average).
    pub avg_latency_ms: AtomicU64,
    /// Request count for this endpoint.
    pub request_count: AtomicU64,
    /// Error count for this endpoint.
    pub error_count: AtomicU64,
    /// Whether endpoint is currently healthy.
    pub healthy: std::sync::atomic::AtomicBool,
    /// RPC client instance.
    client: RpcClient,
}

impl std::fmt::Debug for RpcEndpoint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RpcEndpoint")
            .field("url", &self.url)
            .field("weight", &self.weight)
            .field("avg_latency_ms", &self.avg_latency_ms.load(Ordering::Relaxed))
            .field("request_count", &self.request_count.load(Ordering::Relaxed))
            .field("error_count", &self.error_count.load(Ordering::Relaxed))
            .field("healthy", &self.healthy.load(Ordering::Relaxed))
            .finish()
    }
}

impl RpcEndpoint {
    /// Create a new endpoint with default weight.
    pub fn new(url: &str) -> Self {
        Self::with_weight(url, 50)
    }

    /// Create a new endpoint with custom weight.
    pub fn with_weight(url: &str, weight: u8) -> Self {
        Self {
            url: url.to_string(),
            weight: weight.clamp(1, 100),
            avg_latency_ms: AtomicU64::new(0),
            request_count: AtomicU64::new(0),
            error_count: AtomicU64::new(0),
            healthy: std::sync::atomic::AtomicBool::new(true),
            client: RpcClient::new_with_commitment(
                url.to_string(),
                CommitmentConfig::confirmed(),
            ),
        }
    }

    /// Update latency with exponential moving average.
    fn update_latency(&self, latency_ms: u64) {
        let current = self.avg_latency_ms.load(Ordering::Relaxed);
        let new_avg = if current == 0 {
            latency_ms
        } else {
            // EMA with alpha = 0.3
            (current * 7 + latency_ms * 3) / 10
        };
        self.avg_latency_ms.store(new_avg, Ordering::Relaxed);
    }

    /// Record a successful request.
    fn record_success(&self, latency_ms: u64) {
        self.request_count.fetch_add(1, Ordering::Relaxed);
        self.update_latency(latency_ms);
        self.healthy.store(true, Ordering::Relaxed);
    }

    /// Record a failed request.
    fn record_error(&self) {
        self.request_count.fetch_add(1, Ordering::Relaxed);
        self.error_count.fetch_add(1, Ordering::Relaxed);

        // Mark unhealthy if error rate exceeds 50%
        let total = self.request_count.load(Ordering::Relaxed);
        let errors = self.error_count.load(Ordering::Relaxed);
        if total > 10 && errors * 2 > total {
            self.healthy.store(false, Ordering::Relaxed);
        }
    }
}

/// Premium RPC provider API keys.
#[derive(Debug, Clone, Default)]
pub struct PremiumRpcKeys {
    /// Helius API key.
    pub helius: Option<String>,
    /// Triton API key.
    pub triton: Option<String>,
    /// QuickNode API key.
    pub quicknode: Option<String>,
    /// Alchemy API key.
    pub alchemy: Option<String>,
}

impl PremiumRpcKeys {
    /// Create new empty keys.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set Helius API key.
    pub fn with_helius(mut self, key: impl Into<String>) -> Self {
        self.helius = Some(key.into());
        self
    }

    /// Set Triton API key.
    pub fn with_triton(mut self, key: impl Into<String>) -> Self {
        self.triton = Some(key.into());
        self
    }

    /// Set QuickNode API key.
    pub fn with_quicknode(mut self, key: impl Into<String>) -> Self {
        self.quicknode = Some(key.into());
        self
    }

    /// Set Alchemy API key.
    pub fn with_alchemy(mut self, key: impl Into<String>) -> Self {
        self.alchemy = Some(key.into());
        self
    }

    /// Get all configured endpoint URLs.
    pub fn get_endpoints(&self) -> Vec<String> {
        let mut endpoints = Vec::new();

        if let Some(key) = &self.helius {
            endpoints.push(format!("https://mainnet.helius-rpc.com/?api-key={}", key));
        }

        if let Some(key) = &self.triton {
            endpoints.push(format!("https://rpc.triton.one/?api-key={}", key));
        }

        if let Some(key) = &self.quicknode {
            // QuickNode URLs are typically full URLs with the key embedded
            endpoints.push(key.clone());
        }

        if let Some(key) = &self.alchemy {
            endpoints.push(format!("https://solana-mainnet.g.alchemy.com/v2/{}", key));
        }

        endpoints
    }
}

/// Endpoint health status.
#[derive(Debug, Clone)]
pub struct EndpointHealth {
    /// Endpoint URL.
    pub url: String,
    /// Measured latency in milliseconds.
    pub latency_ms: u64,
    /// Whether endpoint responded successfully.
    pub healthy: bool,
    /// Current slot from endpoint.
    pub slot: u64,
    /// Error message if unhealthy.
    pub error: Option<String>,
}

/// High-performance RPC client with connection pooling.
pub struct FastRpcClient {
    /// RPC endpoints pool.
    endpoints: Vec<Arc<RpcEndpoint>>,
    /// Current endpoint index for round-robin.
    current: AtomicUsize,
    /// Load balancing strategy.
    strategy: RwLock<LoadBalanceStrategy>,
    /// Maximum retry attempts.
    max_retries: usize,
    /// Timeout for single requests.
    timeout: Duration,
}

impl FastRpcClient {
    /// Create with multiple RPC endpoints for redundancy and speed.
    pub fn new(endpoint_urls: Vec<String>) -> Self {
        let endpoints = endpoint_urls
            .into_iter()
            .map(|url| Arc::new(RpcEndpoint::new(&url)))
            .collect();

        Self {
            endpoints,
            current: AtomicUsize::new(0),
            strategy: RwLock::new(LoadBalanceStrategy::RoundRobin),
            max_retries: 3,
            timeout: Duration::from_secs(10),
        }
    }

    /// Create with premium RPC endpoints.
    pub fn with_premium_endpoints(api_keys: PremiumRpcKeys) -> Self {
        let mut endpoints = api_keys.get_endpoints();

        // Add public fallbacks
        endpoints.push("https://api.mainnet-beta.solana.com".to_string());

        Self::new(endpoints)
    }

    /// Create with single endpoint.
    pub fn single(url: &str) -> Self {
        Self::new(vec![url.to_string()])
    }

    /// Set load balancing strategy.
    pub fn set_strategy(&self, strategy: LoadBalanceStrategy) {
        let mut guard = self.strategy.blocking_write();
        *guard = strategy;
    }

    /// Set maximum retry attempts.
    pub fn set_max_retries(&mut self, retries: usize) {
        self.max_retries = retries;
    }

    /// Set request timeout.
    pub fn set_timeout(&mut self, timeout: Duration) {
        self.timeout = timeout;
    }

    /// Get number of configured endpoints.
    pub fn endpoint_count(&self) -> usize {
        self.endpoints.len()
    }

    /// Select next endpoint based on strategy.
    async fn select_endpoint(&self) -> Arc<RpcEndpoint> {
        let strategy = *self.strategy.read().await;

        // Filter healthy endpoints
        let healthy: Vec<_> = self.endpoints
            .iter()
            .filter(|e| e.healthy.load(Ordering::Relaxed))
            .cloned()
            .collect();

        let candidates = if healthy.is_empty() {
            // Fall back to all endpoints if none healthy
            &self.endpoints
        } else {
            &healthy
        };

        match strategy {
            LoadBalanceStrategy::RoundRobin => {
                let idx = self.current.fetch_add(1, Ordering::Relaxed) % candidates.len();
                candidates[idx].clone()
            }
            LoadBalanceStrategy::LeastLatency => {
                candidates
                    .iter()
                    .min_by_key(|e| {
                        let latency = e.avg_latency_ms.load(Ordering::Relaxed);
                        if latency == 0 { u64::MAX / 2 } else { latency }
                    })
                    .cloned()
                    .unwrap_or_else(|| candidates[0].clone())
            }
            LoadBalanceStrategy::Random => {
                use std::collections::hash_map::RandomState;
                use std::hash::{BuildHasher, Hasher};
                let idx = RandomState::new().build_hasher().finish() as usize % candidates.len();
                candidates[idx].clone()
            }
            LoadBalanceStrategy::Weighted => {
                // Simple weighted selection
                let total_weight: u64 = candidates.iter().map(|e| e.weight as u64).sum();
                use std::collections::hash_map::RandomState;
                use std::hash::{BuildHasher, Hasher};
                let mut target = (RandomState::new().build_hasher().finish() % total_weight) as u64;

                for endpoint in candidates {
                    if target < endpoint.weight as u64 {
                        return endpoint.clone();
                    }
                    target -= endpoint.weight as u64;
                }
                candidates[0].clone()
            }
        }
    }

    /// Execute request with retry and failover.
    async fn execute_with_retry<T, F, Fut>(&self, operation: F) -> Result<T>
    where
        F: Fn(Arc<RpcEndpoint>) -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        let mut last_error = ToolkitError::NetworkError("No endpoints available".to_string());
        let mut tried_endpoints = Vec::new();

        for _ in 0..self.max_retries {
            let endpoint = self.select_endpoint().await;

            // Skip if we already tried this endpoint
            if tried_endpoints.contains(&endpoint.url) && tried_endpoints.len() < self.endpoints.len() {
                continue;
            }
            tried_endpoints.push(endpoint.url.clone());

            let start = Instant::now();

            match tokio::time::timeout(self.timeout, operation(endpoint.clone())).await {
                Ok(Ok(result)) => {
                    endpoint.record_success(start.elapsed().as_millis() as u64);
                    return Ok(result);
                }
                Ok(Err(e)) => {
                    endpoint.record_error();
                    last_error = e;
                }
                Err(_) => {
                    endpoint.record_error();
                    last_error = ToolkitError::Timeout(format!(
                        "Request to {} timed out",
                        endpoint.url
                    ));
                }
            }
        }

        Err(last_error)
    }

    /// Send transaction with automatic retry and endpoint failover.
    pub async fn send_transaction_fast(&self, transaction: &Transaction) -> Result<Signature> {
        let tx = transaction.clone();
        self.execute_with_retry(|endpoint| {
            let tx = tx.clone();
            async move {
                endpoint
                    .client
                    .send_and_confirm_transaction(&tx)
                    .await
                    .map_err(|e| ToolkitError::TransactionError(e.to_string()))
            }
        })
        .await
    }

    /// Send transaction without confirmation (faster).
    pub async fn send_transaction_no_confirm(&self, transaction: &Transaction) -> Result<Signature> {
        let tx = transaction.clone();
        self.execute_with_retry(|endpoint| {
            let tx = tx.clone();
            async move {
                endpoint
                    .client
                    .send_transaction(&tx)
                    .await
                    .map_err(|e| ToolkitError::TransactionError(e.to_string()))
            }
        })
        .await
    }

    /// Get multiple accounts in parallel batches.
    pub async fn get_multiple_accounts_fast(
        &self,
        pubkeys: &[Pubkey],
    ) -> Result<Vec<Option<Account>>> {
        if pubkeys.is_empty() {
            return Ok(vec![]);
        }

        // Solana limits to 100 accounts per request
        const BATCH_SIZE: usize = 100;

        let batches: Vec<_> = pubkeys.chunks(BATCH_SIZE).collect();
        let mut handles = Vec::with_capacity(batches.len());

        for batch in batches {
            let batch = batch.to_vec();
            let endpoint = self.select_endpoint().await;

            handles.push(tokio::spawn(async move {
                let start = Instant::now();
                let result = endpoint.client.get_multiple_accounts(&batch).await;

                match &result {
                    Ok(_) => endpoint.record_success(start.elapsed().as_millis() as u64),
                    Err(_) => endpoint.record_error(),
                }

                result.map_err(|e| ToolkitError::RpcError(e))
            }));
        }

        let mut all_accounts = Vec::with_capacity(pubkeys.len());

        for handle in handles {
            let accounts = handle
                .await
                .map_err(|e| ToolkitError::Custom(e.to_string()))??;
            all_accounts.extend(accounts);
        }

        Ok(all_accounts)
    }

    /// Get slot with lowest latency endpoint.
    pub async fn get_slot_fast(&self) -> Result<Slot> {
        self.execute_with_retry(|endpoint| async move {
            endpoint
                .client
                .get_slot()
                .await
                .map_err(|e| ToolkitError::RpcError(e))
        })
        .await
    }

    /// Get latest blockhash with retry.
    pub async fn get_latest_blockhash_fast(&self) -> Result<solana_sdk::hash::Hash> {
        self.execute_with_retry(|endpoint| async move {
            endpoint
                .client
                .get_latest_blockhash()
                .await
                .map_err(|e| ToolkitError::RpcError(e))
        })
        .await
    }

    /// Get account info with retry.
    pub async fn get_account_fast(&self, pubkey: &Pubkey) -> Result<Account> {
        let pk = *pubkey;
        self.execute_with_retry(|endpoint| async move {
            endpoint
                .client
                .get_account(&pk)
                .await
                .map_err(|e| ToolkitError::RpcError(e))
        })
        .await
    }

    /// Get balance with retry.
    pub async fn get_balance_fast(&self, pubkey: &Pubkey) -> Result<u64> {
        let pk = *pubkey;
        self.execute_with_retry(|endpoint| async move {
            endpoint
                .client
                .get_balance(&pk)
                .await
                .map_err(|e| ToolkitError::RpcError(e))
        })
        .await
    }

    /// Health check all endpoints, sort by latency.
    pub async fn benchmark_endpoints(&self) -> Vec<EndpointHealth> {
        let mut handles = Vec::with_capacity(self.endpoints.len());

        for endpoint in &self.endpoints {
            let endpoint = endpoint.clone();
            handles.push(tokio::spawn(async move {
                let start = Instant::now();

                match tokio::time::timeout(
                    Duration::from_secs(5),
                    endpoint.client.get_slot(),
                ).await {
                    Ok(Ok(slot)) => {
                        let latency = start.elapsed().as_millis() as u64;
                        endpoint.record_success(latency);
                        EndpointHealth {
                            url: endpoint.url.clone(),
                            latency_ms: latency,
                            healthy: true,
                            slot,
                            error: None,
                        }
                    }
                    Ok(Err(e)) => {
                        endpoint.record_error();
                        EndpointHealth {
                            url: endpoint.url.clone(),
                            latency_ms: u64::MAX,
                            healthy: false,
                            slot: 0,
                            error: Some(e.to_string()),
                        }
                    }
                    Err(_) => {
                        endpoint.record_error();
                        EndpointHealth {
                            url: endpoint.url.clone(),
                            latency_ms: u64::MAX,
                            healthy: false,
                            slot: 0,
                            error: Some("Timeout".to_string()),
                        }
                    }
                }
            }));
        }

        let mut results = Vec::with_capacity(handles.len());
        for handle in handles {
            if let Ok(health) = handle.await {
                results.push(health);
            }
        }

        // Sort by latency (healthy first, then by latency)
        results.sort_by(|a, b| {
            match (a.healthy, b.healthy) {
                (true, false) => std::cmp::Ordering::Less,
                (false, true) => std::cmp::Ordering::Greater,
                _ => a.latency_ms.cmp(&b.latency_ms),
            }
        });

        results
    }

    /// Get the underlying RPC client for the best endpoint.
    pub async fn get_best_client(&self) -> &RpcClient {
        // Find endpoint with lowest latency
        let best = self.endpoints
            .iter()
            .filter(|e| e.healthy.load(Ordering::Relaxed))
            .min_by_key(|e| e.avg_latency_ms.load(Ordering::Relaxed))
            .unwrap_or(&self.endpoints[0]);

        &best.client
    }

    /// Reset all endpoint statistics.
    pub fn reset_stats(&self) {
        for endpoint in &self.endpoints {
            endpoint.request_count.store(0, Ordering::Relaxed);
            endpoint.error_count.store(0, Ordering::Relaxed);
            endpoint.avg_latency_ms.store(0, Ordering::Relaxed);
            endpoint.healthy.store(true, Ordering::Relaxed);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_premium_rpc_keys() {
        let keys = PremiumRpcKeys::new()
            .with_helius("test-helius-key")
            .with_triton("test-triton-key");

        let endpoints = keys.get_endpoints();
        assert_eq!(endpoints.len(), 2);
        assert!(endpoints[0].contains("helius"));
        assert!(endpoints[1].contains("triton"));
    }

    #[test]
    fn test_endpoint_latency_update() {
        let endpoint = RpcEndpoint::new("https://test.com");

        // First update sets the value directly
        endpoint.update_latency(100);
        assert_eq!(endpoint.avg_latency_ms.load(Ordering::Relaxed), 100);

        // Subsequent updates use EMA
        endpoint.update_latency(200);
        let avg = endpoint.avg_latency_ms.load(Ordering::Relaxed);
        assert!(avg > 100 && avg < 200);
    }

    #[test]
    fn test_fast_rpc_client_creation() {
        let client = FastRpcClient::new(vec![
            "https://endpoint1.com".to_string(),
            "https://endpoint2.com".to_string(),
        ]);

        assert_eq!(client.endpoint_count(), 2);
    }

    #[test]
    fn test_single_endpoint() {
        let client = FastRpcClient::single("https://api.mainnet-beta.solana.com");
        assert_eq!(client.endpoint_count(), 1);
    }

    #[tokio::test]
    async fn test_strategy_setting() {
        let client = FastRpcClient::single("https://test.com");

        // Set strategy using the blocking method
        {
            let mut guard = client.strategy.write().await;
            *guard = LoadBalanceStrategy::LeastLatency;
        }

        let strategy = *client.strategy.read().await;
        assert_eq!(strategy, LoadBalanceStrategy::LeastLatency);
    }
}