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
//! Optimized swap execution with DEX routing and priority fees.
//!
//! This module provides high-performance swap execution designed for trading agents
//! that require minimal latency between quote and landed transaction.
//!
//! # Features
//! - Fast swap execution with configurable priority fees
//! - Jito bundle support for guaranteed inclusion
//! - Direct route optimization for speed
//! - Automatic slippage calculation
//!
//! # Example
//! ```rust,no_run
//! use solana_pipkit::speed::{SwapExecutor, SwapConfig, PriorityLevel, FastRpcClient};
//! use solana_sdk::{signature::Keypair, pubkey::Pubkey};
//! use std::str::FromStr;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let rpc = FastRpcClient::single("https://api.mainnet-beta.solana.com");
//!     let payer = Keypair::new();
//!
//!     let executor = SwapExecutor::new(rpc, payer);
//!
//!     let usdc = Pubkey::from_str("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v")?;
//!     let sol = Pubkey::from_str("So11111111111111111111111111111111111111112")?;
//!
//!     let config = SwapConfig::fast();
//!     let result = executor.swap_fast(&usdc, &sol, 1_000_000, config).await?;
//!
//!     println!("Swap executed in {}ms", result.execution_time_ms);
//!
//!     Ok(())
//! }
//! ```

use crate::speed::{FastRpcClient, FastTransactionBuilder, JupiterApiClient, JupiterQuote};
use crate::{Result, ToolkitError};
use solana_sdk::{
    pubkey::Pubkey,
    signature::{Keypair, Signature},
    signer::Signer,
    transaction::VersionedTransaction,
};
use std::sync::Arc;
use std::time::Instant;

/// Priority fee level for transactions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PriorityLevel {
    /// No priority fee.
    None,
    /// Low priority (25th percentile).
    Low,
    /// Medium priority (50th percentile).
    #[default]
    Medium,
    /// High priority (75th percentile).
    High,
    /// Turbo priority (95th percentile + Jito tip).
    Turbo,
}

impl PriorityLevel {
    /// Get priority fee in micro-lamports.
    pub fn to_microlamports(&self) -> u64 {
        match self {
            PriorityLevel::None => 0,
            PriorityLevel::Low => 1_000,      // 1000 micro-lamports
            PriorityLevel::Medium => 10_000,   // 10000 micro-lamports
            PriorityLevel::High => 100_000,    // 100000 micro-lamports
            PriorityLevel::Turbo => 1_000_000, // 1M micro-lamports
        }
    }

    /// Get Jito tip amount in lamports.
    pub fn jito_tip(&self) -> u64 {
        match self {
            PriorityLevel::Turbo => 10_000, // 0.00001 SOL
            _ => 0,
        }
    }
}

/// Swap configuration.
#[derive(Debug, Clone)]
pub struct SwapConfig {
    /// Slippage in basis points (1 bps = 0.01%).
    pub slippage_bps: u16,
    /// Priority fee level.
    pub priority_fee: PriorityLevel,
    /// Use Jito bundles.
    pub use_jito: bool,
    /// Maximum accounts (limit for faster execution).
    pub max_accounts: Option<u8>,
    /// Only use direct routes (skip multi-hop).
    pub direct_route_only: bool,
    /// Wrap and unwrap SOL automatically.
    pub wrap_unwrap_sol: bool,
}

impl Default for SwapConfig {
    fn default() -> Self {
        Self {
            slippage_bps: 50, // 0.5%
            priority_fee: PriorityLevel::Medium,
            use_jito: false,
            max_accounts: None,
            direct_route_only: false,
            wrap_unwrap_sol: true,
        }
    }
}

impl SwapConfig {
    /// Create config optimized for speed.
    pub fn fast() -> Self {
        Self {
            slippage_bps: 100, // 1% - more tolerance for speed
            priority_fee: PriorityLevel::High,
            use_jito: false,
            max_accounts: Some(20), // Limit accounts for faster execution
            direct_route_only: true, // Direct routes are faster
            wrap_unwrap_sol: true,
        }
    }

    /// Create config for guaranteed execution (Jito).
    pub fn guaranteed() -> Self {
        Self {
            slippage_bps: 50,
            priority_fee: PriorityLevel::Turbo,
            use_jito: true,
            max_accounts: None,
            direct_route_only: false,
            wrap_unwrap_sol: true,
        }
    }

    /// Create config optimized for best price.
    pub fn best_price() -> Self {
        Self {
            slippage_bps: 30, // Tight slippage
            priority_fee: PriorityLevel::Low,
            use_jito: false,
            max_accounts: None,
            direct_route_only: false,
            wrap_unwrap_sol: true,
        }
    }

    /// Set slippage.
    pub fn with_slippage(mut self, bps: u16) -> Self {
        self.slippage_bps = bps;
        self
    }

    /// Set priority level.
    pub fn with_priority(mut self, priority: PriorityLevel) -> Self {
        self.priority_fee = priority;
        self
    }

    /// Enable Jito bundles.
    pub fn with_jito(mut self) -> Self {
        self.use_jito = true;
        self
    }

    /// Set direct routes only.
    pub fn with_direct_routes(mut self) -> Self {
        self.direct_route_only = true;
        self
    }

    /// Set max accounts.
    pub fn with_max_accounts(mut self, max: u8) -> Self {
        self.max_accounts = Some(max);
        self
    }
}

/// Result of a swap execution.
#[derive(Debug, Clone)]
pub struct SwapResult {
    /// Transaction signature.
    pub signature: Signature,
    /// Input amount.
    pub input_amount: u64,
    /// Output amount.
    pub output_amount: u64,
    /// Execution time in milliseconds.
    pub execution_time_ms: u64,
    /// Slot where transaction landed.
    pub slot_landed: u64,
    /// Price impact in basis points.
    pub price_impact_bps: u16,
    /// Route labels (DEXs used).
    pub route_labels: Vec<String>,
}

/// Quote result without execution.
#[derive(Debug, Clone)]
pub struct Quote {
    /// Input mint.
    pub input_mint: Pubkey,
    /// Output mint.
    pub output_mint: Pubkey,
    /// Input amount.
    pub input_amount: u64,
    /// Output amount.
    pub output_amount: u64,
    /// Price impact in basis points.
    pub price_impact_bps: u16,
    /// Route (programs in route).
    pub route: Vec<Pubkey>,
    /// Route labels.
    pub route_labels: Vec<String>,
    /// Minimum output amount with slippage.
    pub min_output_amount: u64,
}

/// Swap error types.
#[derive(Debug, Clone)]
pub enum SwapError {
    /// Quote failed.
    QuoteFailed(String),
    /// Transaction build failed.
    TransactionBuildFailed(String),
    /// Transaction execution failed.
    ExecutionFailed(String),
    /// Slippage exceeded.
    SlippageExceeded { expected: u64, actual: u64 },
    /// Insufficient balance.
    InsufficientBalance { required: u64, available: u64 },
    /// Invalid mint.
    InvalidMint(String),
}

impl std::fmt::Display for SwapError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SwapError::QuoteFailed(msg) => write!(f, "Quote failed: {}", msg),
            SwapError::TransactionBuildFailed(msg) => write!(f, "Transaction build failed: {}", msg),
            SwapError::ExecutionFailed(msg) => write!(f, "Execution failed: {}", msg),
            SwapError::SlippageExceeded { expected, actual } => {
                write!(f, "Slippage exceeded: expected {}, got {}", expected, actual)
            }
            SwapError::InsufficientBalance { required, available } => {
                write!(f, "Insufficient balance: need {}, have {}", required, available)
            }
            SwapError::InvalidMint(msg) => write!(f, "Invalid mint: {}", msg),
        }
    }
}

impl std::error::Error for SwapError {}

impl From<SwapError> for ToolkitError {
    fn from(e: SwapError) -> Self {
        ToolkitError::JupiterError(e.to_string())
    }
}

/// High-performance swap executor.
pub struct SwapExecutor {
    /// RPC client.
    rpc: FastRpcClient,
    /// Transaction builder.
    builder: FastTransactionBuilder,
    /// Jupiter API client.
    jupiter: JupiterApiClient,
    /// Payer keypair.
    payer: Arc<Keypair>,
}

impl SwapExecutor {
    /// Create a new swap executor.
    pub fn new(rpc: FastRpcClient, payer: Keypair) -> Self {
        let payer = Arc::new(payer);
        Self {
            builder: FastTransactionBuilder::new(
                Keypair::from_bytes(&payer.to_bytes()).expect("Valid keypair"),
            ),
            rpc,
            jupiter: JupiterApiClient::new(),
            payer,
        }
    }

    /// Get payer pubkey.
    pub fn payer(&self) -> Pubkey {
        self.payer.pubkey()
    }

    /// Execute swap as fast as possible.
    pub async fn swap_fast(
        &self,
        input_mint: &Pubkey,
        output_mint: &Pubkey,
        amount: u64,
        config: SwapConfig,
    ) -> Result<SwapResult> {
        let start = Instant::now();

        // Get quote with options
        let quote = self
            .jupiter
            .quote_with_options(
                input_mint,
                output_mint,
                amount,
                config.slippage_bps,
                config.direct_route_only,
                config.max_accounts,
            )
            .await
            .map_err(|e| SwapError::QuoteFailed(e.to_string()))?;

        // Get swap transaction
        let priority_fee = if config.priority_fee != PriorityLevel::None {
            Some(config.priority_fee.to_microlamports())
        } else {
            None
        };

        let mut tx = self
            .jupiter
            .swap_transaction_with_options(
                &quote,
                &self.payer.pubkey(),
                priority_fee,
                config.wrap_unwrap_sol,
                false, // Use versioned transactions
            )
            .await
            .map_err(|e| SwapError::TransactionBuildFailed(e.to_string()))?;

        // Get fresh blockhash
        let blockhash = self.rpc.get_latest_blockhash_fast().await?;
        tx.message.set_recent_blockhash(blockhash);

        // Sign
        let signed_tx = VersionedTransaction::try_new(tx.message.clone(), &[self.payer.as_ref()])
            .map_err(|e| SwapError::TransactionBuildFailed(e.to_string()))?;

        // Send and get slot
        let signature = self
            .rpc
            .get_best_client()
            .await
            .send_and_confirm_transaction(&signed_tx)
            .await
            .map_err(|e| SwapError::ExecutionFailed(e.to_string()))?;

        let slot = self.rpc.get_slot_fast().await.unwrap_or(0);
        let execution_time = start.elapsed().as_millis() as u64;

        // Parse price impact
        let price_impact_bps = (quote.price_impact() * 100.0) as u16;

        Ok(SwapResult {
            signature,
            input_amount: quote.in_amount,
            output_amount: quote.out_amount,
            execution_time_ms: execution_time,
            slot_landed: slot,
            price_impact_bps,
            route_labels: quote.route_labels(),
        })
    }

    /// Get quote without executing.
    pub async fn get_quote(
        &self,
        input_mint: &Pubkey,
        output_mint: &Pubkey,
        amount: u64,
    ) -> Result<Quote> {
        let jupiter_quote = self.jupiter.quote(input_mint, output_mint, amount, 50).await?;

        let price_impact_bps = (jupiter_quote.price_impact() * 100.0) as u16;

        Ok(Quote {
            input_mint: *input_mint,
            output_mint: *output_mint,
            input_amount: jupiter_quote.in_amount,
            output_amount: jupiter_quote.out_amount,
            price_impact_bps,
            route: vec![], // Would need to parse from route_plan
            route_labels: jupiter_quote.route_labels(),
            min_output_amount: jupiter_quote.other_amount_threshold,
        })
    }

    /// Execute swap with Jito bundle for guaranteed inclusion.
    pub async fn swap_with_jito(
        &self,
        input_mint: &Pubkey,
        output_mint: &Pubkey,
        amount: u64,
        _tip_lamports: u64,
    ) -> Result<SwapResult> {
        let mut config = SwapConfig::guaranteed();
        config.use_jito = true;

        // Note: Full Jito integration would require separate bundle submission
        // This is a simplified version that uses high priority fees
        self.swap_fast(input_mint, output_mint, amount, config).await
    }

    /// Prepare a swap transaction without executing.
    pub async fn prepare_swap(
        &self,
        input_mint: &Pubkey,
        output_mint: &Pubkey,
        amount: u64,
        config: SwapConfig,
    ) -> Result<(VersionedTransaction, JupiterQuote)> {
        let quote = self
            .jupiter
            .quote_with_options(
                input_mint,
                output_mint,
                amount,
                config.slippage_bps,
                config.direct_route_only,
                config.max_accounts,
            )
            .await?;

        let priority_fee = if config.priority_fee != PriorityLevel::None {
            Some(config.priority_fee.to_microlamports())
        } else {
            None
        };

        let tx = self
            .jupiter
            .swap_transaction_with_options(
                &quote,
                &self.payer.pubkey(),
                priority_fee,
                config.wrap_unwrap_sol,
                false,
            )
            .await?;

        Ok((tx, quote))
    }

    /// Execute a prepared swap transaction.
    pub async fn execute_prepared(&self, mut tx: VersionedTransaction) -> Result<Signature> {
        // Get fresh blockhash
        let blockhash = self.rpc.get_latest_blockhash_fast().await?;
        tx.message.set_recent_blockhash(blockhash);

        // Sign
        let signed_tx = VersionedTransaction::try_new(tx.message, &[self.payer.as_ref()])
            .map_err(|e| ToolkitError::SigningError(e.to_string()))?;

        // Send
        self.rpc
            .get_best_client()
            .await
            .send_and_confirm_transaction(&signed_tx)
            .await
            .map_err(|e| ToolkitError::TransactionError(e.to_string()))
    }

    /// Get multiple quotes in parallel.
    pub async fn get_quotes_parallel(
        &self,
        pairs: Vec<(Pubkey, Pubkey, u64)>,
    ) -> Vec<Result<Quote>> {
        let requests: Vec<_> = pairs
            .into_iter()
            .map(|(input, output, amount)| (input, output, amount, 50u16))
            .collect();

        let jupiter_quotes = self.jupiter.get_quotes_parallel(requests).await;

        jupiter_quotes
            .into_iter()
            .map(|result| {
                result.map(|q| {
                    let price_impact_bps = (q.price_impact() * 100.0) as u16;
                    Quote {
                        input_mint: q.input_mint_pubkey().unwrap_or_default(),
                        output_mint: q.output_mint_pubkey().unwrap_or_default(),
                        input_amount: q.in_amount,
                        output_amount: q.out_amount,
                        price_impact_bps,
                        route: vec![],
                        route_labels: q.route_labels(),
                        min_output_amount: q.other_amount_threshold,
                    }
                })
            })
            .collect()
    }
}

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

    #[test]
    fn test_priority_levels() {
        assert_eq!(PriorityLevel::None.to_microlamports(), 0);
        assert_eq!(PriorityLevel::Low.to_microlamports(), 1_000);
        assert_eq!(PriorityLevel::Medium.to_microlamports(), 10_000);
        assert_eq!(PriorityLevel::High.to_microlamports(), 100_000);
        assert_eq!(PriorityLevel::Turbo.to_microlamports(), 1_000_000);
    }

    #[test]
    fn test_swap_config_presets() {
        let fast = SwapConfig::fast();
        assert_eq!(fast.slippage_bps, 100);
        assert!(fast.direct_route_only);
        assert_eq!(fast.priority_fee, PriorityLevel::High);

        let guaranteed = SwapConfig::guaranteed();
        assert!(guaranteed.use_jito);
        assert_eq!(guaranteed.priority_fee, PriorityLevel::Turbo);

        let best_price = SwapConfig::best_price();
        assert_eq!(best_price.slippage_bps, 30);
        assert_eq!(best_price.priority_fee, PriorityLevel::Low);
    }

    #[test]
    fn test_swap_config_builder() {
        let config = SwapConfig::default()
            .with_slippage(75)
            .with_priority(PriorityLevel::High)
            .with_direct_routes()
            .with_max_accounts(15);

        assert_eq!(config.slippage_bps, 75);
        assert_eq!(config.priority_fee, PriorityLevel::High);
        assert!(config.direct_route_only);
        assert_eq!(config.max_accounts, Some(15));
    }

    #[test]
    fn test_swap_error_display() {
        let err = SwapError::SlippageExceeded {
            expected: 1000,
            actual: 900,
        };
        assert!(err.to_string().contains("Slippage exceeded"));
    }
}