Skip to content

Commit

Permalink
Use common function for impl ManagedDestroy
Browse files Browse the repository at this point in the history
  • Loading branch information
sea212 committed Jan 31, 2024
1 parent 1e527aa commit bdbb76c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
3 changes: 1 addition & 2 deletions zrml/asset-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ pub mod pallet {

/// Keeps track of assets that have to be destroyed.
#[pallet::storage]
pub(super) type DestroyAssets<T: Config> =
StorageValue<_, BoundedVec<AssetInDestruction<T::AssetType>, ConstU32<8192>>, ValueQuery>;
pub(super) type DestroyAssets<T: Config> = StorageValue<_, DestroyAssetsT<T>, ValueQuery>;

/// Keeps track of assets that can't be destroyed.
#[pallet::storage]
Expand Down
41 changes: 16 additions & 25 deletions zrml/asset-router/src/pallet_impl/managed_destroy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

use crate::pallet::*;

impl<T: Config> ManagedDestroy<T::AccountId> for Pallet<T> {
fn managed_destroy(
asset: Self::AssetId,
impl<T: Config> Pallet<T> {
fn add_asset_to_managed_destruction(
destroy_assets: &mut DestroyAssetsT<T>,
asset: T::AssetType,
maybe_check_owner: Option<T::AccountId>,
) -> DispatchResult {
Self::asset_exists(asset).then_some(()).ok_or(Error::<T>::UnknownAsset)?;
let mut destroy_assets = DestroyAssets::<T>::get();
frame_support::ensure!(!destroy_assets.is_full(), Error::<T>::TooManyManagedDestroys);
let asset_to_insert = AssetInDestruction::new(asset);

Expand All @@ -43,8 +43,18 @@ impl<T: Config> ManagedDestroy<T::AccountId> for Pallet<T> {
.map_err(|_| Error::<T>::TooManyManagedDestroys)?;

Self::start_destroy(asset, maybe_check_owner)?;
DestroyAssets::<T>::put(destroy_assets);
Ok(())
}
}

impl<T: Config> ManagedDestroy<T::AccountId> for Pallet<T> {
fn managed_destroy(
asset: Self::AssetId,
maybe_check_owner: Option<T::AccountId>,
) -> DispatchResult {
let mut destroy_assets = DestroyAssets::<T>::get();
Self::add_asset_to_managed_destruction(&mut destroy_assets, asset, maybe_check_owner)?;
DestroyAssets::<T>::put(destroy_assets);
Ok(())
}

Expand All @@ -55,26 +65,7 @@ impl<T: Config> ManagedDestroy<T::AccountId> for Pallet<T> {
let mut destroy_assets = DestroyAssets::<T>::get();

for (asset, maybe_check_owner) in assets {
Self::asset_exists(asset).then_some(()).ok_or(Error::<T>::UnknownAsset)?;
frame_support::ensure!(!destroy_assets.is_full(), Error::<T>::TooManyManagedDestroys);
let asset_to_insert = AssetInDestruction::new(asset);

let idx = match destroy_assets.binary_search(&asset_to_insert) {
Ok(_) => return Err(Error::<T>::DestructionInProgress.into()),
Err(idx) => {
if IndestructibleAssets::<T>::get().binary_search(&asset).is_ok() {
return Err(Error::<T>::DestructionInProgress.into());
}

idx
}
};

destroy_assets
.try_insert(idx, asset_to_insert)
.map_err(|_| Error::<T>::TooManyManagedDestroys)?;

Self::start_destroy(asset, maybe_check_owner)?;
Self::add_asset_to_managed_destruction(&mut destroy_assets, asset, maybe_check_owner)?;
}

DestroyAssets::<T>::put(destroy_assets);
Expand Down
4 changes: 4 additions & 0 deletions zrml/asset-router/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
// You should have received a copy of the GNU General Public License
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

use crate::{BoundedVec, Config, ConstU32};
use core::cmp::Ordering;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

pub(crate) type DestroyAssetsT<T> =
BoundedVec<AssetInDestruction<<T as Config>::AssetType>, ConstU32<8192>>;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Decode, Encode, MaxEncodedLen, TypeInfo)]
pub(crate) enum DestructionState {
Accounts,
Expand Down

0 comments on commit bdbb76c

Please sign in to comment.