Skip to content

Commit

Permalink
pool: add max positions check (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
mootz12 committed Feb 26, 2024
1 parent 31b45c3 commit 1d30893
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pool-factory/src/pool_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ impl PoolFactory for PoolFactoryContract {
panic_with_error!(&e, PoolFactoryError::InvalidPoolInitArgs);
}

// verify max positions is at least 2
if max_positions < 2 {
panic_with_error!(&e, PoolFactoryError::InvalidPoolInitArgs);
}

let mut as_u8s: [u8; 56] = [0; 56];
admin.to_string().copy_into_slice(&mut as_u8s);
let mut salt_as_bytes: Bytes = salt.into_val(&e);
Expand Down
42 changes: 41 additions & 1 deletion pool-factory/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn test_pool_factory() {

#[test]
#[should_panic(expected = "Error(Contract, #1300)")]
fn test_pool_factory_invalid_pool_init_args() {
fn test_pool_factory_invalid_pool_init_args_backstop_rate() {
let e = Env::default();
e.budget().reset_unlimited();
e.mock_all_auths_allowing_non_root_auth();
Expand Down Expand Up @@ -170,6 +170,46 @@ fn test_pool_factory_invalid_pool_init_args() {
);
}

#[test]
#[should_panic(expected = "Error(Contract, #1300)")]
fn test_pool_factory_invalid_pool_init_args_max_positions() {
let e = Env::default();
e.budget().reset_unlimited();
e.mock_all_auths_allowing_non_root_auth();
let (_, pool_factory_client) = create_pool_factory(&e);

let wasm_hash = e.deployer().upload_contract_wasm(pool::WASM);

let backstop_id = Address::generate(&e);
let blnd_id = Address::generate(&e);
let usdc_id = Address::generate(&e);

let pool_init_meta = PoolInitMeta {
backstop: backstop_id.clone(),
pool_hash: wasm_hash.clone(),
blnd_id: blnd_id.clone(),
usdc_id: usdc_id.clone(),
};
pool_factory_client.initialize(&pool_init_meta);

let bombadil = Address::generate(&e);
let oracle = Address::generate(&e);
let backstop_rate: u32 = 0_1000000;
let max_positions: u32 = 1;

let name1 = Symbol::new(&e, "pool1");
let salt = BytesN::<32>::random(&e);

pool_factory_client.deploy(
&bombadil,
&name1,
&salt,
&oracle,
&backstop_rate,
&max_positions,
);
}

#[test]
fn test_pool_factory_frontrun_protection() {
let e = Env::default();
Expand Down
25 changes: 24 additions & 1 deletion pool/src/pool/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ pub fn execute_initialize(
panic_with_error!(e, PoolError::InvalidPoolInitArgs);
}

// verify max positions is at least 2
if *max_positions < 2 {
panic_with_error!(&e, PoolError::InvalidPoolInitArgs);
}

storage::set_admin(e, admin);
storage::set_name(e, name);
storage::set_backstop(e, backstop_address);
Expand Down Expand Up @@ -289,10 +294,28 @@ mod tests {
&blnd_id,
&usdc_id,
);
});
}

#[test]
#[should_panic(expected = "Error(Contract, #1201)")]
fn test_execute_initialize_bad_max_positions() {
let e = Env::default();
let pool = testutils::create_pool(&e);

let admin = Address::generate(&e);
let name = Symbol::new(&e, "pool_name");
let oracle = Address::generate(&e);
let bstop_rate = 0_1000000;
let max_positions = 1;
let backstop_address = Address::generate(&e);
let blnd_id = Address::generate(&e);
let usdc_id = Address::generate(&e);

e.as_contract(&pool, || {
execute_initialize(
&e,
&Address::generate(&e),
&admin,
&name,
&oracle,
&bstop_rate,
Expand Down

0 comments on commit 1d30893

Please sign in to comment.