Skip to content

Commit

Permalink
Improving clarity of PositionKind unpacking
Browse files Browse the repository at this point in the history
  • Loading branch information
vanitymnm committed Feb 26, 2025
1 parent 4e4694b commit 6a51385
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion token-lending/program/src/processor/liquidity_mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ fn reward_vault_authority(
)
}

fn reward_vault_authority_seeds<'a, 'keys>(
fn reward_vault_authority_seeds<'keys>(
lending_market_key: &'keys Pubkey,
reserve_key: &'keys Pubkey,
reward_mint_key: &'keys Pubkey,
Expand Down
37 changes: 22 additions & 15 deletions token-lending/sdk/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,11 +872,7 @@ impl LendingInstruction {
Self::DonateToReserve { liquidity_amount }
}
25 => {
let (position_kind, rest) = match Self::unpack_u8(rest)? {
(0, rest) => (PositionKind::Deposit, rest),
(1, rest) => (PositionKind::Borrow, rest),
_ => return Err(LendingError::InstructionUnpackError.into()),
};
let (position_kind, rest) = Self::unpack_try_from_u8(rest)?;
let (start_time_secs, rest) = Self::unpack_u64(rest)?;
let (end_time_secs, rest) = Self::unpack_u64(rest)?;
let (token_amount, _rest) = Self::unpack_u64(rest)?;
Expand All @@ -888,23 +884,15 @@ impl LendingInstruction {
}
}
26 => {
let (position_kind, rest) = match Self::unpack_u8(rest)? {
(0, rest) => (PositionKind::Deposit, rest),
(1, rest) => (PositionKind::Borrow, rest),
_ => return Err(LendingError::InstructionUnpackError.into()),
};
let (position_kind, rest) = Self::unpack_try_from_u8(rest)?;
let (pool_reward_index, _rest) = Self::unpack_u64(rest)?;
Self::ClosePoolReward {
position_kind,
pool_reward_index,
}
}
27 => {
let (position_kind, rest) = match Self::unpack_u8(rest)? {
(0, rest) => (PositionKind::Deposit, rest),
(1, rest) => (PositionKind::Borrow, rest),
_ => return Err(LendingError::InstructionUnpackError.into()),
};
let (position_kind, rest) = Self::unpack_try_from_u8(rest)?;
let (pool_reward_index, _rest) = Self::unpack_u64(rest)?;
Self::CancelPoolReward {
position_kind,
Expand Down Expand Up @@ -960,6 +948,25 @@ impl LendingInstruction {
Ok((value, rest))
}

fn unpack_try_from_u8<T>(input: &[u8]) -> Result<(T, &[u8]), ProgramError>
where
T: TryFrom<u8>,
ProgramError: From<<T as TryFrom<u8>>::Error>,
{
if input.is_empty() {
msg!("u8 cannot be unpacked");
return Err(LendingError::InstructionUnpackError.into());
}
let (bytes, rest) = input.split_at(1);
let value = bytes
.get(..1)
.and_then(|slice| slice.try_into().ok())
.map(u8::from_le_bytes)
.ok_or(LendingError::InstructionUnpackError)?;

Ok((T::try_from(value)?, rest))
}

fn unpack_bytes32(input: &[u8]) -> Result<(&[u8; 32], &[u8]), ProgramError> {
if input.len() < 32 {
msg!("32 bytes cannot be unpacked");
Expand Down
12 changes: 12 additions & 0 deletions token-lending/sdk/src/state/obligation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,18 @@ impl Pack for Obligation {
}
}

impl TryFrom<u8> for PositionKind {
type Error = ProgramError;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(PositionKind::Deposit),
1 => Ok(PositionKind::Borrow),
_ => Err(LendingError::InstructionUnpackError.into()),
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 6a51385

Please sign in to comment.