Created
May 2, 2025 03:44
-
-
Save buffalojoec/9a40061bbb63664ac6ba3f8ad6ef6621 to your computer and use it in GitHub Desktop.
Did you know Solana account roles (signer, writable) are transaction-wide?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, msg, pubkey::Pubkey}; | |
solana_program::entrypoint!(process); | |
fn process(_program_id: &Pubkey, accounts: &[AccountInfo], _input: &[u8]) -> ProgramResult { | |
let account = accounts.first().unwrap(); | |
msg!("KEY: {}", account.key); | |
msg!("IS_SIGNER: {}", account.is_signer); | |
msg!("IS_WRITABLE: {}", account.is_writable); | |
Ok(()) | |
} | |
#[cfg(all(test, feature = "test-sbf"))] | |
mod tests { | |
use { | |
super::*, | |
solana_sdk::{ | |
instruction::{AccountMeta, Instruction}, | |
signer::Signer, | |
transaction::Transaction, | |
}, | |
solana_program_test::{ProgramTest, ProgramTestContext, tokio}, | |
}; | |
const PROGRAM_ID: Pubkey = solana_program::pubkey!("BpC2Xr5dFTKZ2AHSZWVEdQN7RKgnnsEagG3Gcv13q62n"); | |
fn readonly_ix(key: &Pubkey) -> Instruction { | |
let accounts = vec![AccountMeta::new_readonly(*key, false)]; | |
Instruction::new_with_bytes(PROGRAM_ID, &[], accounts) | |
} | |
fn signer_ix(key: &Pubkey) -> Instruction { | |
let accounts = vec![AccountMeta::new_readonly(*key, true)]; | |
Instruction::new_with_bytes(PROGRAM_ID, &[], accounts) | |
} | |
fn writeable_signer_ix(key: &Pubkey) -> Instruction { | |
let accounts = vec![AccountMeta::new(*key, true)]; | |
Instruction::new_with_bytes(PROGRAM_ID, &[], accounts) | |
} | |
#[tokio::test] | |
async fn test() { | |
let program_test = ProgramTest::new("test_program", PROGRAM_ID, None); | |
let ProgramTestContext { banks_client, last_blockhash, payer, .. } = program_test.start_with_context().await; | |
let tx = Transaction::new_signed_with_payer( | |
&[ | |
readonly_ix(&payer.pubkey()), | |
signer_ix(&payer.pubkey()), | |
writeable_signer_ix(&payer.pubkey()), | |
], | |
Some(&payer.pubkey()), | |
&[&payer], | |
last_blockhash, | |
); | |
banks_client.process_transaction(tx).await.unwrap(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment