referrerpolicy=no-referrer-when-downgrade

pallet_assets/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # Assets Pallet
19//!
20//! A simple, secure module for dealing with sets of assets implementing
21//! [`fungible`](frame_support::traits::fungible) traits, via [`fungibles`] traits.
22//!
23//! The pallet makes heavy use of concepts such as Holds and Freezes from the
24//! [`frame_support::traits::fungible`] traits, therefore you should read and understand those docs
25//! as a prerequisite to understanding this pallet.
26//!
27//! See the [`frame_tokens`] reference docs for more information about the place of the
28//! Assets pallet in FRAME.
29//!
30//! ## Overview
31//!
32//! The Assets module provides functionality for asset management of fungible asset classes
33//! with a fixed supply, including:
34//!
35//! * Asset Issuance (Minting)
36//! * Asset Transferal
37//! * Asset Freezing
38//! * Asset Destruction (Burning)
39//! * Delegated Asset Transfers ("Approval API")
40//!
41//! To use it in your runtime, you need to implement the assets [`Config`].
42//!
43//! The supported dispatchable functions are documented in the [`Call`] enum.
44//!
45//! ### Terminology
46//!
47//! * **Admin**: An account ID uniquely privileged to be able to unfreeze (thaw) an account and its
48//!   assets, as well as forcibly transfer a particular class of assets between arbitrary accounts
49//!   and reduce the balance of a particular class of assets of arbitrary accounts.
50//! * **Asset issuance/minting**: The creation of a new asset, whose total supply will belong to the
51//!   account designated as the beneficiary of the asset. This is a privileged operation.
52//! * **Asset transfer**: The reduction of the balance of an asset of one account with the
53//!   corresponding increase in the balance of another.
54//! * **Asset destruction**: The process of reducing the balance of an asset of one account. This is
55//!   a privileged operation.
56//! * **Fungible asset**: An asset whose units are interchangeable.
57//! * **Issuer**: An account ID uniquely privileged to be able to mint a particular class of assets.
58//! * **Freezer**: An account ID uniquely privileged to be able to freeze an account from
59//!   transferring a particular class of assets.
60//! * **Freezing**: Removing the possibility of an unpermissioned transfer of an asset from a
61//!   particular account.
62//! * **Non-fungible asset**: An asset for which each unit has unique characteristics.
63//! * **Owner**: An account ID uniquely privileged to be able to destroy a particular asset class,
64//!   or to set the Issuer, Freezer, Reserves, or Admin of that asset class.
65//! * **Approval**: The act of allowing an account the permission to transfer some balance of asset
66//!   from the approving account into some third-party destination account.
67//! * **Sufficiency**: The idea of a minimum-balance of an asset being sufficient to allow the
68//!   account's existence on the system without requiring any other existential-deposit.
69//!
70//! ### Goals
71//!
72//! The assets system in Substrate is designed to make the following possible:
73//!
74//! * Issue new assets in a permissioned or permissionless way, if permissionless, then with a
75//!   deposit required.
76//! * Allow accounts to be delegated the ability to transfer assets without otherwise existing
77//!   on-chain (*approvals*).
78//! * Move assets between accounts.
79//! * Update an asset class's total supply.
80//! * Allow administrative activities by specially privileged accounts including freezing account
81//!   balances and minting/burning assets.
82//!
83//! ## Interface
84//!
85//! ### Permissionless Functions
86//!
87//! * `create`: Creates a new asset class, taking the required deposit.
88//! * `transfer`: Transfer sender's assets to another account.
89//! * `transfer_keep_alive`: Transfer sender's assets to another account, keeping the sender alive.
90//! * `approve_transfer`: Create or increase an delegated transfer.
91//! * `cancel_approval`: Rescind a previous approval.
92//! * `transfer_approved`: Transfer third-party's assets to another account.
93//! * `touch`: Create an asset account for non-provider assets. Caller must place a deposit.
94//! * `refund`: Return the deposit (if any) of the caller's asset account or a consumer reference
95//!   (if any) of the caller's account.
96//! * `refund_other`: Return the deposit (if any) of a specified asset account.
97//! * `touch_other`: Create an asset account for specified account. Caller must place a deposit.
98//!
99//! ### Permissioned Functions
100//!
101//! * `force_create`: Creates a new asset class without taking any deposit.
102//! * `force_set_metadata`: Set the metadata of an asset class.
103//! * `force_clear_metadata`: Remove the metadata of an asset class.
104//! * `force_asset_status`: Alter an asset class's attributes.
105//! * `force_cancel_approval`: Rescind a previous approval.
106//!
107//! ### Privileged Functions
108//!
109//! * `destroy`: Destroys an entire asset class; called by the asset class's Owner.
110//! * `mint`: Increases the asset balance of an account; called by the asset class's Issuer.
111//! * `burn`: Decreases the asset balance of an account; called by the asset class's Admin.
112//! * `force_transfer`: Transfers between arbitrary accounts; called by the asset class's Admin.
113//! * `freeze`: Disallows further `transfer`s from an account; called by the asset class's Freezer.
114//! * `thaw`: Allows further `transfer`s to and from an account; called by the asset class's Admin.
115//! * `transfer_ownership`: Changes an asset class's Owner; called by the asset class's Owner.
116//! * `set_team`: Changes an asset class's Admin, Freezer and Issuer; called by the asset class's
117//!   Owner.
118//! * `set_metadata`: Set the metadata of an asset class; called by the asset class's Owner.
119//! * `clear_metadata`: Remove the metadata of an asset class; called by the asset class's Owner.
120//! * `set_reserves`: Set the reserve information of an asset class; called by the asset class's
121//!   Owner.
122//! * `block`: Disallows further `transfer`s to and from an account; called by the asset class's
123//!   Freezer.
124//!
125//! Please refer to the [`Call`] enum and its associated variants for documentation on each
126//! function.
127//!
128//! ### Public Functions
129//! <!-- Original author of descriptions: @gavofyork -->
130//!
131//! * `balance` - Get the asset `id` balance of `who`.
132//! * `total_supply` - Get the total supply of an asset `id`.
133//!
134//! Please refer to the [`Pallet`] struct for details on publicly available functions.
135//!
136//! ### Callbacks
137//!
138//! Using `CallbackHandle` associated type, user can configure custom callback functions which are
139//! executed when new asset is created or an existing asset is destroyed.
140//!
141//! ## Related Modules
142//!
143//! * [`System`](../frame_system/index.html)
144//! * [`Support`](../frame_support/index.html)
145//!
146//! [`frame_tokens`]: ../polkadot_sdk_docs/reference_docs/frame_tokens/index.html
147
148// This recursion limit is needed because we have too many benchmarks and benchmarking will fail if
149// we add more without this limit.
150#![recursion_limit = "1024"]
151// Ensure we're `no_std` when compiling for Wasm.
152#![cfg_attr(not(feature = "std"), no_std)]
153
154#[cfg(feature = "runtime-benchmarks")]
155pub mod benchmarking;
156pub mod migration;
157#[cfg(test)]
158pub mod mock;
159#[cfg(test)]
160mod tests;
161pub mod weights;
162
163mod extra_mutator;
164pub use extra_mutator::*;
165mod functions;
166mod impl_fungibles;
167mod impl_stored_map;
168mod types;
169pub use types::*;
170
171extern crate alloc;
172extern crate core;
173
174use scale_info::TypeInfo;
175use sp_runtime::{
176	traits::{AtLeast32BitUnsigned, CheckedAdd, CheckedSub, Saturating, StaticLookup, Zero},
177	ArithmeticError, DispatchError, TokenError,
178};
179
180use alloc::vec::Vec;
181use core::{fmt::Debug, marker::PhantomData};
182use frame_support::{
183	dispatch::DispatchResult,
184	ensure,
185	pallet_prelude::DispatchResultWithPostInfo,
186	storage::KeyPrefixIterator,
187	traits::{
188		tokens::{
189			fungibles, DepositConsequence, Fortitude,
190			Preservation::{Expendable, Preserve},
191			WithdrawConsequence,
192		},
193		BalanceStatus::Reserved,
194		Currency, EnsureOriginWithArg, Incrementable, ReservableCurrency, StoredMap,
195	},
196};
197use frame_system::Config as SystemConfig;
198
199pub use pallet::*;
200pub use weights::WeightInfo;
201
202type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
203const LOG_TARGET: &str = "runtime::assets";
204
205/// Trait with callbacks that are executed after successful asset creation or destruction.
206pub trait AssetsCallback<AssetId, AccountId> {
207	/// Indicates that asset with `id` was successfully created by the `owner`
208	fn created(_id: &AssetId, _owner: &AccountId) -> Result<(), ()> {
209		Ok(())
210	}
211
212	/// Indicates that asset with `id` has just been destroyed
213	fn destroyed(_id: &AssetId) -> Result<(), ()> {
214		Ok(())
215	}
216}
217
218#[impl_trait_for_tuples::impl_for_tuples(10)]
219impl<AssetId, AccountId> AssetsCallback<AssetId, AccountId> for Tuple {
220	fn created(id: &AssetId, owner: &AccountId) -> Result<(), ()> {
221		for_tuples!( #( Tuple::created(id, owner)?; )* );
222		Ok(())
223	}
224
225	fn destroyed(id: &AssetId) -> Result<(), ()> {
226		for_tuples!( #( Tuple::destroyed(id)?; )* );
227		Ok(())
228	}
229}
230
231/// Auto-increment the [`NextAssetId`] when an asset is created.
232///
233/// This has not effect if the [`NextAssetId`] value is not present.
234pub struct AutoIncAssetId<T, I = ()>(PhantomData<(T, I)>);
235impl<T: Config<I>, I> AssetsCallback<T::AssetId, T::AccountId> for AutoIncAssetId<T, I>
236where
237	T::AssetId: Incrementable,
238{
239	fn created(_: &T::AssetId, _: &T::AccountId) -> Result<(), ()> {
240		let Some(next_id) = NextAssetId::<T, I>::get() else {
241			// Auto increment for the asset id is not enabled.
242			return Ok(());
243		};
244		let next_id = next_id.increment().ok_or(())?;
245		NextAssetId::<T, I>::put(next_id);
246		Ok(())
247	}
248}
249
250#[frame_support::pallet]
251pub mod pallet {
252	use super::*;
253	use codec::HasCompact;
254	use frame_support::{
255		pallet_prelude::*,
256		traits::{tokens::ProvideAssetReserves, AccountTouch, ContainsPair},
257	};
258	use frame_system::pallet_prelude::*;
259
260	/// The in-code storage version.
261	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
262
263	/// The maximum number of configurable reserve locations for one asset class.
264	pub const MAX_RESERVES: u32 = 5;
265
266	#[pallet::pallet]
267	#[pallet::storage_version(STORAGE_VERSION)]
268	pub struct Pallet<T, I = ()>(_);
269
270	#[cfg(feature = "runtime-benchmarks")]
271	pub trait BenchmarkHelper<AssetIdParameter, ReserveIdParameter> {
272		fn create_asset_id_parameter(id: u32) -> AssetIdParameter;
273		fn create_reserve_id_parameter(id: u32) -> ReserveIdParameter;
274	}
275	#[cfg(feature = "runtime-benchmarks")]
276	impl<AssetIdParameter: From<u32>> BenchmarkHelper<AssetIdParameter, ()> for () {
277		fn create_asset_id_parameter(id: u32) -> AssetIdParameter {
278			id.into()
279		}
280		fn create_reserve_id_parameter(_: u32) -> () {
281			()
282		}
283	}
284
285	/// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`].
286	pub mod config_preludes {
287		use super::*;
288		use frame_support::derive_impl;
289		pub struct TestDefaultConfig;
290
291		#[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
292		impl frame_system::DefaultConfig for TestDefaultConfig {}
293
294		#[frame_support::register_default_impl(TestDefaultConfig)]
295		impl DefaultConfig for TestDefaultConfig {
296			#[inject_runtime_type]
297			type RuntimeEvent = ();
298			type Balance = u64;
299			type RemoveItemsLimit = ConstU32<5>;
300			type AssetId = u32;
301			type AssetIdParameter = u32;
302			type ReserveData = ();
303			type AssetDeposit = ConstUint<1>;
304			type AssetAccountDeposit = ConstUint<10>;
305			type MetadataDepositBase = ConstUint<1>;
306			type MetadataDepositPerByte = ConstUint<1>;
307			type ApprovalDeposit = ConstUint<1>;
308			type StringLimit = ConstU32<50>;
309			type Freezer = ();
310			type Holder = ();
311			type Extra = ();
312			type CallbackHandle = ();
313			type WeightInfo = ();
314			#[cfg(feature = "runtime-benchmarks")]
315			type BenchmarkHelper = ();
316		}
317	}
318
319	#[pallet::config(with_default)]
320	/// The module configuration trait.
321	pub trait Config<I: 'static = ()>: frame_system::Config {
322		/// The overarching event type.
323		#[pallet::no_default_bounds]
324		#[allow(deprecated)]
325		type RuntimeEvent: From<Event<Self, I>>
326			+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
327
328		/// The units in which we record balances.
329		type Balance: Member
330			+ Parameter
331			+ HasCompact<Type: DecodeWithMemTracking>
332			+ AtLeast32BitUnsigned
333			+ Default
334			+ Copy
335			+ MaybeSerializeDeserialize
336			+ MaxEncodedLen
337			+ TypeInfo;
338
339		/// Max number of items to destroy per `destroy_accounts` and `destroy_approvals` call.
340		///
341		/// Must be configured to result in a weight that makes each call fit in a block.
342		#[pallet::constant]
343		type RemoveItemsLimit: Get<u32>;
344
345		/// Identifier for the class of asset.
346		type AssetId: Member + Parameter + Clone + MaybeSerializeDeserialize + MaxEncodedLen;
347
348		/// Wrapper around `Self::AssetId` to use in dispatchable call signatures. Allows the use
349		/// of compact encoding in instances of the pallet, which will prevent breaking changes
350		/// resulting from the removal of `HasCompact` from `Self::AssetId`.
351		///
352		/// This type includes the `From<Self::AssetId>` bound, since tightly coupled pallets may
353		/// want to convert an `AssetId` into a parameter for calling dispatchable functions
354		/// directly.
355		type AssetIdParameter: Parameter + From<Self::AssetId> + Into<Self::AssetId> + MaxEncodedLen;
356
357		/// Information about reserve locations for a class of asset.
358		type ReserveData: Debug + Parameter + MaybeSerializeDeserialize + MaxEncodedLen;
359
360		/// The currency mechanism.
361		#[pallet::no_default]
362		type Currency: ReservableCurrency<Self::AccountId>;
363
364		/// Standard asset class creation is only allowed if the origin attempting it and the
365		/// asset class are in this set.
366		#[pallet::no_default]
367		type CreateOrigin: EnsureOriginWithArg<
368			Self::RuntimeOrigin,
369			Self::AssetId,
370			Success = Self::AccountId,
371		>;
372
373		/// The origin which may forcibly create or destroy an asset or otherwise alter privileged
374		/// attributes.
375		#[pallet::no_default]
376		type ForceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
377
378		/// The basic amount of funds that must be reserved for an asset.
379		#[pallet::constant]
380		#[pallet::no_default_bounds]
381		type AssetDeposit: Get<DepositBalanceOf<Self, I>>;
382
383		/// The amount of funds that must be reserved for a non-provider asset account to be
384		/// maintained.
385		#[pallet::constant]
386		#[pallet::no_default_bounds]
387		type AssetAccountDeposit: Get<DepositBalanceOf<Self, I>>;
388
389		/// The basic amount of funds that must be reserved when adding metadata to your asset.
390		#[pallet::constant]
391		#[pallet::no_default_bounds]
392		type MetadataDepositBase: Get<DepositBalanceOf<Self, I>>;
393
394		/// The additional funds that must be reserved for the number of bytes you store in your
395		/// metadata.
396		#[pallet::constant]
397		#[pallet::no_default_bounds]
398		type MetadataDepositPerByte: Get<DepositBalanceOf<Self, I>>;
399
400		/// The amount of funds that must be reserved when creating a new approval.
401		#[pallet::constant]
402		#[pallet::no_default_bounds]
403		type ApprovalDeposit: Get<DepositBalanceOf<Self, I>>;
404
405		/// The maximum length of a name or symbol stored on-chain.
406		#[pallet::constant]
407		type StringLimit: Get<u32>;
408
409		/// A hook to allow a per-asset, per-account minimum balance to be enforced. This must be
410		/// respected in all permissionless operations.
411		type Freezer: FrozenBalance<Self::AssetId, Self::AccountId, Self::Balance>;
412
413		/// A hook to inspect a per-asset, per-account balance that is held. This goes in
414		/// accordance with balance model.
415		type Holder: BalanceOnHold<Self::AssetId, Self::AccountId, Self::Balance>;
416
417		/// Additional data to be stored with an account's asset balance.
418		type Extra: Member + Parameter + Default + MaxEncodedLen;
419
420		/// Callback methods for asset state change (e.g. asset created or destroyed)
421		///
422		/// Types implementing the [`AssetsCallback`] can be chained when listed together as a
423		/// tuple.
424		/// The [`AutoIncAssetId`] callback, in conjunction with the [`NextAssetId`], can be
425		/// used to set up auto-incrementing asset IDs for this collection.
426		type CallbackHandle: AssetsCallback<Self::AssetId, Self::AccountId>;
427
428		/// Weight information for extrinsics in this pallet.
429		type WeightInfo: WeightInfo;
430
431		/// Helper trait for benchmarks.
432		#[cfg(feature = "runtime-benchmarks")]
433		type BenchmarkHelper: BenchmarkHelper<Self::AssetIdParameter, Self::ReserveData>;
434	}
435
436	#[pallet::storage]
437	/// Details of an asset.
438	pub type Asset<T: Config<I>, I: 'static = ()> = StorageMap<
439		_,
440		Blake2_128Concat,
441		T::AssetId,
442		AssetDetails<T::Balance, T::AccountId, DepositBalanceOf<T, I>>,
443	>;
444
445	#[pallet::storage]
446	/// The holdings of a specific account for a specific asset.
447	pub type Account<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
448		_,
449		Blake2_128Concat,
450		T::AssetId,
451		Blake2_128Concat,
452		T::AccountId,
453		AssetAccountOf<T, I>,
454	>;
455
456	#[pallet::storage]
457	/// Approved balance transfers. First balance is the amount approved for transfer. Second
458	/// is the amount of `T::Currency` reserved for storing this.
459	/// First key is the asset ID, second key is the owner and third key is the delegate.
460	pub type Approvals<T: Config<I>, I: 'static = ()> = StorageNMap<
461		_,
462		(
463			NMapKey<Blake2_128Concat, T::AssetId>,
464			NMapKey<Blake2_128Concat, T::AccountId>, // owner
465			NMapKey<Blake2_128Concat, T::AccountId>, // delegate
466		),
467		Approval<T::Balance, DepositBalanceOf<T, I>>,
468	>;
469
470	#[pallet::storage]
471	/// Metadata of an asset.
472	pub type Metadata<T: Config<I>, I: 'static = ()> = StorageMap<
473		_,
474		Blake2_128Concat,
475		T::AssetId,
476		AssetMetadata<DepositBalanceOf<T, I>, BoundedVec<u8, T::StringLimit>>,
477		ValueQuery,
478	>;
479
480	/// Maps an asset to a list of its configured reserve information.
481	#[pallet::storage]
482	pub type Reserves<T: Config<I>, I: 'static = ()> = StorageMap<
483		_,
484		Blake2_128Concat,
485		T::AssetId,
486		BoundedVec<T::ReserveData, ConstU32<MAX_RESERVES>>,
487		ValueQuery,
488	>;
489
490	/// The asset ID enforced for the next asset creation, if any present. Otherwise, this storage
491	/// item has no effect.
492	///
493	/// This can be useful for setting up constraints for IDs of the new assets. For example, by
494	/// providing an initial [`NextAssetId`] and using the [`crate::AutoIncAssetId`] callback, an
495	/// auto-increment model can be applied to all new asset IDs.
496	///
497	/// The initial next asset ID can be set using the [`GenesisConfig`] or the
498	/// [SetNextAssetId](`migration::next_asset_id::SetNextAssetId`) migration.
499	#[pallet::storage]
500	pub type NextAssetId<T: Config<I>, I: 'static = ()> = StorageValue<_, T::AssetId, OptionQuery>;
501
502	#[pallet::genesis_config]
503	#[derive(frame_support::DefaultNoBound)]
504	pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
505		/// Genesis assets: id, owner, is_sufficient, min_balance
506		pub assets: Vec<(T::AssetId, T::AccountId, bool, T::Balance)>,
507		/// Genesis metadata: id, name, symbol, decimals
508		pub metadata: Vec<(T::AssetId, Vec<u8>, Vec<u8>, u8)>,
509		/// Genesis accounts: id, account_id, balance
510		pub accounts: Vec<(T::AssetId, T::AccountId, T::Balance)>,
511		/// Genesis [`NextAssetId`].
512		///
513		/// Refer to the [`NextAssetId`] item for more information.
514		///
515		/// This does not enforce the asset ID for the [assets](`GenesisConfig::assets`) within the
516		/// genesis config. It sets the [`NextAssetId`] after they have been created.
517		pub next_asset_id: Option<T::AssetId>,
518		/// Genesis assets and their reserves
519		pub reserves: Vec<(T::AssetId, Vec<T::ReserveData>)>,
520	}
521
522	#[pallet::genesis_build]
523	impl<T: Config<I>, I: 'static> BuildGenesisConfig for GenesisConfig<T, I> {
524		fn build(&self) {
525			for (id, owner, is_sufficient, min_balance) in &self.assets {
526				assert!(!Asset::<T, I>::contains_key(id), "Asset id already in use");
527				assert!(!min_balance.is_zero(), "Min balance should not be zero");
528				Asset::<T, I>::insert(
529					id,
530					AssetDetails {
531						owner: owner.clone(),
532						issuer: owner.clone(),
533						admin: owner.clone(),
534						freezer: owner.clone(),
535						supply: Zero::zero(),
536						deposit: Zero::zero(),
537						min_balance: *min_balance,
538						is_sufficient: *is_sufficient,
539						accounts: 0,
540						sufficients: 0,
541						approvals: 0,
542						status: AssetStatus::Live,
543					},
544				);
545			}
546
547			for (id, name, symbol, decimals) in &self.metadata {
548				assert!(Asset::<T, I>::contains_key(id), "Asset does not exist");
549
550				let bounded_name: BoundedVec<u8, T::StringLimit> =
551					name.clone().try_into().expect("asset name is too long");
552				let bounded_symbol: BoundedVec<u8, T::StringLimit> =
553					symbol.clone().try_into().expect("asset symbol is too long");
554
555				let metadata = AssetMetadata {
556					deposit: Zero::zero(),
557					name: bounded_name,
558					symbol: bounded_symbol,
559					decimals: *decimals,
560					is_frozen: false,
561				};
562				Metadata::<T, I>::insert(id, metadata);
563			}
564
565			for (id, account_id, amount) in &self.accounts {
566				let result = <Pallet<T, I>>::increase_balance(
567					id.clone(),
568					account_id,
569					*amount,
570					|details| -> DispatchResult {
571						debug_assert!(
572							details.supply.checked_add(&amount).is_some(),
573							"checked in prep; qed"
574						);
575						details.supply = details.supply.saturating_add(*amount);
576						Ok(())
577					},
578				);
579				assert!(result.is_ok());
580			}
581
582			if let Some(next_asset_id) = &self.next_asset_id {
583				NextAssetId::<T, I>::put(next_asset_id);
584			}
585
586			for (id, reserves) in &self.reserves {
587				assert!(!Reserves::<T, I>::contains_key(id), "Asset id already in use");
588				let reserves = BoundedVec::try_from(reserves.clone()).expect("too many reserves");
589				Reserves::<T, I>::insert(id, reserves);
590			}
591		}
592	}
593
594	#[pallet::event]
595	#[pallet::generate_deposit(pub(super) fn deposit_event)]
596	pub enum Event<T: Config<I>, I: 'static = ()> {
597		/// Some asset class was created.
598		Created { asset_id: T::AssetId, creator: T::AccountId, owner: T::AccountId },
599		/// Some assets were issued.
600		Issued { asset_id: T::AssetId, owner: T::AccountId, amount: T::Balance },
601		/// Some assets were transferred.
602		Transferred {
603			asset_id: T::AssetId,
604			from: T::AccountId,
605			to: T::AccountId,
606			amount: T::Balance,
607		},
608		/// Some assets were destroyed.
609		Burned { asset_id: T::AssetId, owner: T::AccountId, balance: T::Balance },
610		/// The management team changed.
611		TeamChanged {
612			asset_id: T::AssetId,
613			issuer: T::AccountId,
614			admin: T::AccountId,
615			freezer: T::AccountId,
616		},
617		/// The owner changed.
618		OwnerChanged { asset_id: T::AssetId, owner: T::AccountId },
619		/// Some account `who` was frozen.
620		Frozen { asset_id: T::AssetId, who: T::AccountId },
621		/// Some account `who` was thawed.
622		Thawed { asset_id: T::AssetId, who: T::AccountId },
623		/// Some asset `asset_id` was frozen.
624		AssetFrozen { asset_id: T::AssetId },
625		/// Some asset `asset_id` was thawed.
626		AssetThawed { asset_id: T::AssetId },
627		/// Accounts were destroyed for given asset.
628		AccountsDestroyed { asset_id: T::AssetId, accounts_destroyed: u32, accounts_remaining: u32 },
629		/// Approvals were destroyed for given asset.
630		ApprovalsDestroyed {
631			asset_id: T::AssetId,
632			approvals_destroyed: u32,
633			approvals_remaining: u32,
634		},
635		/// An asset class is in the process of being destroyed.
636		DestructionStarted { asset_id: T::AssetId },
637		/// An asset class was destroyed.
638		Destroyed { asset_id: T::AssetId },
639		/// Some asset class was force-created.
640		ForceCreated { asset_id: T::AssetId, owner: T::AccountId },
641		/// New metadata has been set for an asset.
642		MetadataSet {
643			asset_id: T::AssetId,
644			name: Vec<u8>,
645			symbol: Vec<u8>,
646			decimals: u8,
647			is_frozen: bool,
648		},
649		/// Metadata has been cleared for an asset.
650		MetadataCleared { asset_id: T::AssetId },
651		/// (Additional) funds have been approved for transfer to a destination account.
652		ApprovedTransfer {
653			asset_id: T::AssetId,
654			source: T::AccountId,
655			delegate: T::AccountId,
656			amount: T::Balance,
657		},
658		/// An approval for account `delegate` was cancelled by `owner`.
659		ApprovalCancelled { asset_id: T::AssetId, owner: T::AccountId, delegate: T::AccountId },
660		/// An `amount` was transferred in its entirety from `owner` to `destination` by
661		/// the approved `delegate`.
662		TransferredApproved {
663			asset_id: T::AssetId,
664			owner: T::AccountId,
665			delegate: T::AccountId,
666			destination: T::AccountId,
667			amount: T::Balance,
668		},
669		/// An asset has had its attributes changed by the `Force` origin.
670		AssetStatusChanged { asset_id: T::AssetId },
671		/// The min_balance of an asset has been updated by the asset owner.
672		AssetMinBalanceChanged { asset_id: T::AssetId, new_min_balance: T::Balance },
673		/// Some account `who` was created with a deposit from `depositor`.
674		Touched { asset_id: T::AssetId, who: T::AccountId, depositor: T::AccountId },
675		/// Some account `who` was blocked.
676		Blocked { asset_id: T::AssetId, who: T::AccountId },
677		/// Some assets were deposited (e.g. for transaction fees).
678		Deposited { asset_id: T::AssetId, who: T::AccountId, amount: T::Balance },
679		/// Some assets were withdrawn from the account (e.g. for transaction fees).
680		Withdrawn { asset_id: T::AssetId, who: T::AccountId, amount: T::Balance },
681		/// Reserve information was set or updated for `asset_id`.
682		ReservesUpdated { asset_id: T::AssetId, reserves: Vec<T::ReserveData> },
683		/// Reserve information was removed for `asset_id`.
684		ReservesRemoved { asset_id: T::AssetId },
685	}
686
687	#[pallet::error]
688	pub enum Error<T, I = ()> {
689		/// Account balance must be greater than or equal to the transfer amount.
690		BalanceLow,
691		/// The account to alter does not exist.
692		NoAccount,
693		/// The signing account has no permission to do the operation.
694		NoPermission,
695		/// The given asset ID is unknown.
696		Unknown,
697		/// The origin account is frozen.
698		Frozen,
699		/// The asset ID is already taken.
700		InUse,
701		/// Invalid witness data given.
702		BadWitness,
703		/// Minimum balance should be non-zero.
704		MinBalanceZero,
705		/// Unable to increment the consumer reference counters on the account. Either no provider
706		/// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one
707		/// fewer then the maximum number of consumers has been reached.
708		UnavailableConsumer,
709		/// Invalid metadata given.
710		BadMetadata,
711		/// No approval exists that would allow the transfer.
712		Unapproved,
713		/// The source account would not survive the transfer and it needs to stay alive.
714		WouldDie,
715		/// The asset-account already exists.
716		AlreadyExists,
717		/// The asset-account doesn't have an associated deposit.
718		NoDeposit,
719		/// The operation would result in funds being burned.
720		WouldBurn,
721		/// The asset is a live asset and is actively being used. Usually emit for operations such
722		/// as `start_destroy` which require the asset to be in a destroying state.
723		LiveAsset,
724		/// The asset is not live, and likely being destroyed.
725		AssetNotLive,
726		/// The asset status is not the expected status.
727		IncorrectStatus,
728		/// The asset should be frozen before the given operation.
729		NotFrozen,
730		/// Callback action resulted in error
731		CallbackFailed,
732		/// The asset ID must be equal to the [`NextAssetId`].
733		BadAssetId,
734		/// The asset cannot be destroyed because some accounts for this asset contain freezes.
735		ContainsFreezes,
736		/// The asset cannot be destroyed because some accounts for this asset contain holds.
737		ContainsHolds,
738		/// Tried setting too many reserves.
739		TooManyReserves,
740	}
741
742	#[pallet::hooks]
743	impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
744		#[cfg(feature = "try-runtime")]
745		fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
746			Self::do_try_state()
747		}
748	}
749
750	#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
751	impl<T: Config<I>, I: 'static> Pallet<T, I> {
752		/// Issue a new class of fungible assets from a public origin.
753		///
754		/// This new asset class has no assets initially and its owner is the origin.
755		///
756		/// The origin must conform to the configured `CreateOrigin` and have sufficient funds free.
757		///
758		/// Funds of sender are reserved by `AssetDeposit`.
759		///
760		/// Parameters:
761		/// - `id`: The identifier of the new asset. This must not be currently in use to identify
762		/// an existing asset. If [`NextAssetId`] is set, then this must be equal to it.
763		/// - `admin`: The admin of this class of assets. The admin is the initial address of each
764		/// member of the asset class's admin team.
765		/// - `min_balance`: The minimum balance of this new asset that any single account must
766		/// have. If an account's balance is reduced below this, then it collapses to zero.
767		///
768		/// Emits `Created` event when successful.
769		///
770		/// Weight: `O(1)`
771		#[pallet::call_index(0)]
772		pub fn create(
773			origin: OriginFor<T>,
774			id: T::AssetIdParameter,
775			admin: AccountIdLookupOf<T>,
776			min_balance: T::Balance,
777		) -> DispatchResult {
778			let id: T::AssetId = id.into();
779			let owner = T::CreateOrigin::ensure_origin(origin, &id)?;
780			let admin = T::Lookup::lookup(admin)?;
781
782			ensure!(!Asset::<T, I>::contains_key(&id), Error::<T, I>::InUse);
783			ensure!(!min_balance.is_zero(), Error::<T, I>::MinBalanceZero);
784
785			if let Some(next_id) = NextAssetId::<T, I>::get() {
786				ensure!(id == next_id, Error::<T, I>::BadAssetId);
787			}
788
789			let deposit = T::AssetDeposit::get();
790			T::Currency::reserve(&owner, deposit)?;
791
792			Asset::<T, I>::insert(
793				id.clone(),
794				AssetDetails {
795					owner: owner.clone(),
796					issuer: admin.clone(),
797					admin: admin.clone(),
798					freezer: admin.clone(),
799					supply: Zero::zero(),
800					deposit,
801					min_balance,
802					is_sufficient: false,
803					accounts: 0,
804					sufficients: 0,
805					approvals: 0,
806					status: AssetStatus::Live,
807				},
808			);
809			ensure!(T::CallbackHandle::created(&id, &owner).is_ok(), Error::<T, I>::CallbackFailed);
810			Self::deposit_event(Event::Created {
811				asset_id: id,
812				creator: owner.clone(),
813				owner: admin,
814			});
815
816			Ok(())
817		}
818
819		/// Issue a new class of fungible assets from a privileged origin.
820		///
821		/// This new asset class has no assets initially.
822		///
823		/// The origin must conform to `ForceOrigin`.
824		///
825		/// Unlike `create`, no funds are reserved.
826		///
827		/// - `id`: The identifier of the new asset. This must not be currently in use to identify
828		/// an existing asset. If [`NextAssetId`] is set, then this must be equal to it.
829		/// - `owner`: The owner of this class of assets. The owner has full superuser permissions
830		/// over this asset, but may later change and configure the permissions using
831		/// `transfer_ownership` and `set_team`.
832		/// - `min_balance`: The minimum balance of this new asset that any single account must
833		/// have. If an account's balance is reduced below this, then it collapses to zero.
834		///
835		/// Emits `ForceCreated` event when successful.
836		///
837		/// Weight: `O(1)`
838		#[pallet::call_index(1)]
839		pub fn force_create(
840			origin: OriginFor<T>,
841			id: T::AssetIdParameter,
842			owner: AccountIdLookupOf<T>,
843			is_sufficient: bool,
844			#[pallet::compact] min_balance: T::Balance,
845		) -> DispatchResult {
846			T::ForceOrigin::ensure_origin(origin)?;
847			let owner = T::Lookup::lookup(owner)?;
848			let id: T::AssetId = id.into();
849			Self::do_force_create(id, owner, is_sufficient, min_balance)
850		}
851
852		/// Start the process of destroying a fungible asset class.
853		///
854		/// `start_destroy` is the first in a series of extrinsics that should be called, to allow
855		/// destruction of an asset class.
856		///
857		/// The origin must conform to `ForceOrigin` or must be `Signed` by the asset's `owner`.
858		///
859		/// - `id`: The identifier of the asset to be destroyed. This must identify an existing
860		///   asset.
861		///
862		/// It will fail with either [`Error::ContainsHolds`] or [`Error::ContainsFreezes`] if
863		/// an account contains holds or freezes in place.
864		#[pallet::call_index(2)]
865		pub fn start_destroy(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
866			let maybe_check_owner = match T::ForceOrigin::try_origin(origin) {
867				Ok(_) => None,
868				Err(origin) => Some(ensure_signed(origin)?),
869			};
870			let id: T::AssetId = id.into();
871			Self::do_start_destroy(id, maybe_check_owner)
872		}
873
874		/// Destroy all accounts associated with a given asset.
875		///
876		/// `destroy_accounts` should only be called after `start_destroy` has been called, and the
877		/// asset is in a `Destroying` state.
878		///
879		/// Due to weight restrictions, this function may need to be called multiple times to fully
880		/// destroy all accounts. It will destroy `RemoveItemsLimit` accounts at a time.
881		///
882		/// - `id`: The identifier of the asset to be destroyed. This must identify an existing
883		///   asset.
884		///
885		/// Each call emits the `Event::DestroyedAccounts` event.
886		#[pallet::call_index(3)]
887		#[pallet::weight(T::WeightInfo::destroy_accounts(T::RemoveItemsLimit::get()))]
888		pub fn destroy_accounts(
889			origin: OriginFor<T>,
890			id: T::AssetIdParameter,
891		) -> DispatchResultWithPostInfo {
892			ensure_signed(origin)?;
893			let id: T::AssetId = id.into();
894			let removed_accounts = Self::do_destroy_accounts(id, T::RemoveItemsLimit::get())?;
895			Ok(Some(T::WeightInfo::destroy_accounts(removed_accounts)).into())
896		}
897
898		/// Destroy all approvals associated with a given asset up to the max (T::RemoveItemsLimit).
899		///
900		/// `destroy_approvals` should only be called after `start_destroy` has been called, and the
901		/// asset is in a `Destroying` state.
902		///
903		/// Due to weight restrictions, this function may need to be called multiple times to fully
904		/// destroy all approvals. It will destroy `RemoveItemsLimit` approvals at a time.
905		///
906		/// - `id`: The identifier of the asset to be destroyed. This must identify an existing
907		///   asset.
908		///
909		/// Each call emits the `Event::DestroyedApprovals` event.
910		#[pallet::call_index(4)]
911		#[pallet::weight(T::WeightInfo::destroy_approvals(T::RemoveItemsLimit::get()))]
912		pub fn destroy_approvals(
913			origin: OriginFor<T>,
914			id: T::AssetIdParameter,
915		) -> DispatchResultWithPostInfo {
916			ensure_signed(origin)?;
917			let id: T::AssetId = id.into();
918			let removed_approvals = Self::do_destroy_approvals(id, T::RemoveItemsLimit::get())?;
919			Ok(Some(T::WeightInfo::destroy_approvals(removed_approvals)).into())
920		}
921
922		/// Complete destroying asset and unreserve currency.
923		///
924		/// `finish_destroy` should only be called after `start_destroy` has been called, and the
925		/// asset is in a `Destroying` state. All accounts or approvals should be destroyed before
926		/// hand.
927		///
928		/// - `id`: The identifier of the asset to be destroyed. This must identify an existing
929		///   asset.
930		///
931		/// Each successful call emits the `Event::Destroyed` event.
932		#[pallet::call_index(5)]
933		pub fn finish_destroy(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
934			ensure_signed(origin)?;
935			let id: T::AssetId = id.into();
936			Self::do_finish_destroy(id)
937		}
938
939		/// Mint assets of a particular class.
940		///
941		/// The origin must be Signed and the sender must be the Issuer of the asset `id`.
942		///
943		/// - `id`: The identifier of the asset to have some amount minted.
944		/// - `beneficiary`: The account to be credited with the minted assets.
945		/// - `amount`: The amount of the asset to be minted.
946		///
947		/// Emits `Issued` event when successful.
948		///
949		/// Weight: `O(1)`
950		/// Modes: Pre-existing balance of `beneficiary`; Account pre-existence of `beneficiary`.
951		#[pallet::call_index(6)]
952		pub fn mint(
953			origin: OriginFor<T>,
954			id: T::AssetIdParameter,
955			beneficiary: AccountIdLookupOf<T>,
956			#[pallet::compact] amount: T::Balance,
957		) -> DispatchResult {
958			let origin = ensure_signed(origin)?;
959			let beneficiary = T::Lookup::lookup(beneficiary)?;
960			let id: T::AssetId = id.into();
961			Self::do_mint(id, &beneficiary, amount, Some(origin))?;
962			Ok(())
963		}
964
965		/// Reduce the balance of `who` by as much as possible up to `amount` assets of `id`.
966		///
967		/// Origin must be Signed and the sender should be the Manager of the asset `id`.
968		///
969		/// Bails with `NoAccount` if the `who` is already dead.
970		///
971		/// - `id`: The identifier of the asset to have some amount burned.
972		/// - `who`: The account to be debited from.
973		/// - `amount`: The maximum amount by which `who`'s balance should be reduced.
974		///
975		/// Emits `Burned` with the actual amount burned. If this takes the balance to below the
976		/// minimum for the asset, then the amount burned is increased to take it to zero.
977		///
978		/// Weight: `O(1)`
979		/// Modes: Post-existence of `who`; Pre & post Zombie-status of `who`.
980		#[pallet::call_index(7)]
981		pub fn burn(
982			origin: OriginFor<T>,
983			id: T::AssetIdParameter,
984			who: AccountIdLookupOf<T>,
985			#[pallet::compact] amount: T::Balance,
986		) -> DispatchResult {
987			let origin = ensure_signed(origin)?;
988			let who = T::Lookup::lookup(who)?;
989			let id: T::AssetId = id.into();
990
991			let f = DebitFlags { keep_alive: false, best_effort: true };
992			Self::do_burn(id, &who, amount, Some(origin), f)?;
993			Ok(())
994		}
995
996		/// Move some assets from the sender account to another.
997		///
998		/// Origin must be Signed.
999		///
1000		/// - `id`: The identifier of the asset to have some amount transferred.
1001		/// - `target`: The account to be credited.
1002		/// - `amount`: The amount by which the sender's balance of assets should be reduced and
1003		/// `target`'s balance increased. The amount actually transferred may be slightly greater in
1004		/// the case that the transfer would otherwise take the sender balance above zero but below
1005		/// the minimum balance. Must be greater than zero.
1006		///
1007		/// Emits `Transferred` with the actual amount transferred. If this takes the source balance
1008		/// to below the minimum for the asset, then the amount transferred is increased to take it
1009		/// to zero.
1010		///
1011		/// Weight: `O(1)`
1012		/// Modes: Pre-existence of `target`; Post-existence of sender; Account pre-existence of
1013		/// `target`.
1014		#[pallet::call_index(8)]
1015		pub fn transfer(
1016			origin: OriginFor<T>,
1017			id: T::AssetIdParameter,
1018			target: AccountIdLookupOf<T>,
1019			#[pallet::compact] amount: T::Balance,
1020		) -> DispatchResult {
1021			let origin = ensure_signed(origin)?;
1022			let dest = T::Lookup::lookup(target)?;
1023			let id: T::AssetId = id.into();
1024
1025			let f = TransferFlags { keep_alive: false, best_effort: false, burn_dust: false };
1026			Self::do_transfer(id, &origin, &dest, amount, None, f).map(|_| ())
1027		}
1028
1029		/// Move some assets from the sender account to another, keeping the sender account alive.
1030		///
1031		/// Origin must be Signed.
1032		///
1033		/// - `id`: The identifier of the asset to have some amount transferred.
1034		/// - `target`: The account to be credited.
1035		/// - `amount`: The amount by which the sender's balance of assets should be reduced and
1036		/// `target`'s balance increased. The amount actually transferred may be slightly greater in
1037		/// the case that the transfer would otherwise take the sender balance above zero but below
1038		/// the minimum balance. Must be greater than zero.
1039		///
1040		/// Emits `Transferred` with the actual amount transferred. If this takes the source balance
1041		/// to below the minimum for the asset, then the amount transferred is increased to take it
1042		/// to zero.
1043		///
1044		/// Weight: `O(1)`
1045		/// Modes: Pre-existence of `target`; Post-existence of sender; Account pre-existence of
1046		/// `target`.
1047		#[pallet::call_index(9)]
1048		pub fn transfer_keep_alive(
1049			origin: OriginFor<T>,
1050			id: T::AssetIdParameter,
1051			target: AccountIdLookupOf<T>,
1052			#[pallet::compact] amount: T::Balance,
1053		) -> DispatchResult {
1054			let source = ensure_signed(origin)?;
1055			let dest = T::Lookup::lookup(target)?;
1056			let id: T::AssetId = id.into();
1057
1058			let f = TransferFlags { keep_alive: true, best_effort: false, burn_dust: false };
1059			Self::do_transfer(id, &source, &dest, amount, None, f).map(|_| ())
1060		}
1061
1062		/// Move some assets from one account to another.
1063		///
1064		/// Origin must be Signed and the sender should be the Admin of the asset `id`.
1065		///
1066		/// - `id`: The identifier of the asset to have some amount transferred.
1067		/// - `source`: The account to be debited.
1068		/// - `dest`: The account to be credited.
1069		/// - `amount`: The amount by which the `source`'s balance of assets should be reduced and
1070		/// `dest`'s balance increased. The amount actually transferred may be slightly greater in
1071		/// the case that the transfer would otherwise take the `source` balance above zero but
1072		/// below the minimum balance. Must be greater than zero.
1073		///
1074		/// Emits `Transferred` with the actual amount transferred. If this takes the source balance
1075		/// to below the minimum for the asset, then the amount transferred is increased to take it
1076		/// to zero.
1077		///
1078		/// Weight: `O(1)`
1079		/// Modes: Pre-existence of `dest`; Post-existence of `source`; Account pre-existence of
1080		/// `dest`.
1081		#[pallet::call_index(10)]
1082		pub fn force_transfer(
1083			origin: OriginFor<T>,
1084			id: T::AssetIdParameter,
1085			source: AccountIdLookupOf<T>,
1086			dest: AccountIdLookupOf<T>,
1087			#[pallet::compact] amount: T::Balance,
1088		) -> DispatchResult {
1089			let origin = ensure_signed(origin)?;
1090			let source = T::Lookup::lookup(source)?;
1091			let dest = T::Lookup::lookup(dest)?;
1092			let id: T::AssetId = id.into();
1093
1094			let f = TransferFlags { keep_alive: false, best_effort: false, burn_dust: false };
1095			Self::do_transfer(id, &source, &dest, amount, Some(origin), f).map(|_| ())
1096		}
1097
1098		/// Disallow further unprivileged transfers of an asset `id` from an account `who`. `who`
1099		/// must already exist as an entry in `Account`s of the asset. If you want to freeze an
1100		/// account that does not have an entry, use `touch_other` first.
1101		///
1102		/// Origin must be Signed and the sender should be the Freezer of the asset `id`.
1103		///
1104		/// - `id`: The identifier of the asset to be frozen.
1105		/// - `who`: The account to be frozen.
1106		///
1107		/// Emits `Frozen`.
1108		///
1109		/// Weight: `O(1)`
1110		#[pallet::call_index(11)]
1111		pub fn freeze(
1112			origin: OriginFor<T>,
1113			id: T::AssetIdParameter,
1114			who: AccountIdLookupOf<T>,
1115		) -> DispatchResult {
1116			let origin = ensure_signed(origin)?;
1117			let id: T::AssetId = id.into();
1118
1119			let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1120			ensure!(
1121				d.status == AssetStatus::Live || d.status == AssetStatus::Frozen,
1122				Error::<T, I>::IncorrectStatus
1123			);
1124			ensure!(origin == d.freezer, Error::<T, I>::NoPermission);
1125			let who = T::Lookup::lookup(who)?;
1126
1127			Account::<T, I>::try_mutate(&id, &who, |maybe_account| -> DispatchResult {
1128				maybe_account.as_mut().ok_or(Error::<T, I>::NoAccount)?.status =
1129					AccountStatus::Frozen;
1130				Ok(())
1131			})?;
1132
1133			Self::deposit_event(Event::<T, I>::Frozen { asset_id: id, who });
1134			Ok(())
1135		}
1136
1137		/// Allow unprivileged transfers to and from an account again.
1138		///
1139		/// Origin must be Signed and the sender should be the Admin of the asset `id`.
1140		///
1141		/// - `id`: The identifier of the asset to be frozen.
1142		/// - `who`: The account to be unfrozen.
1143		///
1144		/// Emits `Thawed`.
1145		///
1146		/// Weight: `O(1)`
1147		#[pallet::call_index(12)]
1148		pub fn thaw(
1149			origin: OriginFor<T>,
1150			id: T::AssetIdParameter,
1151			who: AccountIdLookupOf<T>,
1152		) -> DispatchResult {
1153			let origin = ensure_signed(origin)?;
1154			let id: T::AssetId = id.into();
1155
1156			let details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1157			ensure!(
1158				details.status == AssetStatus::Live || details.status == AssetStatus::Frozen,
1159				Error::<T, I>::IncorrectStatus
1160			);
1161			ensure!(origin == details.admin, Error::<T, I>::NoPermission);
1162			let who = T::Lookup::lookup(who)?;
1163
1164			Account::<T, I>::try_mutate(&id, &who, |maybe_account| -> DispatchResult {
1165				maybe_account.as_mut().ok_or(Error::<T, I>::NoAccount)?.status =
1166					AccountStatus::Liquid;
1167				Ok(())
1168			})?;
1169
1170			Self::deposit_event(Event::<T, I>::Thawed { asset_id: id, who });
1171			Ok(())
1172		}
1173
1174		/// Disallow further unprivileged transfers for the asset class.
1175		///
1176		/// Origin must be Signed and the sender should be the Freezer of the asset `id`.
1177		///
1178		/// - `id`: The identifier of the asset to be frozen.
1179		///
1180		/// Emits `Frozen`.
1181		///
1182		/// Weight: `O(1)`
1183		#[pallet::call_index(13)]
1184		pub fn freeze_asset(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
1185			let origin = ensure_signed(origin)?;
1186			let id: T::AssetId = id.into();
1187
1188			Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
1189				let d = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
1190				ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
1191				ensure!(origin == d.freezer, Error::<T, I>::NoPermission);
1192
1193				d.status = AssetStatus::Frozen;
1194
1195				Self::deposit_event(Event::<T, I>::AssetFrozen { asset_id: id });
1196				Ok(())
1197			})
1198		}
1199
1200		/// Allow unprivileged transfers for the asset again.
1201		///
1202		/// Origin must be Signed and the sender should be the Admin of the asset `id`.
1203		///
1204		/// - `id`: The identifier of the asset to be thawed.
1205		///
1206		/// Emits `Thawed`.
1207		///
1208		/// Weight: `O(1)`
1209		#[pallet::call_index(14)]
1210		pub fn thaw_asset(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
1211			let origin = ensure_signed(origin)?;
1212			let id: T::AssetId = id.into();
1213
1214			Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
1215				let d = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
1216				ensure!(origin == d.admin, Error::<T, I>::NoPermission);
1217				ensure!(d.status == AssetStatus::Frozen, Error::<T, I>::NotFrozen);
1218
1219				d.status = AssetStatus::Live;
1220
1221				Self::deposit_event(Event::<T, I>::AssetThawed { asset_id: id });
1222				Ok(())
1223			})
1224		}
1225
1226		/// Change the Owner of an asset.
1227		///
1228		/// Origin must be Signed and the sender should be the Owner of the asset `id`.
1229		///
1230		/// - `id`: The identifier of the asset.
1231		/// - `owner`: The new Owner of this asset.
1232		///
1233		/// Emits `OwnerChanged`.
1234		///
1235		/// Weight: `O(1)`
1236		#[pallet::call_index(15)]
1237		pub fn transfer_ownership(
1238			origin: OriginFor<T>,
1239			id: T::AssetIdParameter,
1240			owner: AccountIdLookupOf<T>,
1241		) -> DispatchResult {
1242			let origin = ensure_signed(origin)?;
1243			let owner = T::Lookup::lookup(owner)?;
1244			let id: T::AssetId = id.into();
1245
1246			Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
1247				let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
1248				ensure!(details.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
1249				ensure!(origin == details.owner, Error::<T, I>::NoPermission);
1250				if details.owner == owner {
1251					return Ok(());
1252				}
1253
1254				let metadata_deposit = Metadata::<T, I>::get(&id).deposit;
1255				let deposit = details.deposit + metadata_deposit;
1256
1257				// Move the deposit to the new owner.
1258				T::Currency::repatriate_reserved(&details.owner, &owner, deposit, Reserved)?;
1259
1260				details.owner = owner.clone();
1261
1262				Self::deposit_event(Event::OwnerChanged { asset_id: id, owner });
1263				Ok(())
1264			})
1265		}
1266
1267		/// Change the Issuer, Admin and Freezer of an asset.
1268		///
1269		/// Origin must be Signed and the sender should be the Owner of the asset `id`.
1270		///
1271		/// - `id`: The identifier of the asset to be frozen.
1272		/// - `issuer`: The new Issuer of this asset.
1273		/// - `admin`: The new Admin of this asset.
1274		/// - `freezer`: The new Freezer of this asset.
1275		///
1276		/// Emits `TeamChanged`.
1277		///
1278		/// Weight: `O(1)`
1279		#[pallet::call_index(16)]
1280		pub fn set_team(
1281			origin: OriginFor<T>,
1282			id: T::AssetIdParameter,
1283			issuer: AccountIdLookupOf<T>,
1284			admin: AccountIdLookupOf<T>,
1285			freezer: AccountIdLookupOf<T>,
1286		) -> DispatchResult {
1287			let origin = ensure_signed(origin)?;
1288			let issuer = T::Lookup::lookup(issuer)?;
1289			let admin = T::Lookup::lookup(admin)?;
1290			let freezer = T::Lookup::lookup(freezer)?;
1291			let id: T::AssetId = id.into();
1292
1293			Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
1294				let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
1295				ensure!(details.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
1296				ensure!(origin == details.owner, Error::<T, I>::NoPermission);
1297
1298				details.issuer = issuer.clone();
1299				details.admin = admin.clone();
1300				details.freezer = freezer.clone();
1301
1302				Self::deposit_event(Event::TeamChanged { asset_id: id, issuer, admin, freezer });
1303				Ok(())
1304			})
1305		}
1306
1307		/// Set the metadata for an asset.
1308		///
1309		/// Origin must be Signed and the sender should be the Owner of the asset `id`.
1310		///
1311		/// Funds of sender are reserved according to the formula:
1312		/// `MetadataDepositBase + MetadataDepositPerByte * (name.len + symbol.len)` taking into
1313		/// account any already reserved funds.
1314		///
1315		/// - `id`: The identifier of the asset to update.
1316		/// - `name`: The user friendly name of this asset. Limited in length by `StringLimit`.
1317		/// - `symbol`: The exchange symbol for this asset. Limited in length by `StringLimit`.
1318		/// - `decimals`: The number of decimals this asset uses to represent one unit.
1319		///
1320		/// Emits `MetadataSet`.
1321		///
1322		/// Weight: `O(1)`
1323		#[pallet::call_index(17)]
1324		#[pallet::weight(T::WeightInfo::set_metadata(name.len() as u32, symbol.len() as u32))]
1325		pub fn set_metadata(
1326			origin: OriginFor<T>,
1327			id: T::AssetIdParameter,
1328			name: Vec<u8>,
1329			symbol: Vec<u8>,
1330			decimals: u8,
1331		) -> DispatchResult {
1332			let origin = ensure_signed(origin)?;
1333			let id: T::AssetId = id.into();
1334			Self::do_set_metadata(id, &origin, name, symbol, decimals)
1335		}
1336
1337		/// Clear the metadata for an asset.
1338		///
1339		/// Origin must be Signed and the sender should be the Owner of the asset `id`.
1340		///
1341		/// Any deposit is freed for the asset owner.
1342		///
1343		/// - `id`: The identifier of the asset to clear.
1344		///
1345		/// Emits `MetadataCleared`.
1346		///
1347		/// Weight: `O(1)`
1348		#[pallet::call_index(18)]
1349		pub fn clear_metadata(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
1350			let origin = ensure_signed(origin)?;
1351			let id: T::AssetId = id.into();
1352
1353			let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1354			ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
1355			ensure!(origin == d.owner, Error::<T, I>::NoPermission);
1356
1357			Metadata::<T, I>::try_mutate_exists(id.clone(), |metadata| {
1358				let deposit = metadata.take().ok_or(Error::<T, I>::Unknown)?.deposit;
1359				T::Currency::unreserve(&d.owner, deposit);
1360				Self::deposit_event(Event::MetadataCleared { asset_id: id });
1361				Ok(())
1362			})
1363		}
1364
1365		/// Force the metadata for an asset to some value.
1366		///
1367		/// Origin must be ForceOrigin.
1368		///
1369		/// Any deposit is left alone.
1370		///
1371		/// - `id`: The identifier of the asset to update.
1372		/// - `name`: The user friendly name of this asset. Limited in length by `StringLimit`.
1373		/// - `symbol`: The exchange symbol for this asset. Limited in length by `StringLimit`.
1374		/// - `decimals`: The number of decimals this asset uses to represent one unit.
1375		///
1376		/// Emits `MetadataSet`.
1377		///
1378		/// Weight: `O(N + S)` where N and S are the length of the name and symbol respectively.
1379		#[pallet::call_index(19)]
1380		#[pallet::weight(T::WeightInfo::force_set_metadata(name.len() as u32, symbol.len() as u32))]
1381		pub fn force_set_metadata(
1382			origin: OriginFor<T>,
1383			id: T::AssetIdParameter,
1384			name: Vec<u8>,
1385			symbol: Vec<u8>,
1386			decimals: u8,
1387			is_frozen: bool,
1388		) -> DispatchResult {
1389			T::ForceOrigin::ensure_origin(origin)?;
1390			let id: T::AssetId = id.into();
1391
1392			let bounded_name: BoundedVec<u8, T::StringLimit> =
1393				name.clone().try_into().map_err(|_| Error::<T, I>::BadMetadata)?;
1394
1395			let bounded_symbol: BoundedVec<u8, T::StringLimit> =
1396				symbol.clone().try_into().map_err(|_| Error::<T, I>::BadMetadata)?;
1397
1398			ensure!(Asset::<T, I>::contains_key(&id), Error::<T, I>::Unknown);
1399			Metadata::<T, I>::try_mutate_exists(id.clone(), |metadata| {
1400				let deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit);
1401				*metadata = Some(AssetMetadata {
1402					deposit,
1403					name: bounded_name,
1404					symbol: bounded_symbol,
1405					decimals,
1406					is_frozen,
1407				});
1408
1409				Self::deposit_event(Event::MetadataSet {
1410					asset_id: id,
1411					name,
1412					symbol,
1413					decimals,
1414					is_frozen,
1415				});
1416				Ok(())
1417			})
1418		}
1419
1420		/// Clear the metadata for an asset.
1421		///
1422		/// Origin must be ForceOrigin.
1423		///
1424		/// Any deposit is returned.
1425		///
1426		/// - `id`: The identifier of the asset to clear.
1427		///
1428		/// Emits `MetadataCleared`.
1429		///
1430		/// Weight: `O(1)`
1431		#[pallet::call_index(20)]
1432		pub fn force_clear_metadata(
1433			origin: OriginFor<T>,
1434			id: T::AssetIdParameter,
1435		) -> DispatchResult {
1436			T::ForceOrigin::ensure_origin(origin)?;
1437			let id: T::AssetId = id.into();
1438
1439			let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1440			Metadata::<T, I>::try_mutate_exists(id.clone(), |metadata| {
1441				let deposit = metadata.take().ok_or(Error::<T, I>::Unknown)?.deposit;
1442				T::Currency::unreserve(&d.owner, deposit);
1443				Self::deposit_event(Event::MetadataCleared { asset_id: id });
1444				Ok(())
1445			})
1446		}
1447
1448		/// Alter the attributes of a given asset.
1449		///
1450		/// Origin must be `ForceOrigin`.
1451		///
1452		/// - `id`: The identifier of the asset.
1453		/// - `owner`: The new Owner of this asset.
1454		/// - `issuer`: The new Issuer of this asset.
1455		/// - `admin`: The new Admin of this asset.
1456		/// - `freezer`: The new Freezer of this asset.
1457		/// - `min_balance`: The minimum balance of this new asset that any single account must
1458		/// have. If an account's balance is reduced below this, then it collapses to zero.
1459		/// - `is_sufficient`: Whether a non-zero balance of this asset is deposit of sufficient
1460		/// value to account for the state bloat associated with its balance storage. If set to
1461		/// `true`, then non-zero balances may be stored without a `consumer` reference (and thus
1462		/// an ED in the Balances pallet or whatever else is used to control user-account state
1463		/// growth).
1464		/// - `is_frozen`: Whether this asset class is frozen except for permissioned/admin
1465		/// instructions.
1466		///
1467		/// Emits `AssetStatusChanged` with the identity of the asset.
1468		///
1469		/// Weight: `O(1)`
1470		#[pallet::call_index(21)]
1471		pub fn force_asset_status(
1472			origin: OriginFor<T>,
1473			id: T::AssetIdParameter,
1474			owner: AccountIdLookupOf<T>,
1475			issuer: AccountIdLookupOf<T>,
1476			admin: AccountIdLookupOf<T>,
1477			freezer: AccountIdLookupOf<T>,
1478			#[pallet::compact] min_balance: T::Balance,
1479			is_sufficient: bool,
1480			is_frozen: bool,
1481		) -> DispatchResult {
1482			T::ForceOrigin::ensure_origin(origin)?;
1483			let id: T::AssetId = id.into();
1484
1485			Asset::<T, I>::try_mutate(id.clone(), |maybe_asset| {
1486				let mut asset = maybe_asset.take().ok_or(Error::<T, I>::Unknown)?;
1487				ensure!(asset.status != AssetStatus::Destroying, Error::<T, I>::AssetNotLive);
1488				asset.owner = T::Lookup::lookup(owner)?;
1489				asset.issuer = T::Lookup::lookup(issuer)?;
1490				asset.admin = T::Lookup::lookup(admin)?;
1491				asset.freezer = T::Lookup::lookup(freezer)?;
1492				asset.min_balance = min_balance;
1493				asset.is_sufficient = is_sufficient;
1494				if is_frozen {
1495					asset.status = AssetStatus::Frozen;
1496				} else {
1497					asset.status = AssetStatus::Live;
1498				}
1499				*maybe_asset = Some(asset);
1500
1501				Self::deposit_event(Event::AssetStatusChanged { asset_id: id });
1502				Ok(())
1503			})
1504		}
1505
1506		/// Approve an amount of asset for transfer by a delegated third-party account.
1507		///
1508		/// Origin must be Signed.
1509		///
1510		/// Ensures that `ApprovalDeposit` worth of `Currency` is reserved from signing account
1511		/// for the purpose of holding the approval. If some non-zero amount of assets is already
1512		/// approved from signing account to `delegate`, then it is topped up or unreserved to
1513		/// meet the right value.
1514		///
1515		/// NOTE: The signing account does not need to own `amount` of assets at the point of
1516		/// making this call.
1517		///
1518		/// - `id`: The identifier of the asset.
1519		/// - `delegate`: The account to delegate permission to transfer asset.
1520		/// - `amount`: The amount of asset that may be transferred by `delegate`. If there is
1521		/// already an approval in place, then this acts additively.
1522		///
1523		/// Emits `ApprovedTransfer` on success.
1524		///
1525		/// Weight: `O(1)`
1526		#[pallet::call_index(22)]
1527		pub fn approve_transfer(
1528			origin: OriginFor<T>,
1529			id: T::AssetIdParameter,
1530			delegate: AccountIdLookupOf<T>,
1531			#[pallet::compact] amount: T::Balance,
1532		) -> DispatchResult {
1533			let owner = ensure_signed(origin)?;
1534			let delegate = T::Lookup::lookup(delegate)?;
1535			let id: T::AssetId = id.into();
1536			Self::do_approve_transfer(id, &owner, &delegate, amount)
1537		}
1538
1539		/// Cancel all of some asset approved for delegated transfer by a third-party account.
1540		///
1541		/// Origin must be Signed and there must be an approval in place between signer and
1542		/// `delegate`.
1543		///
1544		/// Unreserves any deposit previously reserved by `approve_transfer` for the approval.
1545		///
1546		/// - `id`: The identifier of the asset.
1547		/// - `delegate`: The account delegated permission to transfer asset.
1548		///
1549		/// Emits `ApprovalCancelled` on success.
1550		///
1551		/// Weight: `O(1)`
1552		#[pallet::call_index(23)]
1553		pub fn cancel_approval(
1554			origin: OriginFor<T>,
1555			id: T::AssetIdParameter,
1556			delegate: AccountIdLookupOf<T>,
1557		) -> DispatchResult {
1558			let owner = ensure_signed(origin)?;
1559			let delegate = T::Lookup::lookup(delegate)?;
1560			let id: T::AssetId = id.into();
1561			let mut d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1562			ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
1563
1564			let approval = Approvals::<T, I>::take((id.clone(), &owner, &delegate))
1565				.ok_or(Error::<T, I>::Unknown)?;
1566			T::Currency::unreserve(&owner, approval.deposit);
1567
1568			d.approvals.saturating_dec();
1569			Asset::<T, I>::insert(id.clone(), d);
1570
1571			Self::deposit_event(Event::ApprovalCancelled { asset_id: id, owner, delegate });
1572			Ok(())
1573		}
1574
1575		/// Cancel all of some asset approved for delegated transfer by a third-party account.
1576		///
1577		/// Origin must be either ForceOrigin or Signed origin with the signer being the Admin
1578		/// account of the asset `id`.
1579		///
1580		/// Unreserves any deposit previously reserved by `approve_transfer` for the approval.
1581		///
1582		/// - `id`: The identifier of the asset.
1583		/// - `delegate`: The account delegated permission to transfer asset.
1584		///
1585		/// Emits `ApprovalCancelled` on success.
1586		///
1587		/// Weight: `O(1)`
1588		#[pallet::call_index(24)]
1589		pub fn force_cancel_approval(
1590			origin: OriginFor<T>,
1591			id: T::AssetIdParameter,
1592			owner: AccountIdLookupOf<T>,
1593			delegate: AccountIdLookupOf<T>,
1594		) -> DispatchResult {
1595			let id: T::AssetId = id.into();
1596			let mut d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1597			ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
1598			T::ForceOrigin::try_origin(origin)
1599				.map(|_| ())
1600				.or_else(|origin| -> DispatchResult {
1601					let origin = ensure_signed(origin)?;
1602					ensure!(origin == d.admin, Error::<T, I>::NoPermission);
1603					Ok(())
1604				})?;
1605
1606			let owner = T::Lookup::lookup(owner)?;
1607			let delegate = T::Lookup::lookup(delegate)?;
1608
1609			let approval = Approvals::<T, I>::take((id.clone(), &owner, &delegate))
1610				.ok_or(Error::<T, I>::Unknown)?;
1611			T::Currency::unreserve(&owner, approval.deposit);
1612			d.approvals.saturating_dec();
1613			Asset::<T, I>::insert(id.clone(), d);
1614
1615			Self::deposit_event(Event::ApprovalCancelled { asset_id: id, owner, delegate });
1616			Ok(())
1617		}
1618
1619		/// Transfer some asset balance from a previously delegated account to some third-party
1620		/// account.
1621		///
1622		/// Origin must be Signed and there must be an approval in place by the `owner` to the
1623		/// signer.
1624		///
1625		/// If the entire amount approved for transfer is transferred, then any deposit previously
1626		/// reserved by `approve_transfer` is unreserved.
1627		///
1628		/// - `id`: The identifier of the asset.
1629		/// - `owner`: The account which previously approved for a transfer of at least `amount` and
1630		/// from which the asset balance will be withdrawn.
1631		/// - `destination`: The account to which the asset balance of `amount` will be transferred.
1632		/// - `amount`: The amount of assets to transfer.
1633		///
1634		/// Emits `TransferredApproved` on success.
1635		///
1636		/// Weight: `O(1)`
1637		#[pallet::call_index(25)]
1638		pub fn transfer_approved(
1639			origin: OriginFor<T>,
1640			id: T::AssetIdParameter,
1641			owner: AccountIdLookupOf<T>,
1642			destination: AccountIdLookupOf<T>,
1643			#[pallet::compact] amount: T::Balance,
1644		) -> DispatchResult {
1645			let delegate = ensure_signed(origin)?;
1646			let owner = T::Lookup::lookup(owner)?;
1647			let destination = T::Lookup::lookup(destination)?;
1648			let id: T::AssetId = id.into();
1649			Self::do_transfer_approved(id, &owner, &delegate, &destination, amount)
1650		}
1651
1652		/// Create an asset account for non-provider assets.
1653		///
1654		/// A deposit will be taken from the signer account.
1655		///
1656		/// - `origin`: Must be Signed; the signer account must have sufficient funds for a deposit
1657		///   to be taken.
1658		/// - `id`: The identifier of the asset for the account to be created.
1659		///
1660		/// Emits `Touched` event when successful.
1661		#[pallet::call_index(26)]
1662		#[pallet::weight(T::WeightInfo::touch())]
1663		pub fn touch(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
1664			let who = ensure_signed(origin)?;
1665			let id: T::AssetId = id.into();
1666			Self::do_touch(id, who.clone(), who)
1667		}
1668
1669		/// Return the deposit (if any) of an asset account or a consumer reference (if any) of an
1670		/// account.
1671		///
1672		/// The origin must be Signed.
1673		///
1674		/// - `id`: The identifier of the asset for which the caller would like the deposit
1675		///   refunded.
1676		/// - `allow_burn`: If `true` then assets may be destroyed in order to complete the refund.
1677		///
1678		/// It will fail with either [`Error::ContainsHolds`] or [`Error::ContainsFreezes`] if
1679		/// the asset account contains holds or freezes in place.
1680		///
1681		/// Emits `Refunded` event when successful.
1682		#[pallet::call_index(27)]
1683		#[pallet::weight(T::WeightInfo::refund())]
1684		pub fn refund(
1685			origin: OriginFor<T>,
1686			id: T::AssetIdParameter,
1687			allow_burn: bool,
1688		) -> DispatchResult {
1689			let id: T::AssetId = id.into();
1690			Self::do_refund(id, ensure_signed(origin)?, allow_burn)
1691		}
1692
1693		/// Sets the minimum balance of an asset.
1694		///
1695		/// Only works if there aren't any accounts that are holding the asset or if
1696		/// the new value of `min_balance` is less than the old one.
1697		///
1698		/// Origin must be Signed and the sender has to be the Owner of the
1699		/// asset `id`.
1700		///
1701		/// - `id`: The identifier of the asset.
1702		/// - `min_balance`: The new value of `min_balance`.
1703		///
1704		/// Emits `AssetMinBalanceChanged` event when successful.
1705		#[pallet::call_index(28)]
1706		pub fn set_min_balance(
1707			origin: OriginFor<T>,
1708			id: T::AssetIdParameter,
1709			min_balance: T::Balance,
1710		) -> DispatchResult {
1711			let origin = ensure_signed(origin)?;
1712			let id: T::AssetId = id.into();
1713
1714			let mut details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1715			ensure!(origin == details.owner, Error::<T, I>::NoPermission);
1716
1717			let old_min_balance = details.min_balance;
1718			// If the asset is marked as sufficient it won't be allowed to
1719			// change the min_balance.
1720			ensure!(!details.is_sufficient, Error::<T, I>::NoPermission);
1721
1722			// Ensure that either the new min_balance is less than old
1723			// min_balance or there aren't any accounts holding the asset.
1724			ensure!(
1725				min_balance < old_min_balance || details.accounts == 0,
1726				Error::<T, I>::NoPermission
1727			);
1728
1729			details.min_balance = min_balance;
1730			Asset::<T, I>::insert(&id, details);
1731
1732			Self::deposit_event(Event::AssetMinBalanceChanged {
1733				asset_id: id,
1734				new_min_balance: min_balance,
1735			});
1736			Ok(())
1737		}
1738
1739		/// Create an asset account for `who`.
1740		///
1741		/// A deposit will be taken from the signer account.
1742		///
1743		/// - `origin`: Must be Signed; the signer account must have sufficient funds for a deposit
1744		///   to be taken.
1745		/// - `id`: The identifier of the asset for the account to be created, the asset status must
1746		///   be live.
1747		/// - `who`: The account to be created.
1748		///
1749		/// Emits `Touched` event when successful.
1750		#[pallet::call_index(29)]
1751		#[pallet::weight(T::WeightInfo::touch_other())]
1752		pub fn touch_other(
1753			origin: OriginFor<T>,
1754			id: T::AssetIdParameter,
1755			who: AccountIdLookupOf<T>,
1756		) -> DispatchResult {
1757			let origin = ensure_signed(origin)?;
1758			let who = T::Lookup::lookup(who)?;
1759			let id: T::AssetId = id.into();
1760			Self::do_touch(id, who, origin)
1761		}
1762
1763		/// Return the deposit (if any) of a target asset account. Useful if you are the depositor.
1764		///
1765		/// The origin must be Signed and either the account owner, depositor, or asset `Admin`. In
1766		/// order to burn a non-zero balance of the asset, the caller must be the account and should
1767		/// use `refund`.
1768		///
1769		/// - `id`: The identifier of the asset for the account holding a deposit.
1770		/// - `who`: The account to refund.
1771		///
1772		/// It will fail with either [`Error::ContainsHolds`] or [`Error::ContainsFreezes`] if
1773		/// the asset account contains holds or freezes in place.
1774		///
1775		/// Emits `Refunded` event when successful.
1776		#[pallet::call_index(30)]
1777		#[pallet::weight(T::WeightInfo::refund_other())]
1778		pub fn refund_other(
1779			origin: OriginFor<T>,
1780			id: T::AssetIdParameter,
1781			who: AccountIdLookupOf<T>,
1782		) -> DispatchResult {
1783			let origin = ensure_signed(origin)?;
1784			let who = T::Lookup::lookup(who)?;
1785			let id: T::AssetId = id.into();
1786			Self::do_refund_other(id, &who, Some(origin))
1787		}
1788
1789		/// Disallow further unprivileged transfers of an asset `id` to and from an account `who`.
1790		///
1791		/// Origin must be Signed and the sender should be the Freezer of the asset `id`.
1792		///
1793		/// - `id`: The identifier of the account's asset.
1794		/// - `who`: The account to be unblocked.
1795		///
1796		/// Emits `Blocked`.
1797		///
1798		/// Weight: `O(1)`
1799		#[pallet::call_index(31)]
1800		pub fn block(
1801			origin: OriginFor<T>,
1802			id: T::AssetIdParameter,
1803			who: AccountIdLookupOf<T>,
1804		) -> DispatchResult {
1805			let origin = ensure_signed(origin)?;
1806			let id: T::AssetId = id.into();
1807
1808			let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1809			ensure!(
1810				d.status == AssetStatus::Live || d.status == AssetStatus::Frozen,
1811				Error::<T, I>::IncorrectStatus
1812			);
1813			ensure!(origin == d.freezer, Error::<T, I>::NoPermission);
1814			let who = T::Lookup::lookup(who)?;
1815
1816			Account::<T, I>::try_mutate(&id, &who, |maybe_account| -> DispatchResult {
1817				maybe_account.as_mut().ok_or(Error::<T, I>::NoAccount)?.status =
1818					AccountStatus::Blocked;
1819				Ok(())
1820			})?;
1821
1822			Self::deposit_event(Event::<T, I>::Blocked { asset_id: id, who });
1823			Ok(())
1824		}
1825
1826		/// Transfer the entire transferable balance from the caller asset account.
1827		///
1828		/// NOTE: This function only attempts to transfer _transferable_ balances. This means that
1829		/// any held, frozen, or minimum balance (when `keep_alive` is `true`), will not be
1830		/// transferred by this function. To ensure that this function results in a killed account,
1831		/// you might need to prepare the account by removing any reference counters, storage
1832		/// deposits, etc...
1833		///
1834		/// The dispatch origin of this call must be Signed.
1835		///
1836		/// - `id`: The identifier of the asset for the account holding a deposit.
1837		/// - `dest`: The recipient of the transfer.
1838		/// - `keep_alive`: A boolean to determine if the `transfer_all` operation should send all
1839		///   of the funds the asset account has, causing the sender asset account to be killed
1840		///   (false), or transfer everything except at least the minimum balance, which will
1841		///   guarantee to keep the sender asset account alive (true).
1842		#[pallet::call_index(32)]
1843		#[pallet::weight(T::WeightInfo::transfer_all())]
1844		pub fn transfer_all(
1845			origin: OriginFor<T>,
1846			id: T::AssetIdParameter,
1847			dest: AccountIdLookupOf<T>,
1848			keep_alive: bool,
1849		) -> DispatchResult {
1850			let transactor = ensure_signed(origin)?;
1851			let keep_alive = if keep_alive { Preserve } else { Expendable };
1852			let reducible_balance = <Self as fungibles::Inspect<_>>::reducible_balance(
1853				id.clone().into(),
1854				&transactor,
1855				keep_alive,
1856				Fortitude::Polite,
1857			);
1858			let dest = T::Lookup::lookup(dest)?;
1859			<Self as fungibles::Mutate<_>>::transfer(
1860				id.into(),
1861				&transactor,
1862				&dest,
1863				reducible_balance,
1864				keep_alive,
1865			)?;
1866			Ok(())
1867		}
1868
1869		/// Sets the trusted reserve information of an asset.
1870		///
1871		/// Origin must be the Owner of the asset `id`. The origin must conform to the configured
1872		/// `CreateOrigin` or be the signed `owner` configured during asset creation.
1873		///
1874		/// - `id`: The identifier of the asset.
1875		/// - `reserves`: The full list of trusted reserves information.
1876		///
1877		/// Emits `AssetMinBalanceChanged` event when successful.
1878		#[pallet::call_index(33)]
1879		#[pallet::weight(T::WeightInfo::set_reserves())]
1880		pub fn set_reserves(
1881			origin: OriginFor<T>,
1882			id: T::AssetIdParameter,
1883			reserves: Vec<T::ReserveData>,
1884		) -> DispatchResult {
1885			let id: T::AssetId = id.into();
1886			let origin = ensure_signed(origin.clone())
1887				.or_else(|_| T::CreateOrigin::ensure_origin(origin, &id))?;
1888
1889			let details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1890			ensure!(origin == details.owner, Error::<T, I>::NoPermission);
1891
1892			Self::unchecked_update_reserves(id, reserves)?;
1893			Ok(())
1894		}
1895	}
1896
1897	#[pallet::view_functions]
1898	impl<T: Config<I>, I: 'static> Pallet<T, I> {
1899		/// Provide the asset details for asset `id`.
1900		pub fn asset_details(
1901			id: T::AssetId,
1902		) -> Option<AssetDetails<T::Balance, T::AccountId, DepositBalanceOf<T, I>>> {
1903			Asset::<T, I>::get(id)
1904		}
1905
1906		/// Provide the balance of `who` for asset `id`.
1907		pub fn balance_of(who: T::AccountId, id: T::AssetId) -> Option<<T as Config<I>>::Balance> {
1908			Account::<T, I>::get(id, who).map(|account| account.balance)
1909		}
1910
1911		/// Provide the configured metadata for asset `id`.
1912		pub fn get_metadata(
1913			id: T::AssetId,
1914		) -> Option<AssetMetadata<DepositBalanceOf<T, I>, BoundedVec<u8, T::StringLimit>>> {
1915			Metadata::<T, I>::try_get(id).ok()
1916		}
1917
1918		/// Provide the configured reserves data for asset `id`.
1919		pub fn get_reserves_data(id: T::AssetId) -> Vec<T::ReserveData> {
1920			Self::reserves(&id)
1921		}
1922	}
1923
1924	/// Implements [`AccountTouch`] trait.
1925	/// Note that a depositor can be any account, without any specific privilege.
1926	impl<T: Config<I>, I: 'static> AccountTouch<T::AssetId, T::AccountId> for Pallet<T, I> {
1927		type Balance = DepositBalanceOf<T, I>;
1928
1929		fn deposit_required(_: T::AssetId) -> Self::Balance {
1930			T::AssetAccountDeposit::get()
1931		}
1932
1933		fn should_touch(asset: T::AssetId, who: &T::AccountId) -> bool {
1934			match Asset::<T, I>::get(&asset) {
1935				// refer to the [`Self::new_account`] function for more details.
1936				Some(info) if info.is_sufficient => false,
1937				Some(_) if frame_system::Pallet::<T>::can_accrue_consumers(who, 2) => false,
1938				Some(_) => !Account::<T, I>::contains_key(asset, who),
1939				_ => true,
1940			}
1941		}
1942
1943		fn touch(
1944			asset: T::AssetId,
1945			who: &T::AccountId,
1946			depositor: &T::AccountId,
1947		) -> DispatchResult {
1948			Self::do_touch(asset, who.clone(), depositor.clone())
1949		}
1950	}
1951
1952	/// Implements [`ContainsPair`] trait for a pair of asset and account IDs.
1953	impl<T: Config<I>, I: 'static> ContainsPair<T::AssetId, T::AccountId> for Pallet<T, I> {
1954		/// Check if an account with the given asset ID and account address exists.
1955		fn contains(asset: &T::AssetId, who: &T::AccountId) -> bool {
1956			Account::<T, I>::contains_key(asset, who)
1957		}
1958	}
1959
1960	/// Implements [`ProvideAssetReserves`] trait for getting the list of trusted reserves for a
1961	/// given asset.
1962	impl<T: Config<I>, I: 'static> ProvideAssetReserves<T::AssetId, T::ReserveData> for Pallet<T, I> {
1963		/// Provide the configured reserves for asset `id`.
1964		fn reserves(id: &T::AssetId) -> Vec<T::ReserveData> {
1965			Reserves::<T, I>::get(id).into_inner()
1966		}
1967	}
1968}
1969
1970#[cfg(any(feature = "try-runtime", test))]
1971impl<T: Config<I>, I: 'static> Pallet<T, I> {
1972	pub fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
1973		for asset_id in Reserves::<T, I>::iter_keys() {
1974			ensure!(Asset::<T, I>::contains_key(asset_id.clone()), "Orphaned Reserves data found");
1975		}
1976
1977		for asset_id in Metadata::<T, I>::iter_keys() {
1978			ensure!(Asset::<T, I>::contains_key(asset_id.clone()), "Orphaned Metadata found");
1979		}
1980
1981		for (asset_id, _, _) in Approvals::<T, I>::iter_keys() {
1982			ensure!(Asset::<T, I>::contains_key(asset_id.clone()), "Orphaned Approval found");
1983		}
1984
1985		for (asset_id, _) in Account::<T, I>::iter_keys() {
1986			ensure!(Asset::<T, I>::contains_key(asset_id.clone()), "Orphaned Account found");
1987		}
1988
1989		for (asset_id, details) in Asset::<T, I>::iter() {
1990			if details.status == AssetStatus::Destroying {
1991				continue;
1992			}
1993
1994			let mut calculated_supply = T::Balance::zero();
1995			let mut calculated_accounts = 0u32;
1996			let mut calculated_sufficients = 0u32;
1997
1998			for (who, account) in Account::<T, I>::iter_prefix(&asset_id) {
1999				let held = T::Holder::balance_on_hold(asset_id.clone(), &who).unwrap_or_default();
2000				calculated_supply =
2001					calculated_supply.saturating_add(account.balance).saturating_add(held);
2002				calculated_accounts += 1;
2003
2004				if matches!(account.reason, ExistenceReason::Sufficient) {
2005					calculated_sufficients += 1;
2006				}
2007
2008				if account.balance < details.min_balance {
2009					ensure!(
2010						matches!(
2011							account.reason,
2012							ExistenceReason::DepositHeld(_) | ExistenceReason::DepositFrom(_, _)
2013						),
2014						"Account below min_balance must have a deposit"
2015					);
2016				}
2017			}
2018
2019			// Using >= instead of == because the provided `do_refund` implementation destroys
2020			// the account balance without decrementing the asset supply. This causes the
2021			// tracked supply to permanently exceed the actual sum of balances + holds
2022			// whenever a refund occurs.
2023			ensure!(details.supply >= calculated_supply, "Asset supply mismatch");
2024			ensure!(details.accounts == calculated_accounts, "Asset account count mismatch");
2025			ensure!(
2026				details.sufficients == calculated_sufficients,
2027				"Asset sufficients count mismatch"
2028			);
2029
2030			let calculated_approvals = Approvals::<T, I>::iter_prefix((&asset_id,)).count() as u32;
2031
2032			if details.approvals != calculated_approvals {
2033				log::error!(
2034					"Asset {asset_id:?} approvals count mismatch: calculated {calculated_approvals} vs expected {}",
2035					details.approvals,
2036				);
2037
2038				return Err("Asset approvals count mismatch".into())
2039			}
2040		}
2041		Ok(())
2042	}
2043}
2044
2045sp_core::generate_feature_enabled_macro!(runtime_benchmarks_enabled, feature = "runtime-benchmarks", $);