diff --git a/crates/template/src/get_ix_discriminator.rs b/crates/template/src/get_ix_discriminator.rs new file mode 100644 index 00000000..16307c4e --- /dev/null +++ b/crates/template/src/get_ix_discriminator.rs @@ -0,0 +1,28 @@ +use sha2::{Digest, Sha256}; + +use convert_case::{Case, Casing}; +use trident_idl_spec::IdlInstruction; + +pub const SIGHASH_GLOBAL_NAMESPACE: &str = "global"; + +pub(crate) fn process_discriminator(instruction: &IdlInstruction) -> Vec { + // if discriminator is not provided, generate it + if instruction.discriminator.is_empty() { + let ix_name_snake_case = instruction.name.to_case(Case::Snake); + gen_discriminator(SIGHASH_GLOBAL_NAMESPACE, &ix_name_snake_case).to_vec() + } else { + // if discriminator is provided, use it + instruction.discriminator.clone() + } +} + +fn gen_discriminator(namespace: &str, name: &str) -> [u8; 8] { + let preimage = format!("{namespace}:{name}"); + + let mut hasher = Sha256::new(); + hasher.update(preimage); + + let mut sighash = [0u8; 8]; + sighash.copy_from_slice(&hasher.finalize().as_slice()[..8]); + sighash +} diff --git a/crates/template/src/get_program_id.rs b/crates/template/src/get_program_id.rs new file mode 100644 index 00000000..8b8e3aca --- /dev/null +++ b/crates/template/src/get_program_id.rs @@ -0,0 +1,13 @@ +use trident_idl_spec::Idl; + +pub(crate) fn process_program_id(idl: &Idl) -> String { + // if program ID is present, use it + if !idl.address.is_empty() { + idl.address.clone() + } else { + // if program ID is not present, use placeholder + // We might be able to parse it form program, but it + // might not be necesarry as newer versions of IDL will contain it + "fill corresponding program ID here".to_string() + } +} diff --git a/crates/template/src/instruction_ixops.rs b/crates/template/src/instruction_ixops.rs index dfdcd6fe..91e3b06f 100644 --- a/crates/template/src/instruction_ixops.rs +++ b/crates/template/src/instruction_ixops.rs @@ -1,17 +1,15 @@ use convert_case::{Case, Casing}; use quote::format_ident; -use sha2::{Digest, Sha256}; use std::collections::HashMap; use syn::parse_quote; -use trident_idl_spec::{Idl, IdlInstruction}; +use trident_idl_spec::Idl; use crate::{ get_accounts::get_accounts, get_data::get_data, instruction_account::InstructionAccount, + process_discriminator, process_program_id, }; -pub const SIGHASH_GLOBAL_NAMESPACE: &str = "global"; - // Generate implementation of IxOps trait for each instruction pub(crate) fn get_instruction_ixops( idl: &Idl, @@ -88,36 +86,3 @@ pub(crate) fn get_instruction_ixops( instructions_ixops_impl }) } - -fn process_discriminator(instruction: &IdlInstruction) -> Vec { - // if discriminator is not provided, generate it - if instruction.discriminator.is_empty() { - gen_discriminator(SIGHASH_GLOBAL_NAMESPACE, &instruction.name).to_vec() - } else { - // if discriminator is provided, use it - instruction.discriminator.clone() - } -} - -fn gen_discriminator(namespace: &str, name: &str) -> [u8; 8] { - let preimage = format!("{namespace}:{name}"); - - let mut hasher = Sha256::new(); - hasher.update(preimage); - - let mut sighash = [0u8; 8]; - sighash.copy_from_slice(&hasher.finalize().as_slice()[..8]); - sighash -} - -fn process_program_id(idl: &Idl) -> String { - // if program ID is present, use it - if !idl.address.is_empty() { - idl.address.clone() - } else { - // if program ID is not present, use placeholder - // We might be able to parse it form program, but it - // might not be necesarry as newer versions of IDL will contain it - "fill corresponding program ID here".to_string() - } -} diff --git a/crates/template/src/lib.rs b/crates/template/src/lib.rs index 3cf5ddb6..ce07db04 100644 --- a/crates/template/src/lib.rs +++ b/crates/template/src/lib.rs @@ -2,6 +2,8 @@ mod custom_types; mod fuzz_accounts; mod get_accounts; mod get_data; +mod get_ix_discriminator; +mod get_program_id; mod instruction_account; mod instruction_inputs; mod instruction_ixops; @@ -9,6 +11,8 @@ mod instruction_variants; use custom_types::*; use fuzz_accounts::*; +use get_ix_discriminator::*; +use get_program_id::*; use instruction_inputs::*; use instruction_ixops::*; use instruction_variants::*;