diff --git a/frame/assets/src/functions.rs b/frame/assets/src/functions.rs index c25cc8eb6686e..f87af72965831 100644 --- a/frame/assets/src/functions.rs +++ b/frame/assets/src/functions.rs @@ -157,7 +157,7 @@ impl, I: 'static> Pallet { if details.supply.checked_sub(&amount).is_none() { return Underflow } - if details.is_frozen { + if details.status == AssetStatus::Frozen { return Frozen } if amount.is_zero() { @@ -205,8 +205,8 @@ impl, I: 'static> Pallet { keep_alive: bool, ) -> Result { let details = Asset::::get(id).ok_or(Error::::Unknown)?; - ensure!(details.status == AssetStatus::Live, Error::::AssetNotLive); - ensure!(!details.is_frozen, Error::::Frozen); + ensure!(details.status != AssetStatus::Destroying, Error::::AssetNotLive); + ensure!(details.status != AssetStatus::Frozen, Error::::Frozen); let account = Account::::get(id, who).ok_or(Error::::NoAccount)?; ensure!(!account.is_frozen, Error::::Frozen); @@ -325,7 +325,7 @@ impl, I: 'static> Pallet { let mut details = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(account.balance.is_zero() || allow_burn, Error::::WouldBurn); - ensure!(!details.is_frozen, Error::::Frozen); + ensure!(details.status != AssetStatus::Frozen, Error::::Frozen); ensure!(!account.is_frozen, Error::::Frozen); T::Currency::unreserve(&who, deposit); @@ -392,7 +392,7 @@ impl, I: 'static> Pallet { Self::can_increase(id, beneficiary, amount, true).into_result()?; Asset::::try_mutate(id, |maybe_details| -> DispatchResult { let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; - ensure!(details.status == AssetStatus::Live, Error::::AssetNotLive); + ensure!(details.status != AssetStatus::Destroying, Error::::AssetNotLive); check(details)?; Account::::try_mutate(id, beneficiary, |maybe_account| -> DispatchResult { @@ -433,7 +433,7 @@ impl, I: 'static> Pallet { f: DebitFlags, ) -> Result { let details = Asset::::get(id).ok_or(Error::::Unknown)?; - ensure!(details.status == AssetStatus::Live, Error::::AssetNotLive); + ensure!(details.status != AssetStatus::Destroying, Error::::AssetNotLive); let actual = Self::decrease_balance(id, target, amount, f, |actual, details| { // Check admin rights. @@ -660,7 +660,6 @@ impl, I: 'static> Pallet { accounts: 0, sufficients: 0, approvals: 0, - is_frozen: false, status: AssetStatus::Live, }, ); @@ -679,7 +678,6 @@ impl, I: 'static> Pallet { if let Some(check_owner) = maybe_check_owner { ensure!(details.owner == check_owner, Error::::NoPermission); } - ensure!(details.is_frozen, Error::::NotFrozen); details.status = AssetStatus::Destroying; Self::deposit_event(Event::DestructionStarted { asset_id: id }); @@ -700,8 +698,7 @@ impl, I: 'static> Pallet { let _ = Asset::::try_mutate_exists(id, |maybe_details| -> Result<(), DispatchError> { let mut details = maybe_details.as_mut().ok_or(Error::::Unknown)?; - ensure!(details.is_frozen, Error::::NotFrozen); - // Should only destroy accounts while the asset is being destroyed + // Should only destroy accounts while the asset is in a destroying state ensure!(details.status == AssetStatus::Destroying, Error::::LiveAsset); for (who, v) in Account::::drain_prefix(id) { @@ -740,8 +737,7 @@ impl, I: 'static> Pallet { Asset::::try_mutate_exists(id, |maybe_details| -> Result<(), DispatchError> { let mut details = maybe_details.as_mut().ok_or(Error::::Unknown)?; - ensure!(details.is_frozen, Error::::NotFrozen); - // Should only destroy accounts while the asset is being destroyed + // Should only destroy accounts while the asset is in a destroying state. ensure!(details.status == AssetStatus::Destroying, Error::::LiveAsset); for ((owner, _), approval) in Approvals::::drain_prefix((id,)) { @@ -768,7 +764,6 @@ impl, I: 'static> Pallet { pub(super) fn do_finish_destroy(id: T::AssetId) -> DispatchResult { Asset::::try_mutate_exists(id, |maybe_details| -> Result<(), DispatchError> { let details = maybe_details.take().ok_or(Error::::Unknown)?; - ensure!(details.is_frozen, Error::::NotFrozen); ensure!(details.status == AssetStatus::Destroying, Error::::LiveAsset); ensure!(details.accounts == 0, Error::::InUse); ensure!(details.approvals == 0, Error::::InUse); @@ -795,8 +790,8 @@ impl, I: 'static> Pallet { amount: T::Balance, ) -> DispatchResult { let mut d = Asset::::get(id).ok_or(Error::::Unknown)?; - ensure!(d.status == AssetStatus::Live, Error::::AssetNotLive); - ensure!(!d.is_frozen, Error::::Frozen); + ensure!(d.status != AssetStatus::Destroying, Error::::AssetNotLive); + ensure!(d.status != AssetStatus::Frozen, Error::::Frozen); Approvals::::try_mutate( (id, &owner, &delegate), |maybe_approved| -> DispatchResult { diff --git a/frame/assets/src/lib.rs b/frame/assets/src/lib.rs index a65daee86b73d..e2690373bd4e2 100644 --- a/frame/assets/src/lib.rs +++ b/frame/assets/src/lib.rs @@ -346,7 +346,6 @@ pub mod pallet { accounts: 0, sufficients: 0, approvals: 0, - is_frozen: false, status: AssetStatus::Live, }, ); @@ -562,7 +561,6 @@ pub mod pallet { accounts: 0, sufficients: 0, approvals: 0, - is_frozen: false, status: AssetStatus::Live, }, ); @@ -925,7 +923,7 @@ pub mod pallet { let d = maybe_details.as_mut().ok_or(Error::::Unknown)?; ensure!(origin == d.freezer, Error::::NoPermission); - d.is_frozen = true; + d.status = AssetStatus::Frozen; Self::deposit_event(Event::::AssetFrozen { asset_id: id }); Ok(()) @@ -951,8 +949,9 @@ pub mod pallet { Asset::::try_mutate(id, |maybe_details| { let d = maybe_details.as_mut().ok_or(Error::::Unknown)?; ensure!(origin == d.admin, Error::::NoPermission); + ensure!(d.status != AssetStatus::Destroying, Error::::AssetNotLive); - d.is_frozen = false; + d.status = AssetStatus::Live; Self::deposit_event(Event::::AssetThawed { asset_id: id }); Ok(()) @@ -1211,14 +1210,18 @@ pub mod pallet { Asset::::try_mutate(id, |maybe_asset| { let mut asset = maybe_asset.take().ok_or(Error::::Unknown)?; - ensure!(asset.status == AssetStatus::Live, Error::::AssetNotLive); + ensure!(asset.status != AssetStatus::Destroying, Error::::AssetNotLive); asset.owner = T::Lookup::lookup(owner)?; asset.issuer = T::Lookup::lookup(issuer)?; asset.admin = T::Lookup::lookup(admin)?; asset.freezer = T::Lookup::lookup(freezer)?; asset.min_balance = min_balance; asset.is_sufficient = is_sufficient; - asset.is_frozen = is_frozen; + if is_frozen { + asset.status = AssetStatus::Frozen; + } else { + asset.status = AssetStatus::Live; + } *maybe_asset = Some(asset); Self::deposit_event(Event::AssetStatusChanged { asset_id: id }); diff --git a/frame/assets/src/migration.rs b/frame/assets/src/migration.rs index 77fbb6c176f2e..2f71594176d77 100644 --- a/frame/assets/src/migration.rs +++ b/frame/assets/src/migration.rs @@ -41,6 +41,8 @@ pub mod v1 { impl OldAssetDetails { fn migrate_to_v1(self) -> AssetDetails { + let status = if self.is_frozen { AssetStatus::Frozen } else { AssetStatus::Live }; + AssetDetails { owner: self.owner, issuer: self.issuer, @@ -53,8 +55,7 @@ pub mod v1 { accounts: self.accounts, sufficients: self.sufficients, approvals: self.approvals, - is_frozen: self.is_frozen, - status: AssetStatus::Live, + status, } } } diff --git a/frame/assets/src/types.rs b/frame/assets/src/types.rs index f42d4640f9a1e..557af6bd3f488 100644 --- a/frame/assets/src/types.rs +++ b/frame/assets/src/types.rs @@ -35,6 +35,8 @@ pub(super) type AssetAccountOf = pub(super) enum AssetStatus { /// The asset is active and able to be used. Live, + /// Whether the asset is frozen for non-admin transfers. + Frozen, /// The asset is currently being destroyed, and all actions are no longer permitted on the /// asset. Once set to `Destroying`, the asset can never transition back to a `Live` state. Destroying, @@ -65,8 +67,6 @@ pub struct AssetDetails { pub(super) sufficients: u32, /// The total number of approvals. pub(super) approvals: u32, - /// Whether the asset is frozen for non-admin transfers. - pub(super) is_frozen: bool, /// The status of the asset pub(super) status: AssetStatus, }