Complete Test
tests/pda_test.rs
use litesvm::LiteSVM;use solana_account::Account;use solana_sdk::pubkey::Pubkey;#[test]fn test_pda_creation() {let mut svm = LiteSVM::new();// Program that owns the PDAlet program_id = Pubkey::new_unique();// Derive PDA addresslet seed = b"my_pda";let (pda, bump) = Pubkey::find_program_address(&[seed],&program_id);println!("PDA: {}", pda);println!("Bump: {}", bump);// Create PDA account manually (for testing)svm.set_account(pda, Account {lamports: 1_000_000,data: vec![bump], // Store bump seed in dataowner: program_id,executable: false,rent_epoch: 0,}).unwrap();// Verify PDA was createdlet account = svm.get_account(&pda).unwrap();assert_eq!(account.owner, program_id);assert_eq!(account.data[0], bump);println!("PDA created successfully!");}
Key Points
- PDA Derivation: Use
Pubkey::find_program_address()to derive PDA addresses - Seeds: PDAs are derived from seeds and the program ID
- Bump Seed: The bump seed ensures the address is off the ed25519 curve
- Account Creation: Use
set_account()to manually create accounts for testing - Ownership: PDAs must be owned by the program that derived them
Is this page helpful?