Skip to content

Commit

Permalink
Phoenix: updates validate_bps macro to check the range in a more Rust…
Browse files Browse the repository at this point in the history
…-native way; Pool, Pool-Stable: uses validate_bps macro in the init call and updates tests.
  • Loading branch information
gangov committed Jan 25, 2024
1 parent 25e9734 commit 59ae763
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
9 changes: 8 additions & 1 deletion contracts/pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
token_contract,
};
use decimal::Decimal;
use phoenix::{utils::is_approx_ratio, validate_int_parameters};
use phoenix::{utils::is_approx_ratio, validate_bps, validate_int_parameters};

// Metadata that is added on to the WASM custom section
contractmeta!(
Expand Down Expand Up @@ -144,6 +144,13 @@ impl LiquidityPoolTrait for LiquidityPool {
let token_init_info = lp_init_info.token_init_info;
let stake_init_info = lp_init_info.stake_init_info;

validate_bps!(
swap_fee_bps,
max_allowed_slippage_bps,
max_allowed_spread_bps,
max_referral_bps
);

set_initialized(&env);

// Token info
Expand Down
2 changes: 1 addition & 1 deletion contracts/pool/src/tests/liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ fn provide_liqudity_single_asset_one_third_with_fees() {
}

#[test]
#[should_panic(expected = "Pool: Initialize: Fees must be between 0 and 100%")]
#[should_panic(expected = "The value 10001 is out of range. Must be between 0 and 10000 bps.")]
fn provide_liqudity_too_high_fees() {
let env = Env::default();
env.mock_all_auths();
Expand Down
6 changes: 1 addition & 5 deletions contracts/pool/src/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ pub fn deploy_liquidity_pool_contract<'a>(
stake_init_info,
};

pool.initialize(
&stake_wasm_hash,
&token_wasm_hash,
&lp_init_info,
);
pool.initialize(&stake_wasm_hash, &token_wasm_hash, &lp_init_info);
pool
}
7 changes: 6 additions & 1 deletion contracts/pool_stable/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
token_contract,
};
use decimal::Decimal;
use phoenix::validate_int_parameters;
use phoenix::{validate_bps, validate_int_parameters};

// Minimum amount of initial LP shares to mint
const MINIMUM_LIQUIDITY_AMOUNT: i128 = 1000;
Expand Down Expand Up @@ -140,6 +140,11 @@ impl StableLiquidityPoolTrait for StableLiquidityPool {
let token_init_info = lp_init_info.token_init_info;
let stake_init_info = lp_init_info.stake_init_info;

validate_bps!(
swap_fee_bps,
max_allowed_slippage_bps,
max_allowed_spread_bps
);
set_initialized(&env);

// Token info
Expand Down
2 changes: 1 addition & 1 deletion contracts/pool_stable/src/tests/liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ fn provide_liqudity_single_asset_one_third_with_fees() {
}

#[test]
#[should_panic(expected = "Pool: Initialize: Fees must be between 0 and 100%")]
#[should_panic(expected = "The value 10001 is out of range. Must be between 0 and 10000 bps.")]
fn provide_liqudity_too_high_fees() {
let env = Env::default();
env.mock_all_auths();
Expand Down
7 changes: 4 additions & 3 deletions packages/phoenix/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ macro_rules! validate_bps {
const MIN_BPS: i64 = 0;
const MAX_BPS: i64 = 10_000;
$(
if $value < MIN_BPS || $value > MAX_BPS {
panic!("The value {} is out of range. Must be between {} and {} bps.", $value, MIN_BPS, MAX_BPS);
}
// if $value < MIN_BPS || $value > MAX_BPS {
// panic!("The value {} is out of range. Must be between {} and {} bps.", $value, MIN_BPS, MAX_BPS);
// }
assert!((MIN_BPS..=MAX_BPS).contains(&$value), "The value {} is out of range. Must be between {} and {} bps.", $value, MIN_BPS, MAX_BPS);
)+
}
}
Expand Down

0 comments on commit 59ae763

Please sign in to comment.