Skip to content

Commit

Permalink
added-prop-test-and-clippy-feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
crypto-vincent committed Jan 2, 2023
1 parent 058eb6d commit 4e9d96a
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub struct CollectProfitOfCredixLpDepository<'info> {
bump,
seeds::program = credix_client::ID,
constraint = credix_pass.user == depository.key() @UxdError::InvalidCredixPass,
constraint = credix_pass.disable_withdrawal_fee == true @UxdError::InvalidCredixPassNoFees,
constraint = credix_pass.disable_withdrawal_fee @UxdError::InvalidCredixPassNoFees,
)]
pub credix_pass: Account<'info, credix_client::CredixPass>,

Expand Down Expand Up @@ -424,7 +424,7 @@ pub(crate) fn handler(ctx: Context<CollectProfitOfCredixLpDepository>) -> Result

// Into functions
impl<'info> CollectProfitOfCredixLpDepository<'info> {
fn into_withdraw_funds_from_credix_lp_context(
pub fn into_withdraw_funds_from_credix_lp_context(
&self,
) -> CpiContext<'_, '_, '_, 'info, credix_client::cpi::accounts::WithdrawFunds<'info>> {
let cpi_accounts = credix_client::cpi::accounts::WithdrawFunds {
Expand All @@ -450,7 +450,7 @@ impl<'info> CollectProfitOfCredixLpDepository<'info> {
CpiContext::new(cpi_program, cpi_accounts)
}

fn into_transfer_depository_collateral_to_authority_collateral_context(
pub fn into_transfer_depository_collateral_to_authority_collateral_context(
&self,
) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let cpi_accounts = Transfer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub struct MintWithCredixLpDepository<'info> {
bump,
seeds::program = credix_client::ID,
constraint = credix_pass.user == depository.key() @UxdError::InvalidCredixPass,
constraint = credix_pass.disable_withdrawal_fee == true @UxdError::InvalidCredixPassNoFees,
constraint = credix_pass.disable_withdrawal_fee @UxdError::InvalidCredixPassNoFees,
)]
pub credix_pass: Account<'info, credix_client::CredixPass>,

Expand Down Expand Up @@ -411,7 +411,7 @@ pub(crate) fn handler(
user: ctx.accounts.user.key(),
collateral_amount: collateral_amount,
redeemable_amount: redeemable_amount_after_fees,
minting_fee_paid: minting_fee_paid,
minting_fee_paid,
});

// Accouting for depository
Expand Down Expand Up @@ -442,7 +442,7 @@ pub(crate) fn handler(

// Into functions
impl<'info> MintWithCredixLpDepository<'info> {
fn into_deposit_collateral_to_credix_lp_context(
pub fn into_deposit_collateral_to_credix_lp_context(
&self,
) -> CpiContext<'_, '_, '_, 'info, credix_client::cpi::accounts::DepositFunds<'info>> {
let cpi_accounts = credix_client::cpi::accounts::DepositFunds {
Expand All @@ -464,7 +464,7 @@ impl<'info> MintWithCredixLpDepository<'info> {
CpiContext::new(cpi_program, cpi_accounts)
}

fn into_transfer_user_collateral_to_depository_collateral_context(
pub fn into_transfer_user_collateral_to_depository_collateral_context(
&self,
) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let cpi_accounts = Transfer {
Expand All @@ -476,7 +476,7 @@ impl<'info> MintWithCredixLpDepository<'info> {
CpiContext::new(cpi_program, cpi_accounts)
}

fn into_mint_redeemable_context(&self) -> CpiContext<'_, '_, '_, 'info, MintTo<'info>> {
pub fn into_mint_redeemable_context(&self) -> CpiContext<'_, '_, '_, 'info, MintTo<'info>> {
let cpi_program = self.token_program.to_account_info();
let cpi_accounts = MintTo {
mint: self.redeemable_mint.to_account_info(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub struct RedeemFromCredixLpDepository<'info> {
bump,
seeds::program = credix_client::ID,
constraint = credix_pass.user == depository.key() @UxdError::InvalidCredixPass,
constraint = credix_pass.disable_withdrawal_fee == true @UxdError::InvalidCredixPassNoFees,
constraint = credix_pass.disable_withdrawal_fee @UxdError::InvalidCredixPassNoFees,
)]
pub credix_pass: Account<'info, credix_client::CredixPass>,

Expand Down Expand Up @@ -426,7 +426,7 @@ pub(crate) fn handler(
user: ctx.accounts.user.key(),
redeemable_amount: redeemable_amount,
collateral_amount: collateral_amount_after_precision_loss,
redeeming_fee_paid: redeeming_fee_paid,
redeeming_fee_paid,
});

// Accouting for depository
Expand All @@ -448,7 +448,7 @@ pub(crate) fn handler(

// Into functions
impl<'info> RedeemFromCredixLpDepository<'info> {
fn into_withdraw_funds_from_credix_lp_context(
pub fn into_withdraw_funds_from_credix_lp_context(
&self,
) -> CpiContext<'_, '_, '_, 'info, credix_client::cpi::accounts::WithdrawFunds<'info>> {
let cpi_accounts = credix_client::cpi::accounts::WithdrawFunds {
Expand All @@ -474,7 +474,7 @@ impl<'info> RedeemFromCredixLpDepository<'info> {
CpiContext::new(cpi_program, cpi_accounts)
}

fn into_transfer_depository_collateral_to_user_collateral_context(
pub fn into_transfer_depository_collateral_to_user_collateral_context(
&self,
) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let cpi_accounts = Transfer {
Expand All @@ -486,7 +486,7 @@ impl<'info> RedeemFromCredixLpDepository<'info> {
CpiContext::new(cpi_program, cpi_accounts)
}

fn into_burn_redeemable_context(&self) -> CpiContext<'_, '_, '_, 'info, Burn<'info>> {
pub fn into_burn_redeemable_context(&self) -> CpiContext<'_, '_, '_, 'info, Burn<'info>> {
let cpi_program = self.token_program.to_account_info();
let cpi_accounts = Burn {
mint: self.redeemable_mint.to_account_info(),
Expand Down
17 changes: 17 additions & 0 deletions programs/uxd/src/test/utils/calculate_amount_less_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod test_calculate_amount_less_fees {
use crate::utils::calculate_amount_less_fees;
use anchor_lang::Result;
use proptest::prelude::*;

#[test]
fn test_zero_fees() -> Result<()> {
Expand Down Expand Up @@ -36,4 +37,20 @@ mod test_calculate_amount_less_fees {

Ok(())
}

#[test]
fn test_no_panic() -> Result<()> {
proptest!(|(amount: u64, bps: u8)| {
prop_assert!(calculate_amount_less_fees(amount, bps).is_ok());
});
Ok(())
}

#[test]
fn test_no_increase() -> Result<()> {
proptest!(|(amount: u64, bps: u8)| {
prop_assert!(calculate_amount_less_fees(amount, bps)? <= amount);
});
Ok(())
}
}
24 changes: 24 additions & 0 deletions programs/uxd/src/test/utils/math/compute_amount_less_fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod test_compute_amount_less_fraction {
use crate::utils::compute_amount_less_fraction;
use anchor_lang::Result;
use proptest::prelude::*;

#[test]
fn test_correctness() -> Result<()> {
Expand Down Expand Up @@ -64,4 +65,27 @@ mod test_compute_amount_less_fraction {
assert_eq!(compute_amount_less_fraction(0, 101, 100).is_err(), true);
Ok(())
}

#[test]
fn test_panic_cases() -> Result<()> {
proptest!(|(amount: u64, numerator: u64, denominator: u64)| {
let result = compute_amount_less_fraction(
amount,
numerator,
denominator
);
// Some cases are supposed to fail
if denominator == 0 {
prop_assert!(result.is_err());
return Ok(());
}
if numerator > denominator {
prop_assert!(result.is_err());
return Ok(());
}
// In all other cases, we should not panic
prop_assert!(result.is_ok());
});
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod test_compute_shares_amount_for_value {
use crate::utils::compute_shares_amount_for_value;
use anchor_lang::Result;
use proptest::prelude::*;

#[test]
fn test_correctness() -> Result<()> {
Expand Down Expand Up @@ -57,4 +58,36 @@ mod test_compute_shares_amount_for_value {
assert_eq!(compute_shares_amount_for_value(1, 0, 10).is_err(), true);
Ok(())
}

#[test]
fn test_panic_cases() -> Result<()> {
proptest!(|(value: u64, total_shares_amount: u64, total_shares_value: u64)| {
let result = compute_shares_amount_for_value(
value,
total_shares_amount,
total_shares_value
);
// Some basic cases are supposed to fail
if total_shares_amount == 0 {
prop_assert!(result.is_err());
return Ok(());
}
if total_shares_value == 0 {
prop_assert!(result.is_err());
return Ok(());
}
// u64 is the limit for token amount in solana, we fail if we overflow that
let value: u128 = value.into();
let total_shares_amount: u128 = total_shares_amount.into();
let total_shares_value: u128 = total_shares_value.into();
let max_supply: u128 = u64::MAX.into();
if value * total_shares_amount / total_shares_value > max_supply {
prop_assert!(result.is_err());
return Ok(());
}
// We should not fail in any other case
prop_assert!(result.is_ok());
});
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod test_compute_value_for_shares_amount {
use crate::utils::compute_value_for_shares_amount;
use anchor_lang::Result;
use proptest::prelude::*;

#[test]
fn test_correctness() -> Result<()> {
Expand Down Expand Up @@ -61,4 +62,36 @@ mod test_compute_value_for_shares_amount {
assert_eq!(compute_value_for_shares_amount(1, 10, 0).is_err(), true);
Ok(())
}

#[test]
fn test_panic_cases() -> Result<()> {
proptest!(|(shares_amount: u64, total_shares_amount: u64, total_shares_value: u64)| {
let result = compute_value_for_shares_amount(
shares_amount,
total_shares_amount,
total_shares_value
);
// Some basic cases are supposed to fail
if total_shares_amount == 0 {
prop_assert!(result.is_err());
return Ok(());
}
if total_shares_value == 0 {
prop_assert!(result.is_err());
return Ok(());
}
// u64 is the limit for token amount in solana, we fail if we overflow that
let shares_amount: u128 = shares_amount.into();
let total_shares_amount: u128 = total_shares_amount.into();
let total_shares_value: u128 = total_shares_value.into();
let max_supply: u128 = u64::MAX.into();
if shares_amount * total_shares_value / total_shares_amount > max_supply {
prop_assert!(result.is_err());
return Ok(());
}
// We should not fail in any other case
prop_assert!(result.is_ok());
});
Ok(())
}
}
8 changes: 3 additions & 5 deletions programs/uxd/src/utils/calculate_amount_less_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use anchor_lang::prelude::*;
use super::compute_amount_less_fraction;

pub fn calculate_amount_less_fees(amount: u64, fee_amount_in_bps: u8) -> Result<u64> {
Ok(compute_amount_less_fraction(
amount,
fee_amount_in_bps.into(),
BPS_UNIT_CONVERSION,
)?)
let fraction_numerator: u64 = fee_amount_in_bps.into();
let fraction_denominator: u64 = BPS_UNIT_CONVERSION;
compute_amount_less_fraction(amount, fraction_numerator, fraction_denominator)
}
5 changes: 4 additions & 1 deletion programs/uxd/src/utils/validate_collateral_mint_usdc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{error::UxdError, state::controller::Controller};
use crate::error::UxdError;
use crate::state::controller::Controller;
use anchor_lang::prelude::*;
use anchor_spl::token::Mint;

#[cfg(feature = "production")]
use std::str::FromStr;

pub fn validate_collateral_mint_usdc(
Expand Down

0 comments on commit 4e9d96a

Please sign in to comment.