Add ClassAccount storage to unique pallet (#9940)

* add ClassAccount to uniques storage

* add tests for Class and ClassAccount storage

* fix format

* fix description

* add migration

* remove extra iteration

* Update frame/uniques/src/migration.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_uniques --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/uniques/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* fix format

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Parity Bot <admin@parity.io>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
hamidra
2021-12-18 07:41:05 -08:00
committed by GitHub
parent 8fa5c7608a
commit 94056a4da7
7 changed files with 179 additions and 68 deletions
+27 -2
View File
@@ -40,6 +40,8 @@ mod impl_nonfungibles;
mod types;
pub use types::*;
mod migration;
use codec::{Decode, Encode, HasCompact};
use frame_support::traits::{BalanceStatus::Reserved, Currency, ReservableCurrency};
use frame_system::Config as SystemConfig;
@@ -141,6 +143,19 @@ pub mod pallet {
OptionQuery,
>;
#[pallet::storage]
/// The classes owned by any given account; set out this way so that classes owned by a single
/// account can be enumerated.
pub(super) type ClassAccount<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId,
Blake2_128Concat,
T::ClassId,
(),
OptionQuery,
>;
#[pallet::storage]
/// The assets in existence and their ownership details.
pub(super) type Asset<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
@@ -302,7 +317,11 @@ pub mod pallet {
}
#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {}
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
migration::migrate_to_v1::<T, I, Self>()
}
}
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Get the owner of the asset instance, if the asset exists.
@@ -731,6 +750,8 @@ pub mod pallet {
details.total_deposit,
Reserved,
)?;
ClassAccount::<T, I>::remove(&details.owner, &class);
ClassAccount::<T, I>::insert(&owner, &class, ());
details.owner = owner.clone();
Self::deposit_event(Event::OwnerChanged { class, new_owner: owner });
@@ -906,13 +927,17 @@ pub mod pallet {
Class::<T, I>::try_mutate(class, |maybe_asset| {
let mut asset = maybe_asset.take().ok_or(Error::<T, I>::Unknown)?;
asset.owner = T::Lookup::lookup(owner)?;
let old_owner = asset.owner;
let new_owner = T::Lookup::lookup(owner)?;
asset.owner = new_owner.clone();
asset.issuer = T::Lookup::lookup(issuer)?;
asset.admin = T::Lookup::lookup(admin)?;
asset.freezer = T::Lookup::lookup(freezer)?;
asset.free_holding = free_holding;
asset.is_frozen = is_frozen;
*maybe_asset = Some(asset);
ClassAccount::<T, I>::remove(&old_owner, &class);
ClassAccount::<T, I>::insert(&new_owner, &class, ());
Self::deposit_event(Event::AssetStatusChanged { class });
Ok(())