I'm having a hard time trying to understand the best way (even better, trying to fully understand a way) to handle multiple accounts (both client(js / ts) and program(anchor/rust) side). If I want to call a method from my program and send multiple accounts for it to handle, is there a way to do it without using "remaining_accounts"? If yes, how could it be done? If not, how could "remaining_accounts" be properly used (I'm finding it quite confusing at the moment)? Also, how could both ways be handled, if possible, on the program side?
1 Answer
Was able to find a suitable solution to use remaining_accounts. Below is an example on how to iterate through the remaining_accounts, deserialize and change a variable on the account, and serialize it back.
pub fn some_function(ctx:Context<SomeContext>, ammount_to_change: u64) -> Result<()>
{
for account in ctx.remaining_accounts.iter() {
let _account_key = account.key();
let mut data = account.try_borrow_mut_data()?;
let mut account_to_write = BaseAccount::try_deserialize(&mut data.as_ref()).expect("Error Deserializing Data");
account_to_write.var_to_change += ammount_to_change;
account_to_write.try_serialize(&mut data.as_mut())?;
}
Ok(())
}
The call on the TS side, could be something like
const someOutput = await program.methods.someFunction(new anchor.BN('insert ammount here')).accounts({
}).remainingAccounts([{pubkey: yourPDA, isSigner: false, isWritable: true}]).rpc();
remain_accounts
is from?