Solana基础 - 如何在程序中读取帐户
Solana 中的几乎所有指令都需要至少 2 - 3 个帐户,并且它们
会在指令处理程序中提及,说明这些帐户集的预期顺序。如果我们利用iter() Rust 中的方法,而不是手动索引帐户,那么这相当简单。该 next_account_info方法基本上对可迭代对象的第一个索引进行切片,并返回帐户数组中存在的帐户。让我们看一个简单的指令,它需要一堆帐户并需要解析每个帐户。
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint,
entrypoint::ProgramResult,
pubkey::Pubkey,
};
entrypoint!(process_instruction);
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct HelloState {
is_initialized: bool,
}
// Accounts required
/// 1. [signer] Payer
/// 2. [writable] Hello state account
/// 3. [] Rent account
/// 4. [] System Program
pub fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
// Fetching all the accounts as a iterator (facilitating for loops and iterations)
let accounts_iter = &mut accounts.iter();
// Payer account
let payer_account = next_account_info(accounts_iter)?;
// Hello state account
let hello_state_account = next_account_info(accounts_iter)?;
// Rent account
let rent_account = next_account_info(accounts_iter)?;
// System Program
let system_program = next_account_info(accounts_iter)?;
Ok(())
}https://solana.com/zh/developers/cookbook/programs/read-accounts
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »