Skip to content

Commit

Permalink
Add helpers around StakingType
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrena-Corto committed Nov 1, 2024
1 parent 6addd65 commit 50aeacd
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,28 @@ pub enum StakingType {
LP = 2,
}

impl From<StakingType> for u8 {
fn from(val: StakingType) -> Self {
match val {
StakingType::LM => 1,
StakingType::LP => 2,
}
}
}

impl TryFrom<u8> for StakingType {
type Error = anyhow::Error;

fn try_from(value: u8) -> std::result::Result<Self, Self::Error> {
Ok(match value {
1 => StakingType::LM,
2 => StakingType::LP,
// Return an error if unknown value
_ => anyhow::bail!("Invalid staking type"),
})
}
}

#[account(zero_copy)]
#[derive(Default, Debug, PartialEq, AnchorSerialize, AnchorDeserialize)]
#[repr(C)]
Expand All @@ -537,6 +559,12 @@ pub struct UserStaking {
pub locked_stakes: [LockedStake; MAX_LOCKED_STAKE_COUNT],
}

impl UserStaking {
pub fn get_staking_type(&self) -> StakingType {
StakingType::try_from(self.staking_type).unwrap()
}
}

#[derive(
Copy, Clone, PartialEq, AnchorSerialize, AnchorDeserialize, Default, Debug, Zeroable, Pod,
)]
Expand Down

0 comments on commit 50aeacd

Please sign in to comment.