Skip to content

Commit

Permalink
adds test for the changed logic during stable pool initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
gangov committed Jul 30, 2024
1 parent 9d1f580 commit f34e6ae
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions contracts/pool_stable/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ pub enum ContractError {
SlippageToleranceExceeded = 20,
IssuedSharesLessThanUserRequested = 21,
MaximumAllowedPrecisionViolated = 22,
PresicionMissmatch = 23,
}
48 changes: 35 additions & 13 deletions contracts/pool_stable/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,18 @@ pub fn save_greatest_precision(env: &Env, token1: &Address, token2: &Address) {
let precision1 = token_contract::Client::new(env, token1).decimals();
let precision2 = token_contract::Client::new(env, token2).decimals();

if precision1 > MAXIMUM_ALLOWED_PRECISION || precision2 > MAXIMUM_ALLOWED_PRECISION {
log!(
&env,
"Pool Stable: Save Greatest Precision: precision above the limit"
);
panic_with_error!(env, ContractError::MaximumAllowedPrecisionViolated);
}
verify_precision(env, precision1, precision2);

// NOTE: now that we must have tokens with equal number of decimals, this isn't needed
// let max_precision: u32 = if precision1 > precision2 {
// precision1
// } else {
// precision2
// };

let max_precision: u32 = if precision1 > precision2 {
precision1
} else {
precision2
};
env.storage()
.instance()
.set(&DataKey::MaxPrecision, &max_precision);
.set(&DataKey::MaxPrecision, &precision1);
env.storage()
.instance()
.set(&(DataKey::TokenPrecision, token1), &precision1);
Expand All @@ -116,6 +112,18 @@ pub fn save_greatest_precision(env: &Env, token1: &Address, token2: &Address) {
.set(&(DataKey::TokenPrecision, token2), &precision2);
}

fn verify_precision(env: &Env, p1: u32, p2: u32) {
if p1 > MAXIMUM_ALLOWED_PRECISION || p2 > MAXIMUM_ALLOWED_PRECISION {
log!(&env, "Pool Stable: Initialize: precision above the limit");
panic_with_error!(env, ContractError::MaximumAllowedPrecisionViolated);
}

if p1 != p2 {
log!(&env, "Pool Stable: Initialize: precision missmatch");
panic_with_error!(env, ContractError::PresicionMissmatch);
}
}

#[contracttype]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AmplifierParameters {
Expand Down Expand Up @@ -303,4 +311,18 @@ mod tests {
let env = Env::default();
let _ = utils::get_pool_balance_b(&env);
}

#[test]
#[should_panic(expected = "Pool Stable: Initialize: precision above the limit")]
fn test_should_panic_when_precision_above_the_allowance_used() {
let env = Env::default();
verify_precision(&env, 8, 8);
}

#[test]
#[should_panic(expected = "Pool Stable: Initialize: precision missmatch")]
fn test_should_panic_when_different_precision_used() {
let env = Env::default();
verify_precision(&env, 5, 7);
}
}
2 changes: 1 addition & 1 deletion contracts/pool_stable/src/tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ fn update_config_all_bps_params_should_work() {
}

#[test]
#[should_panic(expected = "Pool Stable: Save Greatest Precision: precision above the limit")]
#[should_panic(expected = "Pool Stable: Initialize: precision above the limit")]
fn initialize_with_incorrect_precision() {
let env = Env::default();
env.mock_all_auths();
Expand Down

0 comments on commit f34e6ae

Please sign in to comment.