Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Relax Copy to Clone for AssetId #12731

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions frame/assets/src/extra_mutator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<T: Config<I>, I: 'static> ExtraMutator<T, I> {
id: T::AssetId,
who: impl sp_std::borrow::Borrow<T::AccountId>,
) -> Option<ExtraMutator<T, I>> {
if let Some(a) = Account::<T, I>::get(id, who.borrow()) {
if let Some(a) = Account::<T, I>::get(&id, who.borrow()) {
Some(ExtraMutator::<T, I> {
id,
who: who.borrow().clone(),
Expand All @@ -77,7 +77,7 @@ impl<T: Config<I>, I: 'static> ExtraMutator<T, I> {
/// Commit any changes to storage.
pub fn commit(&mut self) -> Result<(), ()> {
if let Some(extra) = self.pending.take() {
Account::<T, I>::try_mutate(self.id, self.who.borrow(), |maybe_account| {
Account::<T, I>::try_mutate(&self.id, self.who.borrow(), |maybe_account| {
maybe_account.as_mut().ok_or(()).map(|account| account.extra = extra)
})
} else {
Expand All @@ -88,7 +88,7 @@ impl<T: Config<I>, I: 'static> ExtraMutator<T, I> {
/// Revert any changes, even those already committed by `self` and drop self.
pub fn revert(mut self) -> Result<(), ()> {
self.pending = None;
Account::<T, I>::try_mutate(self.id, self.who.borrow(), |maybe_account| {
Account::<T, I>::try_mutate(&self.id, self.who.borrow(), |maybe_account| {
maybe_account
.as_mut()
.ok_or(())
Expand Down
148 changes: 79 additions & 69 deletions frame/assets/src/functions.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions frame/assets/src/impl_stored_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ use super::*;

impl<T: Config<I>, I: 'static> StoredMap<(T::AssetId, T::AccountId), T::Extra> for Pallet<T, I> {
fn get(id_who: &(T::AssetId, T::AccountId)) -> T::Extra {
let &(id, ref who) = id_who;
let &(ref id, ref who) = id_who;
Account::<T, I>::get(id, who).map(|a| a.extra).unwrap_or_default()
}

fn try_mutate_exists<R, E: From<DispatchError>>(
id_who: &(T::AssetId, T::AccountId),
f: impl FnOnce(&mut Option<T::Extra>) -> Result<R, E>,
) -> Result<R, E> {
let &(id, ref who) = id_who;
let &(ref id, ref who) = id_who;
let mut maybe_extra = Account::<T, I>::get(id, who).map(|a| a.extra);
let r = f(&mut maybe_extra)?;
// They want to write some value or delete it.
Expand Down
56 changes: 28 additions & 28 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub mod pallet {
type AssetId: Member
+ Parameter
+ Default
+ Copy
+ Clone
+ HasCompact
+ MaybeSerializeDeserialize
+ MaxEncodedLen
Expand Down Expand Up @@ -381,7 +381,7 @@ pub mod pallet {

for (id, account_id, amount) in &self.accounts {
let result = <Pallet<T, I>>::increase_balance(
*id,
id.clone(),
account_id,
*amount,
|details| -> DispatchResult {
Expand Down Expand Up @@ -553,14 +553,14 @@ pub mod pallet {
let owner = T::CreateOrigin::ensure_origin(origin, &id)?;
let admin = T::Lookup::lookup(admin)?;

ensure!(!Asset::<T, I>::contains_key(id), Error::<T, I>::InUse);
ensure!(!Asset::<T, I>::contains_key(&id), Error::<T, I>::InUse);
ensure!(!min_balance.is_zero(), Error::<T, I>::MinBalanceZero);

let deposit = T::AssetDeposit::get();
T::Currency::reserve(&owner, deposit)?;

Asset::<T, I>::insert(
id,
id.clone(),
AssetDetails {
owner: owner.clone(),
issuer: admin.clone(),
Expand Down Expand Up @@ -869,15 +869,15 @@ pub mod pallet {
) -> DispatchResult {
let origin = ensure_signed(origin)?;

let d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
ensure!(
d.status == AssetStatus::Live || d.status == AssetStatus::Frozen,
Error::<T, I>::AssetNotLive
);
ensure!(origin == d.freezer, Error::<T, I>::NoPermission);
let who = T::Lookup::lookup(who)?;

Account::<T, I>::try_mutate(id, &who, |maybe_account| -> DispatchResult {
Account::<T, I>::try_mutate(&id, &who, |maybe_account| -> DispatchResult {
maybe_account.as_mut().ok_or(Error::<T, I>::NoAccount)?.is_frozen = true;
Ok(())
})?;
Expand All @@ -904,15 +904,15 @@ pub mod pallet {
) -> DispatchResult {
let origin = ensure_signed(origin)?;

let details = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
let details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
ensure!(
details.status == AssetStatus::Live || details.status == AssetStatus::Frozen,
Error::<T, I>::AssetNotLive
);
ensure!(origin == details.admin, Error::<T, I>::NoPermission);
let who = T::Lookup::lookup(who)?;

Account::<T, I>::try_mutate(id, &who, |maybe_account| -> DispatchResult {
Account::<T, I>::try_mutate(&id, &who, |maybe_account| -> DispatchResult {
maybe_account.as_mut().ok_or(Error::<T, I>::NoAccount)?.is_frozen = false;
Ok(())
})?;
Expand All @@ -937,7 +937,7 @@ pub mod pallet {
) -> DispatchResult {
let origin = ensure_signed(origin)?;

Asset::<T, I>::try_mutate(id, |maybe_details| {
Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
let d = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
ensure!(origin == d.freezer, Error::<T, I>::NoPermission);
Expand Down Expand Up @@ -965,7 +965,7 @@ pub mod pallet {
) -> DispatchResult {
let origin = ensure_signed(origin)?;

Asset::<T, I>::try_mutate(id, |maybe_details| {
Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
let d = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
ensure!(origin == d.admin, Error::<T, I>::NoPermission);
ensure!(d.status == AssetStatus::Frozen, Error::<T, I>::NotFrozen);
Expand Down Expand Up @@ -996,15 +996,15 @@ pub mod pallet {
let origin = ensure_signed(origin)?;
let owner = T::Lookup::lookup(owner)?;

Asset::<T, I>::try_mutate(id, |maybe_details| {
Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
ensure!(details.status == AssetStatus::Live, Error::<T, I>::LiveAsset);
ensure!(origin == details.owner, Error::<T, I>::NoPermission);
if details.owner == owner {
return Ok(())
}

let metadata_deposit = Metadata::<T, I>::get(id).deposit;
let metadata_deposit = Metadata::<T, I>::get(&id).deposit;
let deposit = details.deposit + metadata_deposit;

// Move the deposit to the new owner.
Expand Down Expand Up @@ -1042,7 +1042,7 @@ pub mod pallet {
let admin = T::Lookup::lookup(admin)?;
let freezer = T::Lookup::lookup(freezer)?;

Asset::<T, I>::try_mutate(id, |maybe_details| {
Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
ensure!(details.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
ensure!(origin == details.owner, Error::<T, I>::NoPermission);
Expand Down Expand Up @@ -1102,11 +1102,11 @@ pub mod pallet {
) -> DispatchResult {
let origin = ensure_signed(origin)?;

let d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
ensure!(origin == d.owner, Error::<T, I>::NoPermission);

Metadata::<T, I>::try_mutate_exists(id, |metadata| {
Metadata::<T, I>::try_mutate_exists(id.clone(), |metadata| {
let deposit = metadata.take().ok_or(Error::<T, I>::Unknown)?.deposit;
T::Currency::unreserve(&d.owner, deposit);
Self::deposit_event(Event::MetadataCleared { asset_id: id });
Expand Down Expand Up @@ -1145,8 +1145,8 @@ pub mod pallet {
let bounded_symbol: BoundedVec<u8, T::StringLimit> =
symbol.clone().try_into().map_err(|_| Error::<T, I>::BadMetadata)?;

ensure!(Asset::<T, I>::contains_key(id), Error::<T, I>::Unknown);
Metadata::<T, I>::try_mutate_exists(id, |metadata| {
ensure!(Asset::<T, I>::contains_key(&id), Error::<T, I>::Unknown);
Metadata::<T, I>::try_mutate_exists(id.clone(), |metadata| {
let deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit);
*metadata = Some(AssetMetadata {
deposit,
Expand Down Expand Up @@ -1185,8 +1185,8 @@ pub mod pallet {
) -> DispatchResult {
T::ForceOrigin::ensure_origin(origin)?;

let d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
Metadata::<T, I>::try_mutate_exists(id, |metadata| {
let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
Metadata::<T, I>::try_mutate_exists(id.clone(), |metadata| {
let deposit = metadata.take().ok_or(Error::<T, I>::Unknown)?.deposit;
T::Currency::unreserve(&d.owner, deposit);
Self::deposit_event(Event::MetadataCleared { asset_id: id });
Expand Down Expand Up @@ -1230,7 +1230,7 @@ pub mod pallet {
) -> DispatchResult {
T::ForceOrigin::ensure_origin(origin)?;

Asset::<T, I>::try_mutate(id, |maybe_asset| {
Asset::<T, I>::try_mutate(id.clone(), |maybe_asset| {
let mut asset = maybe_asset.take().ok_or(Error::<T, I>::Unknown)?;
ensure!(asset.status != AssetStatus::Destroying, Error::<T, I>::AssetNotLive);
asset.owner = T::Lookup::lookup(owner)?;
Expand Down Expand Up @@ -1304,14 +1304,14 @@ pub mod pallet {
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let delegate = T::Lookup::lookup(delegate)?;
let mut d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
let mut d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
let approval =
Approvals::<T, I>::take((id, &owner, &delegate)).ok_or(Error::<T, I>::Unknown)?;
let approval = Approvals::<T, I>::take((id.clone(), &owner, &delegate))
.ok_or(Error::<T, I>::Unknown)?;
T::Currency::unreserve(&owner, approval.deposit);

d.approvals.saturating_dec();
Asset::<T, I>::insert(id, d);
Asset::<T, I>::insert(id.clone(), d);

Self::deposit_event(Event::ApprovalCancelled { asset_id: id, owner, delegate });
Ok(())
Expand All @@ -1337,7 +1337,7 @@ pub mod pallet {
owner: AccountIdLookupOf<T>,
delegate: AccountIdLookupOf<T>,
) -> DispatchResult {
let mut d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
let mut d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
T::ForceOrigin::try_origin(origin)
.map(|_| ())
Expand All @@ -1350,11 +1350,11 @@ pub mod pallet {
let owner = T::Lookup::lookup(owner)?;
let delegate = T::Lookup::lookup(delegate)?;

let approval =
Approvals::<T, I>::take((id, &owner, &delegate)).ok_or(Error::<T, I>::Unknown)?;
let approval = Approvals::<T, I>::take((id.clone(), &owner, &delegate))
.ok_or(Error::<T, I>::Unknown)?;
T::Currency::unreserve(&owner, approval.deposit);
d.approvals.saturating_dec();
Asset::<T, I>::insert(id, d);
Asset::<T, I>::insert(id.clone(), d);

Self::deposit_event(Event::ApprovalCancelled { asset_id: id, owner, delegate });
Ok(())
Expand Down
15 changes: 8 additions & 7 deletions frame/support/src/traits/tokens/fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub trait Mutate<AccountId>: Inspect<AccountId> {
who: &AccountId,
amount: Self::Balance,
) -> Result<Self::Balance, DispatchError> {
Self::burn_from(asset, who, Self::reducible_balance(asset, who, false).min(amount))
Self::burn_from(asset.clone(), who, Self::reducible_balance(asset, who, false).min(amount))
}

/// Transfer funds from one account into another. The default implementation uses `mint_into`
Expand All @@ -145,16 +145,17 @@ pub trait Mutate<AccountId>: Inspect<AccountId> {
dest: &AccountId,
amount: Self::Balance,
) -> Result<Self::Balance, DispatchError> {
let extra = Self::can_withdraw(asset, &source, amount).into_result()?;
let extra = Self::can_withdraw(asset.clone(), &source, amount).into_result()?;
// As we first burn and then mint, we don't need to check if `mint` fits into the supply.
// If we can withdraw/burn it, we can also mint it again.
Self::can_deposit(asset, dest, amount.saturating_add(extra), false).into_result()?;
let actual = Self::burn_from(asset, source, amount)?;
Self::can_deposit(asset.clone(), dest, amount.saturating_add(extra), false)
.into_result()?;
let actual = Self::burn_from(asset.clone(), source, amount)?;
debug_assert!(
actual == amount.saturating_add(extra),
"can_withdraw must agree with withdraw; qed"
);
match Self::mint_into(asset, dest, actual) {
match Self::mint_into(asset.clone(), dest, actual) {
Ok(_) => Ok(actual),
Err(err) => {
debug_assert!(false, "can_deposit returned true previously; qed");
Expand Down Expand Up @@ -246,9 +247,9 @@ impl<AccountId, T: Balanced<AccountId> + MutateHold<AccountId>> BalancedHold<Acc
who: &AccountId,
amount: Self::Balance,
) -> (CreditOf<AccountId, Self>, Self::Balance) {
let actual = match Self::release(asset, who, amount, true) {
let actual = match Self::release(asset.clone(), who, amount, true) {
Ok(x) => x,
Err(_) => return (Imbalance::zero(asset), amount),
Err(_) => return (Imbalance::zero(asset.clone()), amount),
};
<Self as fungibles::Balanced<AccountId>>::slash(asset, who, actual)
}
Expand Down
Loading