bolt_anchor_syn/codegen/program/
common.rs

1use crate::IxArg;
2use heck::CamelCase;
3use quote::quote;
4
5// Namespace for calculating instruction sighash signatures for any instruction
6// not affecting program state.
7pub const SIGHASH_GLOBAL_NAMESPACE: &str = "global";
8
9// We don't technically use sighash, because the input arguments aren't given.
10// Rust doesn't have method overloading so no need to use the arguments.
11// However, we do namespace methods in the preeimage so that we can use
12// different traits with the same method name.
13pub fn sighash(namespace: &str, name: &str) -> [u8; 8] {
14    let preimage = format!("{namespace}:{name}");
15
16    let mut sighash = [0u8; 8];
17    sighash.copy_from_slice(&crate::hash::hash(preimage.as_bytes()).to_bytes()[..8]);
18    sighash
19}
20
21pub fn generate_ix_variant(name: String, args: &[IxArg]) -> proc_macro2::TokenStream {
22    let ix_arg_names: Vec<&syn::Ident> = args.iter().map(|arg| &arg.name).collect();
23    let ix_name_camel: proc_macro2::TokenStream = {
24        let n = name.to_camel_case();
25        n.parse().unwrap()
26    };
27
28    if args.is_empty() {
29        quote! {
30            #ix_name_camel
31        }
32    } else {
33        quote! {
34            #ix_name_camel {
35                #(#ix_arg_names),*
36            }
37        }
38    }
39}