Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pallets/pallet-bonded-coins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ pub mod pallet {
/// - `origin`: The origin of the call, requiring the caller to be a
/// manager of the pool.
/// - `pool_id`: The identifier of the pool to be locked.
/// - `lock`: The locks to be applied to the pool.
/// - `lock`: The locks to be applied to the pool. At least one lock
/// flag must be set to `false` for a valid lock.
///
/// # Returns
/// - `DispatchResult`: The result of the dispatch.
Expand All @@ -568,11 +569,16 @@ pub mod pallet {
/// pool.
/// - `Error::<T>::PoolNotLive`: If the pool is not in a live (locked or
/// active) state.
/// - `Error::<T>::InvalidInput`: If all lock flags are `true` (all
/// operations enabled), which would be equivalent to an unlocked
/// (active) pool state.
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::set_lock())]
pub fn set_lock(origin: OriginFor<T>, pool_id: T::PoolId, lock: Locks) -> DispatchResult {
let who = T::DefaultOrigin::ensure_origin(origin)?;

ensure!(lock.any_lock_set(), Error::<T>::InvalidInput);

Pools::<T>::try_mutate(&pool_id, |pool| -> DispatchResult {
let entry = pool.as_mut().ok_or(Error::<T>::PoolUnknown)?;
ensure!(entry.state.is_live(), Error::<T>::PoolNotLive);
Expand Down
39 changes: 39 additions & 0 deletions pallets/pallet-bonded-coins/src/tests/transactions/set_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,45 @@ fn set_lock_works_when_locked() {
});
}

#[test]
fn set_lock_requires_at_least_one_flag_set() {
let pool_details = generate_pool_details(
vec![DEFAULT_BONDED_CURRENCY_ID],
get_linear_bonding_curve(),
true,
Some(PoolStatus::Active),
Some(ACCOUNT_00),
Some(DEFAULT_COLLATERAL_CURRENCY_ID),
Some(ACCOUNT_00),
None,
);
let pool_id: AccountIdOf<Test> = calculate_pool_id(&[DEFAULT_BONDED_CURRENCY_ID]);

ExtBuilder::default()
.with_pools(vec![(pool_id.clone(), pool_details)])
.with_native_balances(vec![(ACCOUNT_00, ONE_HUNDRED_KILT)])
.with_collaterals(vec![DEFAULT_COLLATERAL_CURRENCY_ID])
.with_bonded_balance(vec![
(DEFAULT_COLLATERAL_CURRENCY_ID, pool_id.clone(), u128::MAX / 10),
(DEFAULT_BONDED_CURRENCY_ID, ACCOUNT_00, u128::MAX / 10),
])
.build_and_execute_with_sanity_tests(|| {
let origin = RawOrigin::Signed(ACCOUNT_00).into();

assert_err!(
BondingPallet::set_lock(
origin,
pool_id.clone(),
Locks {
allow_burn: true,
allow_mint: true
}
),
Error::<Test>::InvalidInput
);
});
}

#[test]
fn set_lock_fails_when_not_authorized() {
let pool_details = generate_pool_details(
Expand Down
6 changes: 6 additions & 0 deletions pallets/pallet-bonded-coins/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ pub struct Locks {
pub allow_burn: bool,
}

impl Locks {
pub const fn any_lock_set(&self) -> bool {
!(self.allow_mint && self.allow_burn)
}
}

/// Status of a pool.
#[derive(Clone, Encode, Decode, PartialEq, Eq, TypeInfo, MaxEncodedLen, Debug)]
pub enum PoolStatus<LockType> {
Expand Down
Loading