Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit b0af603

Browse files
author
aliX
authored
Fix minimum_balance of MultiCurrencyAccounting and Currency for GA (#197)
Fix minimum_balance of MultiCurrencyAccounting and Currency for GA to return the existential deposit of the relevant asset
1 parent b472fca commit b0af603

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

prml/generic-asset/src/impls.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ impl<T: Config> MultiCurrencyAccounting for Module<T> {
3333
type PositiveImbalance = PositiveImbalance<T>;
3434
type NegativeImbalance = NegativeImbalance<T>;
3535

36+
fn minimum_balance(currency: Option<T::AssetId>) -> Self::Balance {
37+
<Module<T>>::asset_meta(currency.unwrap_or_else(|| Self::DefaultCurrencyId::asset_id())).existential_deposit()
38+
}
39+
3640
fn total_balance(who: &T::AccountId, currency: Option<T::AssetId>) -> Self::Balance {
3741
<Module<T>>::total_balance(currency.unwrap_or_else(|| Self::DefaultCurrencyId::asset_id()), who)
3842
}
@@ -146,14 +150,30 @@ impl<T: Config> MultiCurrencyAccounting for Module<T> {
146150
#[cfg(test)]
147151
mod tests {
148152
use super::*;
149-
use crate::mock::{new_test_ext_with_balance, new_test_ext_with_default, GenericAsset, Test};
153+
use crate::mock::{
154+
new_test_ext_with_balance, new_test_ext_with_default, GenericAsset, Test, STAKING_ASSET_ID, TEST1_ASSET_ID,
155+
TEST2_ASSET_ID,
156+
};
150157
use frame_support::assert_noop;
151158
use sp_runtime::traits::Zero;
152159

153160
#[test]
154161
fn multi_accounting_minimum_balance() {
155162
new_test_ext_with_default().execute_with(|| {
156-
assert!(<GenericAsset as MultiCurrencyAccounting>::minimum_balance().is_zero());
163+
assert_eq!(
164+
<GenericAsset as MultiCurrencyAccounting>::minimum_balance(Some(TEST1_ASSET_ID)),
165+
3
166+
);
167+
assert_eq!(
168+
<GenericAsset as MultiCurrencyAccounting>::minimum_balance(Some(TEST2_ASSET_ID)),
169+
5
170+
);
171+
assert_eq!(
172+
<GenericAsset as MultiCurrencyAccounting>::minimum_balance(Some(STAKING_ASSET_ID)),
173+
1
174+
);
175+
// When currency is None, the minimum balance for the default currency will be returned.
176+
assert_eq!(<GenericAsset as MultiCurrencyAccounting>::minimum_balance(None), 1);
157177
});
158178
}
159179

prml/generic-asset/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ where
10831083
}
10841084

10851085
fn minimum_balance() -> Self::Balance {
1086-
Zero::zero()
1086+
AssetMeta::<T>::get(U::asset_id()).existential_deposit()
10871087
}
10881088

10891089
fn transfer(

prml/generic-asset/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,5 @@ pub(crate) fn new_test_ext_with_next_asset_id(next_asset_id: u32) -> sp_io::Test
171171
}
172172

173173
pub(crate) fn new_test_ext_with_permissions(permissions: Vec<(u32, u64)>) -> sp_io::TestExternalities {
174-
new_test_ext(vec![0], vec![], 0, permissions, ASSET_ID)
174+
new_test_ext(vec![0], vec![], 0, permissions, TEST2_ASSET_ID + 1)
175175
}

prml/generic-asset/src/tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,30 @@ fn balance_falls_below_a_non_default_existential_deposit() {
408408
});
409409
}
410410

411+
#[test]
412+
fn minimum_balance_is_existential_deposit() {
413+
new_test_ext_with_permissions(vec![(STAKING_ASSET_ID, ALICE), (SPENDING_ASSET_ID, ALICE)]).execute_with(|| {
414+
let stk_min = 11u64;
415+
let spd_min = 17u64;
416+
let staking_asset_info = AssetInfo::new(b"STK".to_vec(), 1, stk_min);
417+
let spending_asset_info = AssetInfo::new(b"SPD".to_vec(), 2, spd_min);
418+
assert_ok!(GenericAsset::create_asset(
419+
Some(STAKING_ASSET_ID),
420+
Some(ALICE),
421+
asset_options(PermissionLatest::new(ALICE)),
422+
staking_asset_info
423+
));
424+
assert_ok!(GenericAsset::create_asset(
425+
Some(SPENDING_ASSET_ID),
426+
Some(ALICE),
427+
asset_options(PermissionLatest::new(ALICE)),
428+
spending_asset_info
429+
));
430+
assert_eq!(StakingAssetCurrency::<Test>::minimum_balance(), stk_min);
431+
assert_eq!(SpendingAssetCurrency::<Test>::minimum_balance(), spd_min);
432+
});
433+
}
434+
411435
#[test]
412436
fn purge_happens_per_asset() {
413437
new_test_ext_with_balance(STAKING_ASSET_ID, ALICE, INITIAL_BALANCE).execute_with(|| {

prml/support/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// are used interchangeably as they make more sense in certain contexts.
2525
use frame_support::traits::{ExistenceRequirement, Imbalance, SignedImbalance, WithdrawReasons};
2626
use sp_runtime::{
27-
traits::{AtLeast32BitUnsigned, MaybeSerializeDeserialize, Saturating, Zero},
27+
traits::{AtLeast32BitUnsigned, MaybeSerializeDeserialize, Saturating},
2828
DispatchError, DispatchResult,
2929
};
3030
use sp_std::{fmt::Debug, result};
@@ -61,9 +61,7 @@ pub trait MultiCurrencyAccounting {
6161

6262
/// The minimum balance any single account may have. This is equivalent to the `Balances` module's
6363
/// `ExistentialDeposit`.
64-
fn minimum_balance() -> Self::Balance {
65-
Zero::zero()
66-
}
64+
fn minimum_balance(currency: Option<Self::CurrencyId>) -> Self::Balance;
6765

6866
/// The combined balance (free + reserved) of `who` for the given `currency`.
6967
fn total_balance(who: &Self::AccountId, currency: Option<Self::CurrencyId>) -> Self::Balance;

0 commit comments

Comments
 (0)