From b885fc135b13f1224824e704ba98ebf8a72a4595 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 11 Mar 2025 16:12:04 +0100 Subject: [PATCH 1/9] feat: flag to disallow asset management changes --- pallets/pallet-bonded-coins/src/benchmarking.rs | 4 ++++ pallets/pallet-bonded-coins/src/lib.rs | 7 ++++++- pallets/pallet-bonded-coins/src/mock.rs | 1 + .../src/tests/transactions/burn_into.rs | 1 + .../src/tests/transactions/create_pool.rs | 10 ++++++++++ pallets/pallet-bonded-coins/src/types.rs | 4 ++++ 6 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pallets/pallet-bonded-coins/src/benchmarking.rs b/pallets/pallet-bonded-coins/src/benchmarking.rs index 68a6a35f7..def0f92f4 100644 --- a/pallets/pallet-bonded-coins/src/benchmarking.rs +++ b/pallets/pallet-bonded-coins/src/benchmarking.rs @@ -232,6 +232,7 @@ mod benchmarks { denomination, bonded_currencies: BoundedVec::truncate_from(bonded_coin_ids.clone()), transferable: true, + enable_asset_management: true, min_operation_balance: 1u128.saturated_into(), deposit: Pallet::::calculate_pool_deposit(bonded_coin_ids.len()), }; @@ -274,6 +275,7 @@ mod benchmarks { currencies, 10, true, + true, 1, ); @@ -312,6 +314,7 @@ mod benchmarks { currencies, 10, true, + true, 1, ); @@ -349,6 +352,7 @@ mod benchmarks { currencies, 10, true, + true, 1, ); diff --git a/pallets/pallet-bonded-coins/src/lib.rs b/pallets/pallet-bonded-coins/src/lib.rs index c88df826c..6445b4581 100644 --- a/pallets/pallet-bonded-coins/src/lib.rs +++ b/pallets/pallet-bonded-coins/src/lib.rs @@ -369,6 +369,7 @@ pub mod pallet { currencies: BoundedVec, T::MaxCurrenciesPerPool>, denomination: u8, transferable: bool, + enable_asset_management: bool, min_operation_balance: u128, ) -> DispatchResult { let who = T::PoolCreateOrigin::ensure_origin(origin)?; @@ -450,6 +451,7 @@ pub mod pallet { collateral_id, currency_ids, transferable, + enable_asset_management, denomination, min_operation_balance, deposit_amount, @@ -498,7 +500,10 @@ pub mod pallet { let number_of_currencies = Self::get_currencies_number(&pool_details); ensure!(number_of_currencies <= currency_count, Error::::CurrencyCount); - ensure!(pool_details.is_manager(&who), Error::::NoPermission); + ensure!( + pool_details.enable_asset_management && pool_details.is_manager(&who), + Error::::NoPermission + ); ensure!(pool_details.state.is_live(), Error::::PoolNotLive); let pool_id_account = pool_id.into(); diff --git a/pallets/pallet-bonded-coins/src/mock.rs b/pallets/pallet-bonded-coins/src/mock.rs index 083982590..d35677014 100644 --- a/pallets/pallet-bonded-coins/src/mock.rs +++ b/pallets/pallet-bonded-coins/src/mock.rs @@ -119,6 +119,7 @@ pub mod runtime { curve, manager, transferable, + enable_asset_management: true, bonded_currencies, state, collateral, diff --git a/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs b/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs index 6d313e1d0..a32d6513a 100644 --- a/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs +++ b/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs @@ -56,6 +56,7 @@ fn burn_first_coin() { curve: get_linear_bonding_curve(), manager: None, transferable: true, + enable_asset_management: true, bonded_currencies: bounded_vec![DEFAULT_BONDED_CURRENCY_ID], state: PoolStatus::Active, collateral: DEFAULT_COLLATERAL_CURRENCY_ID, diff --git a/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs b/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs index edd69b618..62bcab45d 100644 --- a/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs +++ b/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs @@ -57,6 +57,7 @@ fn single_currency() { bounded_vec![bonded_token], DEFAULT_BONDED_DENOMINATION, true, + true, 1, )); @@ -139,6 +140,7 @@ fn multi_currency() { bonded_tokens, DEFAULT_BONDED_DENOMINATION, true, + true, 1 )); @@ -190,6 +192,7 @@ fn multi_currency_with_empty_metadata() { bonded_tokens, DEFAULT_BONDED_DENOMINATION, true, + true, 1 )); @@ -240,6 +243,7 @@ fn can_create_identical_pools() { bounded_vec![bonded_token.clone()], DEFAULT_BONDED_DENOMINATION, true, + true, 1 )); @@ -250,6 +254,7 @@ fn can_create_identical_pools() { bounded_vec![bonded_token], DEFAULT_BONDED_DENOMINATION, true, + true, 1 )); @@ -304,6 +309,7 @@ fn cannot_reuse_names() { bonded_tokens, DEFAULT_BONDED_DENOMINATION, true, + true, 1 ), Error::::InvalidInput @@ -347,6 +353,7 @@ fn cannot_reuse_symbols() { bonded_tokens, DEFAULT_BONDED_DENOMINATION, true, + true, 1 ), Error::::InvalidInput @@ -376,6 +383,7 @@ fn fails_if_collateral_not_exists() { bounded_vec![bonded_token], DEFAULT_BONDED_DENOMINATION, true, + true, 1 ), AssetsPalletErrors::::Unknown @@ -408,6 +416,7 @@ fn cannot_create_circular_pool() { bounded_vec![bonded_token], DEFAULT_BONDED_DENOMINATION, true, + true, 1 ), AssetsPalletErrors::::Unknown @@ -441,6 +450,7 @@ fn handles_asset_id_overflow() { bounded_vec![bonded_token; 2], DEFAULT_BONDED_DENOMINATION, true, + true, 1 ), ArithmeticError::Overflow diff --git a/pallets/pallet-bonded-coins/src/types.rs b/pallets/pallet-bonded-coins/src/types.rs index 12fe093e3..a655869df 100644 --- a/pallets/pallet-bonded-coins/src/types.rs +++ b/pallets/pallet-bonded-coins/src/types.rs @@ -101,6 +101,8 @@ pub struct PoolDetails @@ -116,6 +118,7 @@ where collateral: BaseCurrencyId, bonded_currencies: Currencies, transferable: bool, + enable_asset_management: bool, denomination: u8, min_operation_balance: u128, deposit: DepositBalance, @@ -127,6 +130,7 @@ where collateral, bonded_currencies, transferable, + enable_asset_management, state: PoolStatus::default(), denomination, min_operation_balance, From 39c16791519557a89c75b757330ff7b4beb0f5ec Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 11 Mar 2025 16:17:21 +0100 Subject: [PATCH 2/9] test: reset_team with enable_asset_management=false --- .../src/tests/transactions/reset_team.rs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs b/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs index e96fbca03..58c1d8bb5 100644 --- a/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs +++ b/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs @@ -248,6 +248,46 @@ fn only_manager_can_change_team() { }) } +#[test] +fn fails_if_asset_team_flag_not_set() { + let curve = get_linear_bonding_curve(); + + let mut pool_details = generate_pool_details( + vec![DEFAULT_BONDED_CURRENCY_ID], + curve, + false, + Some(PoolStatus::Active), + Some(ACCOUNT_00), + None, + Some(ACCOUNT_00), + None, + ); + pool_details.enable_asset_management = false; + let pool_id: AccountIdOf = 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]) + .build_and_execute_with_sanity_tests(|| { + let manager_origin = RawOrigin::Signed(ACCOUNT_00).into(); + + assert_err!( + BondingPallet::reset_team( + manager_origin, + pool_id.clone(), + PoolManagingTeam { + admin: ACCOUNT_00, + freezer: ACCOUNT_00, + }, + 1 + ), + BondingPalletErrors::::NoPermission + ); + + assert_eq!(Assets::admin(DEFAULT_BONDED_CURRENCY_ID), Some(pool_id)); + }) +} + #[test] fn handles_currency_number_incorrect() { let pool_details = generate_pool_details( From 7c3c1279c0c43ba3d15b78bc5add04ec6c42e887 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 12 Mar 2025 14:02:07 +0100 Subject: [PATCH 3/9] refactor: bonded currencies settings struct --- .../pallet-bonded-coins/src/benchmarking.rs | 41 ++++--- pallets/pallet-bonded-coins/src/lib.rs | 34 +++--- pallets/pallet-bonded-coins/src/mock.rs | 14 ++- .../src/tests/transactions/burn_into.rs | 12 +- .../src/tests/transactions/create_pool.rs | 106 +++++++++++------- .../src/tests/transactions/reset_team.rs | 2 +- pallets/pallet-bonded-coins/src/try_state.rs | 4 +- pallets/pallet-bonded-coins/src/types.rs | 37 +++--- 8 files changed, 149 insertions(+), 101 deletions(-) diff --git a/pallets/pallet-bonded-coins/src/benchmarking.rs b/pallets/pallet-bonded-coins/src/benchmarking.rs index def0f92f4..ae2d2d4ed 100644 --- a/pallets/pallet-bonded-coins/src/benchmarking.rs +++ b/pallets/pallet-bonded-coins/src/benchmarking.rs @@ -32,6 +32,7 @@ use crate::{ square_root::{SquareRootParameters, SquareRootParametersInput}, Curve, CurveInput, }, + types::CurrencySettings, Call, CollateralAssetIdOf, CollateralBalanceOf, Config, CurveParameterTypeOf, FungiblesAssetIdOf, FungiblesBalanceOf, Pallet, }; @@ -229,11 +230,13 @@ mod benchmarks { owner, state, collateral, - denomination, bonded_currencies: BoundedVec::truncate_from(bonded_coin_ids.clone()), - transferable: true, - enable_asset_management: true, - min_operation_balance: 1u128.saturated_into(), + currency_settings: CurrencySettings { + denomination, + transferable: true, + enable_asset_management: true, + min_operation_balance: 1u128.saturated_into(), + }, deposit: Pallet::::calculate_pool_deposit(bonded_coin_ids.len()), }; Pools::::insert(&pool_id, pool_details); @@ -273,10 +276,12 @@ mod benchmarks { curve, collateral_id, currencies, - 10, - true, - true, - 1, + CurrencySettings { + denomination: 10, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1, + }, ); // Verify @@ -312,10 +317,12 @@ mod benchmarks { curve, collateral_id, currencies, - 10, - true, - true, - 1, + CurrencySettings { + denomination: 10, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1, + }, ); // Verify @@ -350,10 +357,12 @@ mod benchmarks { curve, collateral_id, currencies, - 10, - true, - true, - 1, + CurrencySettings { + denomination: 10, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1, + }, ); // Verify diff --git a/pallets/pallet-bonded-coins/src/lib.rs b/pallets/pallet-bonded-coins/src/lib.rs index 6445b4581..5d879ec1f 100644 --- a/pallets/pallet-bonded-coins/src/lib.rs +++ b/pallets/pallet-bonded-coins/src/lib.rs @@ -93,7 +93,7 @@ pub mod pallet { use crate::{ curves::{balance_to_fixed, fixed_to_balance, BondingFunction, Curve, CurveInput}, traits::{FreezeAccounts, NextAssetIds, ResetTeam}, - types::{Locks, PoolDetails, PoolManagingTeam, PoolStatus, Round, TokenMeta}, + types::{CurrencySettings, Locks, PoolDetails, PoolManagingTeam, PoolStatus, Round, TokenMeta}, WeightInfo, }; @@ -131,6 +131,7 @@ pub mod pallet { BoundedCurrencyVec, CollateralAssetIdOf, DepositBalanceOf, + CurrencySettings, >; /// Minimum required amount of integer and fractional bits to perform ln, @@ -367,13 +368,17 @@ pub mod pallet { curve: CurveInput>, collateral_id: CollateralAssetIdOf, currencies: BoundedVec, T::MaxCurrenciesPerPool>, - denomination: u8, - transferable: bool, - enable_asset_management: bool, - min_operation_balance: u128, + currency_settings: CurrencySettings, ) -> DispatchResult { let who = T::PoolCreateOrigin::ensure_origin(origin)?; + let CurrencySettings { + denomination, + transferable, + enable_asset_management, + min_operation_balance, + } = currency_settings; + ensure!(denomination <= T::MaxDenomination::get(), Error::::InvalidInput); let checked_curve = curve.try_into().map_err(|_| Error::::InvalidInput)?; @@ -501,7 +506,7 @@ pub mod pallet { ensure!(number_of_currencies <= currency_count, Error::::CurrencyCount); ensure!( - pool_details.enable_asset_management && pool_details.is_manager(&who), + pool_details.currency_settings.enable_asset_management && pool_details.is_manager(&who), Error::::NoPermission ); ensure!(pool_details.state.is_live(), Error::::PoolNotLive); @@ -709,7 +714,7 @@ pub mod pallet { ensure!(number_of_currencies <= currency_count, Error::::CurrencyCount); ensure!( - amount_to_mint >= pool_details.min_operation_balance.saturated_into(), + amount_to_mint >= pool_details.currency_settings.min_operation_balance.saturated_into(), TokenError::BelowMinimum ); @@ -729,14 +734,14 @@ pub mod pallet { let (active_pre, passive) = Self::calculate_normalized_passive_issuance( &bonded_currencies, - pool_details.denomination, + pool_details.currency_settings.denomination, currency_idx, round_kind, )?; let normalized_amount_to_mint = balance_to_fixed( amount_to_mint.saturated_into::(), - pool_details.denomination, + pool_details.currency_settings.denomination, round_kind, )?; @@ -769,7 +774,7 @@ pub mod pallet { T::Fungibles::mint_into(target_currency_id.clone(), &beneficiary, amount_to_mint)?; - if !pool_details.transferable { + if !pool_details.currency_settings.transferable { T::Fungibles::freeze(target_currency_id, &beneficiary).map_err(|freeze_error| { log::info!(target: LOG_TARGET, "Failed to freeze account: {:?}", freeze_error); freeze_error.into() @@ -838,7 +843,7 @@ pub mod pallet { let pool_details = Pools::::get(&pool_id).ok_or(Error::::PoolUnknown)?; ensure!( - amount_to_burn >= pool_details.min_operation_balance.saturated_into(), + amount_to_burn >= pool_details.currency_settings.min_operation_balance.saturated_into(), TokenError::BelowMinimum ); @@ -859,12 +864,13 @@ pub mod pallet { let (high, passive) = Self::calculate_normalized_passive_issuance( &bonded_currencies, - pool_details.denomination, + pool_details.currency_settings.denomination, currency_idx, round_kind, )?; - let normalized_amount_to_burn = balance_to_fixed(amount_to_burn, pool_details.denomination, round_kind)?; + let normalized_amount_to_burn = + balance_to_fixed(amount_to_burn, pool_details.currency_settings.denomination, round_kind)?; let low = high .checked_sub(normalized_amount_to_burn) @@ -910,7 +916,7 @@ pub mod pallet { let account_exists = T::Fungibles::total_balance(target_currency_id.clone(), &who) > Zero::zero(); - if !pool_details.transferable && account_exists { + if !pool_details.currency_settings.transferable && account_exists { // Restore locks. T::Fungibles::freeze(target_currency_id, &who).map_err(|freeze_error| { log::info!(target: LOG_TARGET, "Failed to freeze account: {:?}", freeze_error); diff --git a/pallets/pallet-bonded-coins/src/mock.rs b/pallets/pallet-bonded-coins/src/mock.rs index d35677014..0045e2b4c 100644 --- a/pallets/pallet-bonded-coins/src/mock.rs +++ b/pallets/pallet-bonded-coins/src/mock.rs @@ -69,7 +69,7 @@ pub mod runtime { use crate::{ self as pallet_bonded_coins, traits::NextAssetIds, - types::{Locks, PoolStatus}, + types::{CurrencySettings, Locks, PoolStatus}, AccountIdOf, Config, DepositBalanceOf, FungiblesAssetIdOf, FungiblesBalanceOf, PoolDetailsOf, }; @@ -118,14 +118,16 @@ pub mod runtime { PoolDetailsOf:: { curve, manager, - transferable, - enable_asset_management: true, bonded_currencies, state, collateral, - denomination: DEFAULT_BONDED_DENOMINATION, + currency_settings: CurrencySettings { + enable_asset_management: true, + transferable, + min_operation_balance, + denomination: DEFAULT_BONDED_DENOMINATION, + }, owner, - min_operation_balance, deposit: BondingPallet::calculate_pool_deposit(currencies.len()), } } @@ -447,7 +449,7 @@ pub mod runtime { pool_details .bonded_currencies .iter() - .map(|id| (*id, vec![], vec![], pool_details.denomination)) + .map(|id| (*id, vec![], vec![], pool_details.currency_settings.denomination)) .collect::, Vec, u8)>>() }) .chain( diff --git a/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs b/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs index a32d6513a..d691b55da 100644 --- a/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs +++ b/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs @@ -27,7 +27,7 @@ use sp_runtime::{assert_eq_error_rate, bounded_vec, traits::Scale, TokenError}; use crate::{ mock::{runtime::*, *}, - types::{Locks, PoolStatus}, + types::{CurrencySettings, Locks, PoolStatus}, AccountIdOf, Error, PoolDetailsOf, }; @@ -55,14 +55,16 @@ fn burn_first_coin() { PoolDetailsOf:: { curve: get_linear_bonding_curve(), manager: None, - transferable: true, - enable_asset_management: true, bonded_currencies: bounded_vec![DEFAULT_BONDED_CURRENCY_ID], state: PoolStatus::Active, collateral: DEFAULT_COLLATERAL_CURRENCY_ID, - denomination: 0, + currency_settings: CurrencySettings { + transferable: true, + enable_asset_management: true, + denomination: 0, + min_operation_balance: 1, + }, owner: ACCOUNT_99, - min_operation_balance: 1, deposit: BondingPallet::calculate_pool_deposit(1), }, )]) diff --git a/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs b/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs index 62bcab45d..69f3aa3d1 100644 --- a/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs +++ b/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs @@ -28,7 +28,7 @@ use sp_std::ops::Sub; use crate::{ mock::{runtime::*, *}, - types::{Locks, PoolStatus}, + types::{CurrencySettings, Locks, PoolStatus}, AccountIdOf, Error, Event as BondingPalletEvents, Pools, TokenMetaOf, }; @@ -55,10 +55,12 @@ fn single_currency() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bounded_vec![bonded_token], - DEFAULT_BONDED_DENOMINATION, - true, - true, - 1, + CurrencySettings { + denomination: DEFAULT_BONDED_DENOMINATION, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1 + }, )); let pool_id: AccountIdOf = calculate_pool_id(&[new_asset_id]); @@ -67,7 +69,7 @@ fn single_currency() { assert!(details.is_owner(&ACCOUNT_00)); assert!(details.is_manager(&ACCOUNT_00)); - assert!(details.transferable); + assert!(details.currency_settings.transferable); assert_eq!( details.state, PoolStatus::Locked(Locks { @@ -75,7 +77,7 @@ fn single_currency() { allow_burn: false, }) ); - assert_eq!(details.denomination, DEFAULT_BONDED_DENOMINATION); + assert_eq!(details.currency_settings.denomination, DEFAULT_BONDED_DENOMINATION); assert_eq!(details.collateral, DEFAULT_COLLATERAL_CURRENCY_ID); assert_eq!(details.bonded_currencies, vec![new_asset_id]); @@ -138,10 +140,12 @@ fn multi_currency() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bonded_tokens, - DEFAULT_BONDED_DENOMINATION, - true, - true, - 1 + CurrencySettings { + denomination: DEFAULT_BONDED_DENOMINATION, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1 + } )); assert_eq!(NextAssetId::::get(), next_asset_id + 3); @@ -190,10 +194,12 @@ fn multi_currency_with_empty_metadata() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bonded_tokens, - DEFAULT_BONDED_DENOMINATION, - true, - true, - 1 + CurrencySettings { + denomination: DEFAULT_BONDED_DENOMINATION, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1 + } )); assert_eq!(NextAssetId::::get(), next_asset_id + 3); @@ -241,10 +247,12 @@ fn can_create_identical_pools() { curve.clone(), DEFAULT_COLLATERAL_CURRENCY_ID, bounded_vec![bonded_token.clone()], - DEFAULT_BONDED_DENOMINATION, - true, - true, - 1 + CurrencySettings { + denomination: DEFAULT_BONDED_DENOMINATION, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1 + } )); assert_ok!(BondingPallet::create_pool( @@ -252,10 +260,12 @@ fn can_create_identical_pools() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bounded_vec![bonded_token], - DEFAULT_BONDED_DENOMINATION, - true, - true, - 1 + CurrencySettings { + denomination: DEFAULT_BONDED_DENOMINATION, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1 + } )); assert_eq!(NextAssetId::::get(), next_asset_id + 2); @@ -307,10 +317,12 @@ fn cannot_reuse_names() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bonded_tokens, - DEFAULT_BONDED_DENOMINATION, - true, - true, - 1 + CurrencySettings { + denomination: DEFAULT_BONDED_DENOMINATION, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1 + } ), Error::::InvalidInput ); @@ -351,10 +363,12 @@ fn cannot_reuse_symbols() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bonded_tokens, - DEFAULT_BONDED_DENOMINATION, - true, - true, - 1 + CurrencySettings { + denomination: DEFAULT_BONDED_DENOMINATION, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1 + } ), Error::::InvalidInput ); @@ -381,10 +395,12 @@ fn fails_if_collateral_not_exists() { curve, 100, bounded_vec![bonded_token], - DEFAULT_BONDED_DENOMINATION, - true, - true, - 1 + CurrencySettings { + denomination: DEFAULT_BONDED_DENOMINATION, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1 + } ), AssetsPalletErrors::::Unknown ); @@ -414,10 +430,12 @@ fn cannot_create_circular_pool() { // try specifying the id of the currency to be created as collateral next_asset_id, bounded_vec![bonded_token], - DEFAULT_BONDED_DENOMINATION, - true, - true, - 1 + CurrencySettings { + denomination: DEFAULT_BONDED_DENOMINATION, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1 + } ), AssetsPalletErrors::::Unknown ); @@ -448,10 +466,12 @@ fn handles_asset_id_overflow() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bounded_vec![bonded_token; 2], - DEFAULT_BONDED_DENOMINATION, - true, - true, - 1 + CurrencySettings { + denomination: DEFAULT_BONDED_DENOMINATION, + enable_asset_management: true, + transferable: true, + min_operation_balance: 1 + } ), ArithmeticError::Overflow ); diff --git a/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs b/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs index 58c1d8bb5..0f55603a4 100644 --- a/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs +++ b/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs @@ -262,7 +262,7 @@ fn fails_if_asset_team_flag_not_set() { Some(ACCOUNT_00), None, ); - pool_details.enable_asset_management = false; + pool_details.currency_settings.enable_asset_management = false; let pool_id: AccountIdOf = calculate_pool_id(&[DEFAULT_BONDED_CURRENCY_ID]); ExtBuilder::default() .with_pools(vec![(pool_id.clone(), pool_details)]) diff --git a/pallets/pallet-bonded-coins/src/try_state.rs b/pallets/pallet-bonded-coins/src/try_state.rs index b1407bf44..d467381d3 100644 --- a/pallets/pallet-bonded-coins/src/try_state.rs +++ b/pallets/pallet-bonded-coins/src/try_state.rs @@ -18,7 +18,7 @@ pub(crate) fn do_try_state() -> Result<(), TryRuntimeError> { owner, bonded_currencies, state, - denomination, + currency_settings, .. } = pool_details; @@ -61,7 +61,7 @@ pub(crate) fn do_try_state() -> Result<(), TryRuntimeError> { // The Currency in the fungibles pallet should always match with the // denomination stored in the pool. let currency_denomination = T::Fungibles::decimals(currency_id.clone()); - assert_eq!(currency_denomination, denomination); + assert_eq!(currency_denomination, currency_settings.denomination); // if currency has on-zero supply -> collateral in pool account must be // non-zero. diff --git a/pallets/pallet-bonded-coins/src/types.rs b/pallets/pallet-bonded-coins/src/types.rs index a655869df..29403d917 100644 --- a/pallets/pallet-bonded-coins/src/types.rs +++ b/pallets/pallet-bonded-coins/src/types.rs @@ -78,9 +78,22 @@ impl PoolStatus { } } +#[derive(Clone, Encode, Decode, PartialEq, Eq, TypeInfo, MaxEncodedLen, Debug)] +pub struct CurrencySettings { + /// The minimum amount that can be minted/burnt. + pub min_operation_balance: u128, + /// The denomination of the pool. + pub denomination: u8, + /// Whether asset management changes are allowed. + pub enable_asset_management: bool, + /// Whether the pool is transferable or not. + pub transferable: bool, +} + /// Details of a pool. #[derive(Clone, Encode, Decode, PartialEq, Eq, TypeInfo, MaxEncodedLen, Debug)] -pub struct PoolDetails { +pub struct PoolDetails +{ /// The owner of the pool. pub owner: AccountId, /// The manager of the pool. If a manager is set, the pool is permissioned. @@ -93,20 +106,14 @@ pub struct PoolDetails, - /// Whether the pool is transferable or not. - pub transferable: bool, - /// The denomination of the pool. - pub denomination: u8, - /// The minimum amount that can be minted/burnt. - pub min_operation_balance: u128, + + pub currency_settings: BondedCurrencySettings, /// The deposit to be returned upon destruction of this pool. pub deposit: DepositBalance, - /// Whether asset management changes are allowed. - pub enable_asset_management: bool, } impl - PoolDetails + PoolDetails where AccountId: PartialEq + Clone, { @@ -129,11 +136,13 @@ where curve, collateral, bonded_currencies, - transferable, - enable_asset_management, + currency_settings: CurrencySettings { + transferable, + enable_asset_management, + denomination, + min_operation_balance, + }, state: PoolStatus::default(), - denomination, - min_operation_balance, deposit, } } From 42fbfeca10fe9c0851ed56a3f244a02e456bdd0a Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 13 Mar 2025 17:46:46 +0100 Subject: [PATCH 4/9] refactor: unpack currency_settings --- pallets/pallet-bonded-coins/src/lib.rs | 43 +++++++++++++++++--------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/pallets/pallet-bonded-coins/src/lib.rs b/pallets/pallet-bonded-coins/src/lib.rs index 5d879ec1f..1fbfed0f8 100644 --- a/pallets/pallet-bonded-coins/src/lib.rs +++ b/pallets/pallet-bonded-coins/src/lib.rs @@ -505,8 +505,13 @@ pub mod pallet { let number_of_currencies = Self::get_currencies_number(&pool_details); ensure!(number_of_currencies <= currency_count, Error::::CurrencyCount); + let CurrencySettings { + enable_asset_management, + .. + } = pool_details.currency_settings; + ensure!( - pool_details.currency_settings.enable_asset_management && pool_details.is_manager(&who), + enable_asset_management && pool_details.is_manager(&who), Error::::NoPermission ); ensure!(pool_details.state.is_live(), Error::::PoolNotLive); @@ -713,8 +718,15 @@ pub mod pallet { let number_of_currencies = Self::get_currencies_number(&pool_details); ensure!(number_of_currencies <= currency_count, Error::::CurrencyCount); + let CurrencySettings { + min_operation_balance, + denomination, + transferable, + .. + } = pool_details.currency_settings; + ensure!( - amount_to_mint >= pool_details.currency_settings.min_operation_balance.saturated_into(), + amount_to_mint >= min_operation_balance.saturated_into(), TokenError::BelowMinimum ); @@ -734,16 +746,13 @@ pub mod pallet { let (active_pre, passive) = Self::calculate_normalized_passive_issuance( &bonded_currencies, - pool_details.currency_settings.denomination, + denomination, currency_idx, round_kind, )?; - let normalized_amount_to_mint = balance_to_fixed( - amount_to_mint.saturated_into::(), - pool_details.currency_settings.denomination, - round_kind, - )?; + let normalized_amount_to_mint = + balance_to_fixed(amount_to_mint.saturated_into::(), denomination, round_kind)?; let active_post = active_pre .checked_add(normalized_amount_to_mint) @@ -774,7 +783,7 @@ pub mod pallet { T::Fungibles::mint_into(target_currency_id.clone(), &beneficiary, amount_to_mint)?; - if !pool_details.currency_settings.transferable { + if !transferable { T::Fungibles::freeze(target_currency_id, &beneficiary).map_err(|freeze_error| { log::info!(target: LOG_TARGET, "Failed to freeze account: {:?}", freeze_error); freeze_error.into() @@ -842,8 +851,15 @@ pub mod pallet { let pool_details = Pools::::get(&pool_id).ok_or(Error::::PoolUnknown)?; + let CurrencySettings { + min_operation_balance, + denomination, + transferable, + .. + } = pool_details.currency_settings; + ensure!( - amount_to_burn >= pool_details.currency_settings.min_operation_balance.saturated_into(), + amount_to_burn >= min_operation_balance.saturated_into(), TokenError::BelowMinimum ); @@ -864,13 +880,12 @@ pub mod pallet { let (high, passive) = Self::calculate_normalized_passive_issuance( &bonded_currencies, - pool_details.currency_settings.denomination, + denomination, currency_idx, round_kind, )?; - let normalized_amount_to_burn = - balance_to_fixed(amount_to_burn, pool_details.currency_settings.denomination, round_kind)?; + let normalized_amount_to_burn = balance_to_fixed(amount_to_burn, denomination, round_kind)?; let low = high .checked_sub(normalized_amount_to_burn) @@ -916,7 +931,7 @@ pub mod pallet { let account_exists = T::Fungibles::total_balance(target_currency_id.clone(), &who) > Zero::zero(); - if !pool_details.currency_settings.transferable && account_exists { + if !transferable && account_exists { // Restore locks. T::Fungibles::freeze(target_currency_id, &who).map_err(|freeze_error| { log::info!(target: LOG_TARGET, "Failed to freeze account: {:?}", freeze_error); From 98edfab32f3c67e64ac80dc5c2375f33fe40e816 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 13 Mar 2025 18:08:50 +0100 Subject: [PATCH 5/9] refactor: renamings --- .../pallet-bonded-coins/src/benchmarking.rs | 18 ++++---- pallets/pallet-bonded-coins/src/lib.rs | 29 ++++++------ pallets/pallet-bonded-coins/src/mock.rs | 8 ++-- .../src/tests/transactions/burn_into.rs | 6 +-- .../src/tests/transactions/create_pool.rs | 46 +++++++++---------- .../src/tests/transactions/reset_team.rs | 2 +- pallets/pallet-bonded-coins/src/try_state.rs | 4 +- pallets/pallet-bonded-coins/src/types.rs | 17 ++++--- 8 files changed, 63 insertions(+), 67 deletions(-) diff --git a/pallets/pallet-bonded-coins/src/benchmarking.rs b/pallets/pallet-bonded-coins/src/benchmarking.rs index ae2d2d4ed..82e506ed2 100644 --- a/pallets/pallet-bonded-coins/src/benchmarking.rs +++ b/pallets/pallet-bonded-coins/src/benchmarking.rs @@ -32,7 +32,7 @@ use crate::{ square_root::{SquareRootParameters, SquareRootParametersInput}, Curve, CurveInput, }, - types::CurrencySettings, + types::BondedCurrenciesSettings, Call, CollateralAssetIdOf, CollateralBalanceOf, Config, CurveParameterTypeOf, FungiblesAssetIdOf, FungiblesBalanceOf, Pallet, }; @@ -231,10 +231,10 @@ mod benchmarks { state, collateral, bonded_currencies: BoundedVec::truncate_from(bonded_coin_ids.clone()), - currency_settings: CurrencySettings { + currencies_settings: BondedCurrenciesSettings { denomination, transferable: true, - enable_asset_management: true, + allow_reset_team: true, min_operation_balance: 1u128.saturated_into(), }, deposit: Pallet::::calculate_pool_deposit(bonded_coin_ids.len()), @@ -276,9 +276,9 @@ mod benchmarks { curve, collateral_id, currencies, - CurrencySettings { + BondedCurrenciesSettings { denomination: 10, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1, }, @@ -317,9 +317,9 @@ mod benchmarks { curve, collateral_id, currencies, - CurrencySettings { + BondedCurrenciesSettings { denomination: 10, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1, }, @@ -357,9 +357,9 @@ mod benchmarks { curve, collateral_id, currencies, - CurrencySettings { + BondedCurrenciesSettings { denomination: 10, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1, }, diff --git a/pallets/pallet-bonded-coins/src/lib.rs b/pallets/pallet-bonded-coins/src/lib.rs index 1fbfed0f8..b1280f1e6 100644 --- a/pallets/pallet-bonded-coins/src/lib.rs +++ b/pallets/pallet-bonded-coins/src/lib.rs @@ -93,7 +93,7 @@ pub mod pallet { use crate::{ curves::{balance_to_fixed, fixed_to_balance, BondingFunction, Curve, CurveInput}, traits::{FreezeAccounts, NextAssetIds, ResetTeam}, - types::{CurrencySettings, Locks, PoolDetails, PoolManagingTeam, PoolStatus, Round, TokenMeta}, + types::{BondedCurrenciesSettings, Locks, PoolDetails, PoolManagingTeam, PoolStatus, Round, TokenMeta}, WeightInfo, }; @@ -131,7 +131,7 @@ pub mod pallet { BoundedCurrencyVec, CollateralAssetIdOf, DepositBalanceOf, - CurrencySettings, + BondedCurrenciesSettings, >; /// Minimum required amount of integer and fractional bits to perform ln, @@ -368,16 +368,16 @@ pub mod pallet { curve: CurveInput>, collateral_id: CollateralAssetIdOf, currencies: BoundedVec, T::MaxCurrenciesPerPool>, - currency_settings: CurrencySettings, + currencies_settings: BondedCurrenciesSettings, ) -> DispatchResult { let who = T::PoolCreateOrigin::ensure_origin(origin)?; - let CurrencySettings { + let BondedCurrenciesSettings { denomination, transferable, - enable_asset_management, + allow_reset_team, min_operation_balance, - } = currency_settings; + } = currencies_settings; ensure!(denomination <= T::MaxDenomination::get(), Error::::InvalidInput); let checked_curve = curve.try_into().map_err(|_| Error::::InvalidInput)?; @@ -456,7 +456,7 @@ pub mod pallet { collateral_id, currency_ids, transferable, - enable_asset_management, + allow_reset_team, denomination, min_operation_balance, deposit_amount, @@ -505,13 +505,10 @@ pub mod pallet { let number_of_currencies = Self::get_currencies_number(&pool_details); ensure!(number_of_currencies <= currency_count, Error::::CurrencyCount); - let CurrencySettings { - enable_asset_management, - .. - } = pool_details.currency_settings; + let BondedCurrenciesSettings { allow_reset_team, .. } = pool_details.currencies_settings; ensure!( - enable_asset_management && pool_details.is_manager(&who), + allow_reset_team && pool_details.is_manager(&who), Error::::NoPermission ); ensure!(pool_details.state.is_live(), Error::::PoolNotLive); @@ -718,12 +715,12 @@ pub mod pallet { let number_of_currencies = Self::get_currencies_number(&pool_details); ensure!(number_of_currencies <= currency_count, Error::::CurrencyCount); - let CurrencySettings { + let BondedCurrenciesSettings { min_operation_balance, denomination, transferable, .. - } = pool_details.currency_settings; + } = pool_details.currencies_settings; ensure!( amount_to_mint >= min_operation_balance.saturated_into(), @@ -851,12 +848,12 @@ pub mod pallet { let pool_details = Pools::::get(&pool_id).ok_or(Error::::PoolUnknown)?; - let CurrencySettings { + let BondedCurrenciesSettings { min_operation_balance, denomination, transferable, .. - } = pool_details.currency_settings; + } = pool_details.currencies_settings; ensure!( amount_to_burn >= min_operation_balance.saturated_into(), diff --git a/pallets/pallet-bonded-coins/src/mock.rs b/pallets/pallet-bonded-coins/src/mock.rs index 0045e2b4c..5d1078142 100644 --- a/pallets/pallet-bonded-coins/src/mock.rs +++ b/pallets/pallet-bonded-coins/src/mock.rs @@ -69,7 +69,7 @@ pub mod runtime { use crate::{ self as pallet_bonded_coins, traits::NextAssetIds, - types::{CurrencySettings, Locks, PoolStatus}, + types::{BondedCurrenciesSettings, Locks, PoolStatus}, AccountIdOf, Config, DepositBalanceOf, FungiblesAssetIdOf, FungiblesBalanceOf, PoolDetailsOf, }; @@ -121,8 +121,8 @@ pub mod runtime { bonded_currencies, state, collateral, - currency_settings: CurrencySettings { - enable_asset_management: true, + currencies_settings: BondedCurrenciesSettings { + allow_reset_team: true, transferable, min_operation_balance, denomination: DEFAULT_BONDED_DENOMINATION, @@ -449,7 +449,7 @@ pub mod runtime { pool_details .bonded_currencies .iter() - .map(|id| (*id, vec![], vec![], pool_details.currency_settings.denomination)) + .map(|id| (*id, vec![], vec![], pool_details.currencies_settings.denomination)) .collect::, Vec, u8)>>() }) .chain( diff --git a/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs b/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs index d691b55da..17da44bc4 100644 --- a/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs +++ b/pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs @@ -27,7 +27,7 @@ use sp_runtime::{assert_eq_error_rate, bounded_vec, traits::Scale, TokenError}; use crate::{ mock::{runtime::*, *}, - types::{CurrencySettings, Locks, PoolStatus}, + types::{BondedCurrenciesSettings, Locks, PoolStatus}, AccountIdOf, Error, PoolDetailsOf, }; @@ -58,9 +58,9 @@ fn burn_first_coin() { bonded_currencies: bounded_vec![DEFAULT_BONDED_CURRENCY_ID], state: PoolStatus::Active, collateral: DEFAULT_COLLATERAL_CURRENCY_ID, - currency_settings: CurrencySettings { + currencies_settings: BondedCurrenciesSettings { transferable: true, - enable_asset_management: true, + allow_reset_team: true, denomination: 0, min_operation_balance: 1, }, diff --git a/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs b/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs index 69f3aa3d1..8b83f6d16 100644 --- a/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs +++ b/pallets/pallet-bonded-coins/src/tests/transactions/create_pool.rs @@ -28,7 +28,7 @@ use sp_std::ops::Sub; use crate::{ mock::{runtime::*, *}, - types::{CurrencySettings, Locks, PoolStatus}, + types::{BondedCurrenciesSettings, Locks, PoolStatus}, AccountIdOf, Error, Event as BondingPalletEvents, Pools, TokenMetaOf, }; @@ -55,9 +55,9 @@ fn single_currency() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bounded_vec![bonded_token], - CurrencySettings { + BondedCurrenciesSettings { denomination: DEFAULT_BONDED_DENOMINATION, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1 }, @@ -69,7 +69,7 @@ fn single_currency() { assert!(details.is_owner(&ACCOUNT_00)); assert!(details.is_manager(&ACCOUNT_00)); - assert!(details.currency_settings.transferable); + assert!(details.currencies_settings.transferable); assert_eq!( details.state, PoolStatus::Locked(Locks { @@ -77,7 +77,7 @@ fn single_currency() { allow_burn: false, }) ); - assert_eq!(details.currency_settings.denomination, DEFAULT_BONDED_DENOMINATION); + assert_eq!(details.currencies_settings.denomination, DEFAULT_BONDED_DENOMINATION); assert_eq!(details.collateral, DEFAULT_COLLATERAL_CURRENCY_ID); assert_eq!(details.bonded_currencies, vec![new_asset_id]); @@ -140,9 +140,9 @@ fn multi_currency() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bonded_tokens, - CurrencySettings { + BondedCurrenciesSettings { denomination: DEFAULT_BONDED_DENOMINATION, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1 } @@ -194,9 +194,9 @@ fn multi_currency_with_empty_metadata() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bonded_tokens, - CurrencySettings { + BondedCurrenciesSettings { denomination: DEFAULT_BONDED_DENOMINATION, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1 } @@ -247,9 +247,9 @@ fn can_create_identical_pools() { curve.clone(), DEFAULT_COLLATERAL_CURRENCY_ID, bounded_vec![bonded_token.clone()], - CurrencySettings { + BondedCurrenciesSettings { denomination: DEFAULT_BONDED_DENOMINATION, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1 } @@ -260,9 +260,9 @@ fn can_create_identical_pools() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bounded_vec![bonded_token], - CurrencySettings { + BondedCurrenciesSettings { denomination: DEFAULT_BONDED_DENOMINATION, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1 } @@ -317,9 +317,9 @@ fn cannot_reuse_names() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bonded_tokens, - CurrencySettings { + BondedCurrenciesSettings { denomination: DEFAULT_BONDED_DENOMINATION, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1 } @@ -363,9 +363,9 @@ fn cannot_reuse_symbols() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bonded_tokens, - CurrencySettings { + BondedCurrenciesSettings { denomination: DEFAULT_BONDED_DENOMINATION, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1 } @@ -395,9 +395,9 @@ fn fails_if_collateral_not_exists() { curve, 100, bounded_vec![bonded_token], - CurrencySettings { + BondedCurrenciesSettings { denomination: DEFAULT_BONDED_DENOMINATION, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1 } @@ -430,9 +430,9 @@ fn cannot_create_circular_pool() { // try specifying the id of the currency to be created as collateral next_asset_id, bounded_vec![bonded_token], - CurrencySettings { + BondedCurrenciesSettings { denomination: DEFAULT_BONDED_DENOMINATION, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1 } @@ -466,9 +466,9 @@ fn handles_asset_id_overflow() { curve, DEFAULT_COLLATERAL_CURRENCY_ID, bounded_vec![bonded_token; 2], - CurrencySettings { + BondedCurrenciesSettings { denomination: DEFAULT_BONDED_DENOMINATION, - enable_asset_management: true, + allow_reset_team: true, transferable: true, min_operation_balance: 1 } diff --git a/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs b/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs index 0f55603a4..5683cc764 100644 --- a/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs +++ b/pallets/pallet-bonded-coins/src/tests/transactions/reset_team.rs @@ -262,7 +262,7 @@ fn fails_if_asset_team_flag_not_set() { Some(ACCOUNT_00), None, ); - pool_details.currency_settings.enable_asset_management = false; + pool_details.currencies_settings.allow_reset_team = false; let pool_id: AccountIdOf = calculate_pool_id(&[DEFAULT_BONDED_CURRENCY_ID]); ExtBuilder::default() .with_pools(vec![(pool_id.clone(), pool_details)]) diff --git a/pallets/pallet-bonded-coins/src/try_state.rs b/pallets/pallet-bonded-coins/src/try_state.rs index d467381d3..e88481bff 100644 --- a/pallets/pallet-bonded-coins/src/try_state.rs +++ b/pallets/pallet-bonded-coins/src/try_state.rs @@ -18,7 +18,7 @@ pub(crate) fn do_try_state() -> Result<(), TryRuntimeError> { owner, bonded_currencies, state, - currency_settings, + currencies_settings, .. } = pool_details; @@ -61,7 +61,7 @@ pub(crate) fn do_try_state() -> Result<(), TryRuntimeError> { // The Currency in the fungibles pallet should always match with the // denomination stored in the pool. let currency_denomination = T::Fungibles::decimals(currency_id.clone()); - assert_eq!(currency_denomination, currency_settings.denomination); + assert_eq!(currency_denomination, currencies_settings.denomination); // if currency has on-zero supply -> collateral in pool account must be // non-zero. diff --git a/pallets/pallet-bonded-coins/src/types.rs b/pallets/pallet-bonded-coins/src/types.rs index 29403d917..0b7a665ee 100644 --- a/pallets/pallet-bonded-coins/src/types.rs +++ b/pallets/pallet-bonded-coins/src/types.rs @@ -79,21 +79,20 @@ impl PoolStatus { } #[derive(Clone, Encode, Decode, PartialEq, Eq, TypeInfo, MaxEncodedLen, Debug)] -pub struct CurrencySettings { +pub struct BondedCurrenciesSettings { /// The minimum amount that can be minted/burnt. pub min_operation_balance: u128, /// The denomination of the pool. pub denomination: u8, /// Whether asset management changes are allowed. - pub enable_asset_management: bool, + pub allow_reset_team: bool, /// Whether the pool is transferable or not. pub transferable: bool, } /// Details of a pool. #[derive(Clone, Encode, Decode, PartialEq, Eq, TypeInfo, MaxEncodedLen, Debug)] -pub struct PoolDetails -{ +pub struct PoolDetails { /// The owner of the pool. pub owner: AccountId, /// The manager of the pool. If a manager is set, the pool is permissioned. @@ -107,13 +106,13 @@ pub struct PoolDetails, - pub currency_settings: BondedCurrencySettings, + pub currencies_settings: SharedSettings, /// The deposit to be returned upon destruction of this pool. pub deposit: DepositBalance, } impl - PoolDetails + PoolDetails where AccountId: PartialEq + Clone, { @@ -125,7 +124,7 @@ where collateral: BaseCurrencyId, bonded_currencies: Currencies, transferable: bool, - enable_asset_management: bool, + allow_reset_team: bool, denomination: u8, min_operation_balance: u128, deposit: DepositBalance, @@ -136,9 +135,9 @@ where curve, collateral, bonded_currencies, - currency_settings: CurrencySettings { + currencies_settings: BondedCurrenciesSettings { transferable, - enable_asset_management, + allow_reset_team, denomination, min_operation_balance, }, From 8ee5b00f3729a70d8d044a5dc529f8c2051278b0 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 13 Mar 2025 18:16:02 +0100 Subject: [PATCH 6/9] docs: update docstrings --- pallets/pallet-bonded-coins/src/lib.rs | 19 +++++++++++++------ pallets/pallet-bonded-coins/src/types.rs | 10 +++++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/pallets/pallet-bonded-coins/src/lib.rs b/pallets/pallet-bonded-coins/src/lib.rs index b1280f1e6..a6bf0d960 100644 --- a/pallets/pallet-bonded-coins/src/lib.rs +++ b/pallets/pallet-bonded-coins/src/lib.rs @@ -190,7 +190,7 @@ pub mod pallet { #[pallet::constant] type BaseDeposit: Get>; - /// The origin for most permissionless and priviledged operations. + /// The origin for most permissionless and privileged operations. type DefaultOrigin: EnsureOrigin; /// The dedicated origin for creating new bonded currency pools /// (typically permissionless). @@ -340,9 +340,15 @@ pub mod pallet { /// - `currencies`: A bounded vector of token metadata for the bonded /// currencies. Note that no two currencies may use the same name or /// symbol. - /// - `denomination`: The denomination for the bonded currencies. - /// - `transferable`: A boolean indicating if the bonded currencies are - /// transferable. + /// - `currencies_settings`: Options and settings shared by all bonded + /// currencies. These cannot be changed after the pool is created. + /// - `denomination`: The denomination for the bonded currencies. + /// - `transferable`: A boolean indicating if the bonded currencies + /// are transferable. + /// - `allow_reset_team`: Whether asset management team changes are + /// allowed for this pool. + /// - `min_operation_balance`: The minimum amount that can be + /// minted/burnt. /// /// # Returns /// - `DispatchResult`: The result of the dispatch. @@ -485,8 +491,9 @@ pub mod pallet { /// /// # Errors /// - `Error::::PoolUnknown`: If the pool does not exist. - /// - `Error::::NoPermission`: If the caller is not a manager of the - /// pool. + /// - `Error::::NoPermission`: If this pool does not allow changing + /// the asset management team, or if the caller is not a manager of + /// the pool. /// - `Error::::CurrencyCount`: If the actual number of currencies in /// the pool is larger than `currency_count`. /// - Other errors depending on the types in the config. diff --git a/pallets/pallet-bonded-coins/src/types.rs b/pallets/pallet-bonded-coins/src/types.rs index 0b7a665ee..31a3b6a70 100644 --- a/pallets/pallet-bonded-coins/src/types.rs +++ b/pallets/pallet-bonded-coins/src/types.rs @@ -82,11 +82,11 @@ impl PoolStatus { pub struct BondedCurrenciesSettings { /// The minimum amount that can be minted/burnt. pub min_operation_balance: u128, - /// The denomination of the pool. + /// The denomination of all bonded assets the pool. pub denomination: u8, - /// Whether asset management changes are allowed. + /// Whether asset management team changes are allowed. pub allow_reset_team: bool, - /// Whether the pool is transferable or not. + /// Whether assets are transferable or not. pub transferable: bool, } @@ -95,7 +95,7 @@ pub struct BondedCurrenciesSettings { pub struct PoolDetails { /// The owner of the pool. pub owner: AccountId, - /// The manager of the pool. If a manager is set, the pool is permissioned. + /// The manager of the pool, who can execute privileged transactions. pub manager: Option, /// The curve of the pool. pub curve: ParametrizedCurve, @@ -105,7 +105,7 @@ pub struct PoolDetails, - + /// Shared settings of the currencies in the pool. pub currencies_settings: SharedSettings, /// The deposit to be returned upon destruction of this pool. pub deposit: DepositBalance, From 5abf41e13057fe5a84d2e1465a79055ce34125f8 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 13 Mar 2025 18:30:53 +0100 Subject: [PATCH 7/9] fix: runtime api --- pallets/pallet-bonded-coins/src/lib.rs | 2 +- runtime-api/bonded-coins/src/pool_details.rs | 12 +++++++++--- runtimes/peregrine/src/runtime_apis.rs | 14 +++++--------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/pallets/pallet-bonded-coins/src/lib.rs b/pallets/pallet-bonded-coins/src/lib.rs index a6bf0d960..a044fc119 100644 --- a/pallets/pallet-bonded-coins/src/lib.rs +++ b/pallets/pallet-bonded-coins/src/lib.rs @@ -36,7 +36,7 @@ mod types; #[cfg(feature = "runtime-benchmarks")] pub use benchmarking::BenchmarkHelper; -pub use types::{PoolDetails, Round}; +pub use types::{BondedCurrenciesSettings, PoolDetails, Round}; pub use default_weights::WeightInfo; diff --git a/runtime-api/bonded-coins/src/pool_details.rs b/runtime-api/bonded-coins/src/pool_details.rs index 071755d8d..214e1263c 100644 --- a/runtime-api/bonded-coins/src/pool_details.rs +++ b/runtime-api/bonded-coins/src/pool_details.rs @@ -16,7 +16,7 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org -use pallet_bonded_coins::{curves::Curve, PoolDetails}; +use pallet_bonded_coins::{curves::Curve, BondedCurrenciesSettings, PoolDetails}; use parity_scale_codec::{alloc::string::String, Decode, Encode}; use scale_info::TypeInfo; use sp_std::vec::Vec; @@ -28,8 +28,14 @@ pub type CurveOf = Curve; pub type CurrenciesOf = Vec>; /// Human readable pool details. -pub type PoolDetailsOf = - PoolDetails, CollateralDetails, Balance>; +pub type PoolDetailsOf = PoolDetails< + AccountId, + CurveOf, + CurrenciesOf, + CollateralDetails, + Balance, + BondedCurrenciesSettings, +>; /// Collateral currency details used for the runtime API. #[derive(Decode, Encode, TypeInfo)] diff --git a/runtimes/peregrine/src/runtime_apis.rs b/runtimes/peregrine/src/runtime_apis.rs index 71540c512..d471f9dab 100644 --- a/runtimes/peregrine/src/runtime_apis.rs +++ b/runtimes/peregrine/src/runtime_apis.rs @@ -22,8 +22,7 @@ use pallet_bonded_coins::{ curves::{ balance_to_fixed, fixed_to_balance, lmsr::LMSRParameters, polynomial::PolynomialParameters, square_root::SquareRootParameters, BondingFunction, Curve, - }, - PoolDetailsOf, Pools, Round, + }, PoolDetailsOf, Pools, Round, }; use pallet_bonded_coins_runtime_api::{ BondedCurrencyDetails, Coefficient, CollateralDetails, PoolDetailsOf as PoolDetails, @@ -521,7 +520,8 @@ impl_runtime_apis! { high: Balance, ) -> Result { let pool = Pools::::get(pool_id).ok_or(BondedCurrencyError::PoolNotFound)?; - let PoolDetailsOf:: { curve, bonded_currencies, denomination, collateral, .. } = pool; + let PoolDetailsOf:: { curve, bonded_currencies, currencies_settings, collateral, .. } = pool; + let denomination = currencies_settings.denomination; let currency_id = bonded_currencies.get(currency_idx.saturated_into::()).ok_or(BondedCurrencyError::CurrencyNotFound)?; let collateral_denomination = NativeAndForeignAssets::decimals(collateral); @@ -578,15 +578,13 @@ impl_runtime_apis! { let pool = Pools::::get(pool_id).ok_or(BondedCurrencyError::PoolNotFound)?; let PoolDetailsOf:: { curve, + currencies_settings, bonded_currencies, owner, collateral, - denomination, deposit, - min_operation_balance, manager, state, - transferable, } = pool; let currencies = bonded_currencies.iter().map(|currency_id| -> Result, BondedCurrencyError> { @@ -629,12 +627,10 @@ impl_runtime_apis! { curve: fmt_curve, collateral: collateral_details, owner, - denomination, deposit, - min_operation_balance, manager, state, - transferable, + currencies_settings }) } From d3a3013b53e1be98020d65e9e0f982bdf4a08554 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 17 Mar 2025 11:47:15 +0100 Subject: [PATCH 8/9] fix: tests and formatting --- runtimes/common/src/constants.rs | 2 +- runtimes/peregrine/src/runtime_apis.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/runtimes/common/src/constants.rs b/runtimes/common/src/constants.rs index b316ab3d7..1364e999d 100644 --- a/runtimes/common/src/constants.rs +++ b/runtimes/common/src/constants.rs @@ -162,7 +162,7 @@ pub mod bonded_coins { use super::*; /// The size is checked in the runtime by a test. - pub const MAX_POOL_BYTE_LENGTH: u32 = 986; + pub const MAX_POOL_BYTE_LENGTH: u32 = 987; pub const BASE_DEPOSIT: Balance = deposit(1, MAX_POOL_BYTE_LENGTH); const ASSET_ID_BYTE_LENGTH: u32 = 8; /// https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/assets/src/types.rs#L188 diff --git a/runtimes/peregrine/src/runtime_apis.rs b/runtimes/peregrine/src/runtime_apis.rs index d471f9dab..9818f3da5 100644 --- a/runtimes/peregrine/src/runtime_apis.rs +++ b/runtimes/peregrine/src/runtime_apis.rs @@ -22,7 +22,8 @@ use pallet_bonded_coins::{ curves::{ balance_to_fixed, fixed_to_balance, lmsr::LMSRParameters, polynomial::PolynomialParameters, square_root::SquareRootParameters, BondingFunction, Curve, - }, PoolDetailsOf, Pools, Round, + }, + PoolDetailsOf, Pools, Round, }; use pallet_bonded_coins_runtime_api::{ BondedCurrencyDetails, Coefficient, CollateralDetails, PoolDetailsOf as PoolDetails, From 629dcf274fb44d15c67a51a1a3d2392b36a957d0 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 17 Mar 2025 11:59:48 +0100 Subject: [PATCH 9/9] chore: bump storage version --- pallets/pallet-bonded-coins/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pallets/pallet-bonded-coins/src/lib.rs b/pallets/pallet-bonded-coins/src/lib.rs index a044fc119..c0946d781 100644 --- a/pallets/pallet-bonded-coins/src/lib.rs +++ b/pallets/pallet-bonded-coins/src/lib.rs @@ -97,6 +97,9 @@ pub mod pallet { WeightInfo, }; + /// The current storage version. + const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + pub(crate) type AccountIdLookupOf = <::Lookup as sp_runtime::traits::StaticLookup>::Source; @@ -228,6 +231,7 @@ pub mod pallet { } #[pallet::pallet] + #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(_); #[pallet::hooks]