Skip to content

Commit 6a51385

Browse files
committed
Improving clarity of PositionKind unpacking
1 parent 4e4694b commit 6a51385

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

token-lending/program/src/processor/liquidity_mining.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ fn reward_vault_authority(
223223
)
224224
}
225225

226-
fn reward_vault_authority_seeds<'a, 'keys>(
226+
fn reward_vault_authority_seeds<'keys>(
227227
lending_market_key: &'keys Pubkey,
228228
reserve_key: &'keys Pubkey,
229229
reward_mint_key: &'keys Pubkey,

token-lending/sdk/src/instruction.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -872,11 +872,7 @@ impl LendingInstruction {
872872
Self::DonateToReserve { liquidity_amount }
873873
}
874874
25 => {
875-
let (position_kind, rest) = match Self::unpack_u8(rest)? {
876-
(0, rest) => (PositionKind::Deposit, rest),
877-
(1, rest) => (PositionKind::Borrow, rest),
878-
_ => return Err(LendingError::InstructionUnpackError.into()),
879-
};
875+
let (position_kind, rest) = Self::unpack_try_from_u8(rest)?;
880876
let (start_time_secs, rest) = Self::unpack_u64(rest)?;
881877
let (end_time_secs, rest) = Self::unpack_u64(rest)?;
882878
let (token_amount, _rest) = Self::unpack_u64(rest)?;
@@ -888,23 +884,15 @@ impl LendingInstruction {
888884
}
889885
}
890886
26 => {
891-
let (position_kind, rest) = match Self::unpack_u8(rest)? {
892-
(0, rest) => (PositionKind::Deposit, rest),
893-
(1, rest) => (PositionKind::Borrow, rest),
894-
_ => return Err(LendingError::InstructionUnpackError.into()),
895-
};
887+
let (position_kind, rest) = Self::unpack_try_from_u8(rest)?;
896888
let (pool_reward_index, _rest) = Self::unpack_u64(rest)?;
897889
Self::ClosePoolReward {
898890
position_kind,
899891
pool_reward_index,
900892
}
901893
}
902894
27 => {
903-
let (position_kind, rest) = match Self::unpack_u8(rest)? {
904-
(0, rest) => (PositionKind::Deposit, rest),
905-
(1, rest) => (PositionKind::Borrow, rest),
906-
_ => return Err(LendingError::InstructionUnpackError.into()),
907-
};
895+
let (position_kind, rest) = Self::unpack_try_from_u8(rest)?;
908896
let (pool_reward_index, _rest) = Self::unpack_u64(rest)?;
909897
Self::CancelPoolReward {
910898
position_kind,
@@ -960,6 +948,25 @@ impl LendingInstruction {
960948
Ok((value, rest))
961949
}
962950

951+
fn unpack_try_from_u8<T>(input: &[u8]) -> Result<(T, &[u8]), ProgramError>
952+
where
953+
T: TryFrom<u8>,
954+
ProgramError: From<<T as TryFrom<u8>>::Error>,
955+
{
956+
if input.is_empty() {
957+
msg!("u8 cannot be unpacked");
958+
return Err(LendingError::InstructionUnpackError.into());
959+
}
960+
let (bytes, rest) = input.split_at(1);
961+
let value = bytes
962+
.get(..1)
963+
.and_then(|slice| slice.try_into().ok())
964+
.map(u8::from_le_bytes)
965+
.ok_or(LendingError::InstructionUnpackError)?;
966+
967+
Ok((T::try_from(value)?, rest))
968+
}
969+
963970
fn unpack_bytes32(input: &[u8]) -> Result<(&[u8; 32], &[u8]), ProgramError> {
964971
if input.len() < 32 {
965972
msg!("32 bytes cannot be unpacked");

token-lending/sdk/src/state/obligation.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,18 @@ impl Pack for Obligation {
655655
}
656656
}
657657

658+
impl TryFrom<u8> for PositionKind {
659+
type Error = ProgramError;
660+
661+
fn try_from(value: u8) -> Result<Self, Self::Error> {
662+
match value {
663+
0 => Ok(PositionKind::Deposit),
664+
1 => Ok(PositionKind::Borrow),
665+
_ => Err(LendingError::InstructionUnpackError.into()),
666+
}
667+
}
668+
}
669+
658670
#[cfg(test)]
659671
mod test {
660672
use super::*;

0 commit comments

Comments
 (0)