Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Closed pools to be opened #695

Merged
merged 11 commits into from
Jul 18, 2022
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 @@ -646,6 +646,7 @@ mod pallet {
Some(amount),
Some(weights),
)?;
T::Swaps::open_pool(pool_id)?;
Chralt98 marked this conversation as resolved.
Show resolved Hide resolved

// 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::Shares::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 @@ -370,9 +370,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,
);
maltekliemann marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -765,6 +768,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 @@ -846,6 +851,8 @@ mod pallet {
PoolClosed(PoolId),
/// A pool was cleaned up. \[pool_id\]
PoolCleanedUp(PoolId),
/// A pool was opened. \[pool_id\]
PoolActive(PoolId),
maltekliemann marked this conversation as resolved.
Show resolved Hide resolved
/// Someone has exited a pool. \[PoolAssetsEvent\]
PoolExit(
PoolAssetsEvent<
Expand Down Expand Up @@ -1412,7 +1419,7 @@ mod pallet {
);
T::Shares::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 @@ -1667,6 +1674,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