Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 5e1c2bb

Browse files
authored
token-swap: Use sdk version of Pack (#554)
* token-swap: Use sdk version of Pack * Update to get_packed_len per review
1 parent 01e3c19 commit 5e1c2bb

File tree

3 files changed

+25
-35
lines changed

3 files changed

+25
-35
lines changed

token-swap/program/src/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ pub enum SwapError {
3434
/// The provided token account has a delegate.
3535
#[error("Token account has a delegate")]
3636
InvalidDelegate,
37-
/// The swap info is invalid.
38-
#[error("Swap info invalid")]
39-
InvalidSwapInfo,
4037
/// The input token is invalid for swap.
4138
#[error("InvalidInput")]
4239
InvalidInput,

token-swap/program/src/processor.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl Processor {
220220
fee_numerator,
221221
fee_denominator,
222222
};
223-
obj.pack(&mut swap_info.data.borrow_mut());
223+
SwapInfo::pack(obj, &mut swap_info.data.borrow_mut())?;
224224
Ok(())
225225
}
226226

@@ -549,7 +549,6 @@ impl PrintProgramError for SwapError {
549549
SwapError::InvalidSupply => info!("Error: Pool token mint has a non-zero supply"),
550550
SwapError::RepeatedMint => info!("Error: Swap input token accounts have the same mint"),
551551
SwapError::InvalidDelegate => info!("Error: Token account has a delegate"),
552-
SwapError::InvalidSwapInfo => info!("Error: Swap info invalid"),
553552
SwapError::InvalidInput => info!("Error: InvalidInput"),
554553
SwapError::IncorrectSwapAccount => {
555554
info!("Error: Address of the provided swap token account is incorrect")
@@ -585,7 +584,6 @@ mod tests {
585584
processor::Processor as SplProcessor,
586585
state::{Account as SplAccount, Mint as SplMint},
587586
};
588-
use std::mem::size_of;
589587

590588
struct SwapAccountInfo {
591589
nonce: u8,
@@ -617,7 +615,7 @@ mod tests {
617615
token_b_amount: u64,
618616
) -> Self {
619617
let swap_key = pubkey_rand();
620-
let swap_account = Account::new(0, size_of::<SwapInfo>(), &SWAP_PROGRAM_ID);
618+
let swap_account = Account::new(0, SwapInfo::get_packed_len(), &SWAP_PROGRAM_ID);
621619
let (authority_key, nonce) =
622620
Pubkey::find_program_address(&[&swap_key.to_bytes()[..]], &SWAP_PROGRAM_ID);
623621

@@ -1516,7 +1514,7 @@ mod tests {
15161514
mut pool_account,
15171515
) = accounts.setup_token_accounts(&user_key, &depositor_key, deposit_a, deposit_b, 0);
15181516
assert_eq!(
1519-
Err(SwapError::InvalidSwapInfo.into()),
1517+
Err(ProgramError::UninitializedAccount),
15201518
accounts.deposit(
15211519
&depositor_key,
15221520
&token_a_key,
@@ -1960,7 +1958,7 @@ mod tests {
19601958
mut pool_account,
19611959
) = accounts.setup_token_accounts(&user_key, &withdrawer_key, initial_a, initial_b, 0);
19621960
assert_eq!(
1963-
Err(SwapError::InvalidSwapInfo.into()),
1961+
Err(ProgramError::UninitializedAccount),
19641962
accounts.withdraw(
19651963
&withdrawer_key,
19661964
&pool_key,
@@ -2399,7 +2397,7 @@ mod tests {
23992397
_pool_account,
24002398
) = accounts.setup_token_accounts(&user_key, &swapper_key, initial_a, initial_b, 0);
24012399
assert_eq!(
2402-
Err(SwapError::InvalidSwapInfo.into()),
2400+
Err(ProgramError::UninitializedAccount),
24032401
accounts.swap(
24042402
&swapper_key,
24052403
&token_a_key,

token-swap/program/src/state.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//! State transition types
22
3-
use crate::error::SwapError;
43
use arrayref::{array_mut_ref, array_ref, array_refs, mut_array_refs};
5-
use solana_sdk::{program_error::ProgramError, pubkey::Pubkey};
4+
use solana_sdk::{
5+
program_error::ProgramError,
6+
program_pack::{IsInitialized, Pack, Sealed},
7+
pubkey::Pubkey,
8+
};
69

710
/// Program states.
811
#[repr(C)]
@@ -30,26 +33,19 @@ pub struct SwapInfo {
3033
pub fee_denominator: u64,
3134
}
3235

33-
impl SwapInfo {
34-
/// Helper function to get the more efficient packed size of the struct
35-
const fn get_packed_len() -> usize {
36-
114
36+
impl Sealed for SwapInfo {}
37+
impl IsInitialized for SwapInfo {
38+
fn is_initialized(&self) -> bool {
39+
self.is_initialized
3740
}
41+
}
3842

39-
/// Unpacks a byte buffer into a [SwapInfo](struct.SwapInfo.html) and checks
40-
/// that it is initialized.
41-
pub fn unpack(input: &[u8]) -> Result<Self, ProgramError> {
42-
let value = Self::unpack_unchecked(input)?;
43-
if value.is_initialized {
44-
Ok(value)
45-
} else {
46-
Err(SwapError::InvalidSwapInfo.into())
47-
}
48-
}
43+
impl Pack for SwapInfo {
44+
const LEN: usize = 114;
4945

5046
/// Unpacks a byte buffer into a [SwapInfo](struct.SwapInfo.html).
51-
pub fn unpack_unchecked(input: &[u8]) -> Result<Self, ProgramError> {
52-
let input = array_ref![input, 0, SwapInfo::get_packed_len()];
47+
fn unpack_from_slice(input: &[u8]) -> Result<Self, ProgramError> {
48+
let input = array_ref![input, 0, 114];
5349
#[allow(clippy::ptr_offset_with_cast)]
5450
let (is_initialized, nonce, token_a, token_b, pool_mint, fee_numerator, fee_denominator) =
5551
array_refs![input, 1, 1, 32, 32, 32, 8, 8];
@@ -68,9 +64,8 @@ impl SwapInfo {
6864
})
6965
}
7066

71-
/// Packs [SwapInfo](struct.SwapInfo.html) into a byte buffer.
72-
pub fn pack(&self, output: &mut [u8]) {
73-
let output = array_mut_ref![output, 0, SwapInfo::get_packed_len()];
67+
fn pack_into_slice(&self, output: &mut [u8]) {
68+
let output = array_mut_ref![output, 0, 114];
7469
let (is_initialized, nonce, token_a, token_b, pool_mint, fee_numerator, fee_denominator) =
7570
mut_array_refs![output, 1, 1, 32, 32, 32, 8, 8];
7671
is_initialized[0] = self.is_initialized as u8;
@@ -109,8 +104,8 @@ mod tests {
109104
fee_denominator,
110105
};
111106

112-
let mut packed = [0u8; SwapInfo::get_packed_len()];
113-
swap_info.pack(&mut packed);
107+
let mut packed = [0u8; SwapInfo::LEN];
108+
SwapInfo::pack(swap_info, &mut packed).unwrap();
114109
let unpacked = SwapInfo::unpack(&packed).unwrap();
115110
assert_eq!(swap_info, unpacked);
116111

@@ -127,11 +122,11 @@ mod tests {
127122
let unpacked = SwapInfo::unpack(&packed).unwrap();
128123
assert_eq!(swap_info, unpacked);
129124

130-
let packed = [0u8; SwapInfo::get_packed_len()];
125+
let packed = [0u8; SwapInfo::LEN];
131126
let swap_info: SwapInfo = Default::default();
132127
let unpack_unchecked = SwapInfo::unpack_unchecked(&packed).unwrap();
133128
assert_eq!(unpack_unchecked, swap_info);
134129
let err = SwapInfo::unpack(&packed).unwrap_err();
135-
assert_eq!(err, SwapError::InvalidSwapInfo.into());
130+
assert_eq!(err, ProgramError::UninitializedAccount);
136131
}
137132
}

0 commit comments

Comments
 (0)