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?

  • Could you provide some context on where remain_accounts is from?
    – C.OG
    Commented Dec 7, 2022 at 15:52
  • I was looking at possible solutions, and found this one (amongst a few other examples referring the same): solana.stackexchange.com/questions/633/… However, every search that I did regarding this, seems somehow confusing. Therefore, I was looking for a more straight forward solution (if possible) or, last case scenario, understand this one.
    – ASCorreia
    Commented Dec 7, 2022 at 16:33
  • I actually just found this: medium.com/@Arrivant_/gaming-applications-anchor-a89e37aeb5ad Will have a deeper look at it and see what I can extract =) (at first, it looks like a long thread on anchor "remaining_accounts" which can be quite useful)
    – ASCorreia
    Commented Dec 8, 2022 at 0:36
  • Come back when you're done, and post an answer to your own question with what you learned! Commented Dec 21, 2022 at 8:54
  • @steveluscher, forgot about this.. :( will post a possible answer in a min
    – ASCorreia
    Commented Feb 5, 2023 at 23:59

1 Answer 1


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())?;

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.