Skip to content

Commit

Permalink
Fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Jun 24, 2024
1 parent 5bf8810 commit 6745513
Show file tree
Hide file tree
Showing 20 changed files with 82 additions and 84 deletions.
2 changes: 1 addition & 1 deletion governance/program/tests/program_test/cookies.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(dead_code)]
use {
solana_program::{clock::UnixTimestamp, instruction::Instruction, pubkey::Pubkey},
solana_sdk::signature::Keypair,
Expand Down Expand Up @@ -72,7 +73,6 @@ impl TokenOwnerRecordCookie {
.unwrap_or(&self.token_owner)
}

#[allow(dead_code)]
pub fn clone_governance_delegate(&self) -> Keypair {
clone_keypair(&self.governance_delegate)
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/math/src/precise_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl PreciseNumber {
/// Based on testing around the limits, this base is the smallest value that
/// provides an epsilon of 11 digits
fn maximum_sqrt_base() -> Self {
Self::new(std::u128::MAX).unwrap()
Self::new(u128::MAX).unwrap()
}

/// Approximate the square root using Newton's method. Based on testing,
Expand Down
6 changes: 3 additions & 3 deletions libraries/type-length-value/derive/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ impl From<&SplBorshVariableLenPackBuilder> for TokenStream {
let where_clause = &builder.where_clause;
quote! {
impl #generics spl_type_length_value::variable_len_pack::VariableLenPack for #ident #generics #where_clause {
fn pack_into_slice(&self, dst: &mut [u8]) -> Result<(), solana_program::program_error::ProgramError> {
fn pack_into_slice(&self, dst: &mut [u8]) -> Result<(), spl_type_length_value::solana_program::program_error::ProgramError> {
borsh::to_writer(&mut dst[..], self).map_err(Into::into)
}

fn unpack_from_slice(src: &[u8]) -> Result<Self, solana_program::program_error::ProgramError> {
fn unpack_from_slice(src: &[u8]) -> Result<Self, spl_type_length_value::solana_program::program_error::ProgramError> {
solana_program::borsh1::try_from_slice_unchecked(src).map_err(Into::into)
}

fn get_packed_len(&self) -> Result<usize, solana_program::program_error::ProgramError> {
fn get_packed_len(&self) -> Result<usize, spl_type_length_value::solana_program::program_error::ProgramError> {
solana_program::borsh1::get_instance_packed_len(self).map_err(Into::into)
}
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/type-length-value/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ mod test {
// correct due to the good discriminator length and zero length
assert_eq!(
get_discriminators_and_end_index(&[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap(),
(vec![ArrayDiscriminator::try_from(1).unwrap()], 12)
(vec![ArrayDiscriminator::from(1)], 12)
);
// correct since it's just uninitialized data
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion single-pool/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ async fn command_deposit(config: &Config, command_config: DepositCli) -> Command
.into());
}

if stake.delegation.deactivation_epoch < std::u64::MAX {
if stake.delegation.deactivation_epoch < u64::MAX {
return Err(format!(
"Stake account {} is deactivating or deactivated",
stake_account_address
Expand Down
4 changes: 2 additions & 2 deletions token-group/example/tests/initialize_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async fn test_initialize_group() {
&group.pubkey(),
&group_mint.pubkey(),
&group_mint_authority.pubkey(),
group_state.update_authority.try_into().unwrap(),
group_state.update_authority.into(),
group_state.max_size.into(),
);
init_group_ix.accounts[2].is_signer = false;
Expand Down Expand Up @@ -95,7 +95,7 @@ async fn test_initialize_group() {
&group.pubkey(),
&group_mint.pubkey(),
&group_mint_authority.pubkey(),
group_state.update_authority.try_into().unwrap(),
group_state.update_authority.into(),
group_state.max_size.into(),
),
],
Expand Down
2 changes: 1 addition & 1 deletion token-group/example/tests/initialize_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async fn test_initialize_group_member() {
&group.pubkey(),
&group_mint.pubkey(),
&group_mint_authority.pubkey(),
group_state.update_authority.try_into().unwrap(),
group_state.update_authority.into(),
group_state.max_size.into(),
),
],
Expand Down
2 changes: 1 addition & 1 deletion token-group/example/tests/update_group_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async fn test_update_group_authority() {
&group.pubkey(),
&group_mint.pubkey(),
&group_mint_authority.pubkey(),
group_state.update_authority.try_into().unwrap(),
group_state.update_authority.into(),
group_state.max_size.into(),
),
],
Expand Down
2 changes: 1 addition & 1 deletion token-group/example/tests/update_group_max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async fn test_update_group_max_size() {
&group.pubkey(),
&group_mint.pubkey(),
&group_mint_authority.pubkey(),
group_state.update_authority.try_into().unwrap(),
group_state.update_authority.into(),
group_state.max_size.into(),
),
],
Expand Down
1 change: 0 additions & 1 deletion token-lending/program/tests/borrow_obligation_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use {
processor::process_instruction,
state::{FeeCalculation, INITIAL_COLLATERAL_RATIO},
},
std::u64,
};

#[tokio::test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use {
processor::process_instruction,
state::INITIAL_COLLATERAL_RATIO,
},
std::u64,
};

#[tokio::test]
Expand Down
4 changes: 2 additions & 2 deletions token-swap/program/src/curve/constant_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ impl CurveCalculator for ConstantPriceCurve {
) -> Option<PreciseNumber> {
let swap_token_b_value = swap_token_b_amount.checked_mul(self.token_b_price as u128)?;
// special logic in case we're close to the limits, avoid overflowing u128
let value = if swap_token_b_value.saturating_sub(std::u64::MAX.into())
> (std::u128::MAX.saturating_sub(std::u64::MAX.into()))
let value = if swap_token_b_value.saturating_sub(u64::MAX.into())
> (u128::MAX.saturating_sub(u64::MAX.into()))
{
swap_token_b_value
.checked_div(2)?
Expand Down
17 changes: 8 additions & 9 deletions token-swap/program/src/curve/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use {
program_error::ProgramError,
program_pack::{IsInitialized, Pack, Sealed},
},
std::convert::TryFrom,
};

/// Encapsulates all fee information and calculations for swap operations
Expand Down Expand Up @@ -101,26 +100,26 @@ impl Fees {
pub fn owner_withdraw_fee(&self, pool_tokens: u128) -> Option<u128> {
calculate_fee(
pool_tokens,
u128::try_from(self.owner_withdraw_fee_numerator).ok()?,
u128::try_from(self.owner_withdraw_fee_denominator).ok()?,
u128::from(self.owner_withdraw_fee_numerator),
u128::from(self.owner_withdraw_fee_denominator),
)
}

/// Calculate the trading fee in trading tokens
pub fn trading_fee(&self, trading_tokens: u128) -> Option<u128> {
calculate_fee(
trading_tokens,
u128::try_from(self.trade_fee_numerator).ok()?,
u128::try_from(self.trade_fee_denominator).ok()?,
u128::from(self.trade_fee_numerator),
u128::from(self.trade_fee_denominator),
)
}

/// Calculate the owner trading fee in trading tokens
pub fn owner_trading_fee(&self, trading_tokens: u128) -> Option<u128> {
calculate_fee(
trading_tokens,
u128::try_from(self.owner_trade_fee_numerator).ok()?,
u128::try_from(self.owner_trade_fee_denominator).ok()?,
u128::from(self.owner_trade_fee_numerator),
u128::from(self.owner_trade_fee_denominator),
)
}

Expand Down Expand Up @@ -159,8 +158,8 @@ impl Fees {
pub fn host_fee(&self, owner_fee: u128) -> Option<u128> {
calculate_fee(
owner_fee,
u128::try_from(self.host_fee_numerator).ok()?,
u128::try_from(self.host_fee_denominator).ok()?,
u128::from(self.host_fee_numerator),
u128::from(self.host_fee_denominator),
)
}

Expand Down
92 changes: 44 additions & 48 deletions token-swap/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ impl Processor {
let result = token_swap
.swap_curve()
.swap(
to_u128(actual_amount_in)?,
to_u128(source_account.amount)?,
to_u128(dest_account.amount)?,
u128::from(actual_amount_in),
u128::from(source_account.amount),
u128::from(dest_account.amount),
trade_direction,
token_swap.fees(),
)
Expand Down Expand Up @@ -562,7 +562,7 @@ impl Processor {
result.owner_fee,
swap_token_a_amount,
swap_token_b_amount,
to_u128(pool_mint.supply)?,
u128::from(pool_mint.supply),
trade_direction,
RoundDirection::Floor,
)
Expand Down Expand Up @@ -672,9 +672,9 @@ impl Processor {
let token_a = Self::unpack_token_account(token_a_info, token_swap.token_program_id())?;
let token_b = Self::unpack_token_account(token_b_info, token_swap.token_program_id())?;
let pool_mint = Self::unpack_mint(pool_mint_info, token_swap.token_program_id())?;
let current_pool_mint_supply = to_u128(pool_mint.supply)?;
let current_pool_mint_supply = u128::from(pool_mint.supply);
let (pool_token_amount, pool_mint_supply) = if current_pool_mint_supply > 0 {
(to_u128(pool_token_amount)?, current_pool_mint_supply)
(u128::from(pool_token_amount), current_pool_mint_supply)
} else {
(calculator.new_pool_supply(), calculator.new_pool_supply())
};
Expand All @@ -683,8 +683,8 @@ impl Processor {
.pool_tokens_to_trading_tokens(
pool_token_amount,
pool_mint_supply,
to_u128(token_a.amount)?,
to_u128(token_b.amount)?,
u128::from(token_a.amount),
u128::from(token_b.amount),
RoundDirection::Ceiling,
)
.ok_or(SwapError::ZeroTradingTokens)?;
Expand Down Expand Up @@ -794,22 +794,22 @@ impl Processor {
} else {
token_swap
.fees()
.owner_withdraw_fee(to_u128(pool_token_amount)?)
.owner_withdraw_fee(u128::from(pool_token_amount))
.ok_or(SwapError::FeeCalculationFailure)?
}
}
Err(_) => 0,
};
let pool_token_amount = to_u128(pool_token_amount)?
let pool_token_amount = u128::from(pool_token_amount)
.checked_sub(withdraw_fee)
.ok_or(SwapError::CalculationFailure)?;

let results = calculator
.pool_tokens_to_trading_tokens(
pool_token_amount,
to_u128(pool_mint.supply)?,
to_u128(token_a.amount)?,
to_u128(token_b.amount)?,
u128::from(pool_mint.supply),
u128::from(token_a.amount),
u128::from(token_b.amount),
RoundDirection::Floor,
)
.ok_or(SwapError::ZeroTradingTokens)?;
Expand Down Expand Up @@ -942,14 +942,14 @@ impl Processor {
)?;

let pool_mint = Self::unpack_mint(pool_mint_info, token_swap.token_program_id())?;
let pool_mint_supply = to_u128(pool_mint.supply)?;
let pool_mint_supply = u128::from(pool_mint.supply);
let pool_token_amount = if pool_mint_supply > 0 {
token_swap
.swap_curve()
.deposit_single_token_type(
to_u128(source_token_amount)?,
to_u128(swap_token_a.amount)?,
to_u128(swap_token_b.amount)?,
u128::from(source_token_amount),
u128::from(swap_token_a.amount),
u128::from(swap_token_b.amount),
pool_mint_supply,
trade_direction,
token_swap.fees(),
Expand Down Expand Up @@ -1067,14 +1067,14 @@ impl Processor {
)?;

let pool_mint = Self::unpack_mint(pool_mint_info, token_swap.token_program_id())?;
let pool_mint_supply = to_u128(pool_mint.supply)?;
let swap_token_a_amount = to_u128(swap_token_a.amount)?;
let swap_token_b_amount = to_u128(swap_token_b.amount)?;
let pool_mint_supply = u128::from(pool_mint.supply);
let swap_token_a_amount = u128::from(swap_token_a.amount);
let swap_token_b_amount = u128::from(swap_token_b.amount);

let burn_pool_token_amount = token_swap
.swap_curve()
.withdraw_single_token_type_exact_out(
to_u128(destination_token_amount)?,
u128::from(destination_token_amount),
swap_token_a_amount,
swap_token_b_amount,
pool_mint_supply,
Expand Down Expand Up @@ -1250,10 +1250,6 @@ impl Processor {
}
}

fn to_u128(val: u64) -> Result<u128, SwapError> {
val.try_into().map_err(|_| SwapError::ConversionFailure)
}

fn to_u64(val: u128) -> Result<u64, SwapError> {
val.try_into().map_err(|_| SwapError::ConversionFailure)
}
Expand Down Expand Up @@ -4758,9 +4754,9 @@ mod tests {
.calculator
.pool_tokens_to_trading_tokens(
withdraw_amount - withdraw_fee,
pool_mint.base.supply.try_into().unwrap(),
swap_token_a.base.amount.try_into().unwrap(),
swap_token_b.base.amount.try_into().unwrap(),
pool_mint.base.supply.into(),
swap_token_a.base.amount.into(),
swap_token_b.base.amount.into(),
RoundDirection::Floor,
)
.unwrap();
Expand Down Expand Up @@ -4837,10 +4833,10 @@ mod tests {
.swap_curve
.calculator
.pool_tokens_to_trading_tokens(
pool_fee_amount.try_into().unwrap(),
pool_mint.base.supply.try_into().unwrap(),
swap_token_a.base.amount.try_into().unwrap(),
swap_token_b.base.amount.try_into().unwrap(),
pool_fee_amount.into(),
pool_mint.base.supply.into(),
swap_token_a.base.amount.into(),
swap_token_b.base.amount.into(),
RoundDirection::Floor,
)
.unwrap();
Expand Down Expand Up @@ -6005,10 +6001,10 @@ mod tests {
let pool_token_amount = accounts
.swap_curve
.withdraw_single_token_type_exact_out(
destination_a_amount.try_into().unwrap(),
swap_token_a.base.amount.try_into().unwrap(),
swap_token_b.base.amount.try_into().unwrap(),
pool_mint.base.supply.try_into().unwrap(),
destination_a_amount.into(),
swap_token_a.base.amount.into(),
swap_token_b.base.amount.into(),
pool_mint.base.supply.into(),
TradeDirection::AtoB,
&accounts.fees,
)
Expand Down Expand Up @@ -6165,9 +6161,9 @@ mod tests {
let actual_a_to_b_amount = a_to_b_amount - token_a_fee;
let results = swap_curve
.swap(
actual_a_to_b_amount.try_into().unwrap(),
token_a_amount.try_into().unwrap(),
token_b_amount.try_into().unwrap(),
actual_a_to_b_amount.into(),
token_a_amount.into(),
token_b_amount.into(),
TradeDirection::AtoB,
&fees,
)
Expand Down Expand Up @@ -6201,9 +6197,9 @@ mod tests {
.calculator
.withdraw_single_token_type_exact_out(
results.owner_fee,
token_a_amount.try_into().unwrap(),
token_b_amount.try_into().unwrap(),
initial_supply.try_into().unwrap(),
token_a_amount.into(),
token_b_amount.into(),
initial_supply.into(),
TradeDirection::AtoB,
RoundDirection::Floor,
)
Expand Down Expand Up @@ -6243,9 +6239,9 @@ mod tests {

let mut results = swap_curve
.swap(
b_to_a_amount.try_into().unwrap(),
token_b_amount.try_into().unwrap(),
token_a_amount.try_into().unwrap(),
b_to_a_amount.into(),
token_b_amount.into(),
token_a_amount.into(),
TradeDirection::BtoA,
&fees,
)
Expand Down Expand Up @@ -6290,9 +6286,9 @@ mod tests {
.calculator
.withdraw_single_token_type_exact_out(
results.owner_fee,
token_a_amount.try_into().unwrap(),
token_b_amount.try_into().unwrap(),
initial_supply.try_into().unwrap(),
token_a_amount.into(),
token_b_amount.into(),
initial_supply.into(),
TradeDirection::BtoA,
RoundDirection::Floor,
)
Expand Down
Loading

0 comments on commit 6745513

Please sign in to comment.