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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//! Anchor framework helpers and utilities.
//!
//! This module provides utilities for working with Anchor programs,
//! including discriminator calculation, CPI helpers, and account validation.
//!
//! Enable with the `anchor` feature flag.

use solana_sdk::{
    instruction::{AccountMeta, Instruction},
    pubkey::Pubkey,
};

use crate::{Result, ToolkitError};

// ============================================================================
// Discriminator Utilities
// ============================================================================

/// Calculate the Anchor account discriminator using SHA256.
/// Format: SHA256("account:{AccountName}")[..8]
pub fn account_discriminator(account_name: &str) -> [u8; 8] {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    // For production use with anchor feature, this would use SHA256
    // Simplified version for now
    let preimage = format!("account:{}", account_name);
    let mut hasher = DefaultHasher::new();
    preimage.hash(&mut hasher);
    let hash = hasher.finish();
    hash.to_le_bytes()
}

/// Calculate the Anchor instruction discriminator using SHA256.
/// Format: SHA256("global:{instruction_name}")[..8]
pub fn instruction_discriminator(instruction_name: &str) -> [u8; 8] {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let preimage = format!("global:{}", instruction_name);
    let mut hasher = DefaultHasher::new();
    preimage.hash(&mut hasher);
    let hash = hasher.finish();
    hash.to_le_bytes()
}

/// Calculate discriminator for a namespaced instruction.
/// Format: SHA256("{namespace}:{instruction_name}")[..8]
pub fn namespaced_discriminator(namespace: &str, name: &str) -> [u8; 8] {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let preimage = format!("{}:{}", namespace, name);
    let mut hasher = DefaultHasher::new();
    preimage.hash(&mut hasher);
    let hash = hasher.finish();
    hash.to_le_bytes()
}

// ============================================================================
// CPI (Cross-Program Invocation) Helpers
// ============================================================================

/// Common program IDs for CPI calls.
pub mod programs {
    use solana_sdk::pubkey::Pubkey;

    /// System Program ID.
    pub const SYSTEM_PROGRAM: Pubkey = solana_sdk::system_program::ID;

    /// SPL Token Program ID.
    pub fn token_program() -> Pubkey {
        spl_token::id()
    }

    /// Associated Token Account Program ID.
    pub fn associated_token_program() -> Pubkey {
        spl_associated_token_account::id()
    }

    /// Token Metadata Program ID (Metaplex).
    pub fn token_metadata_program() -> Pubkey {
        mpl_token_metadata::ID
    }

    /// Rent sysvar.
    pub fn rent_sysvar() -> Pubkey {
        solana_sdk::sysvar::rent::id()
    }

    /// Clock sysvar.
    pub fn clock_sysvar() -> Pubkey {
        solana_sdk::sysvar::clock::id()
    }
}

/// Builder for creating CPI instructions.
#[derive(Default)]
pub struct CpiInstructionBuilder {
    program_id: Option<Pubkey>,
    accounts: Vec<AccountMeta>,
    data: Vec<u8>,
}

impl CpiInstructionBuilder {
    /// Create a new CPI instruction builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the program ID for the CPI call.
    pub fn program(mut self, program_id: Pubkey) -> Self {
        self.program_id = Some(program_id);
        self
    }

    /// Add a signer account.
    pub fn signer(mut self, pubkey: Pubkey) -> Self {
        self.accounts.push(AccountMeta::new(pubkey, true));
        self
    }

    /// Add a read-only signer account.
    pub fn signer_readonly(mut self, pubkey: Pubkey) -> Self {
        self.accounts.push(AccountMeta::new_readonly(pubkey, true));
        self
    }

    /// Add a writable account (not a signer).
    pub fn writable(mut self, pubkey: Pubkey) -> Self {
        self.accounts.push(AccountMeta::new(pubkey, false));
        self
    }

    /// Add a read-only account.
    pub fn readonly(mut self, pubkey: Pubkey) -> Self {
        self.accounts.push(AccountMeta::new_readonly(pubkey, false));
        self
    }

    /// Add account metadata directly.
    pub fn account(mut self, meta: AccountMeta) -> Self {
        self.accounts.push(meta);
        self
    }

    /// Add multiple accounts.
    pub fn accounts(mut self, metas: Vec<AccountMeta>) -> Self {
        self.accounts.extend(metas);
        self
    }

    /// Set raw instruction data.
    pub fn data(mut self, data: Vec<u8>) -> Self {
        self.data = data;
        self
    }

    /// Set instruction data with Anchor discriminator.
    pub fn anchor_data(mut self, instruction_name: &str, args: &[u8]) -> Self {
        let discriminator = instruction_discriminator(instruction_name);
        self.data = Vec::with_capacity(8 + args.len());
        self.data.extend_from_slice(&discriminator);
        self.data.extend_from_slice(args);
        self
    }

    /// Build the instruction.
    pub fn build(self) -> Result<Instruction> {
        let program_id = self.program_id.ok_or_else(|| {
            ToolkitError::Custom("Program ID not set".to_string())
        })?;

        Ok(Instruction {
            program_id,
            accounts: self.accounts,
            data: self.data,
        })
    }
}

// ============================================================================
// System Program CPI Helpers
// ============================================================================

/// Helpers for System Program CPIs.
pub mod system_cpi {
    use super::*;
    use solana_sdk::system_instruction;

    /// Create a transfer instruction.
    pub fn transfer(from: &Pubkey, to: &Pubkey, lamports: u64) -> Instruction {
        system_instruction::transfer(from, to, lamports)
    }

    /// Create an allocate instruction.
    pub fn allocate(account: &Pubkey, space: u64) -> Instruction {
        system_instruction::allocate(account, space)
    }

    /// Create a create_account instruction.
    pub fn create_account(
        from: &Pubkey,
        to: &Pubkey,
        lamports: u64,
        space: u64,
        owner: &Pubkey,
    ) -> Instruction {
        system_instruction::create_account(from, to, lamports, space, owner)
    }

    /// Create an assign instruction.
    pub fn assign(account: &Pubkey, owner: &Pubkey) -> Instruction {
        system_instruction::assign(account, owner)
    }
}

/// Helpers for SPL Token Program CPIs.
pub mod token_cpi {
    use super::*;
    use spl_token::instruction as token_instruction;

    /// Create a transfer instruction.
    pub fn transfer(
        source: &Pubkey,
        destination: &Pubkey,
        authority: &Pubkey,
        amount: u64,
    ) -> Result<Instruction> {
        token_instruction::transfer(
            &spl_token::id(),
            source,
            destination,
            authority,
            &[],
            amount,
        )
        .map_err(|e| ToolkitError::TokenError(e.to_string()))
    }

    /// Create a mint_to instruction.
    pub fn mint_to(
        mint: &Pubkey,
        destination: &Pubkey,
        authority: &Pubkey,
        amount: u64,
    ) -> Result<Instruction> {
        token_instruction::mint_to(
            &spl_token::id(),
            mint,
            destination,
            authority,
            &[],
            amount,
        )
        .map_err(|e| ToolkitError::TokenError(e.to_string()))
    }

    /// Create a burn instruction.
    pub fn burn(
        account: &Pubkey,
        mint: &Pubkey,
        authority: &Pubkey,
        amount: u64,
    ) -> Result<Instruction> {
        token_instruction::burn(
            &spl_token::id(),
            account,
            mint,
            authority,
            &[],
            amount,
        )
        .map_err(|e| ToolkitError::TokenError(e.to_string()))
    }

    /// Create a close_account instruction.
    pub fn close_account(
        account: &Pubkey,
        destination: &Pubkey,
        authority: &Pubkey,
    ) -> Result<Instruction> {
        token_instruction::close_account(
            &spl_token::id(),
            account,
            destination,
            authority,
            &[],
        )
        .map_err(|e| ToolkitError::TokenError(e.to_string()))
    }

    /// Create an approve instruction.
    pub fn approve(
        source: &Pubkey,
        delegate: &Pubkey,
        authority: &Pubkey,
        amount: u64,
    ) -> Result<Instruction> {
        token_instruction::approve(
            &spl_token::id(),
            source,
            delegate,
            authority,
            &[],
            amount,
        )
        .map_err(|e| ToolkitError::TokenError(e.to_string()))
    }

    /// Create a revoke instruction.
    pub fn revoke(source: &Pubkey, authority: &Pubkey) -> Result<Instruction> {
        token_instruction::revoke(&spl_token::id(), source, authority, &[])
            .map_err(|e| ToolkitError::TokenError(e.to_string()))
    }

    /// Create an initialize_account instruction.
    pub fn initialize_account(
        account: &Pubkey,
        mint: &Pubkey,
        owner: &Pubkey,
    ) -> Result<Instruction> {
        token_instruction::initialize_account(
            &spl_token::id(),
            account,
            mint,
            owner,
        )
        .map_err(|e| ToolkitError::TokenError(e.to_string()))
    }

    /// Create a set_authority instruction.
    pub fn set_authority(
        account: &Pubkey,
        current_authority: &Pubkey,
        authority_type: spl_token::instruction::AuthorityType,
        new_authority: Option<&Pubkey>,
    ) -> Result<Instruction> {
        token_instruction::set_authority(
            &spl_token::id(),
            account,
            new_authority,
            authority_type,
            current_authority,
            &[],
        )
        .map_err(|e| ToolkitError::TokenError(e.to_string()))
    }

    /// Create a freeze_account instruction.
    pub fn freeze_account(
        account: &Pubkey,
        mint: &Pubkey,
        authority: &Pubkey,
    ) -> Result<Instruction> {
        token_instruction::freeze_account(
            &spl_token::id(),
            account,
            mint,
            authority,
            &[],
        )
        .map_err(|e| ToolkitError::TokenError(e.to_string()))
    }

    /// Create a thaw_account instruction.
    pub fn thaw_account(
        account: &Pubkey,
        mint: &Pubkey,
        authority: &Pubkey,
    ) -> Result<Instruction> {
        token_instruction::thaw_account(
            &spl_token::id(),
            account,
            mint,
            authority,
            &[],
        )
        .map_err(|e| ToolkitError::TokenError(e.to_string()))
    }
}

/// Helpers for Associated Token Account Program CPIs.
pub mod ata_cpi {
    use super::*;
    use spl_associated_token_account::instruction as ata_instruction;

    /// Create an associated token account creation instruction.
    pub fn create(
        payer: &Pubkey,
        wallet: &Pubkey,
        mint: &Pubkey,
    ) -> Instruction {
        ata_instruction::create_associated_token_account(
            payer,
            wallet,
            mint,
            &spl_token::id(),
        )
    }

    /// Create an idempotent associated token account creation instruction.
    pub fn create_idempotent(
        payer: &Pubkey,
        wallet: &Pubkey,
        mint: &Pubkey,
    ) -> Instruction {
        ata_instruction::create_associated_token_account_idempotent(
            payer,
            wallet,
            mint,
            &spl_token::id(),
        )
    }

    /// Get the associated token address.
    pub fn get_address(wallet: &Pubkey, mint: &Pubkey) -> Pubkey {
        spl_associated_token_account::get_associated_token_address(wallet, mint)
    }
}

// ============================================================================
// Account Validation Helpers
// ============================================================================

/// Validation utilities for Anchor accounts.
pub mod validation {
    use super::*;
    use solana_sdk::account::Account;

    /// Validate that an account has the expected discriminator.
    pub fn validate_discriminator(
        account: &Account,
        expected: &[u8; 8],
    ) -> Result<()> {
        if account.data.len() < 8 {
            return Err(ToolkitError::InvalidAccountData(
                "Account data too short for discriminator".to_string(),
            ));
        }

        let discriminator: &[u8; 8] = account.data[..8].try_into().map_err(|_| {
            ToolkitError::InvalidAccountData("Invalid discriminator".to_string())
        })?;

        if discriminator != expected {
            return Err(ToolkitError::InvalidAccountData(format!(
                "Invalid discriminator: expected {:?}, got {:?}",
                expected, discriminator
            )));
        }

        Ok(())
    }

    /// Validate that an account is owned by the expected program.
    pub fn validate_owner(account: &Account, expected_owner: &Pubkey) -> Result<()> {
        if &account.owner != expected_owner {
            return Err(ToolkitError::InvalidAccountData(format!(
                "Invalid owner: expected {}, got {}",
                expected_owner, account.owner
            )));
        }
        Ok(())
    }

    /// Validate that an account has at least the expected data length.
    pub fn validate_data_len(account: &Account, min_len: usize) -> Result<()> {
        if account.data.len() < min_len {
            return Err(ToolkitError::InvalidAccountData(format!(
                "Account data too short: expected at least {}, got {}",
                min_len,
                account.data.len()
            )));
        }
        Ok(())
    }

    /// Validate that an account is rent exempt.
    pub fn validate_rent_exempt(account: &Account, rent: &solana_sdk::rent::Rent) -> Result<()> {
        if !rent.is_exempt(account.lamports, account.data.len()) {
            return Err(ToolkitError::InsufficientBalance {
                required: rent.minimum_balance(account.data.len()),
                available: account.lamports,
            });
        }
        Ok(())
    }

    /// Validate that an account is writable (not a program).
    pub fn validate_writable(account: &Account) -> Result<()> {
        if account.executable {
            return Err(ToolkitError::InvalidAccountData(
                "Account is executable and cannot be written to".to_string(),
            ));
        }
        Ok(())
    }

    /// Validate an Anchor account with discriminator and owner.
    pub fn validate_anchor_account(
        account: &Account,
        account_name: &str,
        expected_owner: &Pubkey,
    ) -> Result<()> {
        validate_owner(account, expected_owner)?;
        let discriminator = account_discriminator(account_name);
        validate_discriminator(account, &discriminator)?;
        Ok(())
    }
}

// ============================================================================
// Account Data Serialization Helpers
// ============================================================================

/// Helpers for serializing instruction data.
pub mod serialization {
    use super::*;
    use borsh::BorshSerialize;

    /// Serialize Anchor instruction data with discriminator.
    pub fn serialize_anchor_ix<T: BorshSerialize>(
        instruction_name: &str,
        args: &T,
    ) -> Result<Vec<u8>> {
        let discriminator = instruction_discriminator(instruction_name);
        let mut data = Vec::with_capacity(256);
        data.extend_from_slice(&discriminator);
        args.serialize(&mut data)
            .map_err(|e| ToolkitError::Custom(format!("Serialization error: {}", e)))?;
        Ok(data)
    }

    /// Serialize data with a custom discriminator.
    pub fn serialize_with_discriminator<T: BorshSerialize>(
        discriminator: &[u8; 8],
        args: &T,
    ) -> Result<Vec<u8>> {
        let mut data = Vec::with_capacity(256);
        data.extend_from_slice(discriminator);
        args.serialize(&mut data)
            .map_err(|e| ToolkitError::Custom(format!("Serialization error: {}", e)))?;
        Ok(data)
    }

    /// Serialize just the args without discriminator.
    pub fn serialize_args<T: BorshSerialize>(args: &T) -> Result<Vec<u8>> {
        let mut data = Vec::new();
        args.serialize(&mut data)
            .map_err(|e| ToolkitError::Custom(format!("Serialization error: {}", e)))?;
        Ok(data)
    }
}

// ============================================================================
// Remaining Accounts Builder
// ============================================================================

/// Builder for constructing remaining accounts for Anchor instructions.
#[derive(Default)]
pub struct RemainingAccountsBuilder {
    accounts: Vec<AccountMeta>,
}

impl RemainingAccountsBuilder {
    /// Create a new builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a writable account.
    pub fn writable(mut self, pubkey: Pubkey) -> Self {
        self.accounts.push(AccountMeta::new(pubkey, false));
        self
    }

    /// Add a readonly account.
    pub fn readonly(mut self, pubkey: Pubkey) -> Self {
        self.accounts.push(AccountMeta::new_readonly(pubkey, false));
        self
    }

    /// Add a signer account.
    pub fn signer(mut self, pubkey: Pubkey) -> Self {
        self.accounts.push(AccountMeta::new(pubkey, true));
        self
    }

    /// Add multiple writable accounts.
    pub fn writables(mut self, pubkeys: &[Pubkey]) -> Self {
        for pubkey in pubkeys {
            self.accounts.push(AccountMeta::new(*pubkey, false));
        }
        self
    }

    /// Add multiple readonly accounts.
    pub fn readonlys(mut self, pubkeys: &[Pubkey]) -> Self {
        for pubkey in pubkeys {
            self.accounts.push(AccountMeta::new_readonly(*pubkey, false));
        }
        self
    }

    /// Build the remaining accounts vector.
    pub fn build(self) -> Vec<AccountMeta> {
        self.accounts
    }

    /// Get the number of accounts.
    pub fn len(&self) -> usize {
        self.accounts.len()
    }

    /// Check if empty.
    pub fn is_empty(&self) -> bool {
        self.accounts.is_empty()
    }
}

// ============================================================================
// Common Anchor Account Sizes
// ============================================================================

/// Common account sizes for Anchor programs.
pub mod sizes {
    /// Size of the Anchor account discriminator.
    pub const DISCRIMINATOR: usize = 8;

    /// Size of a Pubkey.
    pub const PUBKEY: usize = 32;

    /// Size of a u64.
    pub const U64: usize = 8;

    /// Size of a u128.
    pub const U128: usize = 16;

    /// Size of an i64.
    pub const I64: usize = 8;

    /// Size of a bool.
    pub const BOOL: usize = 1;

    /// Size of a u8.
    pub const U8: usize = 1;

    /// Size of a u16.
    pub const U16: usize = 2;

    /// Size of a u32.
    pub const U32: usize = 4;

    /// Calculate size for a string with max length.
    pub const fn string(max_len: usize) -> usize {
        4 + max_len // 4 bytes for length prefix + content
    }

    /// Calculate size for a vector with max elements.
    pub const fn vec<const ELEMENT_SIZE: usize>(max_elements: usize) -> usize {
        4 + (ELEMENT_SIZE * max_elements) // 4 bytes for length prefix + elements
    }

    /// Calculate size for an Option<T>.
    pub const fn option(inner_size: usize) -> usize {
        1 + inner_size // 1 byte for Some/None tag + inner
    }
}

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

    #[test]
    fn test_account_discriminator() {
        let disc = account_discriminator("TestAccount");
        assert_eq!(disc.len(), 8);
    }

    #[test]
    fn test_instruction_discriminator() {
        let disc = instruction_discriminator("initialize");
        assert_eq!(disc.len(), 8);
    }

    #[test]
    fn test_cpi_builder() {
        let program_id = Pubkey::new_unique();
        let account = Pubkey::new_unique();

        let ix = CpiInstructionBuilder::new()
            .program(program_id)
            .signer(account)
            .data(vec![1, 2, 3])
            .build()
            .unwrap();

        assert_eq!(ix.program_id, program_id);
        assert_eq!(ix.accounts.len(), 1);
        assert_eq!(ix.data, vec![1, 2, 3]);
    }

    #[test]
    fn test_remaining_accounts_builder() {
        let pubkey1 = Pubkey::new_unique();
        let pubkey2 = Pubkey::new_unique();

        let accounts = RemainingAccountsBuilder::new()
            .writable(pubkey1)
            .readonly(pubkey2)
            .build();

        assert_eq!(accounts.len(), 2);
        assert!(accounts[0].is_writable);
        assert!(!accounts[1].is_writable);
    }

    #[test]
    fn test_sizes() {
        assert_eq!(sizes::DISCRIMINATOR, 8);
        assert_eq!(sizes::PUBKEY, 32);
        assert_eq!(sizes::string(100), 104);
    }
}