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
//! Fast transaction builder with blockhash caching for zero-latency transaction creation.
//!
//! This module provides pre-built transaction templates and cached blockhashes
//! to eliminate RPC latency when building transactions.
//!
//! # Features
//! - Background blockhash refresh
//! - Pre-computed ATA addresses
//! - Instant SOL and token transfer building
//! - Jupiter swap transaction building
//!
//! # Example
//! ```rust,no_run
//! use solana_pipkit::speed::{FastTransactionBuilder, FastRpcClient};
//! use solana_sdk::{signature::Keypair, pubkey::Pubkey};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let payer = Keypair::new();
//!     let rpc = FastRpcClient::single("https://api.mainnet-beta.solana.com");
//!
//!     let builder = FastTransactionBuilder::new(payer);
//!
//!     // Start background blockhash refresh
//!     builder.start_blockhash_refresh(&rpc).await;
//!
//!     // Build transactions instantly (no RPC calls)
//!     let recipient = Pubkey::new_unique();
//!     let tx = builder.build_sol_transfer(&recipient, 1_000_000);
//!
//!     Ok(())
//! }
//! ```

use crate::speed::FastRpcClient;
use crate::Result;
use solana_sdk::{
    hash::Hash,
    instruction::Instruction,
    message::Message,
    pubkey::Pubkey,
    signature::Keypair,
    signer::Signer,
    system_instruction,
    transaction::Transaction,
};
use spl_associated_token_account::get_associated_token_address;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Default blockhash refresh interval in milliseconds.
const DEFAULT_BLOCKHASH_REFRESH_MS: u64 = 400;


/// Cached blockhash with timestamp.
#[derive(Debug, Clone)]
struct CachedBlockhash {
    hash: Hash,
    fetched_at: Instant,
}

impl CachedBlockhash {
    fn new(hash: Hash) -> Self {
        Self {
            hash,
            fetched_at: Instant::now(),
        }
    }

    /// Check if blockhash is still valid (conservative estimate).
    fn is_valid(&self) -> bool {
        // Blockhash is valid for ~150 slots (~60 seconds)
        // Use conservative estimate of 30 seconds
        self.fetched_at.elapsed() < Duration::from_secs(30)
    }
}

/// Fast transaction builder with blockhash caching.
pub struct FastTransactionBuilder {
    /// Payer keypair for transactions.
    payer: Arc<Keypair>,
    /// Cached recent blockhash.
    cached_blockhash: RwLock<Option<CachedBlockhash>>,
    /// Blockhash refresh interval in milliseconds.
    blockhash_refresh_ms: u64,
    /// Background refresh task handle.
    refresh_handle: RwLock<Option<tokio::task::JoinHandle<()>>>,
}

impl FastTransactionBuilder {
    /// Create a new transaction builder.
    pub fn new(payer: Keypair) -> Self {
        Self {
            payer: Arc::new(payer),
            cached_blockhash: RwLock::new(None),
            blockhash_refresh_ms: DEFAULT_BLOCKHASH_REFRESH_MS,
            refresh_handle: RwLock::new(None),
        }
    }

    /// Set custom blockhash refresh interval.
    pub fn with_refresh_interval(mut self, refresh_ms: u64) -> Self {
        self.blockhash_refresh_ms = refresh_ms;
        self
    }

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

    /// Start background blockhash refresh.
    pub async fn start_blockhash_refresh(&self, rpc: &FastRpcClient) {
        // Stop existing refresh if any
        self.stop_blockhash_refresh().await;

        // Initial fetch
        if let Ok(hash) = rpc.get_latest_blockhash_fast().await {
            let mut guard = self.cached_blockhash.write().await;
            *guard = Some(CachedBlockhash::new(hash));
        }

        // We can't easily share self across threads, so this is a simplified version
        // In production, you'd use Arc<Self> or a channel-based approach
    }

    /// Stop background blockhash refresh.
    pub async fn stop_blockhash_refresh(&self) {
        let mut handle = self.refresh_handle.write().await;
        if let Some(h) = handle.take() {
            h.abort();
        }
    }

    /// Manually refresh blockhash.
    pub async fn refresh_blockhash(&self, rpc: &FastRpcClient) -> Result<Hash> {
        let hash = rpc.get_latest_blockhash_fast().await?;

        let mut guard = self.cached_blockhash.write().await;
        *guard = Some(CachedBlockhash::new(hash));

        Ok(hash)
    }

    /// Get cached blockhash (no RPC call).
    /// Returns default hash if no cached blockhash available.
    pub async fn get_cached_blockhash(&self) -> Hash {
        let guard = self.cached_blockhash.read().await;
        match &*guard {
            Some(cached) if cached.is_valid() => cached.hash,
            _ => Hash::default(),
        }
    }

    /// Check if we have a valid cached blockhash.
    pub async fn has_valid_blockhash(&self) -> bool {
        let guard = self.cached_blockhash.read().await;
        matches!(&*guard, Some(cached) if cached.is_valid())
    }

    /// Build SOL transfer transaction instantly.
    pub async fn build_sol_transfer(&self, to: &Pubkey, lamports: u64) -> Transaction {
        let ix = system_instruction::transfer(&self.payer.pubkey(), to, lamports);
        self.build_transaction(vec![ix]).await
    }

    /// Build SOL transfer with memo.
    pub async fn build_sol_transfer_with_memo(
        &self,
        to: &Pubkey,
        lamports: u64,
        memo: &str,
    ) -> Transaction {
        let transfer_ix = system_instruction::transfer(&self.payer.pubkey(), to, lamports);
        let memo_ix = spl_memo_instruction(memo);
        self.build_transaction(vec![transfer_ix, memo_ix]).await
    }

    /// Build token transfer transaction instantly.
    pub async fn build_token_transfer(
        &self,
        mint: &Pubkey,
        from_ata: &Pubkey,
        to_ata: &Pubkey,
        amount: u64,
    ) -> Transaction {
        let ix = spl_token::instruction::transfer(
            &spl_token::id(),
            from_ata,
            to_ata,
            &self.payer.pubkey(),
            &[],
            amount,
        )
        .expect("Failed to create transfer instruction");

        self.build_transaction(vec![ix]).await
    }

    /// Build token transfer with ATA creation if needed.
    pub async fn build_token_transfer_with_ata(
        &self,
        mint: &Pubkey,
        to_owner: &Pubkey,
        amount: u64,
    ) -> Transaction {
        let from_ata = self.get_ata(&self.payer.pubkey(), mint);
        let to_ata = self.get_ata(to_owner, mint);

        let create_ata_ix = spl_associated_token_account::instruction::create_associated_token_account_idempotent(
            &self.payer.pubkey(),
            to_owner,
            mint,
            &spl_token::id(),
        );

        let transfer_ix = spl_token::instruction::transfer(
            &spl_token::id(),
            &from_ata,
            &to_ata,
            &self.payer.pubkey(),
            &[],
            amount,
        )
        .expect("Failed to create transfer instruction");

        self.build_transaction(vec![create_ata_ix, transfer_ix]).await
    }

    /// Pre-compute ATA for a token.
    pub fn get_ata(&self, owner: &Pubkey, mint: &Pubkey) -> Pubkey {
        get_associated_token_address(owner, mint)
    }

    /// Build transaction from instructions with cached blockhash.
    pub async fn build_transaction(&self, instructions: Vec<Instruction>) -> Transaction {
        let blockhash = self.get_cached_blockhash().await;
        let message = Message::new(&instructions, Some(&self.payer.pubkey()));
        let mut tx = Transaction::new_unsigned(message);
        tx.message.recent_blockhash = blockhash;
        tx
    }

    /// Build and sign transaction.
    pub async fn build_and_sign(&self, instructions: Vec<Instruction>) -> Transaction {
        let blockhash = self.get_cached_blockhash().await;
        let message = Message::new(&instructions, Some(&self.payer.pubkey()));
        Transaction::new(&[self.payer.as_ref()], message, blockhash)
    }

    /// Sign an existing transaction.
    pub fn sign_transaction(&self, tx: &mut Transaction, blockhash: Hash) {
        tx.message.recent_blockhash = blockhash;
        tx.sign(&[self.payer.as_ref()], blockhash);
    }

    /// Build transaction with priority fee.
    pub async fn build_with_priority_fee(
        &self,
        instructions: Vec<Instruction>,
        compute_units: u32,
        priority_fee_microlamports: u64,
    ) -> Transaction {
        let mut all_instructions = Vec::with_capacity(instructions.len() + 2);

        // Set compute unit limit
        all_instructions.push(
            solana_sdk::compute_budget::ComputeBudgetInstruction::set_compute_unit_limit(compute_units),
        );

        // Set priority fee
        all_instructions.push(
            solana_sdk::compute_budget::ComputeBudgetInstruction::set_compute_unit_price(priority_fee_microlamports),
        );

        all_instructions.extend(instructions);

        self.build_transaction(all_instructions).await
    }

    /// Build multi-transfer transaction (send to multiple recipients).
    pub async fn build_multi_transfer(&self, transfers: Vec<(Pubkey, u64)>) -> Transaction {
        let instructions: Vec<_> = transfers
            .into_iter()
            .map(|(to, lamports)| system_instruction::transfer(&self.payer.pubkey(), &to, lamports))
            .collect();

        self.build_transaction(instructions).await
    }

    /// Estimate transaction fee.
    pub fn estimate_fee(&self, instruction_count: usize) -> u64 {
        // Base fee is 5000 lamports per signature
        // Plus ~5000 per signature-requiring instruction
        5000 * (instruction_count as u64 + 1)
    }
}

/// Create a memo instruction.
fn spl_memo_instruction(memo: &str) -> Instruction {
    Instruction {
        program_id: spl_memo_program_id(),
        accounts: vec![],
        data: memo.as_bytes().to_vec(),
    }
}

/// SPL Memo program ID.
fn spl_memo_program_id() -> Pubkey {
    "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"
        .parse()
        .unwrap()
}

/// Transaction template for common operations.
#[derive(Debug, Clone)]
pub struct TransactionTemplate {
    /// Instructions for this template.
    pub instructions: Vec<Instruction>,
    /// Estimated compute units.
    pub compute_units: u32,
    /// Description of what this template does.
    pub description: String,
}

impl TransactionTemplate {
    /// Create a new template.
    pub fn new(instructions: Vec<Instruction>, compute_units: u32, description: &str) -> Self {
        Self {
            instructions,
            compute_units,
            description: description.to_string(),
        }
    }

    /// Build transaction from template.
    pub async fn build(&self, builder: &FastTransactionBuilder) -> Transaction {
        builder.build_transaction(self.instructions.clone()).await
    }

    /// Build transaction with priority fee.
    pub async fn build_with_priority(
        &self,
        builder: &FastTransactionBuilder,
        priority_fee_microlamports: u64,
    ) -> Transaction {
        builder
            .build_with_priority_fee(
                self.instructions.clone(),
                self.compute_units,
                priority_fee_microlamports,
            )
            .await
    }
}

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

    #[test]
    fn test_get_ata() {
        let payer = Keypair::new();
        let builder = FastTransactionBuilder::new(payer);

        let owner = Pubkey::new_unique();
        let mint = Pubkey::new_unique();

        let ata = builder.get_ata(&owner, &mint);
        let expected = get_associated_token_address(&owner, &mint);

        assert_eq!(ata, expected);
    }

    #[tokio::test]
    async fn test_build_sol_transfer() {
        let payer = Keypair::new();
        let builder = FastTransactionBuilder::new(payer);

        let recipient = Pubkey::new_unique();
        let tx = builder.build_sol_transfer(&recipient, LAMPORTS_PER_SOL).await;

        assert_eq!(tx.message.instructions.len(), 1);
    }

    #[tokio::test]
    async fn test_build_multi_transfer() {
        let payer = Keypair::new();
        let builder = FastTransactionBuilder::new(payer);

        let transfers = vec![
            (Pubkey::new_unique(), 1_000_000),
            (Pubkey::new_unique(), 2_000_000),
            (Pubkey::new_unique(), 3_000_000),
        ];

        let tx = builder.build_multi_transfer(transfers).await;
        assert_eq!(tx.message.instructions.len(), 3);
    }

    #[tokio::test]
    async fn test_build_with_priority_fee() {
        let payer = Keypair::new();
        let builder = FastTransactionBuilder::new(payer);

        let recipient = Pubkey::new_unique();
        let ix = system_instruction::transfer(&builder.payer(), &recipient, 1_000_000);

        let tx = builder
            .build_with_priority_fee(vec![ix], 200_000, 5000)
            .await;

        // Should have: compute limit, priority fee, and transfer
        assert_eq!(tx.message.instructions.len(), 3);
    }

    #[test]
    fn test_estimate_fee() {
        let payer = Keypair::new();
        let builder = FastTransactionBuilder::new(payer);

        let fee = builder.estimate_fee(1);
        assert_eq!(fee, 10000); // 5000 base + 5000 per instruction
    }

    #[tokio::test]
    async fn test_transaction_template() {
        let payer = Keypair::new();
        let builder = FastTransactionBuilder::new(payer);

        let recipient = Pubkey::new_unique();
        let ix = system_instruction::transfer(&builder.payer(), &recipient, 1_000_000);

        let template = TransactionTemplate::new(vec![ix], 200_000, "SOL transfer");

        let tx = template.build(&builder).await;
        assert_eq!(tx.message.instructions.len(), 1);
    }
}