Skip to content
Open
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/realloc/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"
101 changes: 101 additions & 0 deletions basics/realloc/native/program/tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use litesvm::LiteSVM;
use realloc_program::state::{AddressInfo, WorkInfo};
use realloc_program::{processor::ReallocInstruction, state::EnhancedAddressInfoExtender};
use solana_instruction::Instruction;
use solana_keypair::{Keypair, Signer};
use solana_native_token::LAMPORTS_PER_SOL;
use solana_pubkey::Pubkey;
use solana_transaction::{AccountMeta, Transaction};

#[test]
fn test_realloc() {
let mut svm = LiteSVM::new();

let program_id = Pubkey::new_unique();
let program_bytes = include_bytes!("../../../../../target/deploy/realloc_program.so");

svm.add_program(program_id, program_bytes).unwrap();

let payer = Keypair::new();
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL).unwrap();

let test_account = Keypair::new();

let address_info = AddressInfo {
name: "Jacob".to_string(),
house_number: 123,
street: "Main St.".to_string(),
city: "Chicago".to_string(),
};

let data = borsh::to_vec(&ReallocInstruction::Create(address_info)).unwrap();

let ix = Instruction {
program_id,
accounts: vec![
AccountMeta::new(test_account.pubkey(), true),
AccountMeta::new(payer.pubkey(), true),
AccountMeta::new(solana_system_interface::program::ID, false),
],
data,
};

let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&payer.pubkey()),
&[&payer, &test_account],
svm.latest_blockhash(),
);

let _ = svm.send_transaction(tx).is_ok();

let data = borsh::to_vec(&ReallocInstruction::ReallocateWithoutZeroInit(
EnhancedAddressInfoExtender {
state: "Illinois".to_string(),
zip: 12345,
},
))
.unwrap();

let ix = Instruction {
program_id,
accounts: vec![
AccountMeta::new(test_account.pubkey(), false),
AccountMeta::new(payer.pubkey(), true),
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 data = borsh::to_vec(&ReallocInstruction::ReallocateZeroInit(WorkInfo {
name: "Pete".to_string(),
position: "Engineer".to_string(),
company: "Solana Labs".to_string(),
years_employed: 2,
}))
.unwrap();

let ix = Instruction {
program_id,
accounts: vec![AccountMeta::new(test_account.pubkey(), false)],
data,
};

let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&payer.pubkey()),
&[&payer],
svm.latest_blockhash(),
);

let _ = svm.send_transaction(tx).is_ok();
}