From 2e328c42f72f5f12488dc17da7d84f043c32be22 Mon Sep 17 00:00:00 2001 From: Jean Marchand Date: Thu, 28 Dec 2023 22:02:28 +0100 Subject: [PATCH 1/2] lang: Remove program account info from CPI context --- lang/src/context.rs | 40 +++++++++++++--- lang/syn/src/codegen/accounts/constraints.rs | 20 ++++---- lang/syn/src/codegen/program/cpi.rs | 2 +- lang/syn/src/codegen/program/idl.rs | 1 - spl/src/token_2022.rs | 49 +++++++++++--------- tests/cpi-returns/programs/caller/src/lib.rs | 9 ++-- 6 files changed, 72 insertions(+), 49 deletions(-) diff --git a/lang/src/context.rs b/lang/src/context.rs index 6c2b7dfe92..43996bedab 100644 --- a/lang/src/context.rs +++ b/lang/src/context.rs @@ -174,7 +174,7 @@ where { pub accounts: T, pub remaining_accounts: Vec>, - pub program: AccountInfo<'info>, + pub program_id: Option, pub signer_seeds: &'a [&'b [&'c [u8]]], } @@ -182,29 +182,56 @@ impl<'a, 'b, 'c, 'info, T> CpiContext<'a, 'b, 'c, 'info, T> where T: ToAccountMetas + ToAccountInfos<'info>, { - pub fn new(program: AccountInfo<'info>, accounts: T) -> Self { + #[must_use] + pub fn new(accounts: T) -> Self { Self { accounts, - program, + program_id: None, remaining_accounts: Vec::new(), signer_seeds: &[], } } #[must_use] - pub fn new_with_signer( - program: AccountInfo<'info>, + pub fn new_with_id(accounts: T, program_id: Pubkey) -> Self { + Self { + accounts, + program_id: Some(program_id), + remaining_accounts: Vec::new(), + signer_seeds: &[], + } + } + + #[must_use] + pub fn new_with_signer(accounts: T, signer_seeds: &'a [&'b [&'c [u8]]]) -> Self { + Self { + accounts, + program_id: None, + signer_seeds, + remaining_accounts: Vec::new(), + } + } + + #[must_use] + pub fn new_with_id_and_signer( accounts: T, + program_id: Pubkey, signer_seeds: &'a [&'b [&'c [u8]]], ) -> Self { Self { accounts, - program, + program_id: Some(program_id), signer_seeds, remaining_accounts: Vec::new(), } } + #[must_use] + pub fn with_program_id(mut self, program_id: Pubkey) -> Self { + self.program_id = Some(program_id); + self + } + #[must_use] pub fn with_signer(mut self, signer_seeds: &'a [&'b [&'c [u8]]]) -> Self { self.signer_seeds = signer_seeds; @@ -224,7 +251,6 @@ impl<'info, T: ToAccountInfos<'info> + ToAccountMetas> ToAccountInfos<'info> fn to_account_infos(&self) -> Vec> { let mut infos = self.accounts.to_account_infos(); infos.extend_from_slice(&self.remaining_accounts); - infos.push(self.program.clone()); infos } } diff --git a/lang/syn/src/codegen/accounts/constraints.rs b/lang/syn/src/codegen/accounts/constraints.rs index 0418501df8..87c5ce6811 100644 --- a/lang/syn/src/codegen/accounts/constraints.rs +++ b/lang/syn/src/codegen/accounts/constraints.rs @@ -384,7 +384,6 @@ fn generate_constraint_realloc( if __new_rent_minimum > __field_info.lamports() { anchor_lang::system_program::transfer( anchor_lang::context::CpiContext::new( - system_program.to_account_info(), anchor_lang::system_program::Transfer { from: #payer.to_account_info(), to: __field_info.clone(), @@ -558,13 +557,13 @@ fn generate_constraint_init_group( #create_account // Initialize the token account. - let cpi_program = #token_program.to_account_info(); + let cpi_program_id = #token_program.key(); let accounts = ::anchor_spl::token_interface::InitializeAccount3 { account: #field.to_account_info(), mint: #mint.to_account_info(), authority: #owner.to_account_info(), }; - let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts); + let cpi_ctx = anchor_lang::context::CpiContext::new_with_id(accounts, cpi_program_id); ::anchor_spl::token_interface::initialize_account3(cpi_ctx)?; } @@ -625,7 +624,6 @@ fn generate_constraint_init_group( if !#if_needed || owner_program == &anchor_lang::solana_program::system_program::ID { #payer_optional_check - let cpi_program = associated_token_program.to_account_info(); let cpi_accounts = ::anchor_spl::associated_token::Create { payer: #payer.to_account_info(), associated_token: #field.to_account_info(), @@ -634,7 +632,7 @@ fn generate_constraint_init_group( system_program: system_program.to_account_info(), token_program: #token_program.to_account_info(), }; - let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, cpi_accounts); + let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_accounts); ::anchor_spl::associated_token::create(cpi_ctx)?; } let pa: #ty_decl = #from_account_info_unchecked; @@ -717,11 +715,11 @@ fn generate_constraint_init_group( #create_account // Initialize the mint account. - let cpi_program = #token_program.to_account_info(); + let cpi_program_id = #token_program.key(); let accounts = ::anchor_spl::token_interface::InitializeMint2 { mint: #field.to_account_info(), }; - let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts); + let cpi_ctx = anchor_lang::context::CpiContext::new_with_id(accounts, cpi_program_id); ::anchor_spl::token_interface::initialize_mint2(cpi_ctx, #decimals, &#owner.key(), #freeze_authority)?; } let pa: #ty_decl = #from_account_info_unchecked; @@ -1166,7 +1164,7 @@ fn generate_create_account( from: #payer.to_account_info(), to: #field.to_account_info() }; - let cpi_context = anchor_lang::context::CpiContext::new(system_program.to_account_info(), cpi_accounts); + let cpi_context = anchor_lang::context::CpiContext::new(cpi_accounts); anchor_lang::system_program::create_account(cpi_context.with_signer(&[#seeds_with_nonce]), lamports, space as u64, #owner)?; } else { require_keys_neq!(#payer.key(), #field.key(), anchor_lang::error::ErrorCode::TryingToInitPayerAsProgramAccount); @@ -1180,20 +1178,20 @@ fn generate_create_account( from: #payer.to_account_info(), to: #field.to_account_info(), }; - let cpi_context = anchor_lang::context::CpiContext::new(system_program.to_account_info(), cpi_accounts); + let cpi_context = anchor_lang::context::CpiContext::new(cpi_accounts); anchor_lang::system_program::transfer(cpi_context, required_lamports)?; } // Allocate space. let cpi_accounts = anchor_lang::system_program::Allocate { account_to_allocate: #field.to_account_info() }; - let cpi_context = anchor_lang::context::CpiContext::new(system_program.to_account_info(), cpi_accounts); + let cpi_context = anchor_lang::context::CpiContext::new(cpi_accounts); anchor_lang::system_program::allocate(cpi_context.with_signer(&[#seeds_with_nonce]), #space as u64)?; // Assign to the spl token program. let cpi_accounts = anchor_lang::system_program::Assign { account_to_assign: #field.to_account_info() }; - let cpi_context = anchor_lang::context::CpiContext::new(system_program.to_account_info(), cpi_accounts); + let cpi_context = anchor_lang::context::CpiContext::new(cpi_accounts); anchor_lang::system_program::assign(cpi_context.with_signer(&[#seeds_with_nonce]), #owner)?; } } diff --git a/lang/syn/src/codegen/program/cpi.rs b/lang/syn/src/codegen/program/cpi.rs index c099d2e653..426f8a0e12 100644 --- a/lang/syn/src/codegen/program/cpi.rs +++ b/lang/syn/src/codegen/program/cpi.rs @@ -40,7 +40,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotSerialize)?; let accounts = ctx.to_account_metas(None); anchor_lang::solana_program::instruction::Instruction { - program_id: ctx.program.key(), + program_id: ctx.program_id.unwrap_or(crate::ID), accounts, data, } diff --git a/lang/syn/src/codegen/program/idl.rs b/lang/syn/src/codegen/program/idl.rs index 757a2097e5..0a183918db 100644 --- a/lang/syn/src/codegen/program/idl.rs +++ b/lang/syn/src/codegen/program/idl.rs @@ -223,7 +223,6 @@ pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream { let new_rent_minimum = sysvar_rent.minimum_balance(new_account_space); anchor_lang::system_program::transfer( anchor_lang::context::CpiContext::new( - accounts.system_program.to_account_info(), anchor_lang::system_program::Transfer { from: accounts.authority.to_account_info(), to: accounts.idl.to_account_info(), diff --git a/spl/src/token_2022.rs b/spl/src/token_2022.rs index 05902f92e8..d79017cb72 100644 --- a/spl/src/token_2022.rs +++ b/spl/src/token_2022.rs @@ -17,7 +17,7 @@ pub fn transfer<'info>( ) -> Result<()> { #[allow(deprecated)] let ix = spl_token_2022::instruction::transfer( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.from.key, ctx.accounts.to.key, ctx.accounts.authority.key, @@ -38,7 +38,7 @@ pub fn transfer_checked<'info>( decimals: u8, ) -> Result<()> { let ix = spl_token_2022::instruction::transfer_checked( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.from.key, ctx.accounts.mint.key, ctx.accounts.to.key, @@ -65,7 +65,7 @@ pub fn mint_to<'info>( amount: u64, ) -> Result<()> { let ix = spl_token_2022::instruction::mint_to( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.mint.key, ctx.accounts.to.key, ctx.accounts.authority.key, @@ -82,7 +82,7 @@ pub fn mint_to<'info>( pub fn burn<'info>(ctx: CpiContext<'_, '_, '_, 'info, Burn<'info>>, amount: u64) -> Result<()> { let ix = spl_token_2022::instruction::burn( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.from.key, ctx.accounts.mint.key, ctx.accounts.authority.key, @@ -102,7 +102,7 @@ pub fn approve<'info>( amount: u64, ) -> Result<()> { let ix = spl_token_2022::instruction::approve( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.to.key, ctx.accounts.delegate.key, ctx.accounts.authority.key, @@ -123,7 +123,7 @@ pub fn approve<'info>( pub fn revoke<'info>(ctx: CpiContext<'_, '_, '_, 'info, Revoke<'info>>) -> Result<()> { let ix = spl_token_2022::instruction::revoke( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.source.key, ctx.accounts.authority.key, &[], @@ -140,7 +140,7 @@ pub fn initialize_account<'info>( ctx: CpiContext<'_, '_, '_, 'info, InitializeAccount<'info>>, ) -> Result<()> { let ix = spl_token_2022::instruction::initialize_account( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.account.key, ctx.accounts.mint.key, ctx.accounts.authority.key, @@ -161,7 +161,7 @@ pub fn initialize_account3<'info>( ctx: CpiContext<'_, '_, '_, 'info, InitializeAccount3<'info>>, ) -> Result<()> { let ix = spl_token_2022::instruction::initialize_account3( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.account.key, ctx.accounts.mint.key, ctx.accounts.authority.key, @@ -172,7 +172,7 @@ pub fn initialize_account3<'info>( pub fn close_account<'info>(ctx: CpiContext<'_, '_, '_, 'info, CloseAccount<'info>>) -> Result<()> { let ix = spl_token_2022::instruction::close_account( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.account.key, ctx.accounts.destination.key, ctx.accounts.authority.key, @@ -194,7 +194,7 @@ pub fn freeze_account<'info>( ctx: CpiContext<'_, '_, '_, 'info, FreezeAccount<'info>>, ) -> Result<()> { let ix = spl_token_2022::instruction::freeze_account( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.account.key, ctx.accounts.mint.key, ctx.accounts.authority.key, @@ -214,7 +214,7 @@ pub fn freeze_account<'info>( pub fn thaw_account<'info>(ctx: CpiContext<'_, '_, '_, 'info, ThawAccount<'info>>) -> Result<()> { let ix = spl_token_2022::instruction::thaw_account( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.account.key, ctx.accounts.mint.key, ctx.accounts.authority.key, @@ -239,7 +239,7 @@ pub fn initialize_mint<'info>( freeze_authority: Option<&Pubkey>, ) -> Result<()> { let ix = spl_token_2022::instruction::initialize_mint( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.mint.key, authority, freeze_authority, @@ -256,7 +256,7 @@ pub fn initialize_mint2<'info>( freeze_authority: Option<&Pubkey>, ) -> Result<()> { let ix = spl_token_2022::instruction::initialize_mint2( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.mint.key, authority, freeze_authority, @@ -276,7 +276,7 @@ pub fn set_authority<'info>( } let ix = spl_token_2022::instruction::set_authority( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.account_or_mint.key, spl_new_authority, authority_type, @@ -292,7 +292,10 @@ pub fn set_authority<'info>( } pub fn sync_native<'info>(ctx: CpiContext<'_, '_, '_, 'info, SyncNative<'info>>) -> Result<()> { - let ix = spl_token_2022::instruction::sync_native(ctx.program.key, ctx.accounts.account.key)?; + let ix = spl_token_2022::instruction::sync_native( + &ctx.program_id.unwrap_or(spl_token_2022::ID), + ctx.accounts.account.key, + )?; solana_program::program::invoke(&ix, &[ctx.accounts.account]).map_err(Into::into) } @@ -301,7 +304,7 @@ pub fn get_account_data_size<'info>( extension_types: &[spl_token_2022::extension::ExtensionType], ) -> Result { let ix = spl_token_2022::instruction::get_account_data_size( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.mint.key, extension_types, )?; @@ -309,7 +312,7 @@ pub fn get_account_data_size<'info>( solana_program::program::get_return_data() .ok_or(solana_program::program_error::ProgramError::InvalidInstructionData) .and_then(|(key, data)| { - if key != *ctx.program.key { + if key != ctx.program_id.unwrap_or(spl_token_2022::ID) { Err(solana_program::program_error::ProgramError::IncorrectProgramId) } else { data.try_into().map(u64::from_le_bytes).map_err(|_| { @@ -325,7 +328,7 @@ pub fn initialize_mint_close_authority<'info>( close_authority: Option<&Pubkey>, ) -> Result<()> { let ix = spl_token_2022::instruction::initialize_mint_close_authority( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.mint.key, close_authority, )?; @@ -336,7 +339,7 @@ pub fn initialize_immutable_owner<'info>( ctx: CpiContext<'_, '_, '_, 'info, InitializeImmutableOwner<'info>>, ) -> Result<()> { let ix = spl_token_2022::instruction::initialize_immutable_owner( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.account.key, )?; solana_program::program::invoke(&ix, &[ctx.accounts.account]).map_err(Into::into) @@ -347,7 +350,7 @@ pub fn amount_to_ui_amount<'info>( amount: u64, ) -> Result { let ix = spl_token_2022::instruction::amount_to_ui_amount( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.account.key, amount, )?; @@ -355,7 +358,7 @@ pub fn amount_to_ui_amount<'info>( solana_program::program::get_return_data() .ok_or(solana_program::program_error::ProgramError::InvalidInstructionData) .and_then(|(key, data)| { - if key != *ctx.program.key { + if key != ctx.program_id.unwrap_or(spl_token_2022::ID) { Err(solana_program::program_error::ProgramError::IncorrectProgramId) } else { String::from_utf8(data).map_err(|_| { @@ -371,7 +374,7 @@ pub fn ui_amount_to_amount<'info>( ui_amount: &str, ) -> Result { let ix = spl_token_2022::instruction::ui_amount_to_amount( - ctx.program.key, + &ctx.program_id.unwrap_or(spl_token_2022::ID), ctx.accounts.account.key, ui_amount, )?; @@ -379,7 +382,7 @@ pub fn ui_amount_to_amount<'info>( solana_program::program::get_return_data() .ok_or(solana_program::program_error::ProgramError::InvalidInstructionData) .and_then(|(key, data)| { - if key != *ctx.program.key { + if key != ctx.program_id.unwrap_or(spl_token_2022::ID) { Err(solana_program::program_error::ProgramError::IncorrectProgramId) } else { data.try_into().map(u64::from_le_bytes).map_err(|_| { diff --git a/tests/cpi-returns/programs/caller/src/lib.rs b/tests/cpi-returns/programs/caller/src/lib.rs index ff093e971e..49af0d09cf 100644 --- a/tests/cpi-returns/programs/caller/src/lib.rs +++ b/tests/cpi-returns/programs/caller/src/lib.rs @@ -16,11 +16,10 @@ pub mod caller { } pub fn cpi_call_return_u64(ctx: Context) -> Result<()> { - let cpi_program = ctx.accounts.cpi_return_program.to_account_info(); let cpi_accounts = CpiReturn { account: ctx.accounts.cpi_return.to_account_info(), }; - let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); + let cpi_ctx = CpiContext::new(cpi_accounts); let result = callee::cpi::return_u64(cpi_ctx)?; let solana_return = result.get(); anchor_lang::solana_program::log::sol_log_data(&[&solana_return.try_to_vec().unwrap()]); @@ -28,11 +27,10 @@ pub mod caller { } pub fn cpi_call_return_struct(ctx: Context) -> Result<()> { - let cpi_program = ctx.accounts.cpi_return_program.to_account_info(); let cpi_accounts = CpiReturn { account: ctx.accounts.cpi_return.to_account_info(), }; - let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); + let cpi_ctx = CpiContext::new(cpi_accounts); let result = callee::cpi::return_struct(cpi_ctx)?; let solana_return = result.get(); anchor_lang::solana_program::log::sol_log_data(&[&solana_return.try_to_vec().unwrap()]); @@ -40,11 +38,10 @@ pub mod caller { } pub fn cpi_call_return_vec(ctx: Context) -> Result<()> { - let cpi_program = ctx.accounts.cpi_return_program.to_account_info(); let cpi_accounts = CpiReturn { account: ctx.accounts.cpi_return.to_account_info(), }; - let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); + let cpi_ctx = CpiContext::new(cpi_accounts); let result = callee::cpi::return_vec(cpi_ctx)?; let solana_return = result.get(); anchor_lang::solana_program::log::sol_log_data(&[&solana_return.try_to_vec().unwrap()]); From 07689c65232a816caf5357bcaf5bd5658ac4ba4d Mon Sep 17 00:00:00 2001 From: Jean Marchand Date: Thu, 4 Jan 2024 10:46:46 +0100 Subject: [PATCH 2/2] Fix tests and update the bench --- bench/BINARY_SIZE.md | 6 +- bench/COMPUTE_UNITS.md | 178 +++++++++--------- .../basic-3/programs/puppet-master/src/lib.rs | 3 +- tests/bench/bench.json | 60 +++--- .../programs/cashiers-check/src/lib.rs | 9 +- tests/escrow/programs/escrow/src/lib.rs | 20 +- tests/ido-pool/programs/ido-pool/src/lib.rs | 33 ++-- tests/misc/programs/lamports/src/lib.rs | 11 +- .../programs/token-proxy/src/lib.rs | 24 +-- .../programs/token-wrapper/src/lib.rs | 24 +-- tests/swap/programs/swap/src/lib.rs | 4 +- tests/zero-copy/programs/zero-cpi/src/lib.rs | 3 +- 12 files changed, 178 insertions(+), 197 deletions(-) diff --git a/bench/BINARY_SIZE.md b/bench/BINARY_SIZE.md index ef36554168..a3a3959d2c 100644 --- a/bench/BINARY_SIZE.md +++ b/bench/BINARY_SIZE.md @@ -16,9 +16,9 @@ The programs and their tests are located in [/tests/bench](https://github.com/co Solana version: 1.17.0 -| Program | Binary Size | - | -| ------- | ----------- | ------------------- | -| bench | 743,208 | 🔴 **+152 (0.02%)** | +| Program | Binary Size | - | +| ------- | ----------- | ---------------------- | +| bench | 712,624 | 🟢 **-30,432 (4.10%)** | ### Notable changes diff --git a/bench/COMPUTE_UNITS.md b/bench/COMPUTE_UNITS.md index 9c969678b4..6dfd164741 100644 --- a/bench/COMPUTE_UNITS.md +++ b/bench/COMPUTE_UNITS.md @@ -16,95 +16,95 @@ The programs and their tests are located in [/tests/bench](https://github.com/co Solana version: 1.17.0 -| Instruction | Compute Units | - | -| --------------------------- | ------------- | --- | -| accountInfo1 | 695 | - | -| accountInfo2 | 1,035 | - | -| accountInfo4 | 1,730 | - | -| accountInfo8 | 3,342 | - | -| accountEmptyInit1 | 5,552 | - | -| accountEmpty1 | 819 | - | -| accountEmptyInit2 | 10,421 | - | -| accountEmpty2 | 1,275 | - | -| accountEmptyInit4 | 19,803 | - | -| accountEmpty4 | 2,177 | - | -| accountEmptyInit8 | 38,609 | - | -| accountEmpty8 | 3,990 | - | -| accountSizedInit1 | 5,647 | - | -| accountSized1 | 843 | - | -| accountSizedInit2 | 10,607 | - | -| accountSized2 | 1,317 | - | -| accountSizedInit4 | 20,225 | - | -| accountSized4 | 2,274 | - | -| accountSizedInit8 | 39,376 | - | -| accountSized8 | 4,185 | - | -| accountUnsizedInit1 | 5,740 | - | -| accountUnsized1 | 870 | - | -| accountUnsizedInit2 | 10,856 | - | -| accountUnsized2 | 1,379 | - | -| accountUnsizedInit4 | 20,652 | - | -| accountUnsized4 | 2,411 | - | -| accountUnsizedInit8 | 39,969 | - | -| accountUnsized8 | 4,478 | - | -| boxedAccountEmptyInit1 | 5,605 | - | -| boxedAccountEmpty1 | 856 | - | -| boxedAccountEmptyInit2 | 10,522 | - | -| boxedAccountEmpty2 | 1,347 | - | -| boxedAccountEmptyInit4 | 20,002 | - | -| boxedAccountEmpty4 | 2,324 | - | -| boxedAccountEmptyInit8 | 39,002 | - | -| boxedAccountEmpty8 | 4,311 | - | -| boxedAccountSizedInit1 | 5,686 | - | -| boxedAccountSized1 | 878 | - | -| boxedAccountSizedInit2 | 10,690 | - | -| boxedAccountSized2 | 1,394 | - | -| boxedAccountSizedInit4 | 20,338 | - | -| boxedAccountSized4 | 2,413 | - | -| boxedAccountSizedInit8 | 39,670 | - | -| boxedAccountSized8 | 4,493 | - | -| boxedAccountUnsizedInit1 | 5,774 | - | -| boxedAccountUnsized1 | 908 | - | -| boxedAccountUnsizedInit2 | 10,866 | - | -| boxedAccountUnsized2 | 1,457 | - | -| boxedAccountUnsizedInit4 | 20,688 | - | -| boxedAccountUnsized4 | 2,546 | - | -| boxedAccountUnsizedInit8 | 40,375 | - | -| boxedAccountUnsized8 | 4,759 | - | -| boxedInterfaceAccountMint1 | 2,196 | - | -| boxedInterfaceAccountMint2 | 3,847 | - | -| boxedInterfaceAccountMint4 | 7,132 | - | -| boxedInterfaceAccountMint8 | 13,743 | - | -| boxedInterfaceAccountToken1 | 2,126 | - | -| boxedInterfaceAccountToken2 | 3,706 | - | -| boxedInterfaceAccountToken4 | 6,853 | - | -| boxedInterfaceAccountToken8 | 13,184 | - | -| interfaceAccountMint1 | 2,285 | - | -| interfaceAccountMint2 | 4,178 | - | -| interfaceAccountMint4 | 7,964 | - | -| interfaceAccountMint8 | 15,538 | - | -| interfaceAccountToken1 | 2,212 | - | -| interfaceAccountToken2 | 4,030 | - | -| interfaceAccountToken4 | 7,663 | - | -| interface1 | 741 | - | -| interface2 | 934 | - | -| interface4 | 1,315 | - | -| interface8 | 2,086 | - | -| program1 | 741 | - | -| program2 | 934 | - | -| program4 | 1,317 | - | -| program8 | 2,086 | - | -| signer1 | 675 | - | -| signer2 | 987 | - | -| signer4 | 1,606 | - | -| signer8 | 2,846 | - | -| systemAccount1 | 729 | - | -| systemAccount2 | 1,093 | - | -| systemAccount4 | 1,817 | - | -| systemAccount8 | 3,271 | - | -| uncheckedAccount1 | 657 | - | -| uncheckedAccount2 | 949 | - | -| uncheckedAccount4 | 1,526 | - | -| uncheckedAccount8 | 2,688 | - | +| Instruction | Compute Units | - | +| --------------------------- | ------------- | ------------------- | +| accountInfo1 | 695 | - | +| accountInfo2 | 1,035 | - | +| accountInfo4 | 1,730 | - | +| accountInfo8 | 3,342 | - | +| accountEmptyInit1 | 5,497 | 🟢 **-55 (0.99%)** | +| accountEmpty1 | 819 | - | +| accountEmptyInit2 | 10,183 | 🟢 **-238 (2.28%)** | +| accountEmpty2 | 1,275 | - | +| accountEmptyInit4 | 19,327 | 🟢 **-476 (2.40%)** | +| accountEmpty4 | 2,177 | - | +| accountEmptyInit8 | 37,655 | 🟢 **-954 (2.47%)** | +| accountEmpty8 | 3,990 | - | +| accountSizedInit1 | 5,647 | - | +| accountSized1 | 868 | 🔴 **+25 (2.97%)** | +| accountSizedInit2 | 10,419 | 🟢 **-188 (1.77%)** | +| accountSized2 | 1,367 | 🔴 **+50 (3.80%)** | +| accountSizedInit4 | 19,849 | 🟢 **-376 (1.86%)** | +| accountSized4 | 2,374 | 🔴 **+100 (4.40%)** | +| accountSizedInit8 | 38,622 | 🟢 **-754 (1.91%)** | +| accountSized8 | 4,385 | 🔴 **+200 (4.78%)** | +| accountUnsizedInit1 | 5,682 | 🟢 **-58 (1.01%)** | +| accountUnsized1 | 870 | - | +| accountUnsizedInit2 | 10,618 | 🟢 **-238 (2.19%)** | +| accountUnsized2 | 1,379 | - | +| accountUnsizedInit4 | 20,180 | 🟢 **-472 (2.29%)** | +| accountUnsized4 | 2,411 | - | +| accountUnsizedInit8 | 39,013 | 🟢 **-956 (2.39%)** | +| accountUnsized8 | 4,478 | - | +| boxedAccountEmptyInit1 | 5,547 | 🟢 **-58 (1.03%)** | +| boxedAccountEmpty1 | 856 | - | +| boxedAccountEmptyInit2 | 10,284 | 🟢 **-238 (2.26%)** | +| boxedAccountEmpty2 | 1,347 | - | +| boxedAccountEmptyInit4 | 19,526 | 🟢 **-476 (2.38%)** | +| boxedAccountEmpty4 | 2,324 | - | +| boxedAccountEmptyInit8 | 38,048 | 🟢 **-954 (2.45%)** | +| boxedAccountEmpty8 | 4,311 | - | +| boxedAccountSizedInit1 | 5,686 | - | +| boxedAccountSized1 | 903 | 🔴 **+25 (2.85%)** | +| boxedAccountSizedInit2 | 10,502 | 🟢 **-188 (1.76%)** | +| boxedAccountSized2 | 1,444 | 🔴 **+50 (3.59%)** | +| boxedAccountSizedInit4 | 19,960 | 🟢 **-378 (1.86%)** | +| boxedAccountSized4 | 2,513 | 🔴 **+100 (4.14%)** | +| boxedAccountSizedInit8 | 38,916 | 🟢 **-754 (1.90%)** | +| boxedAccountSized8 | 4,693 | 🔴 **+200 (4.45%)** | +| boxedAccountUnsizedInit1 | 5,774 | - | +| boxedAccountUnsized1 | 908 | - | +| boxedAccountUnsizedInit2 | 10,628 | 🟢 **-238 (2.19%)** | +| boxedAccountUnsized2 | 1,457 | - | +| boxedAccountUnsizedInit4 | 20,212 | 🟢 **-476 (2.30%)** | +| boxedAccountUnsized4 | 2,546 | - | +| boxedAccountUnsizedInit8 | 39,421 | 🟢 **-954 (2.36%)** | +| boxedAccountUnsized8 | 4,759 | - | +| boxedInterfaceAccountMint1 | 2,196 | - | +| boxedInterfaceAccountMint2 | 3,847 | - | +| boxedInterfaceAccountMint4 | 7,132 | - | +| boxedInterfaceAccountMint8 | 13,743 | - | +| boxedInterfaceAccountToken1 | 2,126 | - | +| boxedInterfaceAccountToken2 | 3,706 | - | +| boxedInterfaceAccountToken4 | 6,853 | - | +| boxedInterfaceAccountToken8 | 13,184 | - | +| interfaceAccountMint1 | 2,285 | - | +| interfaceAccountMint2 | 4,178 | - | +| interfaceAccountMint4 | 7,964 | - | +| interfaceAccountMint8 | 15,538 | - | +| interfaceAccountToken1 | 2,212 | - | +| interfaceAccountToken2 | 4,030 | - | +| interfaceAccountToken4 | 7,663 | - | +| interface1 | 741 | - | +| interface2 | 934 | - | +| interface4 | 1,315 | - | +| interface8 | 2,086 | - | +| program1 | 741 | - | +| program2 | 934 | - | +| program4 | 1,317 | - | +| program8 | 2,086 | - | +| signer1 | 675 | - | +| signer2 | 987 | - | +| signer4 | 1,606 | - | +| signer8 | 2,846 | - | +| systemAccount1 | 729 | - | +| systemAccount2 | 1,093 | - | +| systemAccount4 | 1,817 | - | +| systemAccount8 | 3,271 | - | +| uncheckedAccount1 | 657 | - | +| uncheckedAccount2 | 949 | - | +| uncheckedAccount4 | 1,526 | - | +| uncheckedAccount8 | 2,688 | - | ### Notable changes diff --git a/examples/tutorial/basic-3/programs/puppet-master/src/lib.rs b/examples/tutorial/basic-3/programs/puppet-master/src/lib.rs index 29d32bac7b..3136eaa855 100644 --- a/examples/tutorial/basic-3/programs/puppet-master/src/lib.rs +++ b/examples/tutorial/basic-3/programs/puppet-master/src/lib.rs @@ -10,11 +10,10 @@ declare_id!("HmbTLCmaGvZhKnn1Zfa1JVnp7vkMV4DYVxPLWBVoN65L"); mod puppet_master { use super::*; pub fn pull_strings(ctx: Context, data: u64) -> anchor_lang::Result<()> { - let cpi_program = ctx.accounts.puppet_program.to_account_info(); let cpi_accounts = SetData { puppet: ctx.accounts.puppet.to_account_info(), }; - let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); + let cpi_ctx = CpiContext::new(cpi_accounts); puppet::cpi::set_data(cpi_ctx, data) } } diff --git a/tests/bench/bench.json b/tests/bench/bench.json index 0636ac40c6..269d1112b4 100644 --- a/tests/bench/bench.json +++ b/tests/bench/bench.json @@ -561,60 +561,60 @@ "solanaVersion": "1.17.0", "result": { "binarySize": { - "bench": 743208 + "bench": 712624 }, "computeUnits": { "accountInfo1": 695, "accountInfo2": 1035, "accountInfo4": 1730, "accountInfo8": 3342, - "accountEmptyInit1": 5552, + "accountEmptyInit1": 5497, "accountEmpty1": 819, - "accountEmptyInit2": 10421, + "accountEmptyInit2": 10183, "accountEmpty2": 1275, - "accountEmptyInit4": 19803, + "accountEmptyInit4": 19327, "accountEmpty4": 2177, - "accountEmptyInit8": 38609, + "accountEmptyInit8": 37655, "accountEmpty8": 3990, "accountSizedInit1": 5647, - "accountSized1": 843, - "accountSizedInit2": 10607, - "accountSized2": 1317, - "accountSizedInit4": 20225, - "accountSized4": 2274, - "accountSizedInit8": 39376, - "accountSized8": 4185, - "accountUnsizedInit1": 5740, + "accountSized1": 868, + "accountSizedInit2": 10419, + "accountSized2": 1367, + "accountSizedInit4": 19849, + "accountSized4": 2374, + "accountSizedInit8": 38622, + "accountSized8": 4385, + "accountUnsizedInit1": 5682, "accountUnsized1": 870, - "accountUnsizedInit2": 10856, + "accountUnsizedInit2": 10618, "accountUnsized2": 1379, - "accountUnsizedInit4": 20652, + "accountUnsizedInit4": 20180, "accountUnsized4": 2411, - "accountUnsizedInit8": 39969, + "accountUnsizedInit8": 39013, "accountUnsized8": 4478, - "boxedAccountEmptyInit1": 5605, + "boxedAccountEmptyInit1": 5547, "boxedAccountEmpty1": 856, - "boxedAccountEmptyInit2": 10522, + "boxedAccountEmptyInit2": 10284, "boxedAccountEmpty2": 1347, - "boxedAccountEmptyInit4": 20002, + "boxedAccountEmptyInit4": 19526, "boxedAccountEmpty4": 2324, - "boxedAccountEmptyInit8": 39002, + "boxedAccountEmptyInit8": 38048, "boxedAccountEmpty8": 4311, "boxedAccountSizedInit1": 5686, - "boxedAccountSized1": 878, - "boxedAccountSizedInit2": 10690, - "boxedAccountSized2": 1394, - "boxedAccountSizedInit4": 20338, - "boxedAccountSized4": 2413, - "boxedAccountSizedInit8": 39670, - "boxedAccountSized8": 4493, + "boxedAccountSized1": 903, + "boxedAccountSizedInit2": 10502, + "boxedAccountSized2": 1444, + "boxedAccountSizedInit4": 19960, + "boxedAccountSized4": 2513, + "boxedAccountSizedInit8": 38916, + "boxedAccountSized8": 4693, "boxedAccountUnsizedInit1": 5774, "boxedAccountUnsized1": 908, - "boxedAccountUnsizedInit2": 10866, + "boxedAccountUnsizedInit2": 10628, "boxedAccountUnsized2": 1457, - "boxedAccountUnsizedInit4": 20688, + "boxedAccountUnsizedInit4": 20212, "boxedAccountUnsized4": 2546, - "boxedAccountUnsizedInit8": 40375, + "boxedAccountUnsizedInit8": 39421, "boxedAccountUnsized8": 4759, "boxedInterfaceAccountMint1": 2196, "boxedInterfaceAccountMint2": 3847, diff --git a/tests/cashiers-check/programs/cashiers-check/src/lib.rs b/tests/cashiers-check/programs/cashiers-check/src/lib.rs index 6943fe17f1..5f8e1453a0 100644 --- a/tests/cashiers-check/programs/cashiers-check/src/lib.rs +++ b/tests/cashiers-check/programs/cashiers-check/src/lib.rs @@ -26,8 +26,7 @@ pub mod cashiers_check { to: ctx.accounts.vault.to_account_info(), authority: ctx.accounts.owner.clone(), }; - let cpi_program = ctx.accounts.token_program.clone(); - let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); + let cpi_ctx = CpiContext::new(cpi_accounts); token::transfer(cpi_ctx, amount)?; // Print the check. @@ -54,8 +53,7 @@ pub mod cashiers_check { to: ctx.accounts.to.to_account_info(), authority: ctx.accounts.check_signer.clone(), }; - let cpi_program = ctx.accounts.token_program.clone(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::transfer(cpi_ctx, ctx.accounts.check.amount)?; // Burn the check for one time use. ctx.accounts.check.burned = true; @@ -74,8 +72,7 @@ pub mod cashiers_check { to: ctx.accounts.from.to_account_info(), authority: ctx.accounts.check_signer.clone(), }; - let cpi_program = ctx.accounts.token_program.clone(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::transfer(cpi_ctx, ctx.accounts.check.amount)?; ctx.accounts.check.burned = true; Ok(()) diff --git a/tests/escrow/programs/escrow/src/lib.rs b/tests/escrow/programs/escrow/src/lib.rs index 79a21494c7..38a6f3e698 100644 --- a/tests/escrow/programs/escrow/src/lib.rs +++ b/tests/escrow/programs/escrow/src/lib.rs @@ -199,8 +199,8 @@ impl<'info> From<&mut InitializeEscrow<'info>> .clone(), current_authority: accounts.initializer.to_account_info(), }; - let cpi_program = accounts.token_program.to_account_info(); - CpiContext::new(cpi_program, cpi_accounts) + let cpi_program_id = accounts.token_program.key(); + CpiContext::new_with_id(cpi_accounts, cpi_program_id) } } @@ -210,8 +210,8 @@ impl<'info> CancelEscrow<'info> { account_or_mint: self.pda_deposit_token_account.to_account_info(), current_authority: self.pda_account.clone(), }; - let cpi_program = self.token_program.to_account_info(); - CpiContext::new(cpi_program, cpi_accounts) + let cpi_program_id = self.token_program.key(); + CpiContext::new_with_id(cpi_accounts, cpi_program_id) } } @@ -221,8 +221,8 @@ impl<'info> Exchange<'info> { account_or_mint: self.pda_deposit_token_account.to_account_info(), current_authority: self.pda_account.clone(), }; - let cpi_program = self.receive_token_program.to_account_info(); - CpiContext::new(cpi_program, cpi_accounts) + let cpi_program_id = self.receive_token_program.key(); + CpiContext::new_with_id(cpi_accounts, cpi_program_id) } } @@ -236,8 +236,8 @@ impl<'info> Exchange<'info> { to: self.taker_receive_token_account.to_account_info(), authority: self.pda_account.clone(), }; - let cpi_program = self.receive_token_program.to_account_info(); - CpiContext::new(cpi_program, cpi_accounts) + let cpi_program_id = self.receive_token_program.key(); + CpiContext::new_with_id(cpi_accounts, cpi_program_id) } } @@ -254,7 +254,7 @@ impl<'info> Exchange<'info> { .clone(), authority: self.taker.clone(), }; - let cpi_program = self.deposit_token_program.to_account_info(); - CpiContext::new(cpi_program, cpi_accounts) + let cpi_program_id = self.deposit_token_program.key(); + CpiContext::new_with_id(cpi_accounts, cpi_program_id) } } diff --git a/tests/ido-pool/programs/ido-pool/src/lib.rs b/tests/ido-pool/programs/ido-pool/src/lib.rs index 65a44ebd67..917dfde437 100644 --- a/tests/ido-pool/programs/ido-pool/src/lib.rs +++ b/tests/ido-pool/programs/ido-pool/src/lib.rs @@ -55,8 +55,7 @@ pub mod ido_pool { to: ctx.accounts.pool_watermelon.to_account_info(), authority: ctx.accounts.ido_authority.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); + let cpi_ctx = CpiContext::new(cpi_accounts); token::transfer(cpi_ctx, num_ido_tokens)?; Ok(()) @@ -85,8 +84,7 @@ pub mod ido_pool { to: ctx.accounts.pool_usdc.to_account_info(), authority: ctx.accounts.user_authority.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); + let cpi_ctx = CpiContext::new(cpi_accounts); token::transfer(cpi_ctx, amount)?; // Mint Redeemable to user Redeemable account. @@ -101,8 +99,7 @@ pub mod ido_pool { to: ctx.accounts.user_redeemable.to_account_info(), authority: ctx.accounts.ido_account.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::mint_to(cpi_ctx, amount)?; Ok(()) @@ -138,8 +135,7 @@ pub mod ido_pool { from: ctx.accounts.user_redeemable.to_account_info(), authority: ctx.accounts.ido_account.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::burn(cpi_ctx, amount)?; // Transfer USDC from pool account to the user's escrow account. @@ -148,8 +144,7 @@ pub mod ido_pool { to: ctx.accounts.escrow_usdc.to_account_info(), authority: ctx.accounts.ido_account.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::transfer(cpi_ctx, amount)?; Ok(()) @@ -186,8 +181,7 @@ pub mod ido_pool { from: ctx.accounts.user_redeemable.to_account_info(), authority: ctx.accounts.ido_account.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::burn(cpi_ctx, amount)?; // Transfer Watermelon from pool account to user. @@ -196,8 +190,7 @@ pub mod ido_pool { to: ctx.accounts.user_watermelon.to_account_info(), authority: ctx.accounts.ido_account.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::transfer(cpi_ctx, watermelon_amount as u64)?; // Send rent back to user if account is empty @@ -208,8 +201,7 @@ pub mod ido_pool { destination: ctx.accounts.user_authority.clone(), authority: ctx.accounts.ido_account.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::close_account(cpi_ctx)?; } @@ -231,8 +223,7 @@ pub mod ido_pool { to: ctx.accounts.ido_authority_usdc.to_account_info(), authority: ctx.accounts.ido_account.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::transfer(cpi_ctx, ctx.accounts.pool_usdc.amount)?; Ok(()) @@ -259,8 +250,7 @@ pub mod ido_pool { to: ctx.accounts.user_usdc.to_account_info(), authority: ctx.accounts.ido_account.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::transfer(cpi_ctx, amount)?; // Send rent back to user if account is empty @@ -271,8 +261,7 @@ pub mod ido_pool { destination: ctx.accounts.user_authority.clone(), authority: ctx.accounts.ido_account.to_account_info(), }; - let cpi_program = ctx.accounts.token_program.to_account_info(); - let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer); + let cpi_ctx = CpiContext::new_with_signer(cpi_accounts, signer); token::close_account(cpi_ctx)?; } diff --git a/tests/misc/programs/lamports/src/lib.rs b/tests/misc/programs/lamports/src/lib.rs index 84ac072d27..c3b40522f4 100644 --- a/tests/misc/programs/lamports/src/lib.rs +++ b/tests/misc/programs/lamports/src/lib.rs @@ -17,13 +17,10 @@ pub mod lamports { // Transfer to the PDA anchor_lang::system_program::transfer( - CpiContext::new( - ctx.accounts.system_program.to_account_info(), - anchor_lang::system_program::Transfer { - from: signer.to_account_info(), - to: pda.to_account_info(), - }, - ), + CpiContext::new(anchor_lang::system_program::Transfer { + from: signer.to_account_info(), + to: pda.to_account_info(), + }), amount, )?; diff --git a/tests/spl/token-proxy/programs/token-proxy/src/lib.rs b/tests/spl/token-proxy/programs/token-proxy/src/lib.rs index 8f8d6710ed..1630898723 100644 --- a/tests/spl/token-proxy/programs/token-proxy/src/lib.rs +++ b/tests/spl/token-proxy/programs/token-proxy/src/lib.rs @@ -30,8 +30,8 @@ mod token_proxy { to: ctx.accounts.to.to_account_info(), authority: ctx.accounts.authority.clone(), }; - let cpi_program = token_program.to_account_info(); - let cpi_context = CpiContext::new(cpi_program, cpi_accounts); + let cpi_program_id = token_program.key(); + let cpi_context = CpiContext::new_with_id(cpi_accounts, cpi_program_id); token_interface::transfer_checked(cpi_context, amount, mint.decimals) } else { let cpi_accounts = Transfer { @@ -39,8 +39,8 @@ mod token_proxy { to: ctx.accounts.to.to_account_info(), authority: ctx.accounts.authority.clone(), }; - let cpi_program = token_program.to_account_info(); - let cpi_context = CpiContext::new(cpi_program, cpi_accounts); + let cpi_program_id = token_program.key(); + let cpi_context = CpiContext::new_with_id(cpi_accounts, cpi_program_id); #[allow(deprecated)] token_interface::transfer(cpi_context, amount) } @@ -212,8 +212,8 @@ impl<'a, 'b, 'c, 'info> From<&mut ProxyTransfer<'info>> to: accounts.to.to_account_info(), authority: accounts.authority.clone(), }; - let cpi_program = accounts.token_program.to_account_info(); - CpiContext::new(cpi_program, cpi_accounts) + let cpi_program_id = accounts.token_program.key(); + CpiContext::new_with_id(cpi_accounts, cpi_program_id) } } @@ -226,8 +226,8 @@ impl<'a, 'b, 'c, 'info> From<&mut ProxyMintTo<'info>> to: accounts.to.to_account_info(), authority: accounts.authority.clone(), }; - let cpi_program = accounts.token_program.to_account_info(); - CpiContext::new(cpi_program, cpi_accounts) + let cpi_program_id = accounts.token_program.key(); + CpiContext::new_with_id(cpi_accounts, cpi_program_id) } } @@ -238,8 +238,8 @@ impl<'a, 'b, 'c, 'info> From<&mut ProxyBurn<'info>> for CpiContext<'a, 'b, 'c, ' from: accounts.from.to_account_info(), authority: accounts.authority.clone(), }; - let cpi_program = accounts.token_program.to_account_info(); - CpiContext::new(cpi_program, cpi_accounts) + let cpi_program_id = accounts.token_program.key(); + CpiContext::new_with_id(cpi_accounts, cpi_program_id) } } @@ -253,8 +253,8 @@ impl<'a, 'b, 'c, 'info> From<&mut ProxySetAuthority<'info>> account_or_mint: accounts.account_or_mint.clone(), current_authority: accounts.current_authority.clone(), }; // TODO: Support multisig signers - let cpi_program = accounts.token_program.to_account_info(); - CpiContext::new(cpi_program, cpi_accounts) + let cpi_program_id = accounts.token_program.key(); + CpiContext::new_with_id(cpi_accounts, cpi_program_id) } } diff --git a/tests/spl/token-wrapper/programs/token-wrapper/src/lib.rs b/tests/spl/token-wrapper/programs/token-wrapper/src/lib.rs index 58560d86a2..656c58597d 100644 --- a/tests/spl/token-wrapper/programs/token-wrapper/src/lib.rs +++ b/tests/spl/token-wrapper/programs/token-wrapper/src/lib.rs @@ -25,8 +25,7 @@ pub mod token_wrapper { pub fn initialize(ctx: Context, initializer_amount: u64) -> Result<()> { // deposit into vault token_interface::transfer_checked( - CpiContext::new( - ctx.accounts.deposit_token_program.to_account_info(), + CpiContext::new_with_id( token_interface::TransferChecked { from: ctx .accounts @@ -36,6 +35,7 @@ pub mod token_wrapper { to: ctx.accounts.deposit_token_vault.to_account_info(), authority: ctx.accounts.initializer.to_account_info(), }, + ctx.accounts.deposit_token_program.key(), ), initializer_amount, ctx.accounts.deposit_mint.decimals, @@ -50,8 +50,7 @@ pub mod token_wrapper { ]; let signer_seeds = &[&inner_seeds[..]]; token_interface::mint_to( - CpiContext::new_with_signer( - ctx.accounts.wrapped_token_program.to_account_info(), + CpiContext::new_with_id_and_signer( token_interface::MintTo { mint: ctx.accounts.wrapped_mint.to_account_info(), to: ctx @@ -60,6 +59,7 @@ pub mod token_wrapper { .to_account_info(), authority: ctx.accounts.wrapper_authority.to_account_info(), }, + ctx.accounts.wrapped_token_program.key(), signer_seeds, ), initializer_amount, @@ -71,14 +71,14 @@ pub mod token_wrapper { pub fn wrap(ctx: Context, wrap_amount: u64) -> Result<()> { // deposit into vault token_interface::transfer_checked( - CpiContext::new( - ctx.accounts.deposit_token_program.to_account_info(), + CpiContext::new_with_id( token_interface::TransferChecked { from: ctx.accounts.user_deposit_token_account.to_account_info(), mint: ctx.accounts.deposit_mint.to_account_info(), to: ctx.accounts.deposit_token_vault.to_account_info(), authority: ctx.accounts.signer.to_account_info(), }, + ctx.accounts.deposit_token_program.key(), ), wrap_amount, ctx.accounts.deposit_mint.decimals, @@ -93,13 +93,13 @@ pub mod token_wrapper { ]; let signer_seeds = &[&inner_seeds[..]]; token_interface::mint_to( - CpiContext::new_with_signer( - ctx.accounts.wrapped_token_program.to_account_info(), + CpiContext::new_with_id_and_signer( token_interface::MintTo { mint: ctx.accounts.wrapped_mint.to_account_info(), to: ctx.accounts.user_wrapped_token_account.to_account_info(), authority: ctx.accounts.wrapper_authority.to_account_info(), }, + ctx.accounts.wrapped_token_program.key(), signer_seeds, ), wrap_amount, @@ -111,13 +111,13 @@ pub mod token_wrapper { pub fn unwrap(ctx: Context, unwrap_amount: u64) -> Result<()> { // burn wrapped tokens token_interface::burn( - CpiContext::new( - ctx.accounts.wrapped_token_program.to_account_info(), + CpiContext::new_with_id( token_interface::Burn { mint: ctx.accounts.wrapped_mint.to_account_info(), from: ctx.accounts.user_wrapped_token_account.to_account_info(), authority: ctx.accounts.signer.to_account_info(), }, + ctx.accounts.wrapped_token_program.key(), ), unwrap_amount, )?; @@ -131,14 +131,14 @@ pub mod token_wrapper { ]; let signer_seeds = &[&inner_seeds[..]]; token_interface::transfer_checked( - CpiContext::new_with_signer( - ctx.accounts.deposit_token_program.to_account_info(), + CpiContext::new_with_id_and_signer( token_interface::TransferChecked { from: ctx.accounts.deposit_token_vault.to_account_info(), mint: ctx.accounts.deposit_mint.to_account_info(), to: ctx.accounts.user_deposit_token_account.to_account_info(), authority: ctx.accounts.wrapper_authority.to_account_info(), }, + ctx.accounts.deposit_token_program.key(), signer_seeds, ), unwrap_amount, diff --git a/tests/swap/programs/swap/src/lib.rs b/tests/swap/programs/swap/src/lib.rs index 1e4fa5f44c..c04102f1ba 100644 --- a/tests/swap/programs/swap/src/lib.rs +++ b/tests/swap/programs/swap/src/lib.rs @@ -347,7 +347,7 @@ impl<'info> OrderbookClient<'info> { token_program: self.token_program.clone(), rent: self.rent.clone(), }; - let mut ctx = CpiContext::new(self.dex_program.clone(), dex_accs); + let mut ctx = CpiContext::new(dex_accs); if let Some(referral) = referral { ctx = ctx.with_remaining_accounts(vec![referral]); } @@ -376,7 +376,7 @@ impl<'info> OrderbookClient<'info> { vault_signer: self.market.vault_signer.clone(), token_program: self.token_program.clone(), }; - let mut ctx = CpiContext::new(self.dex_program.clone(), settle_accs); + let mut ctx = CpiContext::new(settle_accs); if let Some(referral) = referral { ctx = ctx.with_remaining_accounts(vec![referral]); } diff --git a/tests/zero-copy/programs/zero-cpi/src/lib.rs b/tests/zero-copy/programs/zero-cpi/src/lib.rs index 1ce2fdaa64..307be50fb0 100644 --- a/tests/zero-copy/programs/zero-cpi/src/lib.rs +++ b/tests/zero-copy/programs/zero-cpi/src/lib.rs @@ -9,13 +9,12 @@ declare_id!("ErjUjtqKE5AGWUsjseSJCVLtddM6rhaMbDqmhzraF9h6"); pub mod zero_cpi { use super::*; pub fn check_cpi(ctx: Context, data: u64) -> Result<()> { - let cpi_program = ctx.accounts.zero_copy_program.to_account_info(); let cpi_accounts = UpdateBar { authority: ctx.accounts.authority.clone(), bar: ctx.accounts.bar.to_account_info(), foo: ctx.accounts.foo.to_account_info(), }; - let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts); + let cpi_ctx = CpiContext::new(cpi_accounts); zero_copy::cpi::update_bar(cpi_ctx, data)?; Ok(()) }