1

I'm writing an escrow program for multiple assets in one swap. I was trying an effective way to input token mint & token accounts. So I tried using Vec<> as following but it poped up the following error:

trial_1

Is there a solution to Vec<> error or an effective way to input multiple accounts?

1 Answer 1

8

No, you can only use the remaining_accounts functionality for adding a variable length number of accounts.

From one of the misc code examples in the anchor repo, this is how you can add a vector of remaining accounts with the ts library.

    await program.state.rpc.remainingAccounts({
      remainingAccounts: [
        { pubkey: misc2Program.programId, isWritable: false, isSigner: false },
      ],
    });

You can access them inside the program function with

ctx.remaining_accounts
4
  • so is remaining_accounts an array? Commented Jul 25, 2022 at 18:08
  • 1
    It's an array of account infos docs.rs/anchor-lang/0.6.0/anchor_lang/struct.Context.html
    – Henry E
    Commented Jul 25, 2022 at 18:11
  • so if i have to access one of the accounts, it will be like this: ctx.remaining_accounts[0].key() Commented Jul 25, 2022 at 18:33
  • 1
    yes, this should compile fine. Don't forget to validate the accounts though
    – Henry E
    Commented Jul 25, 2022 at 18:37

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.