Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iboss-ptk committed Aug 1, 2024
1 parent 9ed7315 commit d1d0362
Showing 1 changed file with 150 additions and 2 deletions.
152 changes: 150 additions & 2 deletions contracts/transmuter/src/limiter/limiters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,116 @@ mod tests {
);
}

#[test]
fn test_change_limiters_away_from_limit() {
let mut deps = mock_dependencies();
let limiter = Limiters::new("limiters");

limiter
.register(
&mut deps.storage,
"denom",
"1h",
LimiterParams::ChangeLimiter {
window_config: WindowConfig {
window_size: Uint64::from(3_600_000_000_000u64),
division_count: Uint64::from(4u64),
},
boundary_offset: Decimal::percent(1),
},
)
.unwrap();

let block_time = Timestamp::from_nanos(1661231280000000000);

// Start and set the limit
let value = Decimal::percent(55); // starting limit = 56
limiter
.check_limits_and_update(
&mut deps.storage,
vec![("denom".to_string(), (Decimal::zero(), value))],
block_time,
)
.unwrap();

// Increasing value should fail
let new_block_time = block_time.plus_nanos(900_000_000_000); // 15 minutes later
let new_value = Decimal::percent(57);
let err = limiter
.check_limits_and_update(
&mut deps.storage,
vec![("denom".to_string(), (value, new_value))],
new_block_time,
)
.unwrap_err();

assert_eq!(
err,
ContractError::UpperLimitExceeded {
denom: "denom".to_string(),
upper_limit: Decimal::percent(56),
value: new_value,
}
);

// Move away from limit but still above limit
let value = Decimal::percent(58);
let new_value = Decimal::percent(57);
limiter
.check_limits_and_update(
&mut deps.storage,
vec![("denom".to_string(), (value, new_value))],
new_block_time,
)
.unwrap();

// Move away from limit within the window
let new_block_time = block_time.plus_nanos(900_000_000_000); // 15 minutes later
let value = Decimal::percent(58);
let new_value = Decimal::percent(54); // Moving away from the limit

limiter
.check_limits_and_update(
&mut deps.storage,
vec![("denom".to_string(), (value, new_value))],
new_block_time,
)
.unwrap();

// Try to move further away from the limit
let final_block_time = new_block_time.plus_nanos(900_000_000_000); // Another 15 minutes later
let value = Decimal::percent(58);
let final_value = Decimal::percent(52); // Moving even further away from the limit

limiter
.check_limits_and_update(
&mut deps.storage,
vec![("denom".to_string(), (value, final_value))],
final_block_time,
)
.unwrap();

// Increasing the value from there should fail
let value = Decimal::percent(58);
let new_value = Decimal::percent(59);
let err = limiter
.check_limits_and_update(
&mut deps.storage,
vec![("denom".to_string(), (value, new_value))],
final_block_time,
)
.unwrap_err();

assert_eq!(
err,
ContractError::UpperLimitExceeded {
denom: "denom".to_string(),
upper_limit: Decimal::from_str("0.555").unwrap(),
value: new_value,
}
);
}

#[test]
fn test_static_limiter() {
let mut deps = mock_dependencies();
Expand Down Expand Up @@ -2328,8 +2438,8 @@ mod tests {
.check_limits_and_update(
&mut deps.storage,
vec![
("denoma".to_string(), (value_a, value_a)),
("denomb".to_string(), (value_b, value_b)),
("denoma".to_string(), (value_a - EPSILON, value_a)),
("denomb".to_string(), (value_b + EPSILON, value_b)),
],
block_time,
)
Expand Down Expand Up @@ -2408,6 +2518,44 @@ mod tests {
block_time,
)
.unwrap();

// Test case where start value is over limit but decreasing, even if not yet under limit
let value_b = Decimal::from_str("0.75").unwrap(); // Start above 0.7 limit
let value_a = Decimal::one() - value_b;

let new_value_b = Decimal::from_str("0.72").unwrap(); // Decrease, but still above 0.7 limit
let new_value_a = Decimal::one() - new_value_b;

// This should not error, as we're moving in the right direction
limiter
.check_limits_and_update(
&mut deps.storage,
vec![
("denoma".to_string(), (value_a, new_value_a)),
("denomb".to_string(), (value_b, new_value_b)),
],
block_time,
)
.unwrap();

// Test case where start value is over limit but decreasing for denom a
let value_a = Decimal::from_str("0.65").unwrap(); // Start above 0.6 limit
let value_b = Decimal::one() - value_a;

let new_value_a = Decimal::from_str("0.62").unwrap(); // Decrease, but still above 0.6 limit
let new_value_b = Decimal::one() - new_value_a;

// This should not error, as we're moving in the right direction for denom a
limiter
.check_limits_and_update(
&mut deps.storage,
vec![
("denoma".to_string(), (value_a, new_value_a)),
("denomb".to_string(), (value_b, new_value_b)),
],
block_time,
)
.unwrap();
}

#[test]
Expand Down

0 comments on commit d1d0362

Please sign in to comment.