From dcaeca2a68227d98ef28c8c116c088ba4e9062a3 Mon Sep 17 00:00:00 2001 From: armaniferrante Date: Mon, 31 May 2021 14:06:37 -0700 Subject: [PATCH 1/3] lang: Implement ToAccountInfos and ToAccountMetas for CpiContext --- lang/src/context.rs | 17 +++++++++++++++++ lang/syn/src/codegen/program/cpi.rs | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lang/src/context.rs b/lang/src/context.rs index c2491d54f5..c570cd4ae2 100644 --- a/lang/src/context.rs +++ b/lang/src/context.rs @@ -85,6 +85,23 @@ impl<'info, T: Accounts<'info>> ToAccountInfos<'info> for CpiContext<'_, '_, '_, } } +impl<'info, T: Accounts<'info>> ToAccountMetas for CpiContext<'_, '_, '_, 'info, T> { + fn to_account_metas(&self, is_signer: Option) -> Vec { + let mut metas = self.accounts.to_account_metas(is_signer); + metas.append( + &mut self + .remaining_accounts + .iter() + .map(|acc| match acc.is_writable { + false => AccountMeta::new_readonly(*acc.key, acc.is_signer), + true => AccountMeta::new(*acc.key, acc.is_signer), + }) + .collect(), + ); + metas + } +} + /// Context specifying non-argument inputs for cross-program-invocations /// targeted at program state instructions. pub struct CpiStateContext<'a, 'b, 'c, 'info, T: Accounts<'info>> { diff --git a/lang/syn/src/codegen/program/cpi.rs b/lang/syn/src/codegen/program/cpi.rs index 4a7e248118..413b602887 100644 --- a/lang/syn/src/codegen/program/cpi.rs +++ b/lang/syn/src/codegen/program/cpi.rs @@ -80,14 +80,14 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { .map_err(|_| ProgramError::InvalidInstructionData)?; let mut data = #sighash_tts.to_vec(); data.append(&mut ix_data); - let accounts = ctx.accounts.to_account_metas(None); + let accounts = ctx.to_account_metas(None); anchor_lang::solana_program::instruction::Instruction { program_id: *ctx.program.key, accounts, data, } }; - let mut acc_infos = ctx.accounts.to_account_infos(); + let mut acc_infos = ctx.to_account_infos(); acc_infos.push(ctx.program.clone()); anchor_lang::solana_program::program::invoke_signed( &ix, From c10731eca4f9d39a1da3a9a6541ead4c29fad065 Mon Sep 17 00:00:00 2001 From: armaniferrante Date: Mon, 31 May 2021 14:22:43 -0700 Subject: [PATCH 2/3] changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index df1d5276b9..14b1c52dcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ incremented for features. ## [Unreleased] +### Fixes + +* lang: Allows one to use `remaining_accounts` with `CpiContext` by implementing the `ToAccountMetas` trait on `CpiContext` ([#351](https://github.com/project-serum/anchor/pull/351/files)). + ## [0.7.0] - 2021-05-31 ### Features From d5b988951e22fdc5a16131eac349666d5352e017 Mon Sep 17 00:00:00 2001 From: armaniferrante Date: Mon, 31 May 2021 14:23:34 -0700 Subject: [PATCH 3/3] remove unnecessary changelog section --- CHANGELOG.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14b1c52dcf..d5511f7c04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,8 +35,6 @@ incremented for features. * lang: Associated constraints no longer automatically implement `mut` ([#341](https://github.com/project-serum/anchor/pull/341)). * lang: Associated `space` constraints must now be literal integers instead of literal strings ([#341](https://github.com/project-serum/anchor/pull/341)). -### Fixes - ## [0.6.0] - 2021-05-23 ### Features