Skip to content

Commit

Permalink
remove solana-program from token-group-interface (#7430)
Browse files Browse the repository at this point in the history
* remove solana-program from token-group-interface

* remove now-unused dep

* missed a spot
  • Loading branch information
kevinheavey authored Oct 31, 2024
1 parent b0e553f commit 5d8e76d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 21 deletions.
11 changes: 9 additions & 2 deletions Cargo.lock

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

11 changes: 9 additions & 2 deletions token-group/interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ edition = "2021"

[dependencies]
bytemuck = "1.19.0"
solana-program = "2.1.0"
num-derive = "0.4"
num-traits = "0.2"
solana-decode-error = "2.1.0"
solana-instruction = "2.1.0"
solana-msg = "2.1.0"
solana-program-error = "2.1.0"
solana-pubkey = "2.1.0"
spl-discriminator = { version = "0.3.0" , path = "../../libraries/discriminator" }
spl-pod = { version = "0.4.0", path = "../../libraries/pod", features = ["borsh"] }
spl-program-error = { version = "0.5.0" , path = "../../libraries/program-error" }
thiserror = "1.0"

[dev-dependencies]
solana-sha256-hasher = "2.1.0"
spl-type-length-value = { version = "0.6.0", path = "../../libraries/type-length-value", features = ["derive"] }

[lib]
Expand Down
55 changes: 52 additions & 3 deletions token-group/interface/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
//! Interface error types
use spl_program_error::*;
use {
solana_decode_error::DecodeError,
solana_msg::msg,
solana_program_error::{PrintProgramError, ProgramError},
};

/// Errors that may be returned by the interface.
#[spl_program_error(hash_error_code_start = 3_406_457_176)]
#[repr(u32)]
#[derive(Clone, Debug, Eq, thiserror::Error, num_derive::FromPrimitive, PartialEq)]
pub enum TokenGroupError {
/// Size is greater than proposed max size
#[error("Size is greater than proposed max size")]
SizeExceedsNewMaxSize,
SizeExceedsNewMaxSize = 3_406_457_176,
/// Size is greater than max size
#[error("Size is greater than max size")]
SizeExceedsMaxSize,
Expand All @@ -24,3 +29,47 @@ pub enum TokenGroupError {
#[error("Member account should not be the same as the group account")]
MemberAccountIsGroupAccount,
}

impl From<TokenGroupError> for ProgramError {
fn from(e: TokenGroupError) -> Self {
ProgramError::Custom(e as u32)
}
}

impl<T> DecodeError<T> for TokenGroupError {
fn type_of() -> &'static str {
"TokenGroupError"
}
}

impl PrintProgramError for TokenGroupError {
fn print<E>(&self)
where
E: 'static
+ std::error::Error
+ DecodeError<E>
+ PrintProgramError
+ num_traits::FromPrimitive,
{
match self {
TokenGroupError::SizeExceedsNewMaxSize => {
msg!("Size is greater than proposed max size")
}
TokenGroupError::SizeExceedsMaxSize => {
msg!("Size is greater than max size")
}
TokenGroupError::ImmutableGroup => {
msg!("Group is immutable")
}
TokenGroupError::IncorrectMintAuthority => {
msg!("Incorrect mint authority has signed the instruction",)
}
TokenGroupError::IncorrectUpdateAuthority => {
msg!("Incorrect update authority has signed the instruction",)
}
TokenGroupError::MemberAccountIsGroupAccount => {
msg!("Member account should not be the same as the group account",)
}
}
}
}
18 changes: 8 additions & 10 deletions token-group/interface/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
use {
bytemuck::{Pod, Zeroable},
solana_program::{
instruction::{AccountMeta, Instruction},
program_error::ProgramError,
pubkey::Pubkey,
},
solana_instruction::{AccountMeta, Instruction},
solana_program_error::ProgramError,
solana_pubkey::Pubkey,
spl_discriminator::{ArrayDiscriminator, SplDiscriminate},
spl_pod::{
bytemuck::{pod_bytes_of, pod_from_bytes},
Expand Down Expand Up @@ -243,7 +241,7 @@ pub fn initialize_member(

#[cfg(test)]
mod test {
use {super::*, crate::NAMESPACE, solana_program::hash};
use {super::*, crate::NAMESPACE, solana_sha256_hasher::hashv};

fn instruction_pack_unpack<I>(instruction: TokenGroupInstruction, discriminator: &[u8], data: I)
where
Expand All @@ -265,7 +263,7 @@ mod test {
max_size: 100.into(),
};
let instruction = TokenGroupInstruction::InitializeGroup(data);
let preimage = hash::hashv(&[format!("{NAMESPACE}:initialize_token_group").as_bytes()]);
let preimage = hashv(&[format!("{NAMESPACE}:initialize_token_group").as_bytes()]);
let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
instruction_pack_unpack::<InitializeGroup>(instruction, discriminator, data);
}
Expand All @@ -276,7 +274,7 @@ mod test {
max_size: 200.into(),
};
let instruction = TokenGroupInstruction::UpdateGroupMaxSize(data);
let preimage = hash::hashv(&[format!("{NAMESPACE}:update_group_max_size").as_bytes()]);
let preimage = hashv(&[format!("{NAMESPACE}:update_group_max_size").as_bytes()]);
let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
instruction_pack_unpack::<UpdateGroupMaxSize>(instruction, discriminator, data);
}
Expand All @@ -287,7 +285,7 @@ mod test {
new_authority: OptionalNonZeroPubkey::default(),
};
let instruction = TokenGroupInstruction::UpdateGroupAuthority(data);
let preimage = hash::hashv(&[format!("{NAMESPACE}:update_authority").as_bytes()]);
let preimage = hashv(&[format!("{NAMESPACE}:update_authority").as_bytes()]);
let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
instruction_pack_unpack::<UpdateGroupAuthority>(instruction, discriminator, data);
}
Expand All @@ -296,7 +294,7 @@ mod test {
fn initialize_member_pack() {
let data = InitializeMember {};
let instruction = TokenGroupInstruction::InitializeMember(data);
let preimage = hash::hashv(&[format!("{NAMESPACE}:initialize_member").as_bytes()]);
let preimage = hashv(&[format!("{NAMESPACE}:initialize_member").as_bytes()]);
let discriminator = &preimage.as_ref()[..ArrayDiscriminator::LENGTH];
instruction_pack_unpack::<InitializeMember>(instruction, discriminator, data);
}
Expand Down
9 changes: 5 additions & 4 deletions token-group/interface/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use {
crate::error::TokenGroupError,
bytemuck::{Pod, Zeroable},
solana_program::{program_error::ProgramError, pubkey::Pubkey},
solana_program_error::ProgramError,
solana_pubkey::Pubkey,
spl_discriminator::SplDiscriminate,
spl_pod::{error::PodSliceError, optional_keys::OptionalNonZeroPubkey, primitives::PodU64},
};
Expand Down Expand Up @@ -88,20 +89,20 @@ mod tests {
use {
super::*,
crate::NAMESPACE,
solana_program::hash,
solana_sha256_hasher::hashv,
spl_discriminator::ArrayDiscriminator,
spl_type_length_value::state::{TlvState, TlvStateBorrowed, TlvStateMut},
std::mem::size_of,
};

#[test]
fn discriminators() {
let preimage = hash::hashv(&[format!("{NAMESPACE}:group").as_bytes()]);
let preimage = hashv(&[format!("{NAMESPACE}:group").as_bytes()]);
let discriminator =
ArrayDiscriminator::try_from(&preimage.as_ref()[..ArrayDiscriminator::LENGTH]).unwrap();
assert_eq!(TokenGroup::SPL_DISCRIMINATOR, discriminator);

let preimage = hash::hashv(&[format!("{NAMESPACE}:member").as_bytes()]);
let preimage = hashv(&[format!("{NAMESPACE}:member").as_bytes()]);
let discriminator =
ArrayDiscriminator::try_from(&preimage.as_ref()[..ArrayDiscriminator::LENGTH]).unwrap();
assert_eq!(TokenGroupMember::SPL_DISCRIMINATOR, discriminator);
Expand Down

0 comments on commit 5d8e76d

Please sign in to comment.