Skip to content

Commit 7c3c127

Browse files
committed
refactor: bonded currencies settings struct
1 parent 39c1679 commit 7c3c127

File tree

8 files changed

+149
-101
lines changed

8 files changed

+149
-101
lines changed

pallets/pallet-bonded-coins/src/benchmarking.rs

+25-16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::{
3232
square_root::{SquareRootParameters, SquareRootParametersInput},
3333
Curve, CurveInput,
3434
},
35+
types::CurrencySettings,
3536
Call, CollateralAssetIdOf, CollateralBalanceOf, Config, CurveParameterTypeOf, FungiblesAssetIdOf,
3637
FungiblesBalanceOf, Pallet,
3738
};
@@ -229,11 +230,13 @@ mod benchmarks {
229230
owner,
230231
state,
231232
collateral,
232-
denomination,
233233
bonded_currencies: BoundedVec::truncate_from(bonded_coin_ids.clone()),
234-
transferable: true,
235-
enable_asset_management: true,
236-
min_operation_balance: 1u128.saturated_into(),
234+
currency_settings: CurrencySettings {
235+
denomination,
236+
transferable: true,
237+
enable_asset_management: true,
238+
min_operation_balance: 1u128.saturated_into(),
239+
},
237240
deposit: Pallet::<T>::calculate_pool_deposit(bonded_coin_ids.len()),
238241
};
239242
Pools::<T>::insert(&pool_id, pool_details);
@@ -273,10 +276,12 @@ mod benchmarks {
273276
curve,
274277
collateral_id,
275278
currencies,
276-
10,
277-
true,
278-
true,
279-
1,
279+
CurrencySettings {
280+
denomination: 10,
281+
enable_asset_management: true,
282+
transferable: true,
283+
min_operation_balance: 1,
284+
},
280285
);
281286

282287
// Verify
@@ -312,10 +317,12 @@ mod benchmarks {
312317
curve,
313318
collateral_id,
314319
currencies,
315-
10,
316-
true,
317-
true,
318-
1,
320+
CurrencySettings {
321+
denomination: 10,
322+
enable_asset_management: true,
323+
transferable: true,
324+
min_operation_balance: 1,
325+
},
319326
);
320327

321328
// Verify
@@ -350,10 +357,12 @@ mod benchmarks {
350357
curve,
351358
collateral_id,
352359
currencies,
353-
10,
354-
true,
355-
true,
356-
1,
360+
CurrencySettings {
361+
denomination: 10,
362+
enable_asset_management: true,
363+
transferable: true,
364+
min_operation_balance: 1,
365+
},
357366
);
358367

359368
// Verify

pallets/pallet-bonded-coins/src/lib.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub mod pallet {
9393
use crate::{
9494
curves::{balance_to_fixed, fixed_to_balance, BondingFunction, Curve, CurveInput},
9595
traits::{FreezeAccounts, NextAssetIds, ResetTeam},
96-
types::{Locks, PoolDetails, PoolManagingTeam, PoolStatus, Round, TokenMeta},
96+
types::{CurrencySettings, Locks, PoolDetails, PoolManagingTeam, PoolStatus, Round, TokenMeta},
9797
WeightInfo,
9898
};
9999

@@ -131,6 +131,7 @@ pub mod pallet {
131131
BoundedCurrencyVec<T>,
132132
CollateralAssetIdOf<T>,
133133
DepositBalanceOf<T>,
134+
CurrencySettings,
134135
>;
135136

136137
/// Minimum required amount of integer and fractional bits to perform ln,
@@ -367,13 +368,17 @@ pub mod pallet {
367368
curve: CurveInput<CurveParameterInputOf<T>>,
368369
collateral_id: CollateralAssetIdOf<T>,
369370
currencies: BoundedVec<TokenMetaOf<T>, T::MaxCurrenciesPerPool>,
370-
denomination: u8,
371-
transferable: bool,
372-
enable_asset_management: bool,
373-
min_operation_balance: u128,
371+
currency_settings: CurrencySettings,
374372
) -> DispatchResult {
375373
let who = T::PoolCreateOrigin::ensure_origin(origin)?;
376374

375+
let CurrencySettings {
376+
denomination,
377+
transferable,
378+
enable_asset_management,
379+
min_operation_balance,
380+
} = currency_settings;
381+
377382
ensure!(denomination <= T::MaxDenomination::get(), Error::<T>::InvalidInput);
378383
let checked_curve = curve.try_into().map_err(|_| Error::<T>::InvalidInput)?;
379384

@@ -501,7 +506,7 @@ pub mod pallet {
501506
ensure!(number_of_currencies <= currency_count, Error::<T>::CurrencyCount);
502507

503508
ensure!(
504-
pool_details.enable_asset_management && pool_details.is_manager(&who),
509+
pool_details.currency_settings.enable_asset_management && pool_details.is_manager(&who),
505510
Error::<T>::NoPermission
506511
);
507512
ensure!(pool_details.state.is_live(), Error::<T>::PoolNotLive);
@@ -709,7 +714,7 @@ pub mod pallet {
709714
ensure!(number_of_currencies <= currency_count, Error::<T>::CurrencyCount);
710715

711716
ensure!(
712-
amount_to_mint >= pool_details.min_operation_balance.saturated_into(),
717+
amount_to_mint >= pool_details.currency_settings.min_operation_balance.saturated_into(),
713718
TokenError::BelowMinimum
714719
);
715720

@@ -729,14 +734,14 @@ pub mod pallet {
729734

730735
let (active_pre, passive) = Self::calculate_normalized_passive_issuance(
731736
&bonded_currencies,
732-
pool_details.denomination,
737+
pool_details.currency_settings.denomination,
733738
currency_idx,
734739
round_kind,
735740
)?;
736741

737742
let normalized_amount_to_mint = balance_to_fixed(
738743
amount_to_mint.saturated_into::<u128>(),
739-
pool_details.denomination,
744+
pool_details.currency_settings.denomination,
740745
round_kind,
741746
)?;
742747

@@ -769,7 +774,7 @@ pub mod pallet {
769774

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

772-
if !pool_details.transferable {
777+
if !pool_details.currency_settings.transferable {
773778
T::Fungibles::freeze(target_currency_id, &beneficiary).map_err(|freeze_error| {
774779
log::info!(target: LOG_TARGET, "Failed to freeze account: {:?}", freeze_error);
775780
freeze_error.into()
@@ -838,7 +843,7 @@ pub mod pallet {
838843
let pool_details = Pools::<T>::get(&pool_id).ok_or(Error::<T>::PoolUnknown)?;
839844

840845
ensure!(
841-
amount_to_burn >= pool_details.min_operation_balance.saturated_into(),
846+
amount_to_burn >= pool_details.currency_settings.min_operation_balance.saturated_into(),
842847
TokenError::BelowMinimum
843848
);
844849

@@ -859,12 +864,13 @@ pub mod pallet {
859864

860865
let (high, passive) = Self::calculate_normalized_passive_issuance(
861866
&bonded_currencies,
862-
pool_details.denomination,
867+
pool_details.currency_settings.denomination,
863868
currency_idx,
864869
round_kind,
865870
)?;
866871

867-
let normalized_amount_to_burn = balance_to_fixed(amount_to_burn, pool_details.denomination, round_kind)?;
872+
let normalized_amount_to_burn =
873+
balance_to_fixed(amount_to_burn, pool_details.currency_settings.denomination, round_kind)?;
868874

869875
let low = high
870876
.checked_sub(normalized_amount_to_burn)
@@ -910,7 +916,7 @@ pub mod pallet {
910916

911917
let account_exists = T::Fungibles::total_balance(target_currency_id.clone(), &who) > Zero::zero();
912918

913-
if !pool_details.transferable && account_exists {
919+
if !pool_details.currency_settings.transferable && account_exists {
914920
// Restore locks.
915921
T::Fungibles::freeze(target_currency_id, &who).map_err(|freeze_error| {
916922
log::info!(target: LOG_TARGET, "Failed to freeze account: {:?}", freeze_error);

pallets/pallet-bonded-coins/src/mock.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub mod runtime {
6969
use crate::{
7070
self as pallet_bonded_coins,
7171
traits::NextAssetIds,
72-
types::{Locks, PoolStatus},
72+
types::{CurrencySettings, Locks, PoolStatus},
7373
AccountIdOf, Config, DepositBalanceOf, FungiblesAssetIdOf, FungiblesBalanceOf, PoolDetailsOf,
7474
};
7575

@@ -118,14 +118,16 @@ pub mod runtime {
118118
PoolDetailsOf::<Test> {
119119
curve,
120120
manager,
121-
transferable,
122-
enable_asset_management: true,
123121
bonded_currencies,
124122
state,
125123
collateral,
126-
denomination: DEFAULT_BONDED_DENOMINATION,
124+
currency_settings: CurrencySettings {
125+
enable_asset_management: true,
126+
transferable,
127+
min_operation_balance,
128+
denomination: DEFAULT_BONDED_DENOMINATION,
129+
},
127130
owner,
128-
min_operation_balance,
129131
deposit: BondingPallet::calculate_pool_deposit(currencies.len()),
130132
}
131133
}
@@ -447,7 +449,7 @@ pub mod runtime {
447449
pool_details
448450
.bonded_currencies
449451
.iter()
450-
.map(|id| (*id, vec![], vec![], pool_details.denomination))
452+
.map(|id| (*id, vec![], vec![], pool_details.currency_settings.denomination))
451453
.collect::<Vec<(u32, Vec<u8>, Vec<u8>, u8)>>()
452454
})
453455
.chain(

pallets/pallet-bonded-coins/src/tests/transactions/burn_into.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use sp_runtime::{assert_eq_error_rate, bounded_vec, traits::Scale, TokenError};
2727

2828
use crate::{
2929
mock::{runtime::*, *},
30-
types::{Locks, PoolStatus},
30+
types::{CurrencySettings, Locks, PoolStatus},
3131
AccountIdOf, Error, PoolDetailsOf,
3232
};
3333

@@ -55,14 +55,16 @@ fn burn_first_coin() {
5555
PoolDetailsOf::<Test> {
5656
curve: get_linear_bonding_curve(),
5757
manager: None,
58-
transferable: true,
59-
enable_asset_management: true,
6058
bonded_currencies: bounded_vec![DEFAULT_BONDED_CURRENCY_ID],
6159
state: PoolStatus::Active,
6260
collateral: DEFAULT_COLLATERAL_CURRENCY_ID,
63-
denomination: 0,
61+
currency_settings: CurrencySettings {
62+
transferable: true,
63+
enable_asset_management: true,
64+
denomination: 0,
65+
min_operation_balance: 1,
66+
},
6467
owner: ACCOUNT_99,
65-
min_operation_balance: 1,
6668
deposit: BondingPallet::calculate_pool_deposit(1),
6769
},
6870
)])

0 commit comments

Comments
 (0)