Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing events generation #36

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/anchor-idl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ heck = "0.4.0"
proc-macro2 = "1"
quote = "1"
serde_json = "1.0.81"
sha2 = "0.9.2"
syn = { version = "1", features = ["full"] }

[dev-dependencies]
Expand Down
59 changes: 59 additions & 0 deletions crates/anchor-idl/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::collections::BTreeMap;

use anchor_syn::idl::{IdlEvent, IdlField, IdlTypeDefinition};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use sha2::{Digest, Sha256};

use crate::{generate_struct, StructOpts};

/// Generates event structs.
pub fn generate_events(
events: Option<&[IdlEvent]>,
typedefs: &[IdlTypeDefinition],
struct_opts: &BTreeMap<String, StructOpts>,
) -> TokenStream {
match events {
Some(events) => {
let defined = events.iter().map(|def| {
let struct_name = format_ident!("{}", def.name);
let opts = struct_opts.get(&def.name).copied().unwrap_or_default();

let discriminator: proc_macro2::TokenStream = {
let discriminator_preimage = format!("event:{}", struct_name);
let mut discriminator = [0u8; 8];
let mut hash = Sha256::default();
hash.update(discriminator_preimage.as_bytes());
discriminator.copy_from_slice(&hash.finalize()[..8]);
format!("{:?}", discriminator).parse().unwrap()
};

let fields = def
.fields
.iter()
.map(|f| IdlField {
name: f.name.clone(),
ty: f.ty.clone(),
})
.collect::<Vec<_>>();

let struct_ts = generate_struct(&typedefs, &struct_name, &fields, opts);

quote! {
#struct_ts

impl anchor_lang::Discriminator for #struct_name {
const DISCRIMINATOR: [u8; 8] = #discriminator;
fn discriminator() -> [u8; 8] {
#discriminator
}
}
}
});
quote! {
#(#defined)*
}
}
None => quote!(),
}
}
2 changes: 2 additions & 0 deletions crates/anchor-idl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
pub use anchor_syn::idl::*;

mod account;
mod event;
mod instruction;
mod program;
mod state;
mod typedef;

pub use account::*;
pub use event::*;
pub use instruction::*;
pub use program::*;
pub use state::*;
Expand Down
14 changes: 13 additions & 1 deletion crates/anchor-idl/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};

use crate::{
generate_accounts, generate_ix_handlers, generate_ix_structs, generate_typedefs, GEN_VERSION,
generate_accounts, generate_events, generate_ix_handlers, generate_ix_structs,
generate_typedefs, GEN_VERSION,
};

#[derive(Default, FromMeta)]
Expand Down Expand Up @@ -74,6 +75,11 @@ impl Generator {
let program_name: Ident = format_ident!("{}", idl.name);

let accounts = generate_accounts(&idl.types, &idl.accounts, &self.struct_opts);
let events = generate_events(
idl.events.as_ref().map(|x| &x[..]),
&idl.types,
&self.struct_opts,
);
let typedefs = generate_typedefs(&idl.types, &self.struct_opts);
let ix_handlers = generate_ix_handlers(&idl.instructions);
let ix_structs = generate_ix_structs(&idl.instructions);
Expand All @@ -100,6 +106,12 @@ impl Generator {
#accounts
}

pub mod events {
//! Structs of events generated by program.
use super::*;
#events
}

pub mod ix_accounts {
//! Accounts used in instructions.
use super::*;
Expand Down