extern crate proc_macro;
use quote::quote;
use syn::parse_macro_input;
#[proc_macro_attribute]
pub fn event(
_args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let event_strct = parse_macro_input!(input as syn::ItemEnum);
let event_name = &event_strct.ident;
let (impl_gen, type_gen, where_clause) = event_strct.generics.split_for_impl();
proc_macro::TokenStream::from({
quote! {
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq, Eq, Debug, Clone)]
#event_strct
#[automatically_derived]
impl #impl_gen #event_name #type_gen #where_clause {
pub fn to_instruction(&self) -> Instruction {
Instruction {
program_id: crate::id(),
accounts: vec![],
data: vec![
crate::id().try_to_vec().unwrap(),
self.try_to_vec().unwrap(),
]
.concat(),
}
}
pub fn emit_to<'info>(&self, wrapper_program: AccountInfo<'info>) -> ProgramResult {
invoke(&self.to_instruction(), &[wrapper_program.to_account_info()])
}
pub fn emit<'info>(&self, hpl_events_program: AccountInfo<'info>) -> ProgramResult {
if String::from("EvntXY8sSMZhWLdh26y4aePQWH4VqCk13oCBqydpbdy6")
!= hpl_events_program.key().to_string()
{
return Err(ProgramError::IncorrectProgramId);
};
self.emit_to(hpl_events_program)
}
pub fn wrap<'info>(
&self,
hpl_events_program: AccountInfo<'info>,
emitter_program: Pubkey,
) -> ProgramResult {
invoke(
&Instruction {
program_id: crate::id(),
accounts: vec![],
data: vec![
emitter_program.try_to_vec().unwrap(),
self.try_to_vec().unwrap(),
]
.concat(),
},
&[hpl_events_program],
)
}
}
}
})
}