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

✨ Convert instruction name from IDL into snake case #236

Merged
merged 1 commit into from
Jan 11, 2025
Merged
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
28 changes: 28 additions & 0 deletions crates/template/src/get_ix_discriminator.rs
Original file line number Diff line number Diff line change
@@ -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<u8> {
// 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
}
13 changes: 13 additions & 0 deletions crates/template/src/get_program_id.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
39 changes: 2 additions & 37 deletions crates/template/src/instruction_ixops.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -88,36 +86,3 @@ pub(crate) fn get_instruction_ixops(
instructions_ixops_impl
})
}

fn process_discriminator(instruction: &IdlInstruction) -> Vec<u8> {
// 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()
}
}
4 changes: 4 additions & 0 deletions crates/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ 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;
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::*;
Expand Down