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

Update state.rs #7146

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 15 additions & 15 deletions token-group/interface/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
bytemuck::{Pod, Zeroable},
solana_program::{program_error::ProgramError, pubkey::Pubkey},
spl_discriminator::SplDiscriminate,
spl_pod::{error::PodSliceError, optional_keys::OptionalNonZeroPubkey, primitives::PodU32},
spl_pod::{error::PodSliceError, optional_keys::OptionalNonZeroPubkey, primitives::PodU64},
};

/// Data struct for a `TokenGroup`
Expand All @@ -19,39 +19,39 @@ pub struct TokenGroup {
/// belongs to a particular mint
pub mint: Pubkey,
/// The current number of group members
pub size: PodU32,
pub size: PodU64,
/// The maximum number of group members
pub max_size: PodU32,
pub max_size: PodU64,
}

impl TokenGroup {
/// Creates a new `TokenGroup` state
pub fn new(mint: &Pubkey, update_authority: OptionalNonZeroPubkey, max_size: u32) -> Self {
pub fn new(mint: &Pubkey, update_authority: OptionalNonZeroPubkey, max_size: U64) -> Self {
Self {
mint: *mint,
update_authority,
size: PodU32::default(), // [0, 0, 0, 0]
size: PodU64::default(), // [0, 0, 0, 0]
max_size: max_size.into(),
}
}

/// Updates the max size for a group
pub fn update_max_size(&mut self, new_max_size: u32) -> Result<(), ProgramError> {
pub fn update_max_size(&mut self, new_max_size: U64) -> Result<(), ProgramError> {
// The new max size cannot be less than the current size
if new_max_size < u32::from(self.size) {
if new_max_size < U64::from(self.size) {
return Err(TokenGroupError::SizeExceedsNewMaxSize.into());
}
self.max_size = new_max_size.into();
Ok(())
}

/// Increment the size for a group, returning the new size
pub fn increment_size(&mut self) -> Result<u32, ProgramError> {
pub fn increment_size(&mut self) -> Result<U64, ProgramError> {
// The new size cannot be greater than the max size
let new_size = u32::from(self.size)
let new_size = U64::from(self.size)
.checked_add(1)
.ok_or::<ProgramError>(PodSliceError::CalculationFailure.into())?;
if new_size > u32::from(self.max_size) {
if new_size > U64::from(self.max_size) {
return Err(TokenGroupError::SizeExceedsMaxSize.into());
}
self.size = new_size.into();
Expand All @@ -70,11 +70,11 @@ pub struct TokenGroupMember {
/// The pubkey of the `TokenGroup`
pub group: Pubkey,
/// The member number
pub member_number: PodU32,
pub member_number: PodU64,
}
impl TokenGroupMember {
/// Creates a new `TokenGroupMember` state
pub fn new(mint: &Pubkey, group: &Pubkey, member_number: u32) -> Self {
pub fn new(mint: &Pubkey, group: &Pubkey, member_number: U64) -> Self {
Self {
mint: *mint,
group: *group,
Expand Down Expand Up @@ -156,7 +156,7 @@ mod tests {

let new_max_size = 30;
group.update_max_size(new_max_size).unwrap();
assert_eq!(u32::from(group.max_size), new_max_size);
assert_eq!(U64::from(group.max_size), new_max_size);

// Change the current size to 30
group.size = 30.into();
Expand All @@ -170,7 +170,7 @@ mod tests {

let new_max_size = 30;
group.update_max_size(new_max_size).unwrap();
assert_eq!(u32::from(group.max_size), new_max_size);
assert_eq!(U64::from(group.max_size), new_max_size);
}

#[test]
Expand All @@ -183,7 +183,7 @@ mod tests {
};

group.increment_size().unwrap();
assert_eq!(u32::from(group.size), 1);
assert_eq!(U64::from(group.size), 1);

// Try to increase the current size to 2, which is greater than the max size
assert_eq!(
Expand Down