Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Also relax the Copy bound in the assets pallet
  • Loading branch information
koute committed May 16, 2023
commit a366c9ea9dcd4e6613b142705547828ecea4345f
22 changes: 12 additions & 10 deletions frame/assets/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
amount: T::Balance,
increase_supply: bool,
) -> DepositConsequence {
let details = match Asset::<T, I>::get(id) {
let details = match Asset::<T, I>::get(id.clone()) {
Some(details) => details,
None => return DepositConsequence::UnknownAsset,
};
Expand Down Expand Up @@ -219,10 +219,10 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
who: &T::AccountId,
keep_alive: bool,
) -> Result<T::Balance, DispatchError> {
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, Error::<T, I>::AssetNotLive);

let account = Account::<T, I>::get(id, who).ok_or(Error::<T, I>::NoAccount)?;
let account = Account::<T, I>::get(&id, who).ok_or(Error::<T, I>::NoAccount)?;
ensure!(!account.status.is_frozen(), Error::<T, I>::Frozen);

let amount = if let Some(frozen) = T::Freezer::frozen_balance(id, who) {
Expand Down Expand Up @@ -320,7 +320,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
depositor: T::AccountId,
check_depositor: bool,
) -> DispatchResult {
ensure!(!Account::<T, I>::contains_key(id, &who), Error::<T, I>::AlreadyExists);
ensure!(!Account::<T, I>::contains_key(&id, &who), Error::<T, I>::AlreadyExists);
let deposit = T::AssetAccountDeposit::get();
let mut details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
ensure!(details.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
Expand All @@ -332,7 +332,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
T::Currency::reserve(&depositor, deposit)?;
Asset::<T, I>::insert(&id, details);
Account::<T, I>::insert(
id,
id.clone(),
&who,
AssetAccountOf::<T, I> {
balance: Zero::zero(),
Expand All @@ -350,7 +350,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub(super) fn do_refund(id: T::AssetId, who: T::AccountId, allow_burn: bool) -> DispatchResult {
use AssetStatus::*;
use ExistenceReason::*;
let mut account = Account::<T, I>::get(id, &who).ok_or(Error::<T, I>::NoDeposit)?;
let mut account = Account::<T, I>::get(&id, &who).ok_or(Error::<T, I>::NoDeposit)?;
ensure!(matches!(account.reason, Consumer | DepositHeld(..)), Error::<T, I>::NoDeposit);
let mut details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
ensure!(matches!(details.status, Live | Frozen), Error::<T, I>::IncorrectStatus);
Expand Down Expand Up @@ -380,7 +380,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
who: &T::AccountId,
caller: &T::AccountId,
) -> DispatchResult {
let mut account = Account::<T, I>::get(id, &who).ok_or(Error::<T, I>::NoDeposit)?;
let mut account = Account::<T, I>::get(&id, &who).ok_or(Error::<T, I>::NoDeposit)?;
let (depositor, deposit) =
account.reason.take_deposit_from().ok_or(Error::<T, I>::NoDeposit)?;
let mut details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
Expand All @@ -392,11 +392,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
T::Currency::unreserve(&depositor, deposit);

if let Remove = Self::dead_account(&who, &mut details, &account.reason, false) {
Account::<T, I>::remove(id, &who);
Account::<T, I>::remove(&id, &who);
} else {
debug_assert!(false, "refund did not result in dead account?!");
// deposit may have been refunded, need to update `Account`
Account::<T, I>::insert(id, &who, account);
Account::<T, I>::insert(&id, &who, account);
return Ok(())
}
Asset::<T, I>::insert(&id, details);
Expand Down Expand Up @@ -1004,7 +1004,9 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// 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::<T, I>::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::<Vec<_>>()
}
}
22 changes: 11 additions & 11 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ pub mod pallet {
type RemoveItemsLimit: Get<u32>;

/// 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
Expand Down Expand Up @@ -955,7 +955,7 @@ pub mod pallet {
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)?.status =
AccountStatus::Frozen;
Ok(())
Expand Down Expand Up @@ -992,7 +992,7 @@ pub mod pallet {
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)?.status =
AccountStatus::Liquid;
Ok(())
Expand Down Expand Up @@ -1389,15 +1389,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::<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 @@ -1424,7 +1424,7 @@ pub mod pallet {
delegate: AccountIdLookupOf<T>,
) -> DispatchResult {
let id: T::AssetId = id.into();
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 Down Expand Up @@ -1539,7 +1539,7 @@ pub mod pallet {
let origin = ensure_signed(origin)?;
let id: T::AssetId = id.into();

let mut details = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
let mut details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
ensure!(origin == details.owner, Error::<T, I>::NoPermission);

let old_min_balance = details.min_balance;
Expand Down Expand Up @@ -1629,15 +1629,15 @@ pub mod pallet {
let origin = ensure_signed(origin)?;
let id: T::AssetId = id.into();

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)?.status =
AccountStatus::Blocked;
Ok(())
Expand Down