Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion basics/favorites/native/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion basics/favorites/native/program/src/processor.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion basics/favorites/native/program/src/state.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
83 changes: 83 additions & 0 deletions basics/favorites/native/program/tests/test.rs
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 0 additions & 1 deletion basics/hello-solana/native/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ solana-program.workspace = true
crate-type = ["cdylib", "lib"]

[features]
anchor-debug = []
custom-heap = []
custom-panic = []

Expand Down