Skip to content

Commit

Permalink
Allow Closed pools to be opened (#695)
Browse files Browse the repository at this point in the history
* Add `PoolStatus::Open`

* Fix benchmark

* Add new pool status `Initialized`

* Fix benchmark

* Fix `types.json`

* Update zrml/swaps/src/lib.rs

Co-authored-by: Chralt <[email protected]>

* Update zrml/swaps/src/tests.rs

Co-authored-by: Chralt <[email protected]>

* Extend pool parameters checks

* Fix status errors

* Update changelog

Co-authored-by: Chralt <[email protected]>
  • Loading branch information
maltekliemann and Chralt98 authored Jul 18, 2022
1 parent 024bc6f commit ba6efa8
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 35 deletions.
7 changes: 7 additions & 0 deletions docs/changelog_for_devs.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v0.3.5

- Added `Initialized` status for pools. A pool now starts in `Initialized`
status and must be opened using `Swaps::open_pool`. While the pool is
`Initialized`, it is allowed to call `pool_join` and `pool_exit`, but trading
and single-asset operations are prohibited.

# v0.3.4

- Implemented swap fees for CPMM pools. This means that the following extrinsics
Expand Down
4 changes: 3 additions & 1 deletion misc/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@
"_enum": [
"Active",
"CollectingSubsidy",
"Stale"
"Closed",
"Clean",
"Initialized"
]
},
"RegistrationInfo": {
Expand Down
2 changes: 2 additions & 0 deletions primitives/src/pool_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ pub enum PoolStatus {
Closed,
/// The pool has been cleaned up, usually after the corresponding market has been resolved.
Clean,
/// The pool has just been created.
Initialized,
}
2 changes: 2 additions & 0 deletions primitives/src/traits/swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub trait Swaps<AccountId> {
/// * `pool_id`: Unique pool identifier associated with the pool to be destroyed.
fn destroy_pool_in_subsidy_phase(pool_id: PoolId) -> Result<Weight, DispatchError>;

fn open_pool(pool_id: PoolId) -> Result<Weight, DispatchError>;

/// Pool - Exit with exact pool amount
///
/// Takes an asset from `pool_id` and transfers to `origin`. Differently from `pool_exit`,
Expand Down
1 change: 1 addition & 0 deletions zrml/prediction-markets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ mod pallet {
Some(amount),
Some(weights),
)?;
T::Swaps::open_pool(pool_id)?;

// This errors if a pool already exists!
T::MarketCommons::insert_market_pool(market_id, pool_id)?;
Expand Down
4 changes: 4 additions & 0 deletions zrml/swaps/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ fn bench_create_pool<T: Config>(
.unwrap();
let pool_id = <NextPoolId<T>>::get() - 1;

if scoring_rule == ScoringRule::CPMM {
let _ = Pallet::<T>::open_pool(pool_id);
}

if subsidize {
let min_subsidy = T::MinSubsidy::get();
T::AssetManager::deposit(base_asset, &caller, min_subsidy).unwrap();
Expand Down
25 changes: 23 additions & 2 deletions zrml/swaps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,12 @@ mod pallet {
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let pool = Self::pool_by_id(pool_id)?;
ensure!(
matches!(pool.pool_status, PoolStatus::Initialized | PoolStatus::Active),
Error::<T>::InvalidPoolStatus,
);
let pool_account_id = Pallet::<T>::pool_account_id(pool_id);

Self::check_if_pool_is_active(&pool)?;
let params = PoolParams {
asset_bounds: max_assets_in,
event: |evt| Self::deposit_event(Event::PoolJoin(evt)),
Expand Down Expand Up @@ -766,6 +769,8 @@ mod pallet {
InvalidAmountArgument,
/// Could not create CPMM pool since no fee was supplied.
InvalidFeeArgument,
/// Dispatch called on pool with invalid status.
InvalidPoolStatus,
/// A function that is only valid for pools with specific scoring rules was called for a
/// pool with another scoring rule.
InvalidScoringRule,
Expand Down Expand Up @@ -847,6 +852,8 @@ mod pallet {
PoolClosed(PoolId),
/// A pool was cleaned up. \[pool_id\]
PoolCleanedUp(PoolId),
/// A pool was opened. \[pool_id\]
PoolActive(PoolId),
/// Someone has exited a pool. \[PoolAssetsEvent\]
PoolExit(
PoolAssetsEvent<
Expand Down Expand Up @@ -1423,7 +1430,7 @@ mod pallet {
);
T::AssetManager::deposit(pool_shares_id, &who, amount_unwrapped)?;

let pool_status = PoolStatus::Active;
let pool_status = PoolStatus::Initialized;
let total_subsidy = None;
let total_weight = Some(total_weight);
let weights = Some(map);
Expand Down Expand Up @@ -1684,6 +1691,20 @@ mod pallet {
})
}

fn open_pool(pool_id: PoolId) -> Result<Weight, DispatchError> {
Self::mutate_pool(pool_id, |pool| {
ensure!(
pool.pool_status == PoolStatus::Initialized,
Error::<T>::InvalidStateTransition
);
pool.pool_status = PoolStatus::Active;
Ok(())
})?;
Self::deposit_event(Event::PoolActive(pool_id));
// TODO(#603): Fix weight calculation!
Ok(T::DbWeight::get().reads_writes(1, 1))
}

/// Pool - Exit with exact pool amount
///
/// Takes an asset from `pool_id` and transfers to `origin`. Differently from `pool_exit`,
Expand Down
Loading

0 comments on commit ba6efa8

Please sign in to comment.