diff --git a/Cargo.lock b/Cargo.lock index 9a4b9e2c6..46886ec20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1297,8 +1297,14 @@ version = "0.1.0" dependencies = [ "borsh 1.5.7", "borsh-derive 1.5.7", + "litesvm", + "solana-instruction 3.0.0", + "solana-keypair", + "solana-native-token 3.0.0", "solana-program 3.0.0", + "solana-pubkey 3.0.0", "solana-system-interface 2.0.0", + "solana-transaction", ] [[package]] diff --git a/basics/favorites/native/program/Cargo.toml b/basics/favorites/native/program/Cargo.toml index 568d968dd..fab1cd066 100644 --- a/basics/favorites/native/program/Cargo.toml +++ b/basics/favorites/native/program/Cargo.toml @@ -13,9 +13,16 @@ solana-system-interface.workspace = true crate-type = ["cdylib", "lib"] [features] -anchor-debug = [] custom-heap = [] custom-panic = [] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] } + +[dev-dependencies] +litesvm = "0.8.1" +solana-instruction = "3.0.0" +solana-keypair = "3.0.1" +solana-native-token = "3.0.0" +solana-pubkey = "3.0.0" +solana-transaction = "3.0.1" diff --git a/basics/favorites/native/program/src/processor.rs b/basics/favorites/native/program/src/processor.rs index f8439993e..f28c913fc 100644 --- a/basics/favorites/native/program/src/processor.rs +++ b/basics/favorites/native/program/src/processor.rs @@ -1,7 +1,7 @@ use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey}; use crate::instructions::{create_pda::*, get_pda::*}; -use crate::state::Favorites; +pub use crate::state::Favorites; use borsh::{BorshDeserialize, BorshSerialize}; #[derive(BorshDeserialize, BorshSerialize)] diff --git a/basics/favorites/native/program/src/state.rs b/basics/favorites/native/program/src/state.rs index 67c5f0407..a3ef56c46 100644 --- a/basics/favorites/native/program/src/state.rs +++ b/basics/favorites/native/program/src/state.rs @@ -1,6 +1,6 @@ use borsh::{BorshDeserialize, BorshSerialize}; -#[derive(BorshDeserialize, BorshSerialize, Debug)] +#[derive(BorshDeserialize, BorshSerialize, Debug, Clone)] pub struct Favorites { pub number: u64, pub color: String, diff --git a/basics/favorites/native/program/tests/test.rs b/basics/favorites/native/program/tests/test.rs new file mode 100644 index 000000000..279418659 --- /dev/null +++ b/basics/favorites/native/program/tests/test.rs @@ -0,0 +1,83 @@ +use borsh::BorshDeserialize; +use favorites_native::processor::{Favorites, FavoritesInstruction}; +use litesvm::LiteSVM; +use solana_instruction::{AccountMeta, Instruction}; +use solana_keypair::{Keypair, Signer}; +use solana_native_token::LAMPORTS_PER_SOL; +use solana_pubkey::Pubkey; +use solana_transaction::Transaction; + +#[test] +fn test_favorites() { + let program_id = Pubkey::new_unique(); + let program_bytes = include_bytes!("../../tests/fixtures/favorites_native.so"); + + let mut svm = LiteSVM::new(); + svm.add_program(program_id, program_bytes).unwrap(); + + let payer = Keypair::new(); + + svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap(); + + let favorites_pda = + Pubkey::find_program_address(&[b"favorite", payer.pubkey().as_ref()], &program_id).0; + + let favorites = Favorites { + number: 42, + color: "blue".to_string(), + hobbies: vec![ + "coding".to_string(), + "reading".to_string(), + "travelling".to_string(), + ], + }; + + let data = borsh::to_vec(&FavoritesInstruction::CreatePda(favorites.clone())).unwrap(); + + let ix = Instruction { + program_id, + accounts: vec![ + AccountMeta::new(payer.pubkey(), true), + AccountMeta::new(favorites_pda, false), + AccountMeta::new(solana_system_interface::program::ID, false), + ], + data, + }; + + let tx = Transaction::new_signed_with_payer( + &[ix], + Some(&payer.pubkey()), + &[&payer], + svm.latest_blockhash(), + ); + + let _ = svm.send_transaction(tx).is_ok(); + + let favorites_account_data = svm.get_account(&favorites_pda).unwrap().data; + + let deserialized_data = Favorites::try_from_slice(&favorites_account_data).unwrap(); + + assert_eq!(deserialized_data.number, favorites.number); + assert_eq!(deserialized_data.color, favorites.color); + assert_eq!(deserialized_data.hobbies, favorites.hobbies); + + let data = borsh::to_vec(&FavoritesInstruction::GetPda).unwrap(); + + let ix = Instruction { + program_id, + accounts: vec![ + AccountMeta::new(payer.pubkey(), true), + AccountMeta::new(favorites_pda, false), + ], + data, + }; + + let tx = Transaction::new_signed_with_payer( + &[ix], + Some(&payer.pubkey()), + &[&payer], + svm.latest_blockhash(), + ); + + let _ = svm.send_transaction(tx).is_ok(); +} diff --git a/basics/hello-solana/native/program/Cargo.toml b/basics/hello-solana/native/program/Cargo.toml index 5aa41edbd..239c8bcfe 100644 --- a/basics/hello-solana/native/program/Cargo.toml +++ b/basics/hello-solana/native/program/Cargo.toml @@ -10,7 +10,6 @@ solana-program.workspace = true crate-type = ["cdylib", "lib"] [features] -anchor-debug = [] custom-heap = [] custom-panic = []