I need help in storing data into an account. Here's what I've done so far.
Below is my 'process_instruction' entry point,
pub fn process_instruction(...) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
// get account where we have to store state
let states_account_info = next_account_info(account_info_iter)?;
let xyz: u64 = 1234567;
let counter_struct = Counter {
data: xyz
};
counter_struct.serialize(&mut &mut states_account_info.data.borrow_mut()[..])?;
Ok(())
}
Here's my Counter Struct
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct Counter {
/// mappings of the keys
pub data: u64,
}
And this is how, I create the account inside the test,
let state_account_pubkey = Pubkey::new_unique();
let mut program_test = ProgramTest::new(
"solana_states_save_program",
program_id,
processor!(process_instruction),
);
program_test.add_account(
state_account_pubkey,
Account {
lamports: 5,
owner: program_id,
..Account::default()
},
);
But after executing the test, I get the following error, inside process_instruction() method (on counter_struct.serialize(...) statement),
thread 'main' panicked at 'called Result::unwrap() on an Err value: TransactionError(InstructionError(0, BorshIoError("Unknown")))'
Kindly help.
We also needed to add account data space when creating an Account in tests, which I was missing.
program_test.add_account(
state_account_pubkey,
Account {
lamports: 5,
data: vec![0_u8; mem::size_of::<u32>()],
owner: program_id,
..Account::default()
},
);
Hope, it helps someone.