1

I need a fixed amount(10) to be passed in so I'm not sure if that would be a tuple, vector or array in Rust.

Something like this maybe? see "pub keys" account.

pub struct VerifyReview<'info> {
    #[account(init, payer = author, space = Verify::LEN)]
    pub verify: Account<'info, Verify>,
    #[account(mut)]
    pub author: Signer<'info>, 
    pub keys: Vec<PubKeys>,
    pub system_program: Program<'info, System>,
}

Typescript

    it('passes in keys as account vec', async () => { 
         const contentAccounts = await program.account.content.all(); 
         let theKeys = contentAccounts;
         const verify = anchor.web3.Keypair.generate(); 
         await program.rpc.validateV0({ 
             accounts: { 
                 verify: verify.publicKey, 
                 author: program.provider.wallet.publicKey, 
                 keys: theKeys, 
                 systemProgram: anchor.web3.SystemProgram.programId, 
             }, 
             signers: [verify], 
         }); 
  
         const validateAccount = await program.account.verify.fetch(verify.publicKey); 
     });

1 Answer 1

6

You can use the remaining_accounts feature of the context.

In Typescript you would pass them like this:

await program.rpc.verifyReview({
  accounts: { ... },
  remainingAccounts: theKeys, // This should be an array of `AccountMeta`s
  signers: [...],
});

Or, in newer versions of anchor sdk

await program.methods
  .verifyReview()
  .accounts({...})
  .remainingAccounts(theKeys) // Still an array of AccountMetas
  .signers([...])
  .rpc();

Then your context in rust will look like

pub struct VerifyReview<'info> {
    #[account(init, payer = author, space = Verify::LEN)]
    pub verify: Account<'info, Verify>,
    #[account(mut)]
    pub author: Signer<'info>, 
    pub system_program: Program<'info, System>,
}

And in your handler function you can access the pubkeys like so:

pub fn verify_review(ctx: Context<VerifyReview>) -> Result<()> {
  // Get the first account in `theKeys` array
  let account = ctx.remaining_accounts[0]; // Keep in mind that each account in the remaining accounts array is a AccountInfo type.

  // Get the pubkey of the account
  let account_key = account.key();
  
  ...
}

Remaining accounts are not validated by Anchor in any way. So if you plan to use them be sure to add relevant checks to ensure the accounts have the correct pubkey/data.

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.