Implement Discriminator trait for generated instructions

Access to the discriminators is useful when doing instruction
introspection where you may want to determine the type of other
instructions for your program.
This commit is contained in:
Christian Kamm 2022-08-05 20:24:53 +02:00
parent 2058b6461c
commit b52f236146
1 changed files with 5 additions and 0 deletions

View File

@ -138,6 +138,11 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let sighash_tts: proc_macro2::TokenStream =
format!("{:?}", sighash_arr).parse().unwrap();
quote! {
impl anchor_lang::Discriminator for #ix_name_camel {
fn discriminator() -> [u8; 8] {
#sighash_tts
}
}
impl anchor_lang::InstructionData for #ix_name_camel {
fn data(&self) -> Vec<u8> {
let mut d = #sighash_tts.to_vec();