Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markuspluna committed Nov 30, 2023
1 parent 52fed8d commit c2e585b
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 18 deletions.
45 changes: 42 additions & 3 deletions pool/src/pool/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ mod tests {
let pool_config = PoolConfig {
oracle,
bstop_rate: 0_200_000_000,
status: 1,
status: 3,
};
e.as_contract(&pool, || {
storage::set_pool_config(&e, &pool_config);
Expand Down Expand Up @@ -296,6 +296,45 @@ mod tests {
});
}

#[test]
#[should_panic(expected = "Error(Contract, #11)")]
fn test_require_action_allowed_cancel_liquidation_while_on_ice_panics() {
let e = Env::default();

let pool = testutils::create_pool(&e);
let oracle = Address::random(&e);
let pool_config = PoolConfig {
oracle,
bstop_rate: 0_200_000_000,
status: 3,
};
e.as_contract(&pool, || {
storage::set_pool_config(&e, &pool_config);
let pool = Pool::load(&e);

pool.require_action_allowed(&e, 9);
});
}

#[test]
fn test_require_action_allowed_cancel_liquidation_while_active() {
let e = Env::default();

let pool = testutils::create_pool(&e);
let oracle = Address::random(&e);
let pool_config = PoolConfig {
oracle,
bstop_rate: 0_200_000_000,
status: 0,
};
e.as_contract(&pool, || {
storage::set_pool_config(&e, &pool_config);
let pool = Pool::load(&e);

pool.require_action_allowed(&e, 9);
});
}

#[test]
#[should_panic(expected = "Error(Contract, #11)")]
fn test_require_action_allowed_supply_while_frozen() {
Expand All @@ -306,7 +345,7 @@ mod tests {
let pool_config = PoolConfig {
oracle,
bstop_rate: 0_200_000_000,
status: 2,
status: 5,
};
e.as_contract(&pool, || {
storage::set_pool_config(&e, &pool_config);
Expand All @@ -326,7 +365,7 @@ mod tests {
let pool_config = PoolConfig {
oracle,
bstop_rate: 0_200_000_000,
status: 2,
status: 5,
};
e.as_contract(&pool, || {
storage::set_pool_config(&e, &pool_config);
Expand Down
114 changes: 105 additions & 9 deletions pool/src/pool/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ mod tests {

let new_pool_config = storage::get_pool_config(&e);
assert_eq!(new_pool_config.status, status);
assert_eq!(status, 0);
assert_eq!(status, 1);
});
}

Expand Down Expand Up @@ -266,7 +266,7 @@ mod tests {

let new_pool_config = storage::get_pool_config(&e);
assert_eq!(new_pool_config.status, status);
assert_eq!(status, 1);
assert_eq!(status, 3);
});
}

Expand Down Expand Up @@ -304,7 +304,7 @@ mod tests {
let pool_config = PoolConfig {
oracle: oracle_id,
bstop_rate: 0,
status: 0,
status: 1,
};
e.as_contract(&pool_id, || {
storage::set_admin(&e, &bombadil);
Expand All @@ -314,7 +314,7 @@ mod tests {

let new_pool_config = storage::get_pool_config(&e);
assert_eq!(new_pool_config.status, status);
assert_eq!(status, 1);
assert_eq!(status, 3);
});
}

Expand Down Expand Up @@ -352,7 +352,7 @@ mod tests {
let pool_config = PoolConfig {
oracle: oracle_id,
bstop_rate: 0,
status: 0,
status: 1,
};
e.as_contract(&pool_id, || {
storage::set_admin(&e, &bombadil);
Expand All @@ -362,16 +362,16 @@ mod tests {

let new_pool_config = storage::get_pool_config(&e);
assert_eq!(new_pool_config.status, status);
assert_eq!(status, 2);
assert_eq!(status, 5);
});
}

#[test]
#[should_panic(expected = "Error(Contract, #11)")]
#[should_panic(expected = "Error(Auth, InvalidAction)")]
fn test_update_pool_status_admin_frozen() {
let e = Env::default();
e.budget().reset_unlimited();
e.mock_all_auths_allowing_non_root_auth();
// e.mock_all_auths_allowing_non_root_auth();
let pool_id = create_pool(&e);
let oracle_id = Address::random(&e);

Expand Down Expand Up @@ -400,7 +400,7 @@ mod tests {
let pool_config = PoolConfig {
oracle: oracle_id,
bstop_rate: 0,
status: 3,
status: 4,
};
e.as_contract(&pool_id, || {
storage::set_admin(&e, &bombadil);
Expand All @@ -410,6 +410,102 @@ mod tests {
});
}

#[test]
fn test_admin_update_pool_status_unfreeze() {
let e = Env::default();
e.budget().reset_unlimited();
e.mock_all_auths_allowing_non_root_auth();

let pool_id = create_pool(&e);
let oracle_id = Address::random(&e);

let bombadil = Address::random(&e);
let samwise = Address::random(&e);

let (blnd, blnd_client) = create_token_contract(&e, &bombadil);
let (usdc, usdc_client) = create_token_contract(&e, &bombadil);
let (lp_token, lp_token_client) = create_comet_lp_pool(&e, &bombadil, &blnd, &usdc);
let (backstop_id, backstop_client) = create_backstop(&e);
setup_backstop(&e, &pool_id, &backstop_id, &lp_token, &usdc, &blnd);

// mint lp tokens
blnd_client.mint(&samwise, &500_001_0000000);
blnd_client.approve(&samwise, &lp_token, &i128::MAX, &99999);
usdc_client.mint(&samwise, &12_501_0000000);
usdc_client.approve(&samwise, &lp_token, &i128::MAX, &99999);
lp_token_client.join_pool(
&50_000_0000000,
&vec![&e, 500_001_0000000, 12_501_0000000],
&samwise,
);
backstop_client.deposit(&samwise, &pool_id, &50_000_0000000);
backstop_client.update_tkn_val();
backstop_client.queue_withdrawal(&samwise, &pool_id, &25_000_0000000);

let pool_config = PoolConfig {
oracle: oracle_id,
bstop_rate: 0,
status: 5,
};
e.as_contract(&pool_id, || {
storage::set_admin(&e, &bombadil);
storage::set_pool_config(&e, &pool_config);

let status = execute_update_pool_status(&e, 0);

let new_pool_config = storage::get_pool_config(&e);
assert_eq!(new_pool_config.status, status);
assert_eq!(status, 0);
});
}

#[test]
fn test_admin_update_pool_status_freeze() {
let e = Env::default();
e.budget().reset_unlimited();
e.mock_all_auths_allowing_non_root_auth();
let pool_id = create_pool(&e);
let oracle_id = Address::random(&e);

let bombadil = Address::random(&e);
let samwise = Address::random(&e);

let (blnd, blnd_client) = create_token_contract(&e, &bombadil);
let (usdc, usdc_client) = create_token_contract(&e, &bombadil);
let (lp_token, lp_token_client) = create_comet_lp_pool(&e, &bombadil, &blnd, &usdc);
let (backstop_id, backstop_client) = create_backstop(&e);
setup_backstop(&e, &pool_id, &backstop_id, &lp_token, &usdc, &blnd);

// mint lp tokens
blnd_client.mint(&samwise, &500_001_0000000);
blnd_client.approve(&samwise, &lp_token, &i128::MAX, &99999);
usdc_client.mint(&samwise, &12_501_0000000);
usdc_client.approve(&samwise, &lp_token, &i128::MAX, &99999);
lp_token_client.join_pool(
&50_000_0000000,
&vec![&e, 500_001_0000000, 12_501_0000000],
&samwise,
);
backstop_client.deposit(&samwise, &pool_id, &50_000_0000000);
backstop_client.update_tkn_val();

let pool_config = PoolConfig {
oracle: oracle_id,
bstop_rate: 0,
status: 1,
};
e.as_contract(&pool_id, || {
storage::set_admin(&e, &bombadil);
storage::set_pool_config(&e, &pool_config);

let status = execute_update_pool_status(&e, 4);

let new_pool_config = storage::get_pool_config(&e);
assert_eq!(new_pool_config.status, status);
assert_eq!(status, 4);
});
}

#[test]
fn test_calc_pool_backstop_threshold() {
let e = Env::default();
Expand Down
42 changes: 36 additions & 6 deletions test-suites/tests/test_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ fn test_pool_config() {
);

// Set status (admin only)
pool_fixture.pool.set_status(&1);
pool_fixture.pool.set_status(&2);
assert_eq!(
fixture.env.auths()[0],
(
Expand All @@ -726,14 +726,14 @@ fn test_pool_config() {
function: AuthorizedFunction::Contract((
pool_fixture.pool.address.clone(),
Symbol::new(&fixture.env, "set_status"),
vec![&fixture.env, 1u32.into_val(&fixture.env)]
vec![&fixture.env, 2u32.into_val(&fixture.env)]
)),
sub_invocations: std::vec![]
}
)
);
let new_pool_config = fixture.read_pool_config(0);
assert_eq!(new_pool_config.status, 1);
assert_eq!(new_pool_config.status, 2);
let event = vec![&fixture.env, fixture.env.events().all().last_unchecked()];
assert_eq!(
event,
Expand All @@ -742,7 +742,37 @@ fn test_pool_config() {
(
pool_fixture.pool.address.clone(),
(Symbol::new(&fixture.env, "set_status"), new_admin.clone()).into_val(&fixture.env),
1u32.into_val(&fixture.env)
2u32.into_val(&fixture.env)
)
]
);
//revert to standard status (admin only)
pool_fixture.pool.set_status(&3);
assert_eq!(
fixture.env.auths()[0],
(
new_admin.clone(),
AuthorizedInvocation {
function: AuthorizedFunction::Contract((
pool_fixture.pool.address.clone(),
Symbol::new(&fixture.env, "set_status"),
vec![&fixture.env, 3u32.into_val(&fixture.env)]
)),
sub_invocations: std::vec![]
}
)
);
let new_pool_config = fixture.read_pool_config(0);
assert_eq!(new_pool_config.status, 3);
let event = vec![&fixture.env, fixture.env.events().all().last_unchecked()];
assert_eq!(
event,
vec![
&fixture.env,
(
pool_fixture.pool.address.clone(),
(Symbol::new(&fixture.env, "set_status"), new_admin.clone()).into_val(&fixture.env),
3u32.into_val(&fixture.env)
)
]
);
Expand All @@ -751,7 +781,7 @@ fn test_pool_config() {
pool_fixture.pool.update_status();
assert_eq!(fixture.env.auths().len(), 0);
let new_pool_config = fixture.read_pool_config(0);
assert_eq!(new_pool_config.status, 0);
assert_eq!(new_pool_config.status, 1);
let event = vec![&fixture.env, fixture.env.events().all().last_unchecked()];
assert_eq!(
event,
Expand All @@ -760,7 +790,7 @@ fn test_pool_config() {
(
pool_fixture.pool.address.clone(),
(Symbol::new(&fixture.env, "set_status"),).into_val(&fixture.env),
0u32.into_val(&fixture.env)
1u32.into_val(&fixture.env)
)
]
);
Expand Down

0 comments on commit c2e585b

Please sign in to comment.