diff --git a/frame/asset-rate/src/benchmarking.rs b/frame/asset-rate/src/benchmarking.rs index dde0d764affb2..1209f8db6693b 100644 --- a/frame/asset-rate/src/benchmarking.rs +++ b/frame/asset-rate/src/benchmarking.rs @@ -38,7 +38,7 @@ mod benchmarks { fn create() -> Result<(), BenchmarkError> { let asset_id: T::AssetId = ASSET_ID.into(); #[extrinsic_call] - _(RawOrigin::Root, asset_id, default_conversion_rate()); + _(RawOrigin::Root, asset_id.clone(), default_conversion_rate()); assert_eq!( pallet_asset_rate::ConversionRateToNative::::get(asset_id), @@ -52,12 +52,12 @@ mod benchmarks { let asset_id: T::AssetId = ASSET_ID.into(); assert_ok!(AssetRate::::create( RawOrigin::Root.into(), - asset_id, + asset_id.clone(), default_conversion_rate() )); #[extrinsic_call] - _(RawOrigin::Root, asset_id, FixedU128::from_u32(2)); + _(RawOrigin::Root, asset_id.clone(), FixedU128::from_u32(2)); assert_eq!( pallet_asset_rate::ConversionRateToNative::::get(asset_id), @@ -76,7 +76,7 @@ mod benchmarks { )); #[extrinsic_call] - _(RawOrigin::Root, asset_id); + _(RawOrigin::Root, asset_id.clone()); assert!(pallet_asset_rate::ConversionRateToNative::::get(asset_id).is_none()); Ok(()) diff --git a/frame/asset-rate/src/lib.rs b/frame/asset-rate/src/lib.rs index 8c6597a389833..ecc793184094b 100644 --- a/frame/asset-rate/src/lib.rs +++ b/frame/asset-rate/src/lib.rs @@ -161,10 +161,10 @@ pub mod pallet { T::CreateOrigin::ensure_origin(origin)?; ensure!( - !ConversionRateToNative::::contains_key(asset_id), + !ConversionRateToNative::::contains_key(asset_id.clone()), Error::::AlreadyExists ); - ConversionRateToNative::::set(asset_id, Some(rate)); + ConversionRateToNative::::set(asset_id.clone(), Some(rate)); Self::deposit_event(Event::AssetRateCreated { asset_id, rate }); Ok(()) @@ -184,7 +184,7 @@ pub mod pallet { T::UpdateOrigin::ensure_origin(origin)?; let mut old = FixedU128::zero(); - ConversionRateToNative::::mutate(asset_id, |maybe_rate| { + ConversionRateToNative::::mutate(asset_id.clone(), |maybe_rate| { if let Some(r) = maybe_rate { old = *r; *r = rate; @@ -209,10 +209,10 @@ pub mod pallet { T::RemoveOrigin::ensure_origin(origin)?; ensure!( - ConversionRateToNative::::contains_key(asset_id), + ConversionRateToNative::::contains_key(asset_id.clone()), Error::::UnknownAssetId ); - ConversionRateToNative::::remove(asset_id); + ConversionRateToNative::::remove(asset_id.clone()); Self::deposit_event(Event::AssetRateRemoved { asset_id }); Ok(()) diff --git a/frame/assets/src/benchmarking.rs b/frame/assets/src/benchmarking.rs index a2483650715a2..747e072ddde2b 100644 --- a/frame/assets/src/benchmarking.rs +++ b/frame/assets/src/benchmarking.rs @@ -144,7 +144,7 @@ benchmarks_instance_pallet! { let caller = T::CreateOrigin::ensure_origin(origin, &asset_id.into()).unwrap(); let caller_lookup = T::Lookup::unlookup(caller.clone()); T::Currency::make_free_balance_be(&caller, DepositBalanceOf::::max_value()); - }: _(SystemOrigin::Signed(caller.clone()), asset_id, caller_lookup, 1u32.into()) + }: _(SystemOrigin::Signed(caller.clone()), asset_id.clone(), caller_lookup, 1u32.into()) verify { assert_last_event::(Event::Created { asset_id: asset_id.into(), creator: caller.clone(), owner: caller }.into()); } diff --git a/frame/assets/src/extra_mutator.rs b/frame/assets/src/extra_mutator.rs index 96fb765cb0382..2a44df5f0c661 100644 --- a/frame/assets/src/extra_mutator.rs +++ b/frame/assets/src/extra_mutator.rs @@ -62,7 +62,7 @@ impl, I: 'static> ExtraMutator { id: T::AssetId, who: impl sp_std::borrow::Borrow, ) -> Option> { - if let Some(a) = Account::::get(id, who.borrow()) { + if let Some(a) = Account::::get(&id, who.borrow()) { Some(ExtraMutator:: { id, who: who.borrow().clone(), @@ -77,7 +77,7 @@ impl, I: 'static> ExtraMutator { /// Commit any changes to storage. pub fn commit(&mut self) -> Result<(), ()> { if let Some(extra) = self.pending.take() { - Account::::try_mutate(self.id, self.who.borrow(), |maybe_account| { + Account::::try_mutate(&self.id, &self.who, |maybe_account| { maybe_account.as_mut().ok_or(()).map(|account| account.extra = extra) }) } else { @@ -88,7 +88,7 @@ impl, I: 'static> 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(self.id, self.who.borrow(), |maybe_account| { + Account::::try_mutate(&self.id, &self.who, |maybe_account| { maybe_account .as_mut() .ok_or(()) diff --git a/frame/assets/src/functions.rs b/frame/assets/src/functions.rs index 3f32f7b9440a8..7372dcd4105c2 100644 --- a/frame/assets/src/functions.rs +++ b/frame/assets/src/functions.rs @@ -128,7 +128,7 @@ impl, I: 'static> Pallet { amount: T::Balance, increase_supply: bool, ) -> DepositConsequence { - let details = match Asset::::get(id) { + let details = match Asset::::get(&id) { Some(details) => details, None => return DepositConsequence::UnknownAsset, }; @@ -165,7 +165,7 @@ impl, I: 'static> 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, }; @@ -178,7 +178,7 @@ impl, I: 'static> Pallet { if amount.is_zero() { return Success } - let account = match Account::::get(id, who) { + let account = match Account::::get(&id, who) { Some(a) => a, None => return BalanceLow, }; @@ -186,7 +186,7 @@ impl, I: 'static> Pallet { return Frozen } if let Some(rest) = account.balance.checked_sub(&amount) { - if let Some(frozen) = T::Freezer::frozen_balance(id, who) { + if let Some(frozen) = T::Freezer::frozen_balance(id.clone(), who) { match frozen.checked_add(&details.min_balance) { Some(required) if rest < required => return Frozen, None => return Overflow, @@ -219,10 +219,10 @@ impl, I: 'static> Pallet { who: &T::AccountId, keep_alive: bool, ) -> Result { - let details = Asset::::get(id).ok_or(Error::::Unknown)?; + let details = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(details.status == AssetStatus::Live, Error::::AssetNotLive); - let account = Account::::get(id, who).ok_or(Error::::NoAccount)?; + let account = Account::::get(&id, who).ok_or(Error::::NoAccount)?; ensure!(!account.status.is_frozen(), Error::::Frozen); let amount = if let Some(frozen) = T::Freezer::frozen_balance(id, who) { @@ -265,7 +265,7 @@ impl, I: 'static> Pallet { amount: T::Balance, f: DebitFlags, ) -> Result { - let actual = Self::reducible_balance(id, target, f.keep_alive)?.min(amount); + let actual = Self::reducible_balance(id.clone(), 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); @@ -320,7 +320,7 @@ impl, I: 'static> Pallet { depositor: T::AccountId, check_depositor: bool, ) -> DispatchResult { - ensure!(!Account::::contains_key(id, &who), Error::::AlreadyExists); + ensure!(!Account::::contains_key(&id, &who), Error::::AlreadyExists); let deposit = T::AssetAccountDeposit::get(); let mut details = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(details.status == AssetStatus::Live, Error::::AssetNotLive); @@ -332,7 +332,7 @@ impl, I: 'static> Pallet { T::Currency::reserve(&depositor, deposit)?; Asset::::insert(&id, details); Account::::insert( - id, + &id, &who, AssetAccountOf:: { balance: Zero::zero(), @@ -350,7 +350,7 @@ impl, I: 'static> Pallet { pub(super) fn do_refund(id: T::AssetId, who: T::AccountId, allow_burn: bool) -> DispatchResult { use AssetStatus::*; use ExistenceReason::*; - let mut account = Account::::get(id, &who).ok_or(Error::::NoDeposit)?; + let mut account = Account::::get(&id, &who).ok_or(Error::::NoDeposit)?; ensure!(matches!(account.reason, Consumer | DepositHeld(..)), Error::::NoDeposit); let mut details = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(matches!(details.status, Live | Frozen), Error::::IncorrectStatus); @@ -361,7 +361,7 @@ impl, I: 'static> Pallet { } if let Remove = Self::dead_account(&who, &mut details, &account.reason, false) { - Account::::remove(id, &who); + Account::::remove(&id, &who); } else { debug_assert!(false, "refund did not result in dead account?!"); // deposit may have been refunded, need to update `Account` @@ -380,7 +380,7 @@ impl, I: 'static> Pallet { who: &T::AccountId, caller: &T::AccountId, ) -> DispatchResult { - let mut account = Account::::get(id, &who).ok_or(Error::::NoDeposit)?; + let mut account = Account::::get(&id, &who).ok_or(Error::::NoDeposit)?; let (depositor, deposit) = account.reason.take_deposit_from().ok_or(Error::::NoDeposit)?; let mut details = Asset::::get(&id).ok_or(Error::::Unknown)?; @@ -392,11 +392,11 @@ impl, I: 'static> Pallet { T::Currency::unreserve(&depositor, deposit); if let Remove = Self::dead_account(&who, &mut details, &account.reason, false) { - Account::::remove(id, &who); + Account::::remove(&id, &who); } else { debug_assert!(false, "refund did not result in dead account?!"); // deposit may have been refunded, need to update `Account` - Account::::insert(id, &who, account); + Account::::insert(&id, &who, account); return Ok(()) } Asset::::insert(&id, details); @@ -416,7 +416,7 @@ impl, I: 'static> Pallet { amount: T::Balance, maybe_check_issuer: Option, ) -> DispatchResult { - Self::increase_balance(id, beneficiary, amount, |details| -> DispatchResult { + Self::increase_balance(id.clone(), beneficiary, amount, |details| -> DispatchResult { if let Some(check_issuer) = maybe_check_issuer { ensure!(check_issuer == details.issuer, Error::::NoPermission); } @@ -450,19 +450,20 @@ impl, I: 'static> Pallet { return Ok(()) } - Self::can_increase(id, beneficiary, amount, true).into_result()?; - Asset::::try_mutate(id, |maybe_details| -> DispatchResult { + Self::can_increase(id.clone(), 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); check(details)?; - Account::::try_mutate(id, beneficiary, |maybe_account| -> DispatchResult { + Account::::try_mutate(&id, beneficiary, |maybe_account| -> DispatchResult { match maybe_account { Some(ref mut account) => { account.balance.saturating_accrue(amount); }, maybe_account @ None => { - // Note this should never fail as it's already checked by `can_increase`. + // Note this should never fail as it's already checked by + // `can_increase`. ensure!(amount >= details.min_balance, TokenError::BelowMinimum); *maybe_account = Some(AssetAccountOf:: { balance: amount, @@ -493,13 +494,13 @@ impl, I: 'static> Pallet { maybe_check_admin: Option, f: DebitFlags, ) -> Result { - let d = Asset::::get(id).ok_or(Error::::Unknown)?; + let d = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!( d.status == AssetStatus::Live || d.status == AssetStatus::Frozen, Error::::AssetNotLive ); - let actual = Self::decrease_balance(id, target, amount, f, |actual, details| { + let actual = Self::decrease_balance(id.clone(), target, amount, f, |actual, details| { // Check admin rights. if let Some(check_admin) = maybe_check_admin { ensure!(check_admin == details.admin, Error::::NoPermission); @@ -536,17 +537,17 @@ impl, I: 'static> Pallet { return Ok(amount) } - let details = Asset::::get(id).ok_or(Error::::Unknown)?; + let details = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(details.status == AssetStatus::Live, Error::::AssetNotLive); - let actual = Self::prep_debit(id, target, amount, f)?; + let actual = Self::prep_debit(id.clone(), target, amount, f)?; let mut target_died: Option = None; - Asset::::try_mutate(id, |maybe_details| -> DispatchResult { + Asset::::try_mutate(&id, |maybe_details| -> DispatchResult { let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; check(actual, details)?; - Account::::try_mutate(id, target, |maybe_account| -> DispatchResult { + Account::::try_mutate(&id, target, |maybe_account| -> DispatchResult { let mut account = maybe_account.take().ok_or(Error::::NoAccount)?; debug_assert!(account.balance >= actual, "checked in prep; qed"); @@ -590,7 +591,7 @@ impl, I: 'static> Pallet { f: TransferFlags, ) -> Result { let (balance, died) = - Self::transfer_and_die(id, source, dest, amount, maybe_need_admin, f)?; + Self::transfer_and_die(id.clone(), source, dest, amount, maybe_need_admin, f)?; if let Some(Remove) = died { T::Freezer::died(id, source); } @@ -611,18 +612,18 @@ impl, I: 'static> Pallet { if amount.is_zero() { return Ok((amount, None)) } - let details = Asset::::get(id).ok_or(Error::::Unknown)?; + let details = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(details.status == AssetStatus::Live, Error::::AssetNotLive); // Figure out the debit and credit, together with side-effects. - 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 debit = Self::prep_debit(id.clone(), source, amount, f.into())?; + let (credit, maybe_burn) = Self::prep_credit(id.clone(), dest, amount, debit, f.burn_dust)?; let mut source_account = - Account::::get(id, &source).ok_or(Error::::NoAccount)?; + Account::::get(&id, &source).ok_or(Error::::NoAccount)?; let mut source_died: Option = None; - Asset::::try_mutate(id, |maybe_details| -> DispatchResult { + Asset::::try_mutate(&id, |maybe_details| -> DispatchResult { let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; // Check admin rights. @@ -647,7 +648,7 @@ impl, I: 'static> 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, |maybe_account| -> DispatchResult { + Account::::try_mutate(&id, &dest, |maybe_account| -> DispatchResult { match maybe_account { Some(ref mut account) => { // Calculate new balance; this will not saturate since it's already checked @@ -676,11 +677,11 @@ impl, I: 'static> Pallet { source_died = Some(Self::dead_account(source, details, &source_account.reason, false)); if let Some(Remove) = source_died { - Account::::remove(id, &source); + Account::::remove(&id, &source); return Ok(()) } } - Account::::insert(id, &source, &source_account); + Account::::insert(&id, &source, &source_account); Ok(()) })?; @@ -707,11 +708,11 @@ impl, I: 'static> Pallet { is_sufficient: bool, min_balance: T::Balance, ) -> DispatchResult { - ensure!(!Asset::::contains_key(id), Error::::InUse); + ensure!(!Asset::::contains_key(&id), Error::::InUse); ensure!(!min_balance.is_zero(), Error::::MinBalanceZero); Asset::::insert( - id, + &id, AssetDetails { owner: owner.clone(), issuer: owner.clone(), @@ -738,8 +739,8 @@ impl, I: 'static> Pallet { id: T::AssetId, maybe_check_owner: Option, ) -> DispatchResult { - Asset::::try_mutate_exists(id, |maybe_details| -> Result<(), DispatchError> { - let mut details = maybe_details.as_mut().ok_or(Error::::Unknown)?; + Asset::::try_mutate_exists(id.clone(), |maybe_details| -> Result<(), DispatchError> { + let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; if let Some(check_owner) = maybe_check_owner { ensure!(details.owner == check_owner, Error::::NoPermission); } @@ -761,12 +762,12 @@ impl, I: 'static> Pallet { let mut dead_accounts: Vec = vec![]; let mut remaining_accounts = 0; let _ = - Asset::::try_mutate_exists(id, |maybe_details| -> Result<(), DispatchError> { + Asset::::try_mutate_exists(&id, |maybe_details| -> Result<(), DispatchError> { let mut details = maybe_details.as_mut().ok_or(Error::::Unknown)?; // Should only destroy accounts while the asset is in a destroying state ensure!(details.status == AssetStatus::Destroying, Error::::IncorrectStatus); - for (who, v) in Account::::drain_prefix(id) { + for (who, v) in Account::::drain_prefix(&id) { let _ = Self::dead_account(&who, &mut details, &v.reason, true); dead_accounts.push(who); if dead_accounts.len() >= (max_items as usize) { @@ -778,7 +779,7 @@ impl, I: 'static> Pallet { })?; for who in &dead_accounts { - T::Freezer::died(id, &who); + T::Freezer::died(id.clone(), &who); } Self::deposit_event(Event::AccountsDestroyed { @@ -798,14 +799,15 @@ impl, I: 'static> Pallet { max_items: u32, ) -> Result { let mut removed_approvals = 0; - let _ = - Asset::::try_mutate_exists(id, |maybe_details| -> Result<(), DispatchError> { - let mut details = maybe_details.as_mut().ok_or(Error::::Unknown)?; + let _ = Asset::::try_mutate_exists( + id.clone(), + |maybe_details| -> Result<(), DispatchError> { + let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; // Should only destroy accounts while the asset is in a destroying state. ensure!(details.status == AssetStatus::Destroying, Error::::IncorrectStatus); - for ((owner, _), approval) in Approvals::::drain_prefix((id,)) { + for ((owner, _), approval) in Approvals::::drain_prefix((id.clone(),)) { T::Currency::unreserve(&owner, approval.deposit); removed_approvals = removed_approvals.saturating_add(1); details.approvals = details.approvals.saturating_sub(1); @@ -819,7 +821,8 @@ impl, I: 'static> Pallet { approvals_remaining: details.approvals as u32, }); Ok(()) - })?; + }, + )?; Ok(removed_approvals) } @@ -827,7 +830,7 @@ impl, I: 'static> Pallet { /// /// On success, the `Event::Destroyed` event is emitted. pub(super) fn do_finish_destroy(id: T::AssetId) -> DispatchResult { - Asset::::try_mutate_exists(id, |maybe_details| -> Result<(), DispatchError> { + Asset::::try_mutate_exists(id.clone(), |maybe_details| -> Result<(), DispatchError> { let details = maybe_details.take().ok_or(Error::::Unknown)?; ensure!(details.status == AssetStatus::Destroying, Error::::IncorrectStatus); ensure!(details.accounts == 0, Error::::InUse); @@ -855,10 +858,10 @@ impl, I: 'static> Pallet { delegate: &T::AccountId, amount: T::Balance, ) -> DispatchResult { - let mut d = Asset::::get(id).ok_or(Error::::Unknown)?; + let mut d = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(d.status == AssetStatus::Live, Error::::AssetNotLive); Approvals::::try_mutate( - (id, &owner, &delegate), + (id.clone(), &owner, &delegate), |maybe_approved| -> DispatchResult { let mut approved = match maybe_approved.take() { // an approval already exists and is being updated @@ -879,7 +882,7 @@ impl, I: 'static> Pallet { Ok(()) }, )?; - Asset::::insert(id, d); + Asset::::insert(&id, d); Self::deposit_event(Event::ApprovedTransfer { asset_id: id, source: owner.clone(), @@ -906,22 +909,23 @@ impl, I: 'static> Pallet { ) -> DispatchResult { let mut owner_died: Option = None; - let d = Asset::::get(id).ok_or(Error::::Unknown)?; + let d = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(d.status == AssetStatus::Live, Error::::AssetNotLive); Approvals::::try_mutate_exists( - (id, &owner, delegate), + (id.clone(), &owner, delegate), |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, best_effort: false, burn_dust: false }; - owner_died = Self::transfer_and_die(id, owner, destination, amount, None, f)?.1; + owner_died = + Self::transfer_and_die(id.clone(), owner, destination, amount, None, f)?.1; if remaining.is_zero() { T::Currency::unreserve(owner, approved.deposit); - Asset::::mutate(id, |maybe_details| { + Asset::::mutate(id.clone(), |maybe_details| { if let Some(details) = maybe_details { details.approvals.saturating_dec(); } @@ -954,11 +958,11 @@ impl, I: 'static> Pallet { let bounded_symbol: BoundedVec = symbol.clone().try_into().map_err(|_| Error::::BadMetadata)?; - let d = Asset::::get(id).ok_or(Error::::Unknown)?; + let d = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(d.status == AssetStatus::Live, Error::::AssetNotLive); ensure!(from == &d.owner, Error::::NoPermission); - Metadata::::try_mutate_exists(id, |metadata| { + Metadata::::try_mutate_exists(id.clone(), |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); @@ -999,7 +1003,9 @@ impl, I: 'static> Pallet { /// Returns all the non-zero balances for all assets of the given `account`. pub fn account_balances(account: T::AccountId) -> Vec<(T::AssetId, T::Balance)> { Asset::::iter_keys() - .filter_map(|id| Self::maybe_balance(id, account.clone()).map(|balance| (id, balance))) + .filter_map(|id| { + Self::maybe_balance(id.clone(), account.clone()).map(|balance| (id, balance)) + }) .collect::>() } } diff --git a/frame/assets/src/impl_stored_map.rs b/frame/assets/src/impl_stored_map.rs index 5ead708469a94..a7a5a0859f701 100644 --- a/frame/assets/src/impl_stored_map.rs +++ b/frame/assets/src/impl_stored_map.rs @@ -21,7 +21,7 @@ use super::*; 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; + let (id, who) = id_who; Account::::get(id, who).map(|a| a.extra).unwrap_or_default() } @@ -29,7 +29,7 @@ impl, I: 'static> StoredMap<(T::AssetId, T::AccountId), T::Extra> f id_who: &(T::AssetId, T::AccountId), f: impl FnOnce(&mut Option) -> Result, ) -> Result { - let &(id, ref who) = id_who; + let (id, who) = id_who; let mut maybe_extra = Account::::get(id, who).map(|a| a.extra); let r = f(&mut maybe_extra)?; // They want to write some value or delete it. diff --git a/frame/assets/src/lib.rs b/frame/assets/src/lib.rs index e9259f4b670ad..4b91f5184299d 100644 --- a/frame/assets/src/lib.rs +++ b/frame/assets/src/lib.rs @@ -162,7 +162,7 @@ use sp_runtime::{ traits::{AtLeast32BitUnsigned, CheckedAdd, CheckedSub, Saturating, StaticLookup, Zero}, ArithmeticError, TokenError, }; -use sp_std::{borrow::Borrow, prelude::*}; +use sp_std::prelude::*; use frame_support::{ dispatch::{DispatchError, DispatchResult}, @@ -250,7 +250,7 @@ pub mod pallet { type RemoveItemsLimit: Get; /// Identifier for the class of asset. - type AssetId: Member + Parameter + Copy + MaybeSerializeDeserialize + MaxEncodedLen; + type AssetId: Member + Parameter + Clone + MaybeSerializeDeserialize + MaxEncodedLen; /// Wrapper around `Self::AssetId` to use in dispatchable call signatures. Allows the use /// of compact encoding in instances of the pallet, which will prevent breaking changes @@ -424,7 +424,7 @@ pub mod pallet { for (id, account_id, amount) in &self.accounts { let result = >::increase_balance( - *id, + id.clone(), account_id, *amount, |details| -> DispatchResult { @@ -605,14 +605,14 @@ pub mod pallet { let owner = T::CreateOrigin::ensure_origin(origin, &id)?; let admin = T::Lookup::lookup(admin)?; - ensure!(!Asset::::contains_key(id), Error::::InUse); + 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, + id.clone(), AssetDetails { owner: owner.clone(), issuer: admin.clone(), @@ -937,7 +937,7 @@ pub mod pallet { let origin = ensure_signed(origin)?; let id: T::AssetId = id.into(); - let d = Asset::::get(id).ok_or(Error::::Unknown)?; + let d = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!( d.status == AssetStatus::Live || d.status == AssetStatus::Frozen, Error::::AssetNotLive @@ -945,7 +945,7 @@ pub mod pallet { ensure!(origin == d.freezer, Error::::NoPermission); let who = T::Lookup::lookup(who)?; - Account::::try_mutate(id, &who, |maybe_account| -> DispatchResult { + Account::::try_mutate(&id, &who, |maybe_account| -> DispatchResult { maybe_account.as_mut().ok_or(Error::::NoAccount)?.status = AccountStatus::Frozen; Ok(()) @@ -974,7 +974,7 @@ pub mod pallet { let origin = ensure_signed(origin)?; let id: T::AssetId = id.into(); - let details = Asset::::get(id).ok_or(Error::::Unknown)?; + let details = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!( details.status == AssetStatus::Live || details.status == AssetStatus::Frozen, Error::::AssetNotLive @@ -982,7 +982,7 @@ pub mod pallet { ensure!(origin == details.admin, Error::::NoPermission); let who = T::Lookup::lookup(who)?; - Account::::try_mutate(id, &who, |maybe_account| -> DispatchResult { + Account::::try_mutate(&id, &who, |maybe_account| -> DispatchResult { maybe_account.as_mut().ok_or(Error::::NoAccount)?.status = AccountStatus::Liquid; Ok(()) @@ -1006,7 +1006,7 @@ pub mod pallet { let origin = ensure_signed(origin)?; let id: T::AssetId = id.into(); - Asset::::try_mutate(id, |maybe_details| { + Asset::::try_mutate(id.clone(), |maybe_details| { let d = maybe_details.as_mut().ok_or(Error::::Unknown)?; ensure!(d.status == AssetStatus::Live, Error::::AssetNotLive); ensure!(origin == d.freezer, Error::::NoPermission); @@ -1032,7 +1032,7 @@ pub mod pallet { let origin = ensure_signed(origin)?; let id: T::AssetId = id.into(); - Asset::::try_mutate(id, |maybe_details| { + Asset::::try_mutate(id.clone(), |maybe_details| { let d = maybe_details.as_mut().ok_or(Error::::Unknown)?; ensure!(origin == d.admin, Error::::NoPermission); ensure!(d.status == AssetStatus::Frozen, Error::::NotFrozen); @@ -1064,7 +1064,7 @@ pub mod pallet { let owner = T::Lookup::lookup(owner)?; let id: T::AssetId = id.into(); - Asset::::try_mutate(id, |maybe_details| { + Asset::::try_mutate(id.clone(), |maybe_details| { let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; ensure!(details.status == AssetStatus::Live, Error::::LiveAsset); ensure!(origin == details.owner, Error::::NoPermission); @@ -1072,7 +1072,7 @@ pub mod pallet { 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. @@ -1111,7 +1111,7 @@ pub mod pallet { let freezer = T::Lookup::lookup(freezer)?; let id: T::AssetId = id.into(); - Asset::::try_mutate(id, |maybe_details| { + Asset::::try_mutate(id.clone(), |maybe_details| { let details = maybe_details.as_mut().ok_or(Error::::Unknown)?; ensure!(details.status == AssetStatus::Live, Error::::AssetNotLive); ensure!(origin == details.owner, Error::::NoPermission); @@ -1171,11 +1171,11 @@ pub mod pallet { let origin = ensure_signed(origin)?; let id: T::AssetId = id.into(); - let d = Asset::::get(id).ok_or(Error::::Unknown)?; + let d = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(d.status == AssetStatus::Live, Error::::AssetNotLive); ensure!(origin == d.owner, Error::::NoPermission); - Metadata::::try_mutate_exists(id, |metadata| { + Metadata::::try_mutate_exists(id.clone(), |metadata| { let deposit = metadata.take().ok_or(Error::::Unknown)?.deposit; T::Currency::unreserve(&d.owner, deposit); Self::deposit_event(Event::MetadataCleared { asset_id: id }); @@ -1216,8 +1216,8 @@ pub mod pallet { let bounded_symbol: BoundedVec = symbol.clone().try_into().map_err(|_| 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.clone(), |metadata| { let deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit); *metadata = Some(AssetMetadata { deposit, @@ -1257,8 +1257,8 @@ pub mod pallet { T::ForceOrigin::ensure_origin(origin)?; let id: T::AssetId = id.into(); - let d = Asset::::get(id).ok_or(Error::::Unknown)?; - Metadata::::try_mutate_exists(id, |metadata| { + let d = Asset::::get(&id).ok_or(Error::::Unknown)?; + Metadata::::try_mutate_exists(id.clone(), |metadata| { let deposit = metadata.take().ok_or(Error::::Unknown)?.deposit; T::Currency::unreserve(&d.owner, deposit); Self::deposit_event(Event::MetadataCleared { asset_id: id }); @@ -1303,7 +1303,7 @@ pub mod pallet { T::ForceOrigin::ensure_origin(origin)?; let id: T::AssetId = id.into(); - Asset::::try_mutate(id, |maybe_asset| { + Asset::::try_mutate(id.clone(), |maybe_asset| { let mut asset = maybe_asset.take().ok_or(Error::::Unknown)?; ensure!(asset.status != AssetStatus::Destroying, Error::::AssetNotLive); asset.owner = T::Lookup::lookup(owner)?; @@ -1379,15 +1379,15 @@ pub mod pallet { let owner = ensure_signed(origin)?; let delegate = T::Lookup::lookup(delegate)?; let id: T::AssetId = id.into(); - let mut d = Asset::::get(id).ok_or(Error::::Unknown)?; + let mut d = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(d.status == AssetStatus::Live, Error::::AssetNotLive); - let approval = - Approvals::::take((id, &owner, &delegate)).ok_or(Error::::Unknown)?; + let approval = Approvals::::take((id.clone(), &owner, &delegate)) + .ok_or(Error::::Unknown)?; T::Currency::unreserve(&owner, approval.deposit); d.approvals.saturating_dec(); - Asset::::insert(id, d); + Asset::::insert(id.clone(), d); Self::deposit_event(Event::ApprovalCancelled { asset_id: id, owner, delegate }); Ok(()) @@ -1414,7 +1414,7 @@ pub mod pallet { delegate: AccountIdLookupOf, ) -> DispatchResult { let id: T::AssetId = id.into(); - let mut d = Asset::::get(id).ok_or(Error::::Unknown)?; + let mut d = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(d.status == AssetStatus::Live, Error::::AssetNotLive); T::ForceOrigin::try_origin(origin) .map(|_| ()) @@ -1427,11 +1427,11 @@ pub mod pallet { let owner = T::Lookup::lookup(owner)?; let delegate = T::Lookup::lookup(delegate)?; - let approval = - Approvals::::take((id, &owner, &delegate)).ok_or(Error::::Unknown)?; + let approval = Approvals::::take((id.clone(), &owner, &delegate)) + .ok_or(Error::::Unknown)?; T::Currency::unreserve(&owner, approval.deposit); d.approvals.saturating_dec(); - Asset::::insert(id, d); + Asset::::insert(id.clone(), d); Self::deposit_event(Event::ApprovalCancelled { asset_id: id, owner, delegate }); Ok(()) @@ -1529,7 +1529,7 @@ pub mod pallet { let origin = ensure_signed(origin)?; let id: T::AssetId = id.into(); - let mut details = Asset::::get(id).ok_or(Error::::Unknown)?; + let mut details = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!(origin == details.owner, Error::::NoPermission); let old_min_balance = details.min_balance; @@ -1619,7 +1619,7 @@ pub mod pallet { let origin = ensure_signed(origin)?; let id: T::AssetId = id.into(); - let d = Asset::::get(id).ok_or(Error::::Unknown)?; + let d = Asset::::get(&id).ok_or(Error::::Unknown)?; ensure!( d.status == AssetStatus::Live || d.status == AssetStatus::Frozen, Error::::AssetNotLive @@ -1627,7 +1627,7 @@ pub mod pallet { ensure!(origin == d.freezer, Error::::NoPermission); let who = T::Lookup::lookup(who)?; - Account::::try_mutate(id, &who, |maybe_account| -> DispatchResult { + Account::::try_mutate(&id, &who, |maybe_account| -> DispatchResult { maybe_account.as_mut().ok_or(Error::::NoAccount)?.status = AccountStatus::Blocked; Ok(()) diff --git a/frame/nft-fractionalization/src/benchmarking.rs b/frame/nft-fractionalization/src/benchmarking.rs index a04e8de12ffe8..1600ae78c4bdf 100644 --- a/frame/nft-fractionalization/src/benchmarking.rs +++ b/frame/nft-fractionalization/src/benchmarking.rs @@ -93,7 +93,7 @@ benchmarks! { let collection = T::BenchmarkHelper::collection(0); let nft = T::BenchmarkHelper::nft(0); let (caller, caller_lookup) = mint_nft::(nft); - }: _(SystemOrigin::Signed(caller.clone()), collection, nft, asset, caller_lookup, 1000u32.into()) + }: _(SystemOrigin::Signed(caller.clone()), collection, nft, asset.clone(), caller_lookup, 1000u32.into()) verify { assert_last_event::( Event::NftFractionalized { @@ -115,11 +115,11 @@ benchmarks! { SystemOrigin::Signed(caller.clone()).into(), collection, nft, - asset, + asset.clone(), caller_lookup.clone(), 1000u32.into(), )?; - }: _(SystemOrigin::Signed(caller.clone()), collection, nft, asset, caller_lookup) + }: _(SystemOrigin::Signed(caller.clone()), collection, nft, asset.clone(), caller_lookup) verify { assert_last_event::( Event::NftUnified { diff --git a/frame/nft-fractionalization/src/lib.rs b/frame/nft-fractionalization/src/lib.rs index c61719c5c707a..e1c8b8fea8907 100644 --- a/frame/nft-fractionalization/src/lib.rs +++ b/frame/nft-fractionalization/src/lib.rs @@ -241,13 +241,19 @@ pub mod pallet { let deposit = T::Deposit::get(); T::Currency::hold(&T::HoldReason::get(), &nft_owner, deposit)?; Self::do_lock_nft(nft_collection_id, nft_id)?; - Self::do_create_asset(asset_id, pallet_account.clone())?; - Self::do_mint_asset(asset_id, &beneficiary, fractions)?; - Self::do_set_metadata(asset_id, &who, &pallet_account, &nft_collection_id, &nft_id)?; + Self::do_create_asset(asset_id.clone(), pallet_account.clone())?; + Self::do_mint_asset(asset_id.clone(), &beneficiary, fractions)?; + Self::do_set_metadata( + asset_id.clone(), + &who, + &pallet_account, + &nft_collection_id, + &nft_id, + )?; NftToAsset::::insert( (nft_collection_id, nft_id), - Details { asset: asset_id, fractions, asset_creator: nft_owner, deposit }, + Details { asset: asset_id.clone(), fractions, asset_creator: nft_owner, deposit }, ); Self::deposit_event(Event::NftFractionalized { @@ -295,7 +301,7 @@ pub mod pallet { let deposit = details.deposit; let asset_creator = details.asset_creator; - Self::do_burn_asset(asset_id, &who, details.fractions)?; + Self::do_burn_asset(asset_id.clone(), &who, details.fractions)?; Self::do_unlock_nft(nft_collection_id, nft_id, &beneficiary)?; T::Currency::release(&T::HoldReason::get(), &asset_creator, deposit, BestEffort)?; @@ -356,7 +362,7 @@ pub mod pallet { account: &T::AccountId, amount: AssetBalanceOf, ) -> DispatchResult { - T::Assets::burn_from(asset_id, account, amount, Exact, Polite)?; + T::Assets::burn_from(asset_id.clone(), account, amount, Exact, Polite)?; T::Assets::start_destroy(asset_id, None) } diff --git a/frame/support/src/traits/tokens/fungibles/hold.rs b/frame/support/src/traits/tokens/fungibles/hold.rs index 68580ebff4bce..8fc038c57be3d 100644 --- a/frame/support/src/traits/tokens/fungibles/hold.rs +++ b/frame/support/src/traits/tokens/fungibles/hold.rs @@ -98,7 +98,7 @@ pub trait Inspect: super::Inspect { who: &AccountId, amount: Self::Balance, ) -> DispatchResult { - ensure!(Self::hold_available(asset, reason, who), TokenError::CannotCreateHold); + ensure!(Self::hold_available(asset.clone(), reason, who), TokenError::CannotCreateHold); ensure!( amount <= Self::reducible_balance(asset, who, Protect, Force), TokenError::FundsUnavailable @@ -173,7 +173,7 @@ pub trait Unbalanced: Inspect { mut amount: Self::Balance, precision: Precision, ) -> Result { - let old_balance = Self::balance_on_hold(asset, reason, who); + let old_balance = Self::balance_on_hold(asset.clone(), reason, who); if let BestEffort = precision { amount = amount.min(old_balance); } @@ -193,7 +193,7 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, precision: Precision, ) -> Result { - let old_balance = Self::balance_on_hold(asset, reason, who); + let old_balance = Self::balance_on_hold(asset.clone(), reason, who); let new_balance = if let BestEffort = precision { old_balance.saturating_add(amount) } else { @@ -221,11 +221,13 @@ pub trait Balanced: super::Balanced + Unbalanced (Credit, Self::Balance) { - let decrease = Self::decrease_balance_on_hold(asset, reason, who, amount, BestEffort) - .unwrap_or(Default::default()); + let decrease = + Self::decrease_balance_on_hold(asset.clone(), reason, who, amount, BestEffort) + .unwrap_or(Default::default()); let credit = Imbalance::::new( - asset, decrease, + asset.clone(), + decrease, ); Self::done_slash(asset, reason, who, decrease); (credit, amount.saturating_sub(decrease)) @@ -255,10 +257,10 @@ pub trait Mutate: // NOTE: This doesn't change the total balance of the account so there's no need to // check liquidity. - Self::ensure_can_hold(asset, reason, who, amount)?; + Self::ensure_can_hold(asset.clone(), reason, who, amount)?; // Should be infallible now, but we proceed softly anyway. - Self::decrease_balance(asset, who, amount, Exact, Protect, Force)?; - Self::increase_balance_on_hold(asset, reason, who, amount, BestEffort)?; + Self::decrease_balance(asset.clone(), who, amount, Exact, Protect, Force)?; + Self::increase_balance_on_hold(asset.clone(), reason, who, amount, BestEffort)?; Self::done_hold(asset, reason, who, amount); Ok(()) } @@ -281,13 +283,16 @@ pub trait Mutate: // We want to make sure we can deposit the amount in advance. If we can't then something is // very wrong. - ensure!(Self::can_deposit(asset, who, amount, Extant) == Success, TokenError::CannotCreate); + ensure!( + Self::can_deposit(asset.clone(), who, amount, Extant) == Success, + TokenError::CannotCreate + ); // Get the amount we can actually take from the hold. This might be less than what we want // if we're only doing a best-effort. - let amount = Self::decrease_balance_on_hold(asset, reason, who, amount, precision)?; + let amount = Self::decrease_balance_on_hold(asset.clone(), reason, who, amount, precision)?; // Increase the main balance by what we took. We always do a best-effort here because we // already checked that we can deposit before. - let actual = Self::increase_balance(asset, who, amount, BestEffort)?; + let actual = Self::increase_balance(asset.clone(), who, amount, BestEffort)?; Self::done_release(asset, reason, who, actual); Ok(actual) } @@ -310,14 +315,17 @@ pub trait Mutate: force: Fortitude, ) -> Result { // We must check total-balance requirements if `!force`. - let liquid = Self::reducible_total_balance_on_hold(asset, who, force); + let liquid = Self::reducible_total_balance_on_hold(asset.clone(), who, force); if let BestEffort = precision { amount = amount.min(liquid); } else { ensure!(amount <= liquid, TokenError::Frozen); } - let amount = Self::decrease_balance_on_hold(asset, reason, who, amount, precision)?; - Self::set_total_issuance(asset, Self::total_issuance(asset).saturating_sub(amount)); + let amount = Self::decrease_balance_on_hold(asset.clone(), reason, who, amount, precision)?; + Self::set_total_issuance( + asset.clone(), + Self::total_issuance(asset.clone()).saturating_sub(amount), + ); Self::done_burn_held(asset, reason, who, amount); Ok(amount) } @@ -348,8 +356,8 @@ pub trait Mutate: force: Fortitude, ) -> Result { // We must check total-balance requirements if `!force`. - let have = Self::balance_on_hold(asset, reason, source); - let liquid = Self::reducible_total_balance_on_hold(asset, source, force); + let have = Self::balance_on_hold(asset.clone(), reason, source); + let liquid = Self::reducible_total_balance_on_hold(asset.clone(), source, force); if let BestEffort = precision { amount = amount.min(liquid).min(have); } else { @@ -360,19 +368,20 @@ pub trait Mutate: // We want to make sure we can deposit the amount in advance. If we can't then something is // very wrong. ensure!( - Self::can_deposit(asset, dest, amount, Extant) == Success, + Self::can_deposit(asset.clone(), dest, amount, Extant) == Success, TokenError::CannotCreate ); ensure!( - mode == Free || Self::hold_available(asset, reason, dest), + mode == Free || Self::hold_available(asset.clone(), reason, dest), TokenError::CannotCreateHold ); - let amount = Self::decrease_balance_on_hold(asset, reason, source, amount, precision)?; + let amount = + Self::decrease_balance_on_hold(asset.clone(), reason, source, amount, precision)?; let actual = if mode == OnHold { - Self::increase_balance_on_hold(asset, reason, dest, amount, precision)? + Self::increase_balance_on_hold(asset.clone(), reason, dest, amount, precision)? } else { - Self::increase_balance(asset, dest, amount, precision)? + Self::increase_balance(asset.clone(), dest, amount, precision)? }; Self::done_transfer_on_hold(asset, reason, source, dest, actual); Ok(actual) @@ -405,14 +414,14 @@ pub trait Mutate: expendability: Preservation, force: Fortitude, ) -> Result { - ensure!(Self::hold_available(asset, reason, dest), TokenError::CannotCreateHold); + ensure!(Self::hold_available(asset.clone(), reason, dest), TokenError::CannotCreateHold); ensure!( - Self::can_deposit(asset, dest, amount, Extant) == Success, + Self::can_deposit(asset.clone(), dest, amount, Extant) == Success, TokenError::CannotCreate ); let actual = - Self::decrease_balance(asset, source, amount, precision, expendability, force)?; - Self::increase_balance_on_hold(asset, reason, dest, actual, precision)?; + Self::decrease_balance(asset.clone(), source, amount, precision, expendability, force)?; + Self::increase_balance_on_hold(asset.clone(), reason, dest, actual, precision)?; Self::done_transfer_on_hold(asset, reason, source, dest, actual); Ok(actual) } diff --git a/frame/support/src/traits/tokens/fungibles/imbalance.rs b/frame/support/src/traits/tokens/fungibles/imbalance.rs index ab18eec3811ff..1668268ea2dcf 100644 --- a/frame/support/src/traits/tokens/fungibles/imbalance.rs +++ b/frame/support/src/traits/tokens/fungibles/imbalance.rs @@ -59,7 +59,7 @@ impl< { fn drop(&mut self) { if !self.amount.is_zero() { - OnDrop::handle(self.asset, self.amount) + OnDrop::handle(self.asset.clone(), self.amount) } } } @@ -104,9 +104,9 @@ impl< pub fn split(self, amount: B) -> (Self, Self) { let first = self.amount.min(amount); let second = self.amount - first; - let asset = self.asset; + let asset = self.asset.clone(); sp_std::mem::forget(self); - (Imbalance::new(asset, first), Imbalance::new(asset, second)) + (Imbalance::new(asset.clone(), first), Imbalance::new(asset, second)) } pub fn merge(mut self, other: Self) -> Result { if self.asset == other.asset { @@ -135,7 +135,7 @@ impl< > { if self.asset == other.asset { let (a, b) = (self.amount, other.amount); - let asset = self.asset; + let asset = self.asset.clone(); sp_std::mem::forget((self, other)); if a == b { @@ -154,7 +154,7 @@ impl< } pub fn asset(&self) -> A { - self.asset + self.asset.clone() } } diff --git a/frame/support/src/traits/tokens/fungibles/regular.rs b/frame/support/src/traits/tokens/fungibles/regular.rs index 27d1a50b34805..5a9d3e6e4b56e 100644 --- a/frame/support/src/traits/tokens/fungibles/regular.rs +++ b/frame/support/src/traits/tokens/fungibles/regular.rs @@ -146,7 +146,7 @@ pub trait Unbalanced: Inspect { /// This should not be reimplemented. fn handle_raw_dust(asset: Self::AssetId, amount: Self::Balance) { Self::handle_dust(Dust( - asset, + asset.clone(), amount.min(Self::minimum_balance(asset).saturating_sub(One::one())), )) } @@ -193,13 +193,13 @@ pub trait Unbalanced: Inspect { preservation: Preservation, force: Fortitude, ) -> Result { - let old_balance = Self::balance(asset, who); - let free = Self::reducible_balance(asset, who, preservation, force); + let old_balance = Self::balance(asset.clone(), who); + let free = Self::reducible_balance(asset.clone(), who, preservation, force); if let BestEffort = precision { amount = amount.min(free); } let new_balance = old_balance.checked_sub(&amount).ok_or(TokenError::FundsUnavailable)?; - if let Some(dust) = Self::write_balance(asset, who, new_balance)? { + if let Some(dust) = Self::write_balance(asset.clone(), who, new_balance)? { Self::handle_dust(Dust(asset, dust)); } Ok(old_balance.saturating_sub(new_balance)) @@ -217,13 +217,13 @@ pub trait Unbalanced: Inspect { amount: Self::Balance, precision: Precision, ) -> Result { - let old_balance = Self::balance(asset, who); + let old_balance = Self::balance(asset.clone(), who); let new_balance = if let BestEffort = precision { old_balance.saturating_add(amount) } else { old_balance.checked_add(&amount).ok_or(ArithmeticError::Overflow)? }; - if new_balance < Self::minimum_balance(asset) { + if new_balance < Self::minimum_balance(asset.clone()) { // Attempt to increase from 0 to below minimum -> stays at zero. if let BestEffort = precision { Ok(Self::Balance::default()) @@ -234,7 +234,7 @@ pub trait Unbalanced: Inspect { if new_balance == old_balance { Ok(Self::Balance::default()) } else { - if let Some(dust) = Self::write_balance(asset, who, new_balance)? { + if let Some(dust) = Self::write_balance(asset.clone(), who, new_balance)? { Self::handle_dust(Dust(asset, dust)); } Ok(new_balance.saturating_sub(old_balance)) @@ -258,11 +258,14 @@ pub trait Mutate: Inspect + Unbalanced { who: &AccountId, amount: Self::Balance, ) -> Result { - Self::total_issuance(asset) + Self::total_issuance(asset.clone()) .checked_add(&amount) .ok_or(ArithmeticError::Overflow)?; - let actual = Self::increase_balance(asset, who, amount, Exact)?; - Self::set_total_issuance(asset, Self::total_issuance(asset).saturating_add(actual)); + let actual = Self::increase_balance(asset.clone(), who, amount, Exact)?; + Self::set_total_issuance( + asset.clone(), + Self::total_issuance(asset.clone()).saturating_add(actual), + ); Self::done_mint_into(asset, who, amount); Ok(actual) } @@ -277,13 +280,17 @@ pub trait Mutate: Inspect + Unbalanced { precision: Precision, force: Fortitude, ) -> Result { - let actual = Self::reducible_balance(asset, who, Expendable, force).min(amount); + let actual = Self::reducible_balance(asset.clone(), who, Expendable, force).min(amount); ensure!(actual == amount || precision == BestEffort, TokenError::FundsUnavailable); - Self::total_issuance(asset) + Self::total_issuance(asset.clone()) .checked_sub(&actual) .ok_or(ArithmeticError::Overflow)?; - let actual = Self::decrease_balance(asset, who, actual, BestEffort, Expendable, force)?; - Self::set_total_issuance(asset, Self::total_issuance(asset).saturating_sub(actual)); + let actual = + Self::decrease_balance(asset.clone(), who, actual, BestEffort, Expendable, force)?; + Self::set_total_issuance( + asset.clone(), + Self::total_issuance(asset.clone()).saturating_sub(actual), + ); Self::done_burn_from(asset, who, actual); Ok(actual) } @@ -303,13 +310,17 @@ pub trait Mutate: Inspect + Unbalanced { who: &AccountId, amount: Self::Balance, ) -> Result { - let actual = Self::reducible_balance(asset, who, Expendable, Polite).min(amount); + let actual = Self::reducible_balance(asset.clone(), who, Expendable, Polite).min(amount); ensure!(actual == amount, TokenError::FundsUnavailable); - Self::total_issuance(asset) + Self::total_issuance(asset.clone()) .checked_sub(&actual) .ok_or(ArithmeticError::Overflow)?; - let actual = Self::decrease_balance(asset, who, actual, BestEffort, Expendable, Polite)?; - Self::set_total_issuance(asset, Self::total_issuance(asset).saturating_sub(actual)); + let actual = + Self::decrease_balance(asset.clone(), who, actual, BestEffort, Expendable, Polite)?; + Self::set_total_issuance( + asset.clone(), + Self::total_issuance(asset.clone()).saturating_sub(actual), + ); Self::done_shelve(asset, who, actual); Ok(actual) } @@ -329,11 +340,14 @@ pub trait Mutate: Inspect + Unbalanced { who: &AccountId, amount: Self::Balance, ) -> Result { - Self::total_issuance(asset) + Self::total_issuance(asset.clone()) .checked_add(&amount) .ok_or(ArithmeticError::Overflow)?; - let actual = Self::increase_balance(asset, who, amount, Exact)?; - Self::set_total_issuance(asset, Self::total_issuance(asset).saturating_add(actual)); + let actual = Self::increase_balance(asset.clone(), who, amount, Exact)?; + Self::set_total_issuance( + asset.clone(), + Self::total_issuance(asset.clone()).saturating_add(actual), + ); Self::done_restore(asset, who, amount); Ok(actual) } @@ -346,13 +360,13 @@ pub trait Mutate: Inspect + Unbalanced { amount: Self::Balance, preservation: Preservation, ) -> Result { - let _extra = - Self::can_withdraw(asset, source, amount).into_result(preservation != Expendable)?; - Self::can_deposit(asset, dest, amount, Extant).into_result()?; - Self::decrease_balance(asset, source, amount, BestEffort, preservation, Polite)?; + let _extra = Self::can_withdraw(asset.clone(), source, amount) + .into_result(preservation != Expendable)?; + Self::can_deposit(asset.clone(), dest, amount, Extant).into_result()?; + Self::decrease_balance(asset.clone(), source, amount, BestEffort, preservation, Polite)?; // This should never fail as we checked `can_deposit` earlier. But we do a best-effort // anyway. - let _ = Self::increase_balance(asset, dest, amount, BestEffort); + let _ = Self::increase_balance(asset.clone(), dest, amount, BestEffort); Self::done_transfer(asset, source, dest, amount); Ok(amount) } @@ -363,7 +377,7 @@ pub trait Mutate: Inspect + Unbalanced { /// /// Returns the new balance. fn set_balance(asset: Self::AssetId, who: &AccountId, amount: Self::Balance) -> Self::Balance { - let b = Self::balance(asset, who); + let b = Self::balance(asset.clone(), who); if b > amount { Self::burn_from(asset, who, b - amount, BestEffort, Force).map(|d| b.saturating_sub(d)) } else { @@ -391,7 +405,7 @@ impl> HandleImbalanceDrop { fn handle(asset: U::AssetId, amount: U::Balance) { - U::set_total_issuance(asset, U::total_issuance(asset).saturating_add(amount)) + U::set_total_issuance(asset.clone(), U::total_issuance(asset).saturating_add(amount)) } } @@ -402,7 +416,7 @@ impl> HandleImbalanceDrop { fn handle(asset: U::AssetId, amount: U::Balance) { - U::set_total_issuance(asset, U::total_issuance(asset).saturating_sub(amount)) + U::set_total_issuance(asset.clone(), U::total_issuance(asset).saturating_sub(amount)) } } @@ -423,11 +437,11 @@ pub trait Balanced: Inspect + Unbalanced { /// This is infallible, but doesn't guarantee that the entire `amount` is burnt, for example /// in the case of underflow. fn rescind(asset: Self::AssetId, amount: Self::Balance) -> Debt { - let old = Self::total_issuance(asset); + let old = Self::total_issuance(asset.clone()); let new = old.saturating_sub(amount); - Self::set_total_issuance(asset, new); + Self::set_total_issuance(asset.clone(), new); let delta = old - new; - Self::done_rescind(asset, delta); + Self::done_rescind(asset.clone(), delta); Imbalance::::new( asset, delta, ) @@ -440,11 +454,11 @@ pub trait Balanced: Inspect + Unbalanced { /// This is infallible, but doesn't guarantee that the entire `amount` is issued, for example /// in the case of overflow. fn issue(asset: Self::AssetId, amount: Self::Balance) -> Credit { - let old = Self::total_issuance(asset); + let old = Self::total_issuance(asset.clone()); let new = old.saturating_add(amount); - Self::set_total_issuance(asset, new); + Self::set_total_issuance(asset.clone(), new); let delta = new - old; - Self::done_issue(asset, delta); + Self::done_issue(asset.clone(), delta); Imbalance::::new( asset, delta, ) @@ -458,7 +472,7 @@ pub trait Balanced: Inspect + Unbalanced { asset: Self::AssetId, amount: Self::Balance, ) -> (Debt, Credit) { - (Self::rescind(asset, amount), Self::issue(asset, amount)) + (Self::rescind(asset.clone(), amount), Self::issue(asset, amount)) } /// Mints `value` into the account of `who`, creating it as needed. @@ -476,8 +490,8 @@ pub trait Balanced: Inspect + Unbalanced { value: Self::Balance, precision: Precision, ) -> Result, DispatchError> { - let increase = Self::increase_balance(asset, who, value, precision)?; - Self::done_deposit(asset, who, increase); + let increase = Self::increase_balance(asset.clone(), who, value, precision)?; + Self::done_deposit(asset.clone(), who, increase); Ok(Imbalance::::new( asset, increase, )) @@ -504,8 +518,9 @@ pub trait Balanced: Inspect + Unbalanced { preservation: Preservation, force: Fortitude, ) -> Result, DispatchError> { - let decrease = Self::decrease_balance(asset, who, value, precision, preservation, force)?; - Self::done_withdraw(asset, who, decrease); + let decrease = + Self::decrease_balance(asset.clone(), who, value, precision, preservation, force)?; + Self::done_withdraw(asset.clone(), who, decrease); Ok(Imbalance::::new( asset, decrease, )) @@ -545,7 +560,7 @@ pub trait Balanced: Inspect + Unbalanced { ) -> Result, Debt> { let amount = debt.peek(); let asset = debt.asset(); - let credit = match Self::withdraw(asset, who, amount, Exact, preservation, Polite) { + let credit = match Self::withdraw(asset.clone(), who, amount, Exact, preservation, Polite) { Err(_) => return Err(debt), Ok(d) => d, }; diff --git a/frame/support/src/traits/tokens/misc.rs b/frame/support/src/traits/tokens/misc.rs index d1b17b548fe56..baf3fd5f35464 100644 --- a/frame/support/src/traits/tokens/misc.rs +++ b/frame/support/src/traits/tokens/misc.rs @@ -221,10 +221,10 @@ impl WithdrawReasons { /// Simple amalgamation trait to collect together properties for an AssetId under one roof. pub trait AssetId: - FullCodec + Copy + Eq + PartialEq + Debug + scale_info::TypeInfo + MaxEncodedLen + FullCodec + Clone + Eq + PartialEq + Debug + scale_info::TypeInfo + MaxEncodedLen { } -impl AssetId +impl AssetId for T { }