Skip to content

Commit

Permalink
inline _emit_cpi_invoke to proc_macro declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
ngundotra committed Mar 30, 2023
1 parent ffd6fb1 commit d8a4509
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
12 changes: 10 additions & 2 deletions lang/attribute/event/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,22 @@ pub fn emit_cpi(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let first = &elems[0];
let second = &elems[1];

// use solana_program::account_info::AccountInfo;
// use solana_program::instruction::{AccountMeta, Instruction};
proc_macro::TokenStream::from(quote! {
let program_info: &anchor_lang::solana_program::account_info::AccountInfo = &#first;

let __disc: Vec<u8> = crate::event::EVENT_IX_TAG_LE.to_vec();
let __inner_data: &Vec<u8> = &anchor_lang::Event::data(&#second);
let __ix_data: Vec<u8> = __disc.iter().chain(__inner_data.iter()).cloned().collect();
let ix_data: Vec<u8> = __disc.iter().chain(__inner_data.iter()).cloned().collect();

anchor_lang::__private::_emit_cpi_invoke(__ix_data, program_info)?;
let ix = anchor_lang::solana_program::instruction::Instruction::new_with_bytes(
program_info.key.clone(),
ix_data.as_ref(),
vec![anchor_lang::solana_program::instruction::AccountMeta::new_readonly(*program_info.key, false)],
);
anchor_lang::solana_program::program::invoke(&ix, &[program_info.clone()])
.map_err(|e: anchor_lang::solana_program::program_error::ProgramError| crate::Error::from(e))?;
})
}

Expand Down
13 changes: 0 additions & 13 deletions lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,6 @@ pub mod __private {

pub use bytemuck;

use solana_program::account_info::AccountInfo;
use solana_program::instruction::{AccountMeta, Instruction};
use solana_program::pubkey::Pubkey;

// Used to calculate the maximum between two expressions.
Expand All @@ -361,17 +359,6 @@ pub mod __private {
input.to_bytes()
}
}

#[doc(hidden)]
pub fn _emit_cpi_invoke(ix_data: Vec<u8>, program: &AccountInfo) -> anchor_lang::Result<()> {
let ix: Instruction = Instruction::new_with_bytes(
program.key.clone(),
ix_data.as_ref(),
vec![AccountMeta::new_readonly(*program.key, false)],
);
solana_program::program::invoke(&ix, &[program.clone()])?;
Ok(())
}
}

/// Ensures a condition is true, otherwise returns with the given error.
Expand Down

0 comments on commit d8a4509

Please sign in to comment.