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
37 changes: 25 additions & 12 deletions pallets/pallet-bonded-coins/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::{
square_root::{SquareRootParameters, SquareRootParametersInput},
Curve, CurveInput,
},
types::BondedCurrenciesSettings,
Call, CollateralAssetIdOf, CollateralBalanceOf, Config, CurveParameterTypeOf, FungiblesAssetIdOf,
FungiblesBalanceOf, Pallet,
};
Expand Down Expand Up @@ -229,10 +230,13 @@ mod benchmarks {
owner,
state,
collateral,
denomination,
bonded_currencies: BoundedVec::truncate_from(bonded_coin_ids.clone()),
transferable: true,
min_operation_balance: 1u128.saturated_into(),
currencies_settings: BondedCurrenciesSettings {
denomination,
transferable: true,
allow_reset_team: true,
min_operation_balance: 1u128.saturated_into(),
},
deposit: Pallet::<T>::calculate_pool_deposit(bonded_coin_ids.len()),
};
Pools::<T>::insert(&pool_id, pool_details);
Expand Down Expand Up @@ -272,9 +276,12 @@ mod benchmarks {
curve,
collateral_id,
currencies,
10,
true,
1,
BondedCurrenciesSettings {
denomination: 10,
allow_reset_team: true,
transferable: true,
min_operation_balance: 1,
},
);

// Verify
Expand Down Expand Up @@ -310,9 +317,12 @@ mod benchmarks {
curve,
collateral_id,
currencies,
10,
true,
1,
BondedCurrenciesSettings {
denomination: 10,
allow_reset_team: true,
transferable: true,
min_operation_balance: 1,
},
);

// Verify
Expand Down Expand Up @@ -347,9 +357,12 @@ mod benchmarks {
curve,
collateral_id,
currencies,
10,
true,
1,
BondedCurrenciesSettings {
denomination: 10,
allow_reset_team: true,
transferable: true,
min_operation_balance: 1,
},
);

// Verify
Expand Down
82 changes: 58 additions & 24 deletions pallets/pallet-bonded-coins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -93,10 +93,13 @@ 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::{BondedCurrenciesSettings, Locks, PoolDetails, PoolManagingTeam, PoolStatus, Round, TokenMeta},
WeightInfo,
};

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
Comment on lines +100 to +101
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ntn-x2 I wasn't sure about this one, but we need to bump the storage version if we change the pool struct right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, correct.


pub(crate) type AccountIdLookupOf<T> =
<<T as frame_system::Config>::Lookup as sp_runtime::traits::StaticLookup>::Source;

Expand Down Expand Up @@ -131,6 +134,7 @@ pub mod pallet {
BoundedCurrencyVec<T>,
CollateralAssetIdOf<T>,
DepositBalanceOf<T>,
BondedCurrenciesSettings,
>;

/// Minimum required amount of integer and fractional bits to perform ln,
Expand Down Expand Up @@ -189,7 +193,7 @@ pub mod pallet {
#[pallet::constant]
type BaseDeposit: Get<DepositBalanceOf<Self>>;

/// The origin for most permissionless and priviledged operations.
/// The origin for most permissionless and privileged operations.
type DefaultOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Self::AccountId>;
/// The dedicated origin for creating new bonded currency pools
/// (typically permissionless).
Expand Down Expand Up @@ -227,6 +231,7 @@ pub mod pallet {
}

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

#[pallet::hooks]
Expand Down Expand Up @@ -339,9 +344,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.
Expand All @@ -367,12 +378,17 @@ pub mod pallet {
curve: CurveInput<CurveParameterInputOf<T>>,
collateral_id: CollateralAssetIdOf<T>,
currencies: BoundedVec<TokenMetaOf<T>, T::MaxCurrenciesPerPool>,
denomination: u8,
transferable: bool,
min_operation_balance: u128,
currencies_settings: BondedCurrenciesSettings,
) -> DispatchResult {
let who = T::PoolCreateOrigin::ensure_origin(origin)?;

let BondedCurrenciesSettings {
denomination,
transferable,
allow_reset_team,
min_operation_balance,
} = currencies_settings;

ensure!(denomination <= T::MaxDenomination::get(), Error::<T>::InvalidInput);
let checked_curve = curve.try_into().map_err(|_| Error::<T>::InvalidInput)?;

Expand Down Expand Up @@ -450,6 +466,7 @@ pub mod pallet {
collateral_id,
currency_ids,
transferable,
allow_reset_team,
denomination,
min_operation_balance,
deposit_amount,
Expand Down Expand Up @@ -478,8 +495,9 @@ pub mod pallet {
///
/// # Errors
/// - `Error::<T>::PoolUnknown`: If the pool does not exist.
/// - `Error::<T>::NoPermission`: If the caller is not a manager of the
/// pool.
/// - `Error::<T>::NoPermission`: If this pool does not allow changing
/// the asset management team, or if the caller is not a manager of
/// the pool.
/// - `Error::<T>::CurrencyCount`: If the actual number of currencies in
/// the pool is larger than `currency_count`.
/// - Other errors depending on the types in the config.
Expand All @@ -498,7 +516,12 @@ pub mod pallet {
let number_of_currencies = Self::get_currencies_number(&pool_details);
ensure!(number_of_currencies <= currency_count, Error::<T>::CurrencyCount);

ensure!(pool_details.is_manager(&who), Error::<T>::NoPermission);
let BondedCurrenciesSettings { allow_reset_team, .. } = pool_details.currencies_settings;

ensure!(
allow_reset_team && pool_details.is_manager(&who),
Error::<T>::NoPermission
);
ensure!(pool_details.state.is_live(), Error::<T>::PoolNotLive);

let pool_id_account = pool_id.into();
Expand Down Expand Up @@ -703,8 +726,15 @@ pub mod pallet {
let number_of_currencies = Self::get_currencies_number(&pool_details);
ensure!(number_of_currencies <= currency_count, Error::<T>::CurrencyCount);

let BondedCurrenciesSettings {
min_operation_balance,
denomination,
transferable,
..
} = pool_details.currencies_settings;

ensure!(
amount_to_mint >= pool_details.min_operation_balance.saturated_into(),
amount_to_mint >= min_operation_balance.saturated_into(),
TokenError::BelowMinimum
);

Expand All @@ -724,16 +754,13 @@ pub mod pallet {

let (active_pre, passive) = Self::calculate_normalized_passive_issuance(
&bonded_currencies,
pool_details.denomination,
denomination,
currency_idx,
round_kind,
)?;

let normalized_amount_to_mint = balance_to_fixed(
amount_to_mint.saturated_into::<u128>(),
pool_details.denomination,
round_kind,
)?;
let normalized_amount_to_mint =
balance_to_fixed(amount_to_mint.saturated_into::<u128>(), denomination, round_kind)?;

let active_post = active_pre
.checked_add(normalized_amount_to_mint)
Expand Down Expand Up @@ -764,7 +791,7 @@ pub mod pallet {

T::Fungibles::mint_into(target_currency_id.clone(), &beneficiary, amount_to_mint)?;

if !pool_details.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()
Expand Down Expand Up @@ -832,8 +859,15 @@ pub mod pallet {

let pool_details = Pools::<T>::get(&pool_id).ok_or(Error::<T>::PoolUnknown)?;

let BondedCurrenciesSettings {
min_operation_balance,
denomination,
transferable,
..
} = pool_details.currencies_settings;

ensure!(
amount_to_burn >= pool_details.min_operation_balance.saturated_into(),
amount_to_burn >= min_operation_balance.saturated_into(),
TokenError::BelowMinimum
);

Expand All @@ -854,12 +888,12 @@ pub mod pallet {

let (high, passive) = Self::calculate_normalized_passive_issuance(
&bonded_currencies,
pool_details.denomination,
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, denomination, round_kind)?;

let low = high
.checked_sub(normalized_amount_to_burn)
Expand Down Expand Up @@ -905,7 +939,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 !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);
Expand Down
13 changes: 8 additions & 5 deletions pallets/pallet-bonded-coins/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub mod runtime {
use crate::{
self as pallet_bonded_coins,
traits::NextAssetIds,
types::{Locks, PoolStatus},
types::{BondedCurrenciesSettings, Locks, PoolStatus},
AccountIdOf, Config, DepositBalanceOf, FungiblesAssetIdOf, FungiblesBalanceOf, PoolDetailsOf,
};

Expand Down Expand Up @@ -118,13 +118,16 @@ pub mod runtime {
PoolDetailsOf::<Test> {
curve,
manager,
transferable,
bonded_currencies,
state,
collateral,
denomination: DEFAULT_BONDED_DENOMINATION,
currencies_settings: BondedCurrenciesSettings {
allow_reset_team: true,
transferable,
min_operation_balance,
denomination: DEFAULT_BONDED_DENOMINATION,
},
owner,
min_operation_balance,
deposit: BondingPallet::calculate_pool_deposit(currencies.len()),
}
}
Expand Down Expand Up @@ -446,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.currencies_settings.denomination))
.collect::<Vec<(u32, Vec<u8>, Vec<u8>, u8)>>()
})
.chain(
Expand Down
11 changes: 7 additions & 4 deletions pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use sp_runtime::{assert_eq_error_rate, bounded_vec, traits::Scale, TokenError};

use crate::{
mock::{runtime::*, *},
types::{Locks, PoolStatus},
types::{BondedCurrenciesSettings, Locks, PoolStatus},
AccountIdOf, Error, PoolDetailsOf,
};

Expand Down Expand Up @@ -55,13 +55,16 @@ fn burn_first_coin() {
PoolDetailsOf::<Test> {
curve: get_linear_bonding_curve(),
manager: None,
transferable: true,
bonded_currencies: bounded_vec![DEFAULT_BONDED_CURRENCY_ID],
state: PoolStatus::Active,
collateral: DEFAULT_COLLATERAL_CURRENCY_ID,
denomination: 0,
currencies_settings: BondedCurrenciesSettings {
transferable: true,
allow_reset_team: true,
denomination: 0,
min_operation_balance: 1,
},
owner: ACCOUNT_99,
min_operation_balance: 1,
deposit: BondingPallet::calculate_pool_deposit(1),
},
)])
Expand Down
Loading
Loading