Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1540,10 +1540,15 @@ pub mod pallet {
ensure!(origin == details.owner, Error::<T, I>::NoPermission);

let old_min_balance = details.min_balance;
// Ensure that either the new min_balance is less than old min_balance or there aren't
// any accounts holding the asset.
let is_sufficient = details.is_sufficient;
// Ensure that either the new min_balance is less than old
// min_balance or there aren't any accounts holding the asset.
//
// If the asset is marked as sufficient we will only allow the owner
// to set the min_balance if the count of accounts holding the assets
// is zero.
ensure!(
min_balance < old_min_balance || details.accounts == 0,
(!is_sufficient && min_balance < old_min_balance) || details.accounts == 0,
Error::<T, I>::NoPermission
);

Expand Down
53 changes: 46 additions & 7 deletions frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,23 +1098,62 @@ fn set_min_balance_should_work() {
Balances::make_free_balance_be(&1, 10);
assert_ok!(Assets::create(RuntimeOrigin::signed(1), id, 1, 30));

assert_ok!(Assets::mint(RuntimeOrigin::signed(1), id, 1, 50));
// won't execute because there is an asset holder.
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), id, 1, 100));
// Won't execute because there is an asset holder.
assert_noop!(
Assets::set_min_balance(RuntimeOrigin::signed(1), id, 50),
Error::<Test>::NoPermission
);

// will execute because the new value of min_balance is less than the
// old value. 10 < 30
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), id, 1, 10));
// Force asset status to make this a sufficient asset.
assert_ok!(Assets::force_asset_status(
RuntimeOrigin::root(),
id,
1,
1,
1,
1,
30,
true,
false
));

assert_ok!(Assets::burn(RuntimeOrigin::signed(1), id, 1, 50));
// Won't execute because there is an account holding the asset and the asset is marked as
// sufficient.
assert_noop!(
Assets::set_min_balance(RuntimeOrigin::signed(2), id, 50),
Assets::set_min_balance(RuntimeOrigin::signed(1), id, 10),
Error::<Test>::NoPermission
);

assert_ok!(Assets::burn(RuntimeOrigin::signed(1), id, 1, 100));

// Works because there are no asset holders.
assert_ok!(Assets::set_min_balance(RuntimeOrigin::signed(1), id, 60));
assert_eq!(Asset::<Test>::get(id).unwrap().min_balance, 60);

// Make the asset not sufficient.
assert_ok!(Assets::force_asset_status(
RuntimeOrigin::root(),
id,
1,
1,
1,
1,
60,
false,
false
));

// We mint the asset again.
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), id, 1, 100));

// Will execute because the new value of min_balance is less than the
// old value. 10 < 30
assert_ok!(Assets::set_min_balance(RuntimeOrigin::signed(1), id, 10));
assert_eq!(Asset::<Test>::get(id).unwrap().min_balance, 10);

assert_ok!(Assets::burn(RuntimeOrigin::signed(1), id, 1, 100));

assert_ok!(Assets::set_min_balance(RuntimeOrigin::signed(1), id, 50));
assert_eq!(Asset::<Test>::get(id).unwrap().min_balance, 50);
});
Expand Down