Skip to content

Commit

Permalink
Merge pull request #158 from blend-capital/Issue-156-queue-reserve-inits
Browse files Browse the repository at this point in the history
Issue 156 queue reserve inits
  • Loading branch information
markuspluna authored Dec 21, 2023
2 parents d40bc18 + 685cd26 commit b977cd7
Show file tree
Hide file tree
Showing 13 changed files with 672 additions and 149 deletions.
2 changes: 1 addition & 1 deletion emitter/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn set_queued_swap(e: &Env, swap: &Swap) {
);
}

/// Fetch the current queued backstop swap, or None
/// Delete the current queued backstop swap
pub fn del_queued_swap(e: &Env) {
e.storage().persistent().remove(&Symbol::new(e, SWAP_KEY));
}
Expand Down
3 changes: 2 additions & 1 deletion pool-factory/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn test_pool_factory() {
let name1 = Symbol::new(&e, "pool1");
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);

Expand Down Expand Up @@ -91,7 +92,7 @@ fn test_pool_factory() {
pool::PoolConfig {
oracle: oracle,
bstop_rate: backstop_rate,
status: 3
status: 6
}
);
assert_eq!(
Expand Down
3 changes: 3 additions & 0 deletions pool/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ pub const SCALAR_7: i128 = 1_0000000;

// seconds per year
pub const SECONDS_PER_YEAR: i128 = 31536000;

// approximate week in blocks assuming 5 seconds per block
pub const SECONDS_PER_WEEK: u64 = 604800;
49 changes: 34 additions & 15 deletions pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,35 @@ pub trait Pool {
/// If the caller is not the admin
fn update_pool(e: Env, backstop_take_rate: u64);

/// (Admin only) Initialize a reserve in the pool
/// (Admin only) Queues setting data for a reserve in the pool
///
/// ### Arguments
/// * `asset` - The underlying asset to add as a reserve
/// * `config` - The ReserveConfig for the reserve
///
/// ### Panics
/// If the caller is not the admin or the reserve is already setup
fn init_reserve(e: Env, asset: Address, metadata: ReserveConfig) -> u32;
/// If the caller is not the admin
fn queue_set_reserve(e: Env, asset: Address, metadata: ReserveConfig);

/// (Admin only) Update a reserve in the pool
/// (Admin only) Cancels the queued set of a reserve in the pool
///
/// ### Arguments
/// * `asset` - The underlying asset to add as a reserve
/// * `config` - The ReserveConfig for the reserve
///
/// ### Panics
/// If the caller is not the admin or the reserve does not exist
fn update_reserve(e: Env, asset: Address, config: ReserveConfig);
/// If the caller is not the admin or the reserve is not queued for initialization
fn cancel_set_reserve(e: Env, asset: Address);

/// (Admin only) Executes the queued set of a reserve in the pool
///
/// ### Arguments
/// * `asset` - The underlying asset to add as a reserve
///
/// ### Panics
/// If the reserve is not queued for initialization
/// or is already setup
/// or has invalid metadata
fn set_reserve(e: Env, asset: Address) -> u32;

/// Fetch the positions for an address
///
Expand Down Expand Up @@ -261,27 +271,36 @@ impl Pool for PoolContract {
.publish((Symbol::new(&e, "update_pool"), admin), backstop_take_rate);
}

fn init_reserve(e: Env, asset: Address, config: ReserveConfig) -> u32 {
fn queue_set_reserve(e: Env, asset: Address, metadata: ReserveConfig) {
storage::extend_instance(&e);
let admin = storage::get_admin(&e);
admin.require_auth();

let index = pool::initialize_reserve(&e, &asset, &config);
pool::execute_queue_set_reserve(&e, &asset, &metadata);

e.events()
.publish((Symbol::new(&e, "init_reserve"), admin), (asset, index));
index
e.events().publish(
(Symbol::new(&e, "queue_set_reserve"), admin),
(asset, metadata),
);
}

fn update_reserve(e: Env, asset: Address, config: ReserveConfig) {
fn cancel_set_reserve(e: Env, asset: Address) {
storage::extend_instance(&e);
let admin = storage::get_admin(&e);
admin.require_auth();

pool::execute_update_reserve(&e, &asset, &config);
pool::execute_cancel_queued_set_reserve(&e, &asset);

e.events()
.publish((Symbol::new(&e, "cancel_set_reserve"), admin), asset);
}

fn set_reserve(e: Env, asset: Address) -> u32 {
let index = pool::execute_set_queued_reserve(&e, &asset);

e.events()
.publish((Symbol::new(&e, "update_reserve"), admin), asset);
.publish((Symbol::new(&e, "set_reserve"),), (asset, index));
index
}

fn get_positions(e: Env, address: Address) -> Positions {
Expand Down
1 change: 1 addition & 0 deletions pool/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub enum PoolError {
NegativeAmount = 4,
InvalidPoolInitArgs = 5,
InvalidReserveMetadata = 6,
InitNotUnlocked = 7,
StatusNotAllowed = 8,
// Pool State Errors (10-19)
InvalidHf = 10,
Expand Down
Loading

0 comments on commit b977cd7

Please sign in to comment.