diff --git a/frame/assets/src/benchmarking.rs b/frame/assets/src/benchmarking.rs index 227d45623d688..0d80ec5923d26 100644 --- a/frame/assets/src/benchmarking.rs +++ b/frame/assets/src/benchmarking.rs @@ -24,7 +24,7 @@ use super::*; use sp_runtime::traits::Bounded; use frame_system::RawOrigin as SystemOrigin; use frame_benchmarking::{ - benchmarks, account, whitelisted_caller, whitelist_account, impl_benchmark_test_suite + benchmarks_instance_pallet, account, whitelisted_caller, whitelist_account, impl_benchmark_test_suite }; use frame_support::traits::Get; use frame_support::{traits::EnsureOrigin, dispatch::UnfilteredDispatchable}; @@ -33,13 +33,13 @@ use crate::Pallet as Assets; const SEED: u32 = 0; -fn create_default_asset(is_sufficient: bool) +fn create_default_asset, I: 'static>(is_sufficient: bool) -> (T::AccountId, ::Source) { let caller: T::AccountId = whitelisted_caller(); let caller_lookup = T::Lookup::unlookup(caller.clone()); let root = SystemOrigin::Root.into(); - assert!(Assets::::force_create( + assert!(Assets::::force_create( root, Default::default(), caller_lookup.clone(), @@ -49,14 +49,14 @@ fn create_default_asset(is_sufficient: bool) (caller, caller_lookup) } -fn create_default_minted_asset(is_sufficient: bool, amount: T::Balance) +fn create_default_minted_asset, I: 'static>(is_sufficient: bool, amount: T::Balance) -> (T::AccountId, ::Source) { - let (caller, caller_lookup) = create_default_asset::(is_sufficient); + let (caller, caller_lookup) = create_default_asset::(is_sufficient); if !is_sufficient { T::Currency::make_free_balance_be(&caller, T::Currency::minimum_balance()); } - assert!(Assets::::mint( + assert!(Assets::::mint( SystemOrigin::Signed(caller.clone()).into(), Default::default(), caller_lookup.clone(), @@ -65,42 +65,42 @@ fn create_default_minted_asset(is_sufficient: bool, amount: T::Balanc (caller, caller_lookup) } -fn swap_is_sufficient(s: &mut bool) { - Asset::::mutate(&T::AssetId::default(), |maybe_a| +fn swap_is_sufficient, I: 'static>(s: &mut bool) { + Asset::::mutate(&T::AssetId::default(), |maybe_a| if let Some(ref mut a) = maybe_a { sp_std::mem::swap(s, &mut a.is_sufficient) } ); } -fn add_consumers(minter: T::AccountId, n: u32) { +fn add_consumers, I: 'static>(minter: T::AccountId, n: u32) { let origin = SystemOrigin::Signed(minter); let mut s = false; - swap_is_sufficient::(&mut s); + swap_is_sufficient::(&mut s); for i in 0..n { let target = account("consumer", i, SEED); T::Currency::make_free_balance_be(&target, T::Currency::minimum_balance()); let target_lookup = T::Lookup::unlookup(target); - assert!(Assets::::mint(origin.clone().into(), Default::default(), target_lookup, 100u32.into()).is_ok()); + assert!(Assets::::mint(origin.clone().into(), Default::default(), target_lookup, 100u32.into()).is_ok()); } - swap_is_sufficient::(&mut s); + swap_is_sufficient::(&mut s); } -fn add_sufficients(minter: T::AccountId, n: u32) { +fn add_sufficients, I: 'static>(minter: T::AccountId, n: u32) { let origin = SystemOrigin::Signed(minter); let mut s = true; - swap_is_sufficient::(&mut s); + swap_is_sufficient::(&mut s); for i in 0..n { let target = account("sufficient", i, SEED); let target_lookup = T::Lookup::unlookup(target); - assert!(Assets::::mint(origin.clone().into(), Default::default(), target_lookup, 100u32.into()).is_ok()); + assert!(Assets::::mint(origin.clone().into(), Default::default(), target_lookup, 100u32.into()).is_ok()); } - swap_is_sufficient::(&mut s); + swap_is_sufficient::(&mut s); } -fn add_approvals(minter: T::AccountId, n: u32) { +fn add_approvals, I: 'static>(minter: T::AccountId, n: u32) { T::Currency::deposit_creating(&minter, T::ApprovalDeposit::get() * n.into()); let minter_lookup = T::Lookup::unlookup(minter.clone()); let origin = SystemOrigin::Signed(minter); - Assets::::mint( + Assets::::mint( origin.clone().into(), Default::default(), minter_lookup, @@ -110,7 +110,7 @@ fn add_approvals(minter: T::AccountId, n: u32) { let target = account("approval", i, SEED); T::Currency::make_free_balance_be(&target, T::Currency::minimum_balance()); let target_lookup = T::Lookup::unlookup(target); - Assets::::approve_transfer( + Assets::::approve_transfer( origin.clone().into(), Default::default(), target_lookup, @@ -119,7 +119,7 @@ fn add_approvals(minter: T::AccountId, n: u32) { } } -fn assert_last_event(generic_event: ::Event) { +fn assert_last_event, I: 'static>(generic_event: >::Event) { let events = frame_system::Pallet::::events(); let system_event: ::Event = generic_event.into(); // compare to the last event record @@ -127,7 +127,7 @@ fn assert_last_event(generic_event: ::Event) { assert_eq!(event, &system_event); } -fn assert_event(generic_event: ::Event) { +fn assert_event, I: 'static>(generic_event: >::Event) { let system_event: ::Event = generic_event.into(); let events = frame_system::Pallet::::events(); assert!(events.iter().any(|event_record| { @@ -135,14 +135,14 @@ fn assert_event(generic_event: ::Event) { })); } -benchmarks! { +benchmarks_instance_pallet! { create { let caller: T::AccountId = whitelisted_caller(); let caller_lookup = T::Lookup::unlookup(caller.clone()); - T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); + T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, 1u32.into()) verify { - assert_last_event::(Event::Created(Default::default(), caller.clone(), caller).into()); + assert_last_event::(Event::Created(Default::default(), caller.clone(), caller).into()); } force_create { @@ -150,127 +150,127 @@ benchmarks! { let caller_lookup = T::Lookup::unlookup(caller.clone()); }: _(SystemOrigin::Root, Default::default(), caller_lookup, true, 1u32.into()) verify { - assert_last_event::(Event::ForceCreated(Default::default(), caller).into()); + assert_last_event::(Event::ForceCreated(Default::default(), caller).into()); } destroy { let c in 0 .. 5_000; let s in 0 .. 5_000; let a in 0 .. 5_00; - let (caller, _) = create_default_asset::(true); - add_consumers::(caller.clone(), c); - add_sufficients::(caller.clone(), s); - add_approvals::(caller.clone(), a); - let witness = Asset::::get(T::AssetId::default()).unwrap().destroy_witness(); + let (caller, _) = create_default_asset::(true); + add_consumers::(caller.clone(), c); + add_sufficients::(caller.clone(), s); + add_approvals::(caller.clone(), a); + let witness = Asset::::get(T::AssetId::default()).unwrap().destroy_witness(); }: _(SystemOrigin::Signed(caller), Default::default(), witness) verify { - assert_last_event::(Event::Destroyed(Default::default()).into()); + assert_last_event::(Event::Destroyed(Default::default()).into()); } mint { - let (caller, caller_lookup) = create_default_asset::(true); + let (caller, caller_lookup) = create_default_asset::(true); let amount = T::Balance::from(100u32); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, amount) verify { - assert_last_event::(Event::Issued(Default::default(), caller, amount).into()); + assert_last_event::(Event::Issued(Default::default(), caller, amount).into()); } burn { let amount = T::Balance::from(100u32); - let (caller, caller_lookup) = create_default_minted_asset::(true, amount); + let (caller, caller_lookup) = create_default_minted_asset::(true, amount); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, amount) verify { - assert_last_event::(Event::Burned(Default::default(), caller, amount).into()); + assert_last_event::(Event::Burned(Default::default(), caller, amount).into()); } transfer { let amount = T::Balance::from(100u32); - let (caller, caller_lookup) = create_default_minted_asset::(true, amount); + let (caller, caller_lookup) = create_default_minted_asset::(true, amount); let target: T::AccountId = account("target", 0, SEED); let target_lookup = T::Lookup::unlookup(target.clone()); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), target_lookup, amount) verify { - assert_last_event::(Event::Transferred(Default::default(), caller, target, amount).into()); + assert_last_event::(Event::Transferred(Default::default(), caller, target, amount).into()); } transfer_keep_alive { let mint_amount = T::Balance::from(200u32); let amount = T::Balance::from(100u32); - let (caller, caller_lookup) = create_default_minted_asset::(true, mint_amount); + let (caller, caller_lookup) = create_default_minted_asset::(true, mint_amount); let target: T::AccountId = account("target", 0, SEED); let target_lookup = T::Lookup::unlookup(target.clone()); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), target_lookup, amount) verify { assert!(frame_system::Pallet::::account_exists(&caller)); - assert_last_event::(Event::Transferred(Default::default(), caller, target, amount).into()); + assert_last_event::(Event::Transferred(Default::default(), caller, target, amount).into()); } force_transfer { let amount = T::Balance::from(100u32); - let (caller, caller_lookup) = create_default_minted_asset::(true, amount); + let (caller, caller_lookup) = create_default_minted_asset::(true, amount); let target: T::AccountId = account("target", 0, SEED); let target_lookup = T::Lookup::unlookup(target.clone()); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, target_lookup, amount) verify { - assert_last_event::( + assert_last_event::( Event::Transferred(Default::default(), caller, target, amount).into() ); } freeze { - let (caller, caller_lookup) = create_default_minted_asset::(true, 100u32.into()); + let (caller, caller_lookup) = create_default_minted_asset::(true, 100u32.into()); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup) verify { - assert_last_event::(Event::Frozen(Default::default(), caller).into()); + assert_last_event::(Event::Frozen(Default::default(), caller).into()); } thaw { - let (caller, caller_lookup) = create_default_minted_asset::(true, 100u32.into()); - Assets::::freeze( + let (caller, caller_lookup) = create_default_minted_asset::(true, 100u32.into()); + Assets::::freeze( SystemOrigin::Signed(caller.clone()).into(), Default::default(), caller_lookup.clone(), )?; }: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup) verify { - assert_last_event::(Event::Thawed(Default::default(), caller).into()); + assert_last_event::(Event::Thawed(Default::default(), caller).into()); } freeze_asset { - let (caller, caller_lookup) = create_default_minted_asset::(true, 100u32.into()); + let (caller, caller_lookup) = create_default_minted_asset::(true, 100u32.into()); }: _(SystemOrigin::Signed(caller.clone()), Default::default()) verify { - assert_last_event::(Event::AssetFrozen(Default::default()).into()); + assert_last_event::(Event::AssetFrozen(Default::default()).into()); } thaw_asset { - let (caller, caller_lookup) = create_default_minted_asset::(true, 100u32.into()); - Assets::::freeze_asset( + let (caller, caller_lookup) = create_default_minted_asset::(true, 100u32.into()); + Assets::::freeze_asset( SystemOrigin::Signed(caller.clone()).into(), Default::default(), )?; }: _(SystemOrigin::Signed(caller.clone()), Default::default()) verify { - assert_last_event::(Event::AssetThawed(Default::default()).into()); + assert_last_event::(Event::AssetThawed(Default::default()).into()); } transfer_ownership { - let (caller, _) = create_default_asset::(true); + let (caller, _) = create_default_asset::(true); let target: T::AccountId = account("target", 0, SEED); let target_lookup = T::Lookup::unlookup(target.clone()); }: _(SystemOrigin::Signed(caller), Default::default(), target_lookup) verify { - assert_last_event::(Event::OwnerChanged(Default::default(), target).into()); + assert_last_event::(Event::OwnerChanged(Default::default(), target).into()); } set_team { - let (caller, _) = create_default_asset::(true); + let (caller, _) = create_default_asset::(true); let target0 = T::Lookup::unlookup(account("target", 0, SEED)); let target1 = T::Lookup::unlookup(account("target", 1, SEED)); let target2 = T::Lookup::unlookup(account("target", 2, SEED)); }: _(SystemOrigin::Signed(caller), Default::default(), target0.clone(), target1.clone(), target2.clone()) verify { - assert_last_event::(Event::TeamChanged( + assert_last_event::(Event::TeamChanged( Default::default(), account("target", 0, SEED), account("target", 1, SEED), @@ -286,23 +286,23 @@ benchmarks! { let symbol = vec![0u8; s as usize]; let decimals = 12; - let (caller, _) = create_default_asset::(true); - T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); + let (caller, _) = create_default_asset::(true); + T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); }: _(SystemOrigin::Signed(caller), Default::default(), name.clone(), symbol.clone(), decimals) verify { let id = Default::default(); - assert_last_event::(Event::MetadataSet(id, name, symbol, decimals, false).into()); + assert_last_event::(Event::MetadataSet(id, name, symbol, decimals, false).into()); } clear_metadata { - let (caller, _) = create_default_asset::(true); - T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); + let (caller, _) = create_default_asset::(true); + T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); let dummy = vec![0u8; T::StringLimit::get() as usize]; let origin = SystemOrigin::Signed(caller.clone()).into(); - Assets::::set_metadata(origin, Default::default(), dummy.clone(), dummy, 12)?; + Assets::::set_metadata(origin, Default::default(), dummy.clone(), dummy, 12)?; }: _(SystemOrigin::Signed(caller), Default::default()) verify { - assert_last_event::(Event::MetadataCleared(Default::default()).into()); + assert_last_event::(Event::MetadataCleared(Default::default()).into()); } force_set_metadata { @@ -313,10 +313,10 @@ benchmarks! { let symbol = vec![0u8; s as usize]; let decimals = 12; - create_default_asset::(true); + create_default_asset::(true); let origin = T::ForceOrigin::successful_origin(); - let call = Call::::force_set_metadata( + let call = Call::::force_set_metadata( Default::default(), name.clone(), symbol.clone(), @@ -326,28 +326,28 @@ benchmarks! { }: { call.dispatch_bypass_filter(origin)? } verify { let id = Default::default(); - assert_last_event::(Event::MetadataSet(id, name, symbol, decimals, false).into()); + assert_last_event::(Event::MetadataSet(id, name, symbol, decimals, false).into()); } force_clear_metadata { - let (caller, _) = create_default_asset::(true); - T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); + let (caller, _) = create_default_asset::(true); + T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); let dummy = vec![0u8; T::StringLimit::get() as usize]; let origin = SystemOrigin::Signed(caller.clone()).into(); - Assets::::set_metadata(origin, Default::default(), dummy.clone(), dummy, 12)?; + Assets::::set_metadata(origin, Default::default(), dummy.clone(), dummy, 12)?; let origin = T::ForceOrigin::successful_origin(); - let call = Call::::force_clear_metadata(Default::default()); + let call = Call::::force_clear_metadata(Default::default()); }: { call.dispatch_bypass_filter(origin)? } verify { - assert_last_event::(Event::MetadataCleared(Default::default()).into()); + assert_last_event::(Event::MetadataCleared(Default::default()).into()); } force_asset_status { - let (caller, caller_lookup) = create_default_asset::(true); + let (caller, caller_lookup) = create_default_asset::(true); let origin = T::ForceOrigin::successful_origin(); - let call = Call::::force_asset_status( + let call = Call::::force_asset_status( Default::default(), caller_lookup.clone(), caller_lookup.clone(), @@ -359,12 +359,12 @@ benchmarks! { ); }: { call.dispatch_bypass_filter(origin)? } verify { - assert_last_event::(Event::AssetStatusChanged(Default::default()).into()); + assert_last_event::(Event::AssetStatusChanged(Default::default()).into()); } approve_transfer { - let (caller, _) = create_default_minted_asset::(true, 100u32.into()); - T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); + let (caller, _) = create_default_minted_asset::(true, 100u32.into()); + T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); let id = Default::default(); let delegate: T::AccountId = account("delegate", 0, SEED); @@ -372,12 +372,12 @@ benchmarks! { let amount = 100u32.into(); }: _(SystemOrigin::Signed(caller.clone()), id, delegate_lookup, amount) verify { - assert_last_event::(Event::ApprovedTransfer(id, caller, delegate, amount).into()); + assert_last_event::(Event::ApprovedTransfer(id, caller, delegate, amount).into()); } transfer_approved { - let (owner, owner_lookup) = create_default_minted_asset::(true, 100u32.into()); - T::Currency::make_free_balance_be(&owner, DepositBalanceOf::::max_value()); + let (owner, owner_lookup) = create_default_minted_asset::(true, 100u32.into()); + T::Currency::make_free_balance_be(&owner, DepositBalanceOf::::max_value()); let id = Default::default(); let delegate: T::AccountId = account("delegate", 0, SEED); @@ -385,44 +385,44 @@ benchmarks! { let delegate_lookup = T::Lookup::unlookup(delegate.clone()); let amount = 100u32.into(); let origin = SystemOrigin::Signed(owner.clone()).into(); - Assets::::approve_transfer(origin, id, delegate_lookup.clone(), amount)?; + Assets::::approve_transfer(origin, id, delegate_lookup.clone(), amount)?; let dest: T::AccountId = account("dest", 0, SEED); let dest_lookup = T::Lookup::unlookup(dest.clone()); }: _(SystemOrigin::Signed(delegate.clone()), id, owner_lookup, dest_lookup, amount) verify { assert!(T::Currency::reserved_balance(&owner).is_zero()); - assert_event::(Event::Transferred(id, owner, dest, amount).into()); + assert_event::(Event::Transferred(id, owner, dest, amount).into()); } cancel_approval { - let (caller, _) = create_default_minted_asset::(true, 100u32.into()); - T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); + let (caller, _) = create_default_minted_asset::(true, 100u32.into()); + T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); let id = Default::default(); let delegate: T::AccountId = account("delegate", 0, SEED); let delegate_lookup = T::Lookup::unlookup(delegate.clone()); let amount = 100u32.into(); let origin = SystemOrigin::Signed(caller.clone()).into(); - Assets::::approve_transfer(origin, id, delegate_lookup.clone(), amount)?; + Assets::::approve_transfer(origin, id, delegate_lookup.clone(), amount)?; }: _(SystemOrigin::Signed(caller.clone()), id, delegate_lookup) verify { - assert_last_event::(Event::ApprovalCancelled(id, caller, delegate).into()); + assert_last_event::(Event::ApprovalCancelled(id, caller, delegate).into()); } force_cancel_approval { - let (caller, caller_lookup) = create_default_minted_asset::(true, 100u32.into()); - T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); + let (caller, caller_lookup) = create_default_minted_asset::(true, 100u32.into()); + T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); let id = Default::default(); let delegate: T::AccountId = account("delegate", 0, SEED); let delegate_lookup = T::Lookup::unlookup(delegate.clone()); let amount = 100u32.into(); let origin = SystemOrigin::Signed(caller.clone()).into(); - Assets::::approve_transfer(origin, id, delegate_lookup.clone(), amount)?; + Assets::::approve_transfer(origin, id, delegate_lookup.clone(), amount)?; }: _(SystemOrigin::Signed(caller.clone()), id, caller_lookup, delegate_lookup) verify { - assert_last_event::(Event::ApprovalCancelled(id, caller, delegate).into()); + assert_last_event::(Event::ApprovalCancelled(id, caller, delegate).into()); } } diff --git a/frame/assets/src/extra_mutator.rs b/frame/assets/src/extra_mutator.rs index 26a9a3f357c52..d86d78ce3e376 100644 --- a/frame/assets/src/extra_mutator.rs +++ b/frame/assets/src/extra_mutator.rs @@ -25,20 +25,23 @@ use super::*; /// any uncommitted changes (see `commit` function) will be automatically committed to storage when /// dropped. Changes, even after committed, may be reverted to their original values with the /// `revert` function. -pub struct ExtraMutator { +pub struct ExtraMutator, I: 'static = ()> { id: T::AssetId, who: T::AccountId, original: T::Extra, pending: Option, } -impl Drop for ExtraMutator { +impl, I: 'static> Drop for ExtraMutator { fn drop(&mut self) { - debug_assert!(self.commit().is_ok(), "attempt to write to non-existent asset account"); + debug_assert!( + self.commit().is_ok(), + "attempt to write to non-existent asset account" + ); } } -impl sp_std::ops::Deref for ExtraMutator { +impl, I: 'static> sp_std::ops::Deref for ExtraMutator { type Target = T::Extra; fn deref(&self) -> &T::Extra { match self.pending { @@ -48,7 +51,7 @@ impl sp_std::ops::Deref for ExtraMutator { } } -impl sp_std::ops::DerefMut for ExtraMutator { +impl, I: 'static> sp_std::ops::DerefMut for ExtraMutator { fn deref_mut(&mut self) -> &mut T::Extra { if self.pending.is_none() { self.pending = Some(self.original.clone()); @@ -57,15 +60,16 @@ impl sp_std::ops::DerefMut for ExtraMutator { } } -impl ExtraMutator { - pub(super) fn maybe_new(id: T::AssetId, who: impl sp_std::borrow::Borrow) - -> Option> - { - if Account::::contains_key(id, who.borrow()) { - Some(ExtraMutator:: { +impl, I: 'static> ExtraMutator { + pub(super) fn maybe_new( + id: T::AssetId, + who: impl sp_std::borrow::Borrow, + ) -> Option> { + if Account::::contains_key(id, who.borrow()) { + Some(ExtraMutator:: { id, who: who.borrow().clone(), - original: Account::::get(id, who.borrow()).extra, + original: Account::::get(id, who.borrow()).extra, pending: None, }) } else { @@ -73,18 +77,17 @@ impl ExtraMutator { } } - /// Commit any changes to storage. pub fn commit(&mut self) -> Result<(), ()> { if let Some(extra) = self.pending.take() { - Account::::try_mutate_exists(self.id, self.who.borrow(), |maybe_account| + Account::::try_mutate_exists(self.id, self.who.borrow(), |maybe_account| { if let Some(ref mut account) = maybe_account { account.extra = extra; Ok(()) } else { Err(()) } - ) + }) } else { Ok(()) } @@ -93,13 +96,13 @@ impl ExtraMutator { /// Revert any changes, even those already committed by `self` and drop self. pub fn revert(mut self) -> Result<(), ()> { self.pending = None; - Account::::try_mutate_exists(self.id, self.who.borrow(), |maybe_account| + Account::::try_mutate_exists(self.id, self.who.borrow(), |maybe_account| { if let Some(ref mut account) = maybe_account { account.extra = self.original.clone(); Ok(()) } else { Err(()) } - ) + }) } } diff --git a/frame/assets/src/functions.rs b/frame/assets/src/functions.rs index 197b010b6eb87..3f2abe0617e18 100644 --- a/frame/assets/src/functions.rs +++ b/frame/assets/src/functions.rs @@ -20,37 +20,40 @@ use super::*; // The main implementation block for the module. -impl Pallet { +impl, I: 'static> Pallet { // Public immutables /// Return the extra "sid-car" data for `id`/`who`, or `None` if the account doesn't exist. - pub fn adjust_extra(id: T::AssetId, who: impl sp_std::borrow::Borrow) - -> Option> - { + pub fn adjust_extra( + id: T::AssetId, + who: impl sp_std::borrow::Borrow, + ) -> Option> { ExtraMutator::maybe_new(id, who) } /// Get the asset `id` balance of `who`. pub fn balance(id: T::AssetId, who: impl sp_std::borrow::Borrow) -> T::Balance { - Account::::get(id, who.borrow()).balance + Account::::get(id, who.borrow()).balance } /// Get the total supply of an asset `id`. pub fn total_supply(id: T::AssetId) -> T::Balance { - Asset::::get(id).map(|x| x.supply).unwrap_or_else(Zero::zero) + Asset::::get(id) + .map(|x| x.supply) + .unwrap_or_else(Zero::zero) } pub(super) fn new_account( who: &T::AccountId, - d: &mut AssetDetails>, + d: &mut AssetDetails>, ) -> Result { - let accounts = d.accounts.checked_add(1).ok_or(Error::::Overflow)?; + let accounts = d.accounts.checked_add(1).ok_or(Error::::Overflow)?; let is_sufficient = if d.is_sufficient { frame_system::Pallet::::inc_sufficients(who); d.sufficients += 1; true } else { - frame_system::Pallet::::inc_consumers(who).map_err(|_| Error::::NoProvider)?; + frame_system::Pallet::::inc_consumers(who).map_err(|_| Error::::NoProvider)?; false }; d.accounts = accounts; @@ -60,7 +63,7 @@ impl Pallet { pub(super) fn dead_account( what: T::AssetId, who: &T::AccountId, - d: &mut AssetDetails>, + d: &mut AssetDetails>, sufficient: bool, ) { if sufficient { @@ -73,15 +76,19 @@ impl Pallet { T::Freezer::died(what, who) } - pub(super) fn can_increase(id: T::AssetId, who: &T::AccountId, amount: T::Balance) -> DepositConsequence { - let details = match Asset::::get(id) { + pub(super) fn can_increase( + id: T::AssetId, + who: &T::AccountId, + amount: T::Balance, + ) -> DepositConsequence { + let details = match Asset::::get(id) { Some(details) => details, None => return DepositConsequence::UnknownAsset, }; if details.supply.checked_add(&amount).is_none() { return DepositConsequence::Overflow } - let account = Account::::get(id, who); + let account = Account::::get(id, who); if account.balance.checked_add(&amount).is_none() { return DepositConsequence::Overflow } @@ -108,7 +115,7 @@ impl Pallet { keep_alive: bool, ) -> WithdrawConsequence { use WithdrawConsequence::*; - let details = match Asset::::get(id) { + let details = match Asset::::get(id) { Some(details) => details, None => return UnknownAsset, }; @@ -118,7 +125,7 @@ impl Pallet { if details.is_frozen { return Frozen } - let account = Account::::get(id, who); + let account = Account::::get(id, who); if account.is_frozen { return Frozen } @@ -155,19 +162,21 @@ impl Pallet { id: T::AssetId, who: &T::AccountId, keep_alive: bool, - ) -> Result> { - let details = match Asset::::get(id) { + ) -> Result> { + let details = match Asset::::get(id) { Some(details) => details, - None => return Err(Error::::Unknown), + None => return Err(Error::::Unknown), }; - ensure!(!details.is_frozen, Error::::Frozen); + ensure!(!details.is_frozen, Error::::Frozen); - let account = Account::::get(id, who); - ensure!(!account.is_frozen, Error::::Frozen); + let account = Account::::get(id, who); + ensure!(!account.is_frozen, Error::::Frozen); let amount = if let Some(frozen) = T::Freezer::frozen_balance(id, who) { // Frozen balance: account CANNOT be deleted - let required = frozen.checked_add(&details.min_balance).ok_or(Error::::Overflow)?; + let required = frozen + .checked_add(&details.min_balance) + .ok_or(Error::::Overflow)?; account.balance.saturating_sub(required) } else { let is_provider = false; @@ -204,9 +213,8 @@ impl Pallet { amount: T::Balance, f: DebitFlags, ) -> Result { - let actual = Self::reducible_balance(id, target, f.keep_alive)? - .min(amount); - ensure!(f.best_effort || actual >= amount, Error::::BalanceLow); + let actual = Self::reducible_balance(id, target, f.keep_alive)?.min(amount); + ensure!(f.best_effort || actual >= amount, Error::::BalanceLow); let conseq = Self::can_decrease(id, target, actual, f.keep_alive); let actual = match conseq.into_result() { @@ -263,7 +271,10 @@ impl Pallet { ) -> DispatchResult { Self::increase_balance(id, beneficiary, amount, |details| -> DispatchResult { if let Some(check_issuer) = maybe_check_issuer { - ensure!(&check_issuer == &details.issuer, Error::::NoPermission); + ensure!( + &check_issuer == &details.issuer, + Error::::NoPermission + ); } debug_assert!(T::Balance::max_value() - details.supply >= amount, "checked in prep; qed"); details.supply = details.supply.saturating_add(amount); @@ -283,17 +294,19 @@ impl Pallet { id: T::AssetId, beneficiary: &T::AccountId, amount: T::Balance, - check: impl FnOnce(&mut AssetDetails>) -> DispatchResult, + check: impl FnOnce( + &mut AssetDetails>, + ) -> DispatchResult, ) -> DispatchResult { if amount.is_zero() { return Ok(()) } Self::can_increase(id, beneficiary, amount).into_result()?; - Asset::::try_mutate(id, |maybe_details| -> DispatchResult { - let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; + Asset::::try_mutate(id, |maybe_details| -> DispatchResult { + let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; check(details)?; - Account::::try_mutate(id, beneficiary, |t| -> DispatchResult { + Account::::try_mutate(id, beneficiary, |t| -> DispatchResult { let new_balance = t.balance.saturating_add(amount); ensure!(new_balance >= details.min_balance, TokenError::BelowMinimum); if t.balance.is_zero() { @@ -324,7 +337,7 @@ impl Pallet { let actual = Self::decrease_balance(id, target, amount, f, |actual, details| { // Check admin rights. if let Some(check_admin) = maybe_check_admin { - ensure!(&check_admin == &details.admin, Error::::NoPermission); + ensure!(&check_admin == &details.admin, Error::::NoPermission); } debug_assert!(details.supply >= actual, "checked in prep; qed"); @@ -351,19 +364,19 @@ impl Pallet { f: DebitFlags, check: impl FnOnce( T::Balance, - &mut AssetDetails>, + &mut AssetDetails>, ) -> DispatchResult, ) -> Result { if amount.is_zero() { return Ok(amount) } let actual = Self::prep_debit(id, target, amount, f)?; - Asset::::try_mutate(id, |maybe_details| -> DispatchResult { - let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; + Asset::::try_mutate(id, |maybe_details| -> DispatchResult { + let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; check(actual, details)?; - Account::::try_mutate_exists(id, target, |maybe_account| -> DispatchResult { + Account::::try_mutate_exists(id, target, |maybe_account| -> DispatchResult { let mut account = maybe_account.take().unwrap_or_default(); debug_assert!(account.balance >= actual, "checked in prep; qed"); @@ -411,14 +424,14 @@ impl Pallet { let debit = Self::prep_debit(id, &source, amount, f.into())?; let (credit, maybe_burn) = Self::prep_credit(id, &dest, amount, debit, f.burn_dust)?; - let mut source_account = Account::::get(id, &source); + let mut source_account = Account::::get(id, &source); - Asset::::try_mutate(id, |maybe_details| -> DispatchResult { - let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; + Asset::::try_mutate(id, |maybe_details| -> DispatchResult { + let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; // Check admin rights. if let Some(need_admin) = maybe_need_admin { - ensure!(&need_admin == &details.admin, Error::::NoPermission); + ensure!(&need_admin == &details.admin, Error::::NoPermission); } // Skip if source == dest @@ -437,7 +450,7 @@ impl Pallet { debug_assert!(source_account.balance >= debit, "checked in prep; qed"); source_account.balance = source_account.balance.saturating_sub(debit); - Account::::try_mutate(id, &dest, |a| -> DispatchResult { + Account::::try_mutate(id, &dest, |a| -> DispatchResult { // Calculate new balance; this will not saturate since it's already checked in prep. debug_assert!(a.balance.checked_add(&credit).is_some(), "checked in prep; qed"); let new_balance = a.balance.saturating_add(credit); @@ -455,9 +468,9 @@ impl Pallet { if source_account.balance < details.min_balance { debug_assert!(source_account.balance.is_zero(), "checked in prep; qed"); Self::dead_account(id, &source, details, source_account.sufficient); - Account::::remove(id, &source); + Account::::remove(id, &source); } else { - Account::::insert(id, &source, &source_account) + Account::::insert(id, &source, &source_account) } Ok(()) diff --git a/frame/assets/src/impl_fungibles.rs b/frame/assets/src/impl_fungibles.rs index a4cff9b7e9a62..d0ab13072a88d 100644 --- a/frame/assets/src/impl_fungibles.rs +++ b/frame/assets/src/impl_fungibles.rs @@ -19,23 +19,24 @@ use super::*; -impl fungibles::Inspect<::AccountId> for Pallet { +impl, I: 'static> fungibles::Inspect<::AccountId> for Pallet { type AssetId = T::AssetId; type Balance = T::Balance; fn total_issuance(asset: Self::AssetId) -> Self::Balance { - Asset::::get(asset).map(|x| x.supply).unwrap_or_else(Zero::zero) + Asset::::get(asset) + .map(|x| x.supply) + .unwrap_or_else(Zero::zero) } fn minimum_balance(asset: Self::AssetId) -> Self::Balance { - Asset::::get(asset).map(|x| x.min_balance).unwrap_or_else(Zero::zero) + Asset::::get(asset) + .map(|x| x.min_balance) + .unwrap_or_else(Zero::zero) } - fn balance( - asset: Self::AssetId, - who: &::AccountId, - ) -> Self::Balance { - Pallet::::balance(asset, who) + fn balance(asset: Self::AssetId, who: &::AccountId) -> Self::Balance { + Pallet::::balance(asset, who) } fn reducible_balance( @@ -43,7 +44,7 @@ impl fungibles::Inspect<::AccountId> for Pallet who: &::AccountId, keep_alive: bool, ) -> Self::Balance { - Pallet::::reducible_balance(asset, who, keep_alive).unwrap_or(Zero::zero()) + Pallet::::reducible_balance(asset, who, keep_alive).unwrap_or(Zero::zero()) } fn can_deposit( @@ -51,7 +52,7 @@ impl fungibles::Inspect<::AccountId> for Pallet who: &::AccountId, amount: Self::Balance, ) -> DepositConsequence { - Pallet::::can_increase(asset, who, amount) + Pallet::::can_increase(asset, who, amount) } fn can_withdraw( @@ -59,11 +60,11 @@ impl fungibles::Inspect<::AccountId> for Pallet who: &::AccountId, amount: Self::Balance, ) -> WithdrawConsequence { - Pallet::::can_decrease(asset, who, amount, false) + Pallet::::can_decrease(asset, who, amount, false) } } -impl fungibles::Mutate<::AccountId> for Pallet { +impl, I: 'static> fungibles::Mutate<::AccountId> for Pallet { fn mint_into( asset: Self::AssetId, who: &::AccountId, @@ -97,7 +98,7 @@ impl fungibles::Mutate<::AccountId> for Pallet } } -impl fungibles::Transfer for Pallet { +impl, I: 'static> fungibles::Transfer for Pallet { fn transfer( asset: Self::AssetId, source: &T::AccountId, @@ -114,13 +115,15 @@ impl fungibles::Transfer for Pallet { } } -impl fungibles::Unbalanced for Pallet { +impl, I: 'static> fungibles::Unbalanced for Pallet { fn set_balance(_: Self::AssetId, _: &T::AccountId, _: Self::Balance) -> DispatchResult { unreachable!("set_balance is not used if other functions are impl'd"); } fn set_total_issuance(id: T::AssetId, amount: Self::Balance) { - Asset::::mutate_exists(id, |maybe_asset| if let Some(ref mut asset) = maybe_asset { - asset.supply = amount + Asset::::mutate_exists(id, |maybe_asset| { + if let Some(ref mut asset) = maybe_asset { + asset.supply = amount + } }); } fn decrease_balance(asset: T::AssetId, who: &T::AccountId, amount: Self::Balance) diff --git a/frame/assets/src/impl_stored_map.rs b/frame/assets/src/impl_stored_map.rs index a8a6f95557dfb..6e91e5c1322f7 100644 --- a/frame/assets/src/impl_stored_map.rs +++ b/frame/assets/src/impl_stored_map.rs @@ -19,11 +19,11 @@ use super::*; -impl StoredMap<(T::AssetId, T::AccountId), T::Extra> for Pallet { +impl, I: 'static> StoredMap<(T::AssetId, T::AccountId), T::Extra> for Pallet { fn get(id_who: &(T::AssetId, T::AccountId)) -> T::Extra { let &(id, ref who) = id_who; - if Account::::contains_key(id, who) { - Account::::get(id, who).extra + if Account::::contains_key(id, who) { + Account::::get(id, who).extra } else { Default::default() } @@ -34,13 +34,13 @@ impl StoredMap<(T::AssetId, T::AccountId), T::Extra> for Pallet { f: impl FnOnce(&mut Option) -> Result, ) -> Result { let &(id, ref who) = id_who; - let mut maybe_extra = Some(Account::::get(id, who).extra); + let mut maybe_extra = Some(Account::::get(id, who).extra); let r = f(&mut maybe_extra)?; // They want to write some value or delete it. // If the account existed and they want to write a value, then we write. // If the account didn't exist and they want to delete it, then we let it pass. // Otherwise, we fail. - Account::::try_mutate_exists(id, who, |maybe_account| { + Account::::try_mutate_exists(id, who, |maybe_account| { if let Some(extra) = maybe_extra { // They want to write a value. Let this happen only if the account actually exists. if let Some(ref mut account) = maybe_account { diff --git a/frame/assets/src/lib.rs b/frame/assets/src/lib.rs index 2a162c2c936b1..e8dfd50f4086a 100644 --- a/frame/assets/src/lib.rs +++ b/frame/assets/src/lib.rs @@ -165,13 +165,13 @@ pub mod pallet { #[pallet::pallet] #[pallet::generate_store(pub(super) trait Store)] - pub struct Pallet(_); + pub struct Pallet(_); #[pallet::config] /// The module configuration trait. - pub trait Config: frame_system::Config { + pub trait Config: frame_system::Config { /// The overarching event type. - type Event: From> + IsType<::Event>; + type Event: From> + IsType<::Event>; /// The units in which we record balances. type Balance: Member + Parameter + AtLeast32BitUnsigned + Default + Copy; @@ -187,17 +187,17 @@ pub mod pallet { type ForceOrigin: EnsureOrigin; /// The basic amount of funds that must be reserved for an asset. - type AssetDeposit: Get>; + type AssetDeposit: Get>; /// The basic amount of funds that must be reserved when adding metadata to your asset. - type MetadataDepositBase: Get>; + type MetadataDepositBase: Get>; /// The additional funds that must be reserved for the number of bytes you store in your /// metadata. - type MetadataDepositPerByte: Get>; + type MetadataDepositPerByte: Get>; /// The amount of funds that must be reserved when creating a new approval. - type ApprovalDeposit: Get>; + type ApprovalDeposit: Get>; /// The maximum length of a name or symbol stored on-chain. type StringLimit: Get; @@ -215,16 +215,16 @@ pub mod pallet { #[pallet::storage] /// Details of an asset. - pub(super) type Asset = StorageMap< + pub(super) type Asset, I: 'static = ()> = StorageMap< _, Blake2_128Concat, T::AssetId, - AssetDetails>, + AssetDetails>, >; #[pallet::storage] /// The number of units of assets held by any given account. - pub(super) type Account = StorageDoubleMap< + pub(super) type Account, I: 'static = ()> = StorageDoubleMap< _, Blake2_128Concat, T::AssetId, @@ -237,30 +237,34 @@ pub mod pallet { #[pallet::storage] /// Approved balance transfers. First balance is the amount approved for transfer. Second /// is the amount of `T::Currency` reserved for storing this. - pub(super) type Approvals = StorageDoubleMap< + pub(super) type Approvals, I: 'static = ()> = StorageDoubleMap< _, Blake2_128Concat, T::AssetId, Blake2_128Concat, ApprovalKey, - Approval>, + Approval>, OptionQuery, >; #[pallet::storage] /// Metadata of an asset. - pub(super) type Metadata = StorageMap< + pub(super) type Metadata, I: 'static = ()> = StorageMap< _, Blake2_128Concat, T::AssetId, - AssetMetadata>, + AssetMetadata>, ValueQuery, >; #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] - #[pallet::metadata(T::AccountId = "AccountId", T::Balance = "Balance", T::AssetId = "AssetId")] - pub enum Event { + #[pallet::metadata( + T::AccountId = "AccountId", + T::Balance = "Balance", + T::AssetId = "AssetId" + )] + pub enum Event, I: 'static = ()> { /// Some asset class was created. \[asset_id, creator, owner\] Created(T::AssetId, T::AccountId, T::AccountId), /// Some assets were issued. \[asset_id, owner, total_supply\] @@ -305,7 +309,7 @@ pub mod pallet { } #[pallet::error] - pub enum Error { + pub enum Error { /// Account balance must be greater than or equal to the transfer amount. BalanceLow, /// Balance should be non-zero. @@ -335,10 +339,10 @@ pub mod pallet { } #[pallet::hooks] - impl Hooks> for Pallet {} + impl, I: 'static> Hooks> for Pallet {} #[pallet::call] - impl Pallet { + impl, I: 'static> Pallet { /// Issue a new class of fungible assets from a public origin. /// /// This new asset class has no assets initially and its owner is the origin. @@ -368,26 +372,29 @@ pub mod pallet { let owner = ensure_signed(origin)?; let admin = T::Lookup::lookup(admin)?; - ensure!(!Asset::::contains_key(id), Error::::InUse); - ensure!(!min_balance.is_zero(), Error::::MinBalanceZero); + ensure!(!Asset::::contains_key(id), Error::::InUse); + ensure!(!min_balance.is_zero(), Error::::MinBalanceZero); let deposit = T::AssetDeposit::get(); T::Currency::reserve(&owner, deposit)?; - Asset::::insert(id, AssetDetails { - owner: owner.clone(), - issuer: admin.clone(), - admin: admin.clone(), - freezer: admin.clone(), - supply: Zero::zero(), - deposit, - min_balance, - is_sufficient: false, - accounts: 0, - sufficients: 0, - approvals: 0, - is_frozen: false, - }); + Asset::::insert( + id, + AssetDetails { + owner: owner.clone(), + issuer: admin.clone(), + admin: admin.clone(), + freezer: admin.clone(), + supply: Zero::zero(), + deposit, + min_balance, + is_sufficient: false, + accounts: 0, + sufficients: 0, + approvals: 0, + is_frozen: false, + }, + ); Self::deposit_event(Event::Created(id, owner, admin)); Ok(()) } @@ -424,23 +431,26 @@ pub mod pallet { T::ForceOrigin::ensure_origin(origin)?; let owner = T::Lookup::lookup(owner)?; - ensure!(!Asset::::contains_key(id), Error::::InUse); - ensure!(!min_balance.is_zero(), Error::::MinBalanceZero); - - Asset::::insert(id, AssetDetails { - owner: owner.clone(), - issuer: owner.clone(), - admin: owner.clone(), - freezer: owner.clone(), - supply: Zero::zero(), - deposit: Zero::zero(), - min_balance, - is_sufficient, - accounts: 0, - sufficients: 0, - approvals: 0, - is_frozen: false, - }); + ensure!(!Asset::::contains_key(id), Error::::InUse); + ensure!(!min_balance.is_zero(), Error::::MinBalanceZero); + + Asset::::insert( + id, + AssetDetails { + owner: owner.clone(), + issuer: owner.clone(), + admin: owner.clone(), + freezer: owner.clone(), + supply: Zero::zero(), + deposit: Zero::zero(), + min_balance, + is_sufficient, + accounts: 0, + sufficients: 0, + approvals: 0, + is_frozen: false, + }, + ); Self::deposit_event(Event::ForceCreated(id, owner)); Ok(()) } @@ -473,25 +483,28 @@ pub mod pallet { Ok(_) => None, Err(origin) => Some(ensure_signed(origin)?), }; - Asset::::try_mutate_exists(id, |maybe_details| { - let mut details = maybe_details.take().ok_or(Error::::Unknown)?; + Asset::::try_mutate_exists(id, |maybe_details| { + let mut details = maybe_details.take().ok_or(Error::::Unknown)?; if let Some(check_owner) = maybe_check_owner { - ensure!(details.owner == check_owner, Error::::NoPermission); + ensure!(details.owner == check_owner, Error::::NoPermission); } - ensure!(details.accounts == witness.accounts, Error::::BadWitness); - ensure!(details.sufficients == witness.sufficients, Error::::BadWitness); - ensure!(details.approvals == witness.approvals, Error::::BadWitness); + ensure!(details.accounts == witness.accounts, Error::::BadWitness); + ensure!(details.sufficients == witness.sufficients, Error::::BadWitness); + ensure!(details.approvals == witness.approvals, Error::::BadWitness); - for (who, v) in Account::::drain_prefix(id) { + for (who, v) in Account::::drain_prefix(id) { Self::dead_account(id, &who, &mut details, v.sufficient); } debug_assert_eq!(details.accounts, 0); debug_assert_eq!(details.sufficients, 0); - let metadata = Metadata::::take(&id); - T::Currency::unreserve(&details.owner, details.deposit.saturating_add(metadata.deposit)); + let metadata = Metadata::::take(&id); + T::Currency::unreserve( + &details.owner, + details.deposit.saturating_add(metadata.deposit), + ); - Approvals::::remove_prefix(&id); + Approvals::::remove_prefix(&id); Self::deposit_event(Event::Destroyed(id)); // NOTE: could use postinfo to reflect the actual number of accounts/sufficient/approvals @@ -685,14 +698,17 @@ pub mod pallet { ) -> DispatchResult { let origin = ensure_signed(origin)?; - let d = Asset::::get(id).ok_or(Error::::Unknown)?; - ensure!(&origin == &d.freezer, Error::::NoPermission); + let d = Asset::::get(id).ok_or(Error::::Unknown)?; + ensure!(&origin == &d.freezer, Error::::NoPermission); let who = T::Lookup::lookup(who)?; - ensure!(Account::::contains_key(id, &who), Error::::BalanceZero); + ensure!( + Account::::contains_key(id, &who), + Error::::BalanceZero + ); - Account::::mutate(id, &who, |a| a.is_frozen = true); + Account::::mutate(id, &who, |a| a.is_frozen = true); - Self::deposit_event(Event::::Frozen(id, who)); + Self::deposit_event(Event::::Frozen(id, who)); Ok(()) } @@ -715,14 +731,17 @@ pub mod pallet { ) -> DispatchResult { let origin = ensure_signed(origin)?; - let details = Asset::::get(id).ok_or(Error::::Unknown)?; - ensure!(&origin == &details.admin, Error::::NoPermission); + let details = Asset::::get(id).ok_or(Error::::Unknown)?; + ensure!(&origin == &details.admin, Error::::NoPermission); let who = T::Lookup::lookup(who)?; - ensure!(Account::::contains_key(id, &who), Error::::BalanceZero); + ensure!( + Account::::contains_key(id, &who), + Error::::BalanceZero + ); - Account::::mutate(id, &who, |a| a.is_frozen = false); + Account::::mutate(id, &who, |a| a.is_frozen = false); - Self::deposit_event(Event::::Thawed(id, who)); + Self::deposit_event(Event::::Thawed(id, who)); Ok(()) } @@ -742,13 +761,13 @@ pub mod pallet { ) -> DispatchResult { let origin = ensure_signed(origin)?; - Asset::::try_mutate(id, |maybe_details| { - let d = maybe_details.as_mut().ok_or(Error::::Unknown)?; - ensure!(&origin == &d.freezer, Error::::NoPermission); + Asset::::try_mutate(id, |maybe_details| { + let d = maybe_details.as_mut().ok_or(Error::::Unknown)?; + ensure!(&origin == &d.freezer, Error::::NoPermission); d.is_frozen = true; - Self::deposit_event(Event::::AssetFrozen(id)); + Self::deposit_event(Event::::AssetFrozen(id)); Ok(()) }) } @@ -769,13 +788,13 @@ pub mod pallet { ) -> DispatchResult { let origin = ensure_signed(origin)?; - Asset::::try_mutate(id, |maybe_details| { - let d = maybe_details.as_mut().ok_or(Error::::Unknown)?; - ensure!(&origin == &d.admin, Error::::NoPermission); + Asset::::try_mutate(id, |maybe_details| { + let d = maybe_details.as_mut().ok_or(Error::::Unknown)?; + ensure!(&origin == &d.admin, Error::::NoPermission); d.is_frozen = false; - Self::deposit_event(Event::::AssetThawed(id)); + Self::deposit_event(Event::::AssetThawed(id)); Ok(()) }) } @@ -799,12 +818,14 @@ pub mod pallet { let origin = ensure_signed(origin)?; let owner = T::Lookup::lookup(owner)?; - Asset::::try_mutate(id, |maybe_details| { - let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; - ensure!(&origin == &details.owner, Error::::NoPermission); - if details.owner == owner { return Ok(()) } + Asset::::try_mutate(id, |maybe_details| { + let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; + ensure!(&origin == &details.owner, Error::::NoPermission); + if details.owner == owner { + return Ok(()); + } - let metadata_deposit = Metadata::::get(id).deposit; + let metadata_deposit = Metadata::::get(id).deposit; let deposit = details.deposit + metadata_deposit; // Move the deposit to the new owner. @@ -842,9 +863,9 @@ pub mod pallet { let admin = T::Lookup::lookup(admin)?; let freezer = T::Lookup::lookup(freezer)?; - Asset::::try_mutate(id, |maybe_details| { - let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; - ensure!(&origin == &details.owner, Error::::NoPermission); + Asset::::try_mutate(id, |maybe_details| { + let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; + ensure!(&origin == &details.owner, Error::::NoPermission); details.issuer = issuer.clone(); details.admin = admin.clone(); @@ -881,14 +902,17 @@ pub mod pallet { ) -> DispatchResult { let origin = ensure_signed(origin)?; - ensure!(name.len() <= T::StringLimit::get() as usize, Error::::BadMetadata); - ensure!(symbol.len() <= T::StringLimit::get() as usize, Error::::BadMetadata); + ensure!(name.len() <= T::StringLimit::get() as usize, Error::::BadMetadata); + ensure!(symbol.len() <= T::StringLimit::get() as usize, Error::::BadMetadata); - let d = Asset::::get(id).ok_or(Error::::Unknown)?; - ensure!(&origin == &d.owner, Error::::NoPermission); + let d = Asset::::get(id).ok_or(Error::::Unknown)?; + ensure!(&origin == &d.owner, Error::::NoPermission); - Metadata::::try_mutate_exists(id, |metadata| { - ensure!(metadata.as_ref().map_or(true, |m| !m.is_frozen), Error::::NoPermission); + Metadata::::try_mutate_exists(id, |metadata| { + ensure!( + metadata.as_ref().map_or(true, |m| !m.is_frozen), + Error::::NoPermission + ); let old_deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit); let new_deposit = T::MetadataDepositPerByte::get() @@ -932,11 +956,11 @@ pub mod pallet { ) -> DispatchResult { let origin = ensure_signed(origin)?; - let d = Asset::::get(id).ok_or(Error::::Unknown)?; - ensure!(&origin == &d.owner, Error::::NoPermission); + let d = Asset::::get(id).ok_or(Error::::Unknown)?; + ensure!(&origin == &d.owner, Error::::NoPermission); - Metadata::::try_mutate_exists(id, |metadata| { - let deposit = metadata.take().ok_or(Error::::Unknown)?.deposit; + Metadata::::try_mutate_exists(id, |metadata| { + let deposit = metadata.take().ok_or(Error::::Unknown)?.deposit; T::Currency::unreserve(&d.owner, deposit); Self::deposit_event(Event::MetadataCleared(id)); Ok(()) @@ -968,11 +992,11 @@ pub mod pallet { ) -> DispatchResult { T::ForceOrigin::ensure_origin(origin)?; - ensure!(name.len() <= T::StringLimit::get() as usize, Error::::BadMetadata); - ensure!(symbol.len() <= T::StringLimit::get() as usize, Error::::BadMetadata); + ensure!(name.len() <= T::StringLimit::get() as usize, Error::::BadMetadata); + ensure!(symbol.len() <= T::StringLimit::get() as usize, Error::::BadMetadata); - ensure!(Asset::::contains_key(id), Error::::Unknown); - Metadata::::try_mutate_exists(id, |metadata| { + ensure!(Asset::::contains_key(id), Error::::Unknown); + Metadata::::try_mutate_exists(id, |metadata| { let deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit); *metadata = Some(AssetMetadata { deposit, @@ -1005,9 +1029,9 @@ pub mod pallet { ) -> DispatchResult { T::ForceOrigin::ensure_origin(origin)?; - let d = Asset::::get(id).ok_or(Error::::Unknown)?; - Metadata::::try_mutate_exists(id, |metadata| { - let deposit = metadata.take().ok_or(Error::::Unknown)?.deposit; + let d = Asset::::get(id).ok_or(Error::::Unknown)?; + Metadata::::try_mutate_exists(id, |metadata| { + let deposit = metadata.take().ok_or(Error::::Unknown)?.deposit; T::Currency::unreserve(&d.owner, deposit); Self::deposit_event(Event::MetadataCleared(id)); Ok(()) @@ -1050,8 +1074,8 @@ pub mod pallet { ) -> DispatchResult { T::ForceOrigin::ensure_origin(origin)?; - Asset::::try_mutate(id, |maybe_asset| { - let mut asset = maybe_asset.take().ok_or(Error::::Unknown)?; + Asset::::try_mutate(id, |maybe_asset| { + let mut asset = maybe_asset.take().ok_or(Error::::Unknown)?; asset.owner = T::Lookup::lookup(owner)?; asset.issuer = T::Lookup::lookup(issuer)?; asset.admin = T::Lookup::lookup(admin)?; @@ -1097,7 +1121,7 @@ pub mod pallet { let delegate = T::Lookup::lookup(delegate)?; let key = ApprovalKey { owner, delegate }; - Approvals::::try_mutate(id, &key, |maybe_approved| -> DispatchResult { + Approvals::::try_mutate(id, &key, |maybe_approved| -> DispatchResult { let mut approved = maybe_approved.take().unwrap_or_default(); let deposit_required = T::ApprovalDeposit::get(); if approved.deposit < deposit_required { @@ -1135,7 +1159,7 @@ pub mod pallet { let owner = ensure_signed(origin)?; let delegate = T::Lookup::lookup(delegate)?; let key = ApprovalKey { owner, delegate }; - let approval = Approvals::::take(id, &key).ok_or(Error::::Unknown)?; + let approval = Approvals::::take(id, &key).ok_or(Error::::Unknown)?; T::Currency::unreserve(&key.owner, approval.deposit); Self::deposit_event(Event::ApprovalCancelled(id, key.owner, key.delegate)); @@ -1166,8 +1190,8 @@ pub mod pallet { .map(|_| ()) .or_else(|origin| -> DispatchResult { let origin = ensure_signed(origin)?; - let d = Asset::::get(id).ok_or(Error::::Unknown)?; - ensure!(&origin == &d.admin, Error::::NoPermission); + let d = Asset::::get(id).ok_or(Error::::Unknown)?; + ensure!(&origin == &d.admin, Error::::NoPermission); Ok(()) })?; @@ -1175,7 +1199,7 @@ pub mod pallet { let delegate = T::Lookup::lookup(delegate)?; let key = ApprovalKey { owner, delegate }; - let approval = Approvals::::take(id, &key).ok_or(Error::::Unknown)?; + let approval = Approvals::::take(id, &key).ok_or(Error::::Unknown)?; T::Currency::unreserve(&key.owner, approval.deposit); Self::deposit_event(Event::ApprovalCancelled(id, key.owner, key.delegate)); @@ -1213,9 +1237,12 @@ pub mod pallet { let destination = T::Lookup::lookup(destination)?; let key = ApprovalKey { owner, delegate }; - Approvals::::try_mutate_exists(id, &key, |maybe_approved| -> DispatchResult { - let mut approved = maybe_approved.take().ok_or(Error::::Unapproved)?; - let remaining = approved.amount.checked_sub(&amount).ok_or(Error::::Unapproved)?; + Approvals::::try_mutate_exists(id, &key, |maybe_approved| -> DispatchResult { + let mut approved = maybe_approved.take().ok_or(Error::::Unapproved)?; + let remaining = approved + .amount + .checked_sub(&amount) + .ok_or(Error::::Unapproved)?; let f = TransferFlags { keep_alive: false, diff --git a/frame/assets/src/types.rs b/frame/assets/src/types.rs index 7e0e235b1b7e6..f3f17c00a218f 100644 --- a/frame/assets/src/types.rs +++ b/frame/assets/src/types.rs @@ -15,11 +15,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Various basic tyoes for use in the assets pallet. +//! Various basic types for use in the assets pallet. use super::*; -pub(super) type DepositBalanceOf = <::Currency as Currency<::AccountId>>::Balance; +pub(super) type DepositBalanceOf = + <>::Currency as Currency<::AccountId>>::Balance; #[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug)] pub struct AssetDetails<