Skip to content

Commit

Permalink
lang: Fix cpi feature instructions not accounting for discriminator…
Browse files Browse the repository at this point in the history
… overrides
  • Loading branch information
acheroncrypto committed Nov 20, 2024
1 parent 8ec7ec8 commit f3c3ea3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
10 changes: 6 additions & 4 deletions lang/syn/src/codegen/program/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ pub fn gen_discriminator(namespace: &str, name: impl ToString) -> proc_macro2::T

pub fn generate_ix_variant(name: String, args: &[IxArg]) -> proc_macro2::TokenStream {
let ix_arg_names: Vec<&syn::Ident> = args.iter().map(|arg| &arg.name).collect();
let ix_name_camel: proc_macro2::TokenStream = {
let n = name.to_camel_case();
n.parse().unwrap()
};
let ix_name_camel = generate_ix_variant_name(name);

if args.is_empty() {
quote! {
Expand All @@ -42,3 +39,8 @@ pub fn generate_ix_variant(name: String, args: &[IxArg]) -> proc_macro2::TokenSt
}
}
}

pub fn generate_ix_variant_name(name: String) -> proc_macro2::TokenStream {
let n = name.to_camel_case();
n.parse().unwrap()
}
11 changes: 6 additions & 5 deletions lang/syn/src/codegen/program/cpi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::codegen::program::common::{
gen_discriminator, generate_ix_variant, SIGHASH_GLOBAL_NAMESPACE,
};
use crate::codegen::program::common::{generate_ix_variant, generate_ix_variant_name};
use crate::Program;
use heck::SnakeCase;
use quote::{quote, ToTokens};
Expand All @@ -14,10 +12,13 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let accounts_ident: proc_macro2::TokenStream = format!("crate::cpi::accounts::{}", &ix.anchor_ident.to_string()).parse().unwrap();
let cpi_method = {
let name = &ix.raw_method.sig.ident;
let ix_variant = generate_ix_variant(name.to_string(), &ix.args);
let ix_variant = generate_ix_variant(&name, &ix.args);
let method_name = &ix.ident;
let args: Vec<&syn::PatType> = ix.args.iter().map(|arg| &arg.raw_arg).collect();
let discriminator = gen_discriminator(SIGHASH_GLOBAL_NAMESPACE, name);
let discriminator = {
let name = generate_ix_variant_name(name.to_string());
quote! { <instruction::#name as anchor_lang::Discriminator>::DISCRIMINATOR }
};
let ret_type = &ix.returns.ty.to_token_stream();
let ix_cfgs = &ix.cfgs;
let (method_ret, maybe_return) = match ret_type.to_string().as_str() {
Expand Down
6 changes: 0 additions & 6 deletions lang/syn/src/codegen/program/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::codegen::program::common::*;
use crate::program_codegen::idl::idl_accounts_and_functions;
use crate::Program;
use heck::CamelCase;
use quote::{quote, ToTokens};

// Generate non-inlined wrappers for each instruction handler, since Solana's
Expand Down Expand Up @@ -191,11 +190,6 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
}
}

fn generate_ix_variant_name(name: String) -> proc_macro2::TokenStream {
let n = name.to_camel_case();
n.parse().unwrap()
}

/// Generate the event module based on whether the `event-cpi` feature is enabled.
fn generate_event_cpi_mod() -> proc_macro2::TokenStream {
#[cfg(feature = "event-cpi")]
Expand Down

0 comments on commit f3c3ea3

Please sign in to comment.