Skip to content

Commit

Permalink
Merge pull request #174 from blend-capital/issue_159_add_position_caps
Browse files Browse the repository at this point in the history
Issue 159 add position caps
  • Loading branch information
mootz12 authored Dec 29, 2023
2 parents 7c3724e + f37f382 commit 043e98f
Show file tree
Hide file tree
Showing 22 changed files with 452 additions and 20 deletions.
3 changes: 3 additions & 0 deletions mocks/mock-pool-factory/src/pool_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub trait MockPoolFactoryTrait {
salt: BytesN<32>,
oracle: Address,
backstop_take_rate: u64,
max_positions: u32,
) -> Address;

/// Checks if contract address was deployed by the factory
Expand Down Expand Up @@ -65,6 +66,7 @@ impl MockPoolFactoryTrait for MockPoolFactory {
_salt: BytesN<32>,
oracle: Address,
backstop_take_rate: u64,
max_positions: u32,
) -> Address {
storage::extend_instance(&e);
let pool_init_meta = storage::get_pool_init_meta(&e);
Expand All @@ -79,6 +81,7 @@ impl MockPoolFactoryTrait for MockPoolFactory {
init_args.push_back(name.to_val());
init_args.push_back(oracle.to_val());
init_args.push_back(backstop_take_rate.into_val(&e));
init_args.push_back(max_positions.into_val(&e));
init_args.push_back(pool_init_meta.backstop.to_val());
init_args.push_back(pool_init_meta.blnd_id.to_val());
init_args.push_back(pool_init_meta.usdc_id.to_val());
Expand Down
4 changes: 4 additions & 0 deletions pool-factory/src/pool_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ pub trait PoolFactory {
/// * `name` - The name of the pool
/// * `oracle` - The oracle address for the pool
/// * `backstop_take_rate` - The backstop take rate for the pool
/// * `max_positions` - The maximum user positions supported by the pool
fn deploy(
e: Env,
admin: Address,
name: Symbol,
salt: BytesN<32>,
oracle: Address,
backstop_take_rate: u64,
max_positions: u32,
) -> Address;

/// Checks if contract address was deployed by the factory
Expand Down Expand Up @@ -60,6 +62,7 @@ impl PoolFactory for PoolFactoryContract {
salt: BytesN<32>,
oracle: Address,
backstop_take_rate: u64,
max_positions: u32,
) -> Address {
admin.require_auth();
storage::extend_instance(&e);
Expand All @@ -75,6 +78,7 @@ impl PoolFactory for PoolFactoryContract {
init_args.push_back(name.to_val());
init_args.push_back(oracle.to_val());
init_args.push_back(backstop_take_rate.into_val(&e));
init_args.push_back(max_positions.into_val(&e));
init_args.push_back(pool_init_meta.backstop.to_val());
init_args.push_back(pool_init_meta.blnd_id.to_val());
init_args.push_back(pool_init_meta.usdc_id.to_val());
Expand Down
24 changes: 19 additions & 5 deletions pool-factory/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn test_pool_factory() {
let oracle = Address::generate(&e);
let backstop_id = Address::generate(&e);
let backstop_rate: u64 = 100000;
let max_positions: u32 = 6;
let blnd_id = Address::generate(&e);
let usdc_id = Address::generate(&e);

Expand All @@ -49,8 +50,14 @@ fn test_pool_factory() {
let name2 = Symbol::new(&e, "pool2");
let salt = BytesN::<32>::random(&e);

let deployed_pool_address_1 =
pool_factory_client.deploy(&bombadil, &name1, &salt, &oracle, &backstop_rate);
let deployed_pool_address_1 = pool_factory_client.deploy(
&bombadil,
&name1,
&salt,
&oracle,
&backstop_rate,
&max_positions,
);

let event = vec![&e, e.events().all().last_unchecked()];
assert_eq!(
Expand All @@ -66,8 +73,14 @@ fn test_pool_factory() {
);

let salt = BytesN::<32>::random(&e);
let deployed_pool_address_2 =
pool_factory_client.deploy(&bombadil, &name2, &salt, &oracle, &backstop_rate);
let deployed_pool_address_2 = pool_factory_client.deploy(
&bombadil,
&name2,
&salt,
&oracle,
&backstop_rate,
&max_positions,
);

e.as_contract(&deployed_pool_address_1, || {
assert_eq!(
Expand All @@ -92,7 +105,8 @@ fn test_pool_factory() {
pool::PoolConfig {
oracle: oracle,
bstop_rate: backstop_rate,
status: 6
status: 6,
max_positions: 6
}
);
assert_eq!(
Expand Down
8 changes: 8 additions & 0 deletions pool/src/auctions/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ mod tests {
oracle: oracle_id,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
Expand Down Expand Up @@ -487,6 +488,7 @@ mod tests {
oracle: oracle_id,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
Expand Down Expand Up @@ -597,6 +599,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_user_positions(&e, &samwise, &positions);
Expand Down Expand Up @@ -739,6 +742,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let positions: Positions = Positions {
collateral: map![
Expand Down Expand Up @@ -846,6 +850,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let positions: Positions = Positions {
collateral: map![
Expand Down Expand Up @@ -966,6 +971,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let positions: Positions = Positions {
collateral: map![
Expand Down Expand Up @@ -1154,6 +1160,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let positions: Positions = Positions {
collateral: map![
Expand Down Expand Up @@ -1275,6 +1282,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let positions: Positions = Positions {
collateral: map![
Expand Down
4 changes: 4 additions & 0 deletions pool/src/auctions/backstop_interest_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ mod tests {
oracle: oracle_id,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
Expand Down Expand Up @@ -340,6 +341,7 @@ mod tests {
oracle: oracle_id,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
Expand Down Expand Up @@ -451,6 +453,7 @@ mod tests {
oracle: oracle_id,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
Expand Down Expand Up @@ -545,6 +548,7 @@ mod tests {
oracle: Address::generate(&e),
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let mut auction_data = AuctionData {
bid: map![&e, (usdc_id.clone(), 95_0000000)],
Expand Down
6 changes: 6 additions & 0 deletions pool/src/auctions/bad_debt_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ mod tests {
oracle: oracle_id,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
Expand Down Expand Up @@ -438,6 +439,7 @@ mod tests {
oracle: oracle_id,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
Expand Down Expand Up @@ -572,6 +574,7 @@ mod tests {
oracle: oracle_id,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
Expand Down Expand Up @@ -679,6 +682,7 @@ mod tests {
oracle: Address::generate(&e),
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let mut auction_data = AuctionData {
bid: map![&e, (underlying_0, 10_0000000), (underlying_1, 2_5000000)],
Expand Down Expand Up @@ -822,6 +826,7 @@ mod tests {
oracle: Address::generate(&e),
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let mut auction_data = AuctionData {
bid: map![
Expand Down Expand Up @@ -972,6 +977,7 @@ mod tests {
oracle: Address::generate(&e),
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let mut auction_data = AuctionData {
bid: map![
Expand Down
7 changes: 7 additions & 0 deletions pool/src/auctions/user_liquidation_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ mod tests {
oracle,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
Expand Down Expand Up @@ -289,6 +290,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
e.as_contract(&pool_address, || {
storage::set_user_positions(&e, &samwise, &positions);
Expand Down Expand Up @@ -394,6 +396,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let positions: Positions = Positions {
collateral: map![
Expand Down Expand Up @@ -501,6 +504,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let positions: Positions = Positions {
collateral: map![
Expand Down Expand Up @@ -609,6 +613,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let positions: Positions = Positions {
collateral: map![
Expand Down Expand Up @@ -728,6 +733,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let positions: Positions = Positions {
collateral: map![
Expand Down Expand Up @@ -903,6 +909,7 @@ mod tests {
oracle: oracle_address,
bstop_rate: 0_100_000_000,
status: 0,
max_positions: 4,
};
let positions: Positions = Positions {
collateral: map![
Expand Down
18 changes: 13 additions & 5 deletions pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub trait Pool {
/// * `name` - The name of the pool
/// * `oracle` - The contract address of the oracle
/// * `backstop_take_rate` - The take rate for the backstop in stroops
/// * `max_positions` - The maximum number of positions a user is permitted to have
///
/// Pool Factory supplied:
/// * `backstop_id` - The contract address of the pool's backstop module
Expand All @@ -34,6 +35,7 @@ pub trait Pool {
name: Symbol,
oracle: Address,
bstop_rate: u64,
max_positions: u32,
backstop_id: Address,
blnd_id: Address,
usdc_id: Address,
Expand All @@ -52,10 +54,11 @@ pub trait Pool {
///
/// ### Arguments
/// * `backstop_take_rate` - The new take rate for the backstop
/// * `max_positions` - The new maximum number of allowed positions for a single user's account
///
/// ### Panics
/// If the caller is not the admin
fn update_pool(e: Env, backstop_take_rate: u64);
fn update_pool(e: Env, backstop_take_rate: u64, max_positions: u32);

/// (Admin only) Queues setting data for a reserve in the pool
///
Expand Down Expand Up @@ -231,6 +234,7 @@ impl Pool for PoolContract {
name: Symbol,
oracle: Address,
bstop_rate: u64,
max_postions: u32,
backstop_id: Address,
blnd_id: Address,
usdc_id: Address,
Expand All @@ -243,6 +247,7 @@ impl Pool for PoolContract {
&name,
&oracle,
&bstop_rate,
&max_postions,
&backstop_id,
&blnd_id,
&usdc_id,
Expand All @@ -253,22 +258,25 @@ impl Pool for PoolContract {
storage::extend_instance(&e);
let admin = storage::get_admin(&e);
admin.require_auth();
new_admin.require_auth();

storage::set_admin(&e, &new_admin);

e.events()
.publish((Symbol::new(&e, "set_admin"), admin), new_admin);
}

fn update_pool(e: Env, backstop_take_rate: u64) {
fn update_pool(e: Env, backstop_take_rate: u64, max_positions: u32) {
storage::extend_instance(&e);
let admin = storage::get_admin(&e);
admin.require_auth();

pool::execute_update_pool(&e, backstop_take_rate);
pool::execute_update_pool(&e, backstop_take_rate, max_positions);

e.events()
.publish((Symbol::new(&e, "update_pool"), admin), backstop_take_rate);
e.events().publish(
(Symbol::new(&e, "update_pool"), admin),
(backstop_take_rate, max_positions),
);
}

fn queue_set_reserve(e: Env, asset: Address, metadata: ReserveConfig) {
Expand Down
1 change: 1 addition & 0 deletions pool/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum PoolError {
InvalidHf = 10,
InvalidPoolStatus = 11,
InvalidUtilRate = 12,
MaxPositionsExceeded = 13,
// Emission Errors (20-29)
EmissionFailure = 20,
// Oracle Errors (30-39)
Expand Down
Loading

0 comments on commit 043e98f

Please sign in to comment.