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 /// Some assets were issued as Credit (no owner yet).
686 IssuedCredit { asset_id: T::AssetId, amount: T::Balance },
687 /// Some assets Credit was destroyed.
688 BurnedCredit { asset_id: T::AssetId, amount: T::Balance },
689 /// Some assets were burned and a Debt was created.
690 IssuedDebt { asset_id: T::AssetId, amount: T::Balance },
691 /// Some assets Debt was destroyed (and assets issued).
692 BurnedDebt { asset_id: T::AssetId, amount: T::Balance },
693 }
694
695 #[pallet::error]
696 pub enum Error<T, I = ()> {
697 /// Account balance must be greater than or equal to the transfer amount.
698 BalanceLow,
699 /// The account to alter does not exist.
700 NoAccount,
701 /// The signing account has no permission to do the operation.
702 NoPermission,
703 /// The given asset ID is unknown.
704 Unknown,
705 /// The origin account is frozen.
706 Frozen,
707 /// The asset ID is already taken.
708 InUse,
709 /// Invalid witness data given.
710 BadWitness,
711 /// Minimum balance should be non-zero.
712 MinBalanceZero,
713 /// Unable to increment the consumer reference counters on the account. Either no provider
714 /// reference exists to allow a non-zero balance of a non-self-sufficient asset, or one
715 /// fewer then the maximum number of consumers has been reached.
716 UnavailableConsumer,
717 /// Invalid metadata given.
718 BadMetadata,
719 /// No approval exists that would allow the transfer.
720 Unapproved,
721 /// The source account would not survive the transfer and it needs to stay alive.
722 WouldDie,
723 /// The asset-account already exists.
724 AlreadyExists,
725 /// The asset-account doesn't have an associated deposit.
726 NoDeposit,
727 /// The operation would result in funds being burned.
728 WouldBurn,
729 /// The asset is a live asset and is actively being used. Usually emit for operations such
730 /// as `start_destroy` which require the asset to be in a destroying state.
731 LiveAsset,
732 /// The asset is not live, and likely being destroyed.
733 AssetNotLive,
734 /// The asset status is not the expected status.
735 IncorrectStatus,
736 /// The asset should be frozen before the given operation.
737 NotFrozen,
738 /// Callback action resulted in error
739 CallbackFailed,
740 /// The asset ID must be equal to the [`NextAssetId`].
741 BadAssetId,
742 /// The asset cannot be destroyed because some accounts for this asset contain freezes.
743 ContainsFreezes,
744 /// The asset cannot be destroyed because some accounts for this asset contain holds.
745 ContainsHolds,
746 /// Tried setting too many reserves.
747 TooManyReserves,
748 }
749
750 #[pallet::hooks]
751 impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
752 #[cfg(feature = "try-runtime")]
753 fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
754 Self::do_try_state()
755 }
756 }
757
758 #[pallet::call(weight(<T as Config<I>>::WeightInfo))]
759 impl<T: Config<I>, I: 'static> Pallet<T, I> {
760 /// Issue a new class of fungible assets from a public origin.
761 ///
762 /// This new asset class has no assets initially and its owner is the origin.
763 ///
764 /// The origin must conform to the configured `CreateOrigin` and have sufficient funds free.
765 ///
766 /// Funds of sender are reserved by `AssetDeposit`.
767 ///
768 /// Parameters:
769 /// - `id`: The identifier of the new asset. This must not be currently in use to identify
770 /// an existing asset. If [`NextAssetId`] is set, then this must be equal to it.
771 /// - `admin`: The admin of this class of assets. The admin is the initial address of each
772 /// member of the asset class's admin team.
773 /// - `min_balance`: The minimum balance of this new asset that any single account must
774 /// have. If an account's balance is reduced below this, then it collapses to zero.
775 ///
776 /// Emits `Created` event when successful.
777 ///
778 /// Weight: `O(1)`
779 #[pallet::call_index(0)]
780 pub fn create(
781 origin: OriginFor<T>,
782 id: T::AssetIdParameter,
783 admin: AccountIdLookupOf<T>,
784 min_balance: T::Balance,
785 ) -> DispatchResult {
786 let id: T::AssetId = id.into();
787 let owner = T::CreateOrigin::ensure_origin(origin, &id)?;
788 let admin = T::Lookup::lookup(admin)?;
789
790 ensure!(!Asset::<T, I>::contains_key(&id), Error::<T, I>::InUse);
791 ensure!(!min_balance.is_zero(), Error::<T, I>::MinBalanceZero);
792
793 if let Some(next_id) = NextAssetId::<T, I>::get() {
794 ensure!(id == next_id, Error::<T, I>::BadAssetId);
795 }
796
797 let deposit = T::AssetDeposit::get();
798 T::Currency::reserve(&owner, deposit)?;
799
800 Asset::<T, I>::insert(
801 id.clone(),
802 AssetDetails {
803 owner: owner.clone(),
804 issuer: admin.clone(),
805 admin: admin.clone(),
806 freezer: admin.clone(),
807 supply: Zero::zero(),
808 deposit,
809 min_balance,
810 is_sufficient: false,
811 accounts: 0,
812 sufficients: 0,
813 approvals: 0,
814 status: AssetStatus::Live,
815 },
816 );
817 ensure!(T::CallbackHandle::created(&id, &owner).is_ok(), Error::<T, I>::CallbackFailed);
818 Self::deposit_event(Event::Created {
819 asset_id: id,
820 creator: owner.clone(),
821 owner: admin,
822 });
823
824 Ok(())
825 }
826
827 /// Issue a new class of fungible assets from a privileged origin.
828 ///
829 /// This new asset class has no assets initially.
830 ///
831 /// The origin must conform to `ForceOrigin`.
832 ///
833 /// Unlike `create`, no funds are reserved.
834 ///
835 /// - `id`: The identifier of the new asset. This must not be currently in use to identify
836 /// an existing asset. If [`NextAssetId`] is set, then this must be equal to it.
837 /// - `owner`: The owner of this class of assets. The owner has full superuser permissions
838 /// over this asset, but may later change and configure the permissions using
839 /// `transfer_ownership` and `set_team`.
840 /// - `min_balance`: The minimum balance of this new asset that any single account must
841 /// have. If an account's balance is reduced below this, then it collapses to zero.
842 ///
843 /// Emits `ForceCreated` event when successful.
844 ///
845 /// Weight: `O(1)`
846 #[pallet::call_index(1)]
847 pub fn force_create(
848 origin: OriginFor<T>,
849 id: T::AssetIdParameter,
850 owner: AccountIdLookupOf<T>,
851 is_sufficient: bool,
852 #[pallet::compact] min_balance: T::Balance,
853 ) -> DispatchResult {
854 T::ForceOrigin::ensure_origin(origin)?;
855 let owner = T::Lookup::lookup(owner)?;
856 let id: T::AssetId = id.into();
857 Self::do_force_create(id, owner, is_sufficient, min_balance)
858 }
859
860 /// Start the process of destroying a fungible asset class.
861 ///
862 /// `start_destroy` is the first in a series of extrinsics that should be called, to allow
863 /// destruction of an asset class.
864 ///
865 /// The origin must conform to `ForceOrigin` or must be `Signed` by the asset's `owner`.
866 ///
867 /// - `id`: The identifier of the asset to be destroyed. This must identify an existing
868 /// asset.
869 ///
870 /// It will fail with either [`Error::ContainsHolds`] or [`Error::ContainsFreezes`] if
871 /// an account contains holds or freezes in place.
872 #[pallet::call_index(2)]
873 pub fn start_destroy(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
874 let maybe_check_owner = match T::ForceOrigin::try_origin(origin) {
875 Ok(_) => None,
876 Err(origin) => Some(ensure_signed(origin)?),
877 };
878 let id: T::AssetId = id.into();
879 Self::do_start_destroy(id, maybe_check_owner)
880 }
881
882 /// Destroy all accounts associated with a given asset.
883 ///
884 /// `destroy_accounts` should only be called after `start_destroy` has been called, and the
885 /// asset is in a `Destroying` state.
886 ///
887 /// Due to weight restrictions, this function may need to be called multiple times to fully
888 /// destroy all accounts. It will destroy `RemoveItemsLimit` accounts at a time.
889 ///
890 /// - `id`: The identifier of the asset to be destroyed. This must identify an existing
891 /// asset.
892 ///
893 /// Each call emits the `Event::DestroyedAccounts` event.
894 #[pallet::call_index(3)]
895 #[pallet::weight(T::WeightInfo::destroy_accounts(T::RemoveItemsLimit::get()))]
896 pub fn destroy_accounts(
897 origin: OriginFor<T>,
898 id: T::AssetIdParameter,
899 ) -> DispatchResultWithPostInfo {
900 ensure_signed(origin)?;
901 let id: T::AssetId = id.into();
902 let removed_accounts = Self::do_destroy_accounts(id, T::RemoveItemsLimit::get())?;
903 Ok(Some(T::WeightInfo::destroy_accounts(removed_accounts)).into())
904 }
905
906 /// Destroy all approvals associated with a given asset up to the max (T::RemoveItemsLimit).
907 ///
908 /// `destroy_approvals` should only be called after `start_destroy` has been called, and the
909 /// asset is in a `Destroying` state.
910 ///
911 /// Due to weight restrictions, this function may need to be called multiple times to fully
912 /// destroy all approvals. It will destroy `RemoveItemsLimit` approvals at a time.
913 ///
914 /// - `id`: The identifier of the asset to be destroyed. This must identify an existing
915 /// asset.
916 ///
917 /// Each call emits the `Event::DestroyedApprovals` event.
918 #[pallet::call_index(4)]
919 #[pallet::weight(T::WeightInfo::destroy_approvals(T::RemoveItemsLimit::get()))]
920 pub fn destroy_approvals(
921 origin: OriginFor<T>,
922 id: T::AssetIdParameter,
923 ) -> DispatchResultWithPostInfo {
924 ensure_signed(origin)?;
925 let id: T::AssetId = id.into();
926 let removed_approvals = Self::do_destroy_approvals(id, T::RemoveItemsLimit::get())?;
927 Ok(Some(T::WeightInfo::destroy_approvals(removed_approvals)).into())
928 }
929
930 /// Complete destroying asset and unreserve currency.
931 ///
932 /// `finish_destroy` should only be called after `start_destroy` has been called, and the
933 /// asset is in a `Destroying` state. All accounts or approvals should be destroyed before
934 /// hand.
935 ///
936 /// - `id`: The identifier of the asset to be destroyed. This must identify an existing
937 /// asset.
938 ///
939 /// Each successful call emits the `Event::Destroyed` event.
940 #[pallet::call_index(5)]
941 pub fn finish_destroy(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
942 ensure_signed(origin)?;
943 let id: T::AssetId = id.into();
944 Self::do_finish_destroy(id)
945 }
946
947 /// Mint assets of a particular class.
948 ///
949 /// The origin must be Signed and the sender must be the Issuer of the asset `id`.
950 ///
951 /// - `id`: The identifier of the asset to have some amount minted.
952 /// - `beneficiary`: The account to be credited with the minted assets.
953 /// - `amount`: The amount of the asset to be minted.
954 ///
955 /// Emits `Issued` event when successful.
956 ///
957 /// Weight: `O(1)`
958 /// Modes: Pre-existing balance of `beneficiary`; Account pre-existence of `beneficiary`.
959 #[pallet::call_index(6)]
960 pub fn mint(
961 origin: OriginFor<T>,
962 id: T::AssetIdParameter,
963 beneficiary: AccountIdLookupOf<T>,
964 #[pallet::compact] amount: T::Balance,
965 ) -> DispatchResult {
966 let origin = ensure_signed(origin)?;
967 let beneficiary = T::Lookup::lookup(beneficiary)?;
968 let id: T::AssetId = id.into();
969 Self::do_mint(id, &beneficiary, amount, Some(origin))?;
970 Ok(())
971 }
972
973 /// Reduce the balance of `who` by as much as possible up to `amount` assets of `id`.
974 ///
975 /// Origin must be Signed and the sender should be the Manager of the asset `id`.
976 ///
977 /// Bails with `NoAccount` if the `who` is already dead.
978 ///
979 /// - `id`: The identifier of the asset to have some amount burned.
980 /// - `who`: The account to be debited from.
981 /// - `amount`: The maximum amount by which `who`'s balance should be reduced.
982 ///
983 /// Emits `Burned` with the actual amount burned. If this takes the balance to below the
984 /// minimum for the asset, then the amount burned is increased to take it to zero.
985 ///
986 /// Weight: `O(1)`
987 /// Modes: Post-existence of `who`; Pre & post Zombie-status of `who`.
988 #[pallet::call_index(7)]
989 pub fn burn(
990 origin: OriginFor<T>,
991 id: T::AssetIdParameter,
992 who: AccountIdLookupOf<T>,
993 #[pallet::compact] amount: T::Balance,
994 ) -> DispatchResult {
995 let origin = ensure_signed(origin)?;
996 let who = T::Lookup::lookup(who)?;
997 let id: T::AssetId = id.into();
998
999 let f = DebitFlags { keep_alive: false, best_effort: true };
1000 Self::do_burn(id, &who, amount, Some(origin), f)?;
1001 Ok(())
1002 }
1003
1004 /// Move some assets from the sender account to another.
1005 ///
1006 /// Origin must be Signed.
1007 ///
1008 /// - `id`: The identifier of the asset to have some amount transferred.
1009 /// - `target`: The account to be credited.
1010 /// - `amount`: The amount by which the sender's balance of assets should be reduced and
1011 /// `target`'s balance increased. The amount actually transferred may be slightly greater in
1012 /// the case that the transfer would otherwise take the sender balance above zero but below
1013 /// the minimum balance. Must be greater than zero.
1014 ///
1015 /// Emits `Transferred` with the actual amount transferred. If this takes the source balance
1016 /// to below the minimum for the asset, then the amount transferred is increased to take it
1017 /// to zero.
1018 ///
1019 /// Weight: `O(1)`
1020 /// Modes: Pre-existence of `target`; Post-existence of sender; Account pre-existence of
1021 /// `target`.
1022 #[pallet::call_index(8)]
1023 pub fn transfer(
1024 origin: OriginFor<T>,
1025 id: T::AssetIdParameter,
1026 target: AccountIdLookupOf<T>,
1027 #[pallet::compact] amount: T::Balance,
1028 ) -> DispatchResult {
1029 let origin = ensure_signed(origin)?;
1030 let dest = T::Lookup::lookup(target)?;
1031 let id: T::AssetId = id.into();
1032
1033 let f = TransferFlags { keep_alive: false, best_effort: false, burn_dust: false };
1034 Self::do_transfer(id, &origin, &dest, amount, None, f).map(|_| ())
1035 }
1036
1037 /// Move some assets from the sender account to another, keeping the sender account alive.
1038 ///
1039 /// Origin must be Signed.
1040 ///
1041 /// - `id`: The identifier of the asset to have some amount transferred.
1042 /// - `target`: The account to be credited.
1043 /// - `amount`: The amount by which the sender's balance of assets should be reduced and
1044 /// `target`'s balance increased. The amount actually transferred may be slightly greater in
1045 /// the case that the transfer would otherwise take the sender balance above zero but below
1046 /// the minimum balance. Must be greater than zero.
1047 ///
1048 /// Emits `Transferred` with the actual amount transferred. If this takes the source balance
1049 /// to below the minimum for the asset, then the amount transferred is increased to take it
1050 /// to zero.
1051 ///
1052 /// Weight: `O(1)`
1053 /// Modes: Pre-existence of `target`; Post-existence of sender; Account pre-existence of
1054 /// `target`.
1055 #[pallet::call_index(9)]
1056 pub fn transfer_keep_alive(
1057 origin: OriginFor<T>,
1058 id: T::AssetIdParameter,
1059 target: AccountIdLookupOf<T>,
1060 #[pallet::compact] amount: T::Balance,
1061 ) -> DispatchResult {
1062 let source = ensure_signed(origin)?;
1063 let dest = T::Lookup::lookup(target)?;
1064 let id: T::AssetId = id.into();
1065
1066 let f = TransferFlags { keep_alive: true, best_effort: false, burn_dust: false };
1067 Self::do_transfer(id, &source, &dest, amount, None, f).map(|_| ())
1068 }
1069
1070 /// Move some assets from one account to another.
1071 ///
1072 /// Origin must be Signed and the sender should be the Admin of the asset `id`.
1073 ///
1074 /// - `id`: The identifier of the asset to have some amount transferred.
1075 /// - `source`: The account to be debited.
1076 /// - `dest`: The account to be credited.
1077 /// - `amount`: The amount by which the `source`'s balance of assets should be reduced and
1078 /// `dest`'s balance increased. The amount actually transferred may be slightly greater in
1079 /// the case that the transfer would otherwise take the `source` balance above zero but
1080 /// below the minimum balance. Must be greater than zero.
1081 ///
1082 /// Emits `Transferred` with the actual amount transferred. If this takes the source balance
1083 /// to below the minimum for the asset, then the amount transferred is increased to take it
1084 /// to zero.
1085 ///
1086 /// Weight: `O(1)`
1087 /// Modes: Pre-existence of `dest`; Post-existence of `source`; Account pre-existence of
1088 /// `dest`.
1089 #[pallet::call_index(10)]
1090 pub fn force_transfer(
1091 origin: OriginFor<T>,
1092 id: T::AssetIdParameter,
1093 source: AccountIdLookupOf<T>,
1094 dest: AccountIdLookupOf<T>,
1095 #[pallet::compact] amount: T::Balance,
1096 ) -> DispatchResult {
1097 let origin = ensure_signed(origin)?;
1098 let source = T::Lookup::lookup(source)?;
1099 let dest = T::Lookup::lookup(dest)?;
1100 let id: T::AssetId = id.into();
1101
1102 let f = TransferFlags { keep_alive: false, best_effort: false, burn_dust: false };
1103 Self::do_transfer(id, &source, &dest, amount, Some(origin), f).map(|_| ())
1104 }
1105
1106 /// Disallow further unprivileged transfers of an asset `id` from an account `who`. `who`
1107 /// must already exist as an entry in `Account`s of the asset. If you want to freeze an
1108 /// account that does not have an entry, use `touch_other` first.
1109 ///
1110 /// Origin must be Signed and the sender should be the Freezer of the asset `id`.
1111 ///
1112 /// - `id`: The identifier of the asset to be frozen.
1113 /// - `who`: The account to be frozen.
1114 ///
1115 /// Emits `Frozen`.
1116 ///
1117 /// Weight: `O(1)`
1118 #[pallet::call_index(11)]
1119 pub fn freeze(
1120 origin: OriginFor<T>,
1121 id: T::AssetIdParameter,
1122 who: AccountIdLookupOf<T>,
1123 ) -> DispatchResult {
1124 let origin = ensure_signed(origin)?;
1125 let id: T::AssetId = id.into();
1126
1127 let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1128 ensure!(
1129 d.status == AssetStatus::Live || d.status == AssetStatus::Frozen,
1130 Error::<T, I>::IncorrectStatus
1131 );
1132 ensure!(origin == d.freezer, Error::<T, I>::NoPermission);
1133 let who = T::Lookup::lookup(who)?;
1134
1135 Account::<T, I>::try_mutate(&id, &who, |maybe_account| -> DispatchResult {
1136 maybe_account.as_mut().ok_or(Error::<T, I>::NoAccount)?.status =
1137 AccountStatus::Frozen;
1138 Ok(())
1139 })?;
1140
1141 Self::deposit_event(Event::<T, I>::Frozen { asset_id: id, who });
1142 Ok(())
1143 }
1144
1145 /// Allow unprivileged transfers to and from an account again.
1146 ///
1147 /// Origin must be Signed and the sender should be the Admin of the asset `id`.
1148 ///
1149 /// - `id`: The identifier of the asset to be frozen.
1150 /// - `who`: The account to be unfrozen.
1151 ///
1152 /// Emits `Thawed`.
1153 ///
1154 /// Weight: `O(1)`
1155 #[pallet::call_index(12)]
1156 pub fn thaw(
1157 origin: OriginFor<T>,
1158 id: T::AssetIdParameter,
1159 who: AccountIdLookupOf<T>,
1160 ) -> DispatchResult {
1161 let origin = ensure_signed(origin)?;
1162 let id: T::AssetId = id.into();
1163
1164 let details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1165 ensure!(
1166 details.status == AssetStatus::Live || details.status == AssetStatus::Frozen,
1167 Error::<T, I>::IncorrectStatus
1168 );
1169 ensure!(origin == details.admin, Error::<T, I>::NoPermission);
1170 let who = T::Lookup::lookup(who)?;
1171
1172 Account::<T, I>::try_mutate(&id, &who, |maybe_account| -> DispatchResult {
1173 maybe_account.as_mut().ok_or(Error::<T, I>::NoAccount)?.status =
1174 AccountStatus::Liquid;
1175 Ok(())
1176 })?;
1177
1178 Self::deposit_event(Event::<T, I>::Thawed { asset_id: id, who });
1179 Ok(())
1180 }
1181
1182 /// Disallow further unprivileged transfers for the asset class.
1183 ///
1184 /// Origin must be Signed and the sender should be the Freezer of the asset `id`.
1185 ///
1186 /// - `id`: The identifier of the asset to be frozen.
1187 ///
1188 /// Emits `Frozen`.
1189 ///
1190 /// Weight: `O(1)`
1191 #[pallet::call_index(13)]
1192 pub fn freeze_asset(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
1193 let origin = ensure_signed(origin)?;
1194 let id: T::AssetId = id.into();
1195
1196 Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
1197 let d = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
1198 ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
1199 ensure!(origin == d.freezer, Error::<T, I>::NoPermission);
1200
1201 d.status = AssetStatus::Frozen;
1202
1203 Self::deposit_event(Event::<T, I>::AssetFrozen { asset_id: id });
1204 Ok(())
1205 })
1206 }
1207
1208 /// Allow unprivileged transfers for the asset again.
1209 ///
1210 /// Origin must be Signed and the sender should be the Admin of the asset `id`.
1211 ///
1212 /// - `id`: The identifier of the asset to be thawed.
1213 ///
1214 /// Emits `Thawed`.
1215 ///
1216 /// Weight: `O(1)`
1217 #[pallet::call_index(14)]
1218 pub fn thaw_asset(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
1219 let origin = ensure_signed(origin)?;
1220 let id: T::AssetId = id.into();
1221
1222 Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
1223 let d = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
1224 ensure!(origin == d.admin, Error::<T, I>::NoPermission);
1225 ensure!(d.status == AssetStatus::Frozen, Error::<T, I>::NotFrozen);
1226
1227 d.status = AssetStatus::Live;
1228
1229 Self::deposit_event(Event::<T, I>::AssetThawed { asset_id: id });
1230 Ok(())
1231 })
1232 }
1233
1234 /// Change the Owner of an asset.
1235 ///
1236 /// Origin must be Signed and the sender should be the Owner of the asset `id`.
1237 ///
1238 /// - `id`: The identifier of the asset.
1239 /// - `owner`: The new Owner of this asset.
1240 ///
1241 /// Emits `OwnerChanged`.
1242 ///
1243 /// Weight: `O(1)`
1244 #[pallet::call_index(15)]
1245 pub fn transfer_ownership(
1246 origin: OriginFor<T>,
1247 id: T::AssetIdParameter,
1248 owner: AccountIdLookupOf<T>,
1249 ) -> DispatchResult {
1250 let origin = ensure_signed(origin)?;
1251 let owner = T::Lookup::lookup(owner)?;
1252 let id: T::AssetId = id.into();
1253
1254 Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
1255 let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
1256 ensure!(details.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
1257 ensure!(origin == details.owner, Error::<T, I>::NoPermission);
1258 if details.owner == owner {
1259 return Ok(());
1260 }
1261
1262 let metadata_deposit = Metadata::<T, I>::get(&id).deposit;
1263 let deposit = details.deposit + metadata_deposit;
1264
1265 // Move the deposit to the new owner.
1266 T::Currency::repatriate_reserved(&details.owner, &owner, deposit, Reserved)?;
1267
1268 details.owner = owner.clone();
1269
1270 Self::deposit_event(Event::OwnerChanged { asset_id: id, owner });
1271 Ok(())
1272 })
1273 }
1274
1275 /// Change the Issuer, Admin and Freezer of an asset.
1276 ///
1277 /// Origin must be Signed and the sender should be the Owner of the asset `id`.
1278 ///
1279 /// - `id`: The identifier of the asset to be frozen.
1280 /// - `issuer`: The new Issuer of this asset.
1281 /// - `admin`: The new Admin of this asset.
1282 /// - `freezer`: The new Freezer of this asset.
1283 ///
1284 /// Emits `TeamChanged`.
1285 ///
1286 /// Weight: `O(1)`
1287 #[pallet::call_index(16)]
1288 pub fn set_team(
1289 origin: OriginFor<T>,
1290 id: T::AssetIdParameter,
1291 issuer: AccountIdLookupOf<T>,
1292 admin: AccountIdLookupOf<T>,
1293 freezer: AccountIdLookupOf<T>,
1294 ) -> DispatchResult {
1295 let origin = ensure_signed(origin)?;
1296 let issuer = T::Lookup::lookup(issuer)?;
1297 let admin = T::Lookup::lookup(admin)?;
1298 let freezer = T::Lookup::lookup(freezer)?;
1299 let id: T::AssetId = id.into();
1300
1301 Asset::<T, I>::try_mutate(id.clone(), |maybe_details| {
1302 let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
1303 ensure!(details.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
1304 ensure!(origin == details.owner, Error::<T, I>::NoPermission);
1305
1306 details.issuer = issuer.clone();
1307 details.admin = admin.clone();
1308 details.freezer = freezer.clone();
1309
1310 Self::deposit_event(Event::TeamChanged { asset_id: id, issuer, admin, freezer });
1311 Ok(())
1312 })
1313 }
1314
1315 /// Set the metadata for an asset.
1316 ///
1317 /// Origin must be Signed and the sender should be the Owner of the asset `id`.
1318 ///
1319 /// Funds of sender are reserved according to the formula:
1320 /// `MetadataDepositBase + MetadataDepositPerByte * (name.len + symbol.len)` taking into
1321 /// account any already reserved funds.
1322 ///
1323 /// - `id`: The identifier of the asset to update.
1324 /// - `name`: The user friendly name of this asset. Limited in length by `StringLimit`.
1325 /// - `symbol`: The exchange symbol for this asset. Limited in length by `StringLimit`.
1326 /// - `decimals`: The number of decimals this asset uses to represent one unit.
1327 ///
1328 /// Emits `MetadataSet`.
1329 ///
1330 /// Weight: `O(1)`
1331 #[pallet::call_index(17)]
1332 #[pallet::weight(T::WeightInfo::set_metadata(name.len() as u32, symbol.len() as u32))]
1333 pub fn set_metadata(
1334 origin: OriginFor<T>,
1335 id: T::AssetIdParameter,
1336 name: Vec<u8>,
1337 symbol: Vec<u8>,
1338 decimals: u8,
1339 ) -> DispatchResult {
1340 let origin = ensure_signed(origin)?;
1341 let id: T::AssetId = id.into();
1342 Self::do_set_metadata(id, &origin, name, symbol, decimals)
1343 }
1344
1345 /// Clear the metadata for an asset.
1346 ///
1347 /// Origin must be Signed and the sender should be the Owner of the asset `id`.
1348 ///
1349 /// Any deposit is freed for the asset owner.
1350 ///
1351 /// - `id`: The identifier of the asset to clear.
1352 ///
1353 /// Emits `MetadataCleared`.
1354 ///
1355 /// Weight: `O(1)`
1356 #[pallet::call_index(18)]
1357 pub fn clear_metadata(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
1358 let origin = ensure_signed(origin)?;
1359 let id: T::AssetId = id.into();
1360
1361 let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1362 ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
1363 ensure!(origin == d.owner, Error::<T, I>::NoPermission);
1364
1365 Metadata::<T, I>::try_mutate_exists(id.clone(), |metadata| {
1366 let deposit = metadata.take().ok_or(Error::<T, I>::Unknown)?.deposit;
1367 T::Currency::unreserve(&d.owner, deposit);
1368 Self::deposit_event(Event::MetadataCleared { asset_id: id });
1369 Ok(())
1370 })
1371 }
1372
1373 /// Force the metadata for an asset to some value.
1374 ///
1375 /// Origin must be ForceOrigin.
1376 ///
1377 /// Any deposit is left alone.
1378 ///
1379 /// - `id`: The identifier of the asset to update.
1380 /// - `name`: The user friendly name of this asset. Limited in length by `StringLimit`.
1381 /// - `symbol`: The exchange symbol for this asset. Limited in length by `StringLimit`.
1382 /// - `decimals`: The number of decimals this asset uses to represent one unit.
1383 ///
1384 /// Emits `MetadataSet`.
1385 ///
1386 /// Weight: `O(N + S)` where N and S are the length of the name and symbol respectively.
1387 #[pallet::call_index(19)]
1388 #[pallet::weight(T::WeightInfo::force_set_metadata(name.len() as u32, symbol.len() as u32))]
1389 pub fn force_set_metadata(
1390 origin: OriginFor<T>,
1391 id: T::AssetIdParameter,
1392 name: Vec<u8>,
1393 symbol: Vec<u8>,
1394 decimals: u8,
1395 is_frozen: bool,
1396 ) -> DispatchResult {
1397 T::ForceOrigin::ensure_origin(origin)?;
1398 let id: T::AssetId = id.into();
1399
1400 let bounded_name: BoundedVec<u8, T::StringLimit> =
1401 name.clone().try_into().map_err(|_| Error::<T, I>::BadMetadata)?;
1402
1403 let bounded_symbol: BoundedVec<u8, T::StringLimit> =
1404 symbol.clone().try_into().map_err(|_| Error::<T, I>::BadMetadata)?;
1405
1406 ensure!(Asset::<T, I>::contains_key(&id), Error::<T, I>::Unknown);
1407 Metadata::<T, I>::try_mutate_exists(id.clone(), |metadata| {
1408 let deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit);
1409 *metadata = Some(AssetMetadata {
1410 deposit,
1411 name: bounded_name,
1412 symbol: bounded_symbol,
1413 decimals,
1414 is_frozen,
1415 });
1416
1417 Self::deposit_event(Event::MetadataSet {
1418 asset_id: id,
1419 name,
1420 symbol,
1421 decimals,
1422 is_frozen,
1423 });
1424 Ok(())
1425 })
1426 }
1427
1428 /// Clear the metadata for an asset.
1429 ///
1430 /// Origin must be ForceOrigin.
1431 ///
1432 /// Any deposit is returned.
1433 ///
1434 /// - `id`: The identifier of the asset to clear.
1435 ///
1436 /// Emits `MetadataCleared`.
1437 ///
1438 /// Weight: `O(1)`
1439 #[pallet::call_index(20)]
1440 pub fn force_clear_metadata(
1441 origin: OriginFor<T>,
1442 id: T::AssetIdParameter,
1443 ) -> DispatchResult {
1444 T::ForceOrigin::ensure_origin(origin)?;
1445 let id: T::AssetId = id.into();
1446
1447 let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1448 Metadata::<T, I>::try_mutate_exists(id.clone(), |metadata| {
1449 let deposit = metadata.take().ok_or(Error::<T, I>::Unknown)?.deposit;
1450 T::Currency::unreserve(&d.owner, deposit);
1451 Self::deposit_event(Event::MetadataCleared { asset_id: id });
1452 Ok(())
1453 })
1454 }
1455
1456 /// Alter the attributes of a given asset.
1457 ///
1458 /// Origin must be `ForceOrigin`.
1459 ///
1460 /// - `id`: The identifier of the asset.
1461 /// - `owner`: The new Owner of this asset.
1462 /// - `issuer`: The new Issuer of this asset.
1463 /// - `admin`: The new Admin of this asset.
1464 /// - `freezer`: The new Freezer of this asset.
1465 /// - `min_balance`: The minimum balance of this new asset that any single account must
1466 /// have. If an account's balance is reduced below this, then it collapses to zero.
1467 /// - `is_sufficient`: Whether a non-zero balance of this asset is deposit of sufficient
1468 /// value to account for the state bloat associated with its balance storage. If set to
1469 /// `true`, then non-zero balances may be stored without a `consumer` reference (and thus
1470 /// an ED in the Balances pallet or whatever else is used to control user-account state
1471 /// growth).
1472 /// - `is_frozen`: Whether this asset class is frozen except for permissioned/admin
1473 /// instructions.
1474 ///
1475 /// Emits `AssetStatusChanged` with the identity of the asset.
1476 ///
1477 /// Weight: `O(1)`
1478 #[pallet::call_index(21)]
1479 pub fn force_asset_status(
1480 origin: OriginFor<T>,
1481 id: T::AssetIdParameter,
1482 owner: AccountIdLookupOf<T>,
1483 issuer: AccountIdLookupOf<T>,
1484 admin: AccountIdLookupOf<T>,
1485 freezer: AccountIdLookupOf<T>,
1486 #[pallet::compact] min_balance: T::Balance,
1487 is_sufficient: bool,
1488 is_frozen: bool,
1489 ) -> DispatchResult {
1490 T::ForceOrigin::ensure_origin(origin)?;
1491 let id: T::AssetId = id.into();
1492
1493 Asset::<T, I>::try_mutate(id.clone(), |maybe_asset| {
1494 let mut asset = maybe_asset.take().ok_or(Error::<T, I>::Unknown)?;
1495 ensure!(asset.status != AssetStatus::Destroying, Error::<T, I>::AssetNotLive);
1496 asset.owner = T::Lookup::lookup(owner)?;
1497 asset.issuer = T::Lookup::lookup(issuer)?;
1498 asset.admin = T::Lookup::lookup(admin)?;
1499 asset.freezer = T::Lookup::lookup(freezer)?;
1500 asset.min_balance = min_balance;
1501 asset.is_sufficient = is_sufficient;
1502 if is_frozen {
1503 asset.status = AssetStatus::Frozen;
1504 } else {
1505 asset.status = AssetStatus::Live;
1506 }
1507 *maybe_asset = Some(asset);
1508
1509 Self::deposit_event(Event::AssetStatusChanged { asset_id: id });
1510 Ok(())
1511 })
1512 }
1513
1514 /// Approve an amount of asset for transfer by a delegated third-party account.
1515 ///
1516 /// Origin must be Signed.
1517 ///
1518 /// Ensures that `ApprovalDeposit` worth of `Currency` is reserved from signing account
1519 /// for the purpose of holding the approval. If some non-zero amount of assets is already
1520 /// approved from signing account to `delegate`, then it is topped up or unreserved to
1521 /// meet the right value.
1522 ///
1523 /// NOTE: The signing account does not need to own `amount` of assets at the point of
1524 /// making this call.
1525 ///
1526 /// - `id`: The identifier of the asset.
1527 /// - `delegate`: The account to delegate permission to transfer asset.
1528 /// - `amount`: The amount of asset that may be transferred by `delegate`. If there is
1529 /// already an approval in place, then this acts additively.
1530 ///
1531 /// Emits `ApprovedTransfer` on success.
1532 ///
1533 /// Weight: `O(1)`
1534 #[pallet::call_index(22)]
1535 pub fn approve_transfer(
1536 origin: OriginFor<T>,
1537 id: T::AssetIdParameter,
1538 delegate: AccountIdLookupOf<T>,
1539 #[pallet::compact] amount: T::Balance,
1540 ) -> DispatchResult {
1541 let owner = ensure_signed(origin)?;
1542 let delegate = T::Lookup::lookup(delegate)?;
1543 let id: T::AssetId = id.into();
1544 Self::do_approve_transfer(id, &owner, &delegate, amount)
1545 }
1546
1547 /// Cancel all of some asset approved for delegated transfer by a third-party account.
1548 ///
1549 /// Origin must be Signed and there must be an approval in place between signer and
1550 /// `delegate`.
1551 ///
1552 /// Unreserves any deposit previously reserved by `approve_transfer` for the approval.
1553 ///
1554 /// - `id`: The identifier of the asset.
1555 /// - `delegate`: The account delegated permission to transfer asset.
1556 ///
1557 /// Emits `ApprovalCancelled` on success.
1558 ///
1559 /// Weight: `O(1)`
1560 #[pallet::call_index(23)]
1561 pub fn cancel_approval(
1562 origin: OriginFor<T>,
1563 id: T::AssetIdParameter,
1564 delegate: AccountIdLookupOf<T>,
1565 ) -> DispatchResult {
1566 let owner = ensure_signed(origin)?;
1567 let delegate = T::Lookup::lookup(delegate)?;
1568 let id: T::AssetId = id.into();
1569 Self::do_cancel_approval(&id, &owner, &delegate)
1570 }
1571
1572 /// Cancel all of some asset approved for delegated transfer by a third-party account.
1573 ///
1574 /// Origin must be either ForceOrigin or Signed origin with the signer being the Admin
1575 /// account of the asset `id`.
1576 ///
1577 /// Unreserves any deposit previously reserved by `approve_transfer` for the approval.
1578 ///
1579 /// - `id`: The identifier of the asset.
1580 /// - `delegate`: The account delegated permission to transfer asset.
1581 ///
1582 /// Emits `ApprovalCancelled` on success.
1583 ///
1584 /// Weight: `O(1)`
1585 #[pallet::call_index(24)]
1586 pub fn force_cancel_approval(
1587 origin: OriginFor<T>,
1588 id: T::AssetIdParameter,
1589 owner: AccountIdLookupOf<T>,
1590 delegate: AccountIdLookupOf<T>,
1591 ) -> DispatchResult {
1592 let id: T::AssetId = id.into();
1593 let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1594 T::ForceOrigin::try_origin(origin)
1595 .map(|_| ())
1596 .or_else(|origin| -> DispatchResult {
1597 let origin = ensure_signed(origin)?;
1598 ensure!(origin == d.admin, Error::<T, I>::NoPermission);
1599 Ok(())
1600 })?;
1601
1602 let owner = T::Lookup::lookup(owner)?;
1603 let delegate = T::Lookup::lookup(delegate)?;
1604 Self::do_cancel_approval(&id, &owner, &delegate)
1605 }
1606
1607 /// Transfer some asset balance from a previously delegated account to some third-party
1608 /// account.
1609 ///
1610 /// Origin must be Signed and there must be an approval in place by the `owner` to the
1611 /// signer.
1612 ///
1613 /// If the entire amount approved for transfer is transferred, then any deposit previously
1614 /// reserved by `approve_transfer` is unreserved.
1615 ///
1616 /// - `id`: The identifier of the asset.
1617 /// - `owner`: The account which previously approved for a transfer of at least `amount` and
1618 /// from which the asset balance will be withdrawn.
1619 /// - `destination`: The account to which the asset balance of `amount` will be transferred.
1620 /// - `amount`: The amount of assets to transfer.
1621 ///
1622 /// Emits `TransferredApproved` on success.
1623 ///
1624 /// Weight: `O(1)`
1625 #[pallet::call_index(25)]
1626 pub fn transfer_approved(
1627 origin: OriginFor<T>,
1628 id: T::AssetIdParameter,
1629 owner: AccountIdLookupOf<T>,
1630 destination: AccountIdLookupOf<T>,
1631 #[pallet::compact] amount: T::Balance,
1632 ) -> DispatchResult {
1633 let delegate = ensure_signed(origin)?;
1634 let owner = T::Lookup::lookup(owner)?;
1635 let destination = T::Lookup::lookup(destination)?;
1636 let id: T::AssetId = id.into();
1637 Self::do_transfer_approved(id, &owner, &delegate, &destination, amount)
1638 }
1639
1640 /// Create an asset account for non-provider assets.
1641 ///
1642 /// A deposit will be taken from the signer account.
1643 ///
1644 /// - `origin`: Must be Signed; the signer account must have sufficient funds for a deposit
1645 /// to be taken.
1646 /// - `id`: The identifier of the asset for the account to be created.
1647 ///
1648 /// Emits `Touched` event when successful.
1649 #[pallet::call_index(26)]
1650 #[pallet::weight(T::WeightInfo::touch())]
1651 pub fn touch(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
1652 let who = ensure_signed(origin)?;
1653 let id: T::AssetId = id.into();
1654 Self::do_touch(id, who.clone(), who)
1655 }
1656
1657 /// Return the deposit (if any) of an asset account or a consumer reference (if any) of an
1658 /// account.
1659 ///
1660 /// The origin must be Signed.
1661 ///
1662 /// - `id`: The identifier of the asset for which the caller would like the deposit
1663 /// refunded.
1664 /// - `allow_burn`: If `true` then assets may be destroyed in order to complete the refund.
1665 ///
1666 /// It will fail with either [`Error::ContainsHolds`] or [`Error::ContainsFreezes`] if
1667 /// the asset account contains holds or freezes in place.
1668 ///
1669 /// Emits `Refunded` event when successful.
1670 #[pallet::call_index(27)]
1671 #[pallet::weight(T::WeightInfo::refund())]
1672 pub fn refund(
1673 origin: OriginFor<T>,
1674 id: T::AssetIdParameter,
1675 allow_burn: bool,
1676 ) -> DispatchResult {
1677 let id: T::AssetId = id.into();
1678 Self::do_refund(id, ensure_signed(origin)?, allow_burn)
1679 }
1680
1681 /// Sets the minimum balance of an asset.
1682 ///
1683 /// Only works if there aren't any accounts that are holding the asset or if
1684 /// the new value of `min_balance` is less than the old one.
1685 ///
1686 /// Origin must be Signed and the sender has to be the Owner of the
1687 /// asset `id`.
1688 ///
1689 /// - `id`: The identifier of the asset.
1690 /// - `min_balance`: The new value of `min_balance`.
1691 ///
1692 /// Emits `AssetMinBalanceChanged` event when successful.
1693 #[pallet::call_index(28)]
1694 pub fn set_min_balance(
1695 origin: OriginFor<T>,
1696 id: T::AssetIdParameter,
1697 min_balance: T::Balance,
1698 ) -> DispatchResult {
1699 let origin = ensure_signed(origin)?;
1700 let id: T::AssetId = id.into();
1701
1702 let mut details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1703 ensure!(origin == details.owner, Error::<T, I>::NoPermission);
1704
1705 let old_min_balance = details.min_balance;
1706 // If the asset is marked as sufficient it won't be allowed to
1707 // change the min_balance.
1708 ensure!(!details.is_sufficient, Error::<T, I>::NoPermission);
1709
1710 // Ensure that either the new min_balance is less than old
1711 // min_balance or there aren't any accounts holding the asset.
1712 ensure!(
1713 min_balance < old_min_balance || details.accounts == 0,
1714 Error::<T, I>::NoPermission
1715 );
1716
1717 details.min_balance = min_balance;
1718 Asset::<T, I>::insert(&id, details);
1719
1720 Self::deposit_event(Event::AssetMinBalanceChanged {
1721 asset_id: id,
1722 new_min_balance: min_balance,
1723 });
1724 Ok(())
1725 }
1726
1727 /// Create an asset account for `who`.
1728 ///
1729 /// A deposit will be taken from the signer account.
1730 ///
1731 /// - `origin`: Must be Signed; the signer account must have sufficient funds for a deposit
1732 /// to be taken.
1733 /// - `id`: The identifier of the asset for the account to be created, the asset status must
1734 /// be live.
1735 /// - `who`: The account to be created.
1736 ///
1737 /// Emits `Touched` event when successful.
1738 #[pallet::call_index(29)]
1739 #[pallet::weight(T::WeightInfo::touch_other())]
1740 pub fn touch_other(
1741 origin: OriginFor<T>,
1742 id: T::AssetIdParameter,
1743 who: AccountIdLookupOf<T>,
1744 ) -> DispatchResult {
1745 let origin = ensure_signed(origin)?;
1746 let who = T::Lookup::lookup(who)?;
1747 let id: T::AssetId = id.into();
1748 Self::do_touch(id, who, origin)
1749 }
1750
1751 /// Return the deposit (if any) of a target asset account. Useful if you are the depositor.
1752 ///
1753 /// The origin must be Signed and either the account owner, depositor, or asset `Admin`. In
1754 /// order to burn a non-zero balance of the asset, the caller must be the account and should
1755 /// use `refund`.
1756 ///
1757 /// - `id`: The identifier of the asset for the account holding a deposit.
1758 /// - `who`: The account to refund.
1759 ///
1760 /// It will fail with either [`Error::ContainsHolds`] or [`Error::ContainsFreezes`] if
1761 /// the asset account contains holds or freezes in place.
1762 ///
1763 /// Emits `Refunded` event when successful.
1764 #[pallet::call_index(30)]
1765 #[pallet::weight(T::WeightInfo::refund_other())]
1766 pub fn refund_other(
1767 origin: OriginFor<T>,
1768 id: T::AssetIdParameter,
1769 who: AccountIdLookupOf<T>,
1770 ) -> DispatchResult {
1771 let origin = ensure_signed(origin)?;
1772 let who = T::Lookup::lookup(who)?;
1773 let id: T::AssetId = id.into();
1774 Self::do_refund_other(id, &who, Some(origin))
1775 }
1776
1777 /// Disallow further unprivileged transfers of an asset `id` to and from an account `who`.
1778 ///
1779 /// Origin must be Signed and the sender should be the Freezer of the asset `id`.
1780 ///
1781 /// - `id`: The identifier of the account's asset.
1782 /// - `who`: The account to be unblocked.
1783 ///
1784 /// Emits `Blocked`.
1785 ///
1786 /// Weight: `O(1)`
1787 #[pallet::call_index(31)]
1788 pub fn block(
1789 origin: OriginFor<T>,
1790 id: T::AssetIdParameter,
1791 who: AccountIdLookupOf<T>,
1792 ) -> DispatchResult {
1793 let origin = ensure_signed(origin)?;
1794 let id: T::AssetId = id.into();
1795
1796 let d = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1797 ensure!(
1798 d.status == AssetStatus::Live || d.status == AssetStatus::Frozen,
1799 Error::<T, I>::IncorrectStatus
1800 );
1801 ensure!(origin == d.freezer, Error::<T, I>::NoPermission);
1802 let who = T::Lookup::lookup(who)?;
1803
1804 Account::<T, I>::try_mutate(&id, &who, |maybe_account| -> DispatchResult {
1805 maybe_account.as_mut().ok_or(Error::<T, I>::NoAccount)?.status =
1806 AccountStatus::Blocked;
1807 Ok(())
1808 })?;
1809
1810 Self::deposit_event(Event::<T, I>::Blocked { asset_id: id, who });
1811 Ok(())
1812 }
1813
1814 /// Transfer the entire transferable balance from the caller asset account.
1815 ///
1816 /// NOTE: This function only attempts to transfer _transferable_ balances. This means that
1817 /// any held, frozen, or minimum balance (when `keep_alive` is `true`), will not be
1818 /// transferred by this function. To ensure that this function results in a killed account,
1819 /// you might need to prepare the account by removing any reference counters, storage
1820 /// deposits, etc...
1821 ///
1822 /// The dispatch origin of this call must be Signed.
1823 ///
1824 /// - `id`: The identifier of the asset for the account holding a deposit.
1825 /// - `dest`: The recipient of the transfer.
1826 /// - `keep_alive`: A boolean to determine if the `transfer_all` operation should send all
1827 /// of the funds the asset account has, causing the sender asset account to be killed
1828 /// (false), or transfer everything except at least the minimum balance, which will
1829 /// guarantee to keep the sender asset account alive (true).
1830 #[pallet::call_index(32)]
1831 #[pallet::weight(T::WeightInfo::transfer_all())]
1832 pub fn transfer_all(
1833 origin: OriginFor<T>,
1834 id: T::AssetIdParameter,
1835 dest: AccountIdLookupOf<T>,
1836 keep_alive: bool,
1837 ) -> DispatchResult {
1838 let transactor = ensure_signed(origin)?;
1839 let keep_alive = if keep_alive { Preserve } else { Expendable };
1840 let reducible_balance = <Self as fungibles::Inspect<_>>::reducible_balance(
1841 id.clone().into(),
1842 &transactor,
1843 keep_alive,
1844 Fortitude::Polite,
1845 );
1846 let dest = T::Lookup::lookup(dest)?;
1847 <Self as fungibles::Mutate<_>>::transfer(
1848 id.into(),
1849 &transactor,
1850 &dest,
1851 reducible_balance,
1852 keep_alive,
1853 )?;
1854 Ok(())
1855 }
1856
1857 /// Sets the trusted reserve information of an asset.
1858 ///
1859 /// Origin must be the Owner of the asset `id`. The origin must conform to the configured
1860 /// `CreateOrigin` or be the signed `owner` configured during asset creation.
1861 ///
1862 /// - `id`: The identifier of the asset.
1863 /// - `reserves`: The full list of trusted reserves information.
1864 ///
1865 /// Emits `AssetMinBalanceChanged` event when successful.
1866 #[pallet::call_index(33)]
1867 #[pallet::weight(T::WeightInfo::set_reserves(reserves.len() as u32))]
1868 pub fn set_reserves(
1869 origin: OriginFor<T>,
1870 id: T::AssetIdParameter,
1871 reserves: BoundedVec<T::ReserveData, ConstU32<MAX_RESERVES>>,
1872 ) -> DispatchResult {
1873 let id: T::AssetId = id.into();
1874 let origin = ensure_signed(origin.clone())
1875 .or_else(|_| T::CreateOrigin::ensure_origin(origin, &id))?;
1876
1877 let details = Asset::<T, I>::get(&id).ok_or(Error::<T, I>::Unknown)?;
1878 ensure!(origin == details.owner, Error::<T, I>::NoPermission);
1879
1880 Self::unchecked_update_reserves(id, reserves)?;
1881 Ok(())
1882 }
1883 }
1884
1885 #[pallet::view_functions]
1886 impl<T: Config<I>, I: 'static> Pallet<T, I> {
1887 /// Provide the asset details for asset `id`.
1888 pub fn asset_details(
1889 id: T::AssetId,
1890 ) -> Option<AssetDetails<T::Balance, T::AccountId, DepositBalanceOf<T, I>>> {
1891 Asset::<T, I>::get(id)
1892 }
1893
1894 /// Provide the balance of `who` for asset `id`.
1895 pub fn balance_of(who: T::AccountId, id: T::AssetId) -> Option<<T as Config<I>>::Balance> {
1896 Account::<T, I>::get(id, who).map(|account| account.balance)
1897 }
1898
1899 /// Provide the configured metadata for asset `id`.
1900 pub fn get_metadata(
1901 id: T::AssetId,
1902 ) -> Option<AssetMetadata<DepositBalanceOf<T, I>, BoundedVec<u8, T::StringLimit>>> {
1903 Metadata::<T, I>::try_get(id).ok()
1904 }
1905
1906 /// Provide the configured reserves data for asset `id`.
1907 pub fn get_reserves_data(id: T::AssetId) -> Vec<T::ReserveData> {
1908 Self::reserves(&id)
1909 }
1910 }
1911
1912 /// Implements [`AccountTouch`] trait.
1913 /// Note that a depositor can be any account, without any specific privilege.
1914 impl<T: Config<I>, I: 'static> AccountTouch<T::AssetId, T::AccountId> for Pallet<T, I> {
1915 type Balance = DepositBalanceOf<T, I>;
1916
1917 fn deposit_required(_: T::AssetId) -> Self::Balance {
1918 T::AssetAccountDeposit::get()
1919 }
1920
1921 fn should_touch(asset: T::AssetId, who: &T::AccountId) -> bool {
1922 match Asset::<T, I>::get(&asset) {
1923 // refer to the [`Self::new_account`] function for more details.
1924 Some(info) if info.is_sufficient => false,
1925 Some(_) if frame_system::Pallet::<T>::can_accrue_consumers(who, 2) => false,
1926 Some(_) => !Account::<T, I>::contains_key(asset, who),
1927 _ => true,
1928 }
1929 }
1930
1931 fn touch(
1932 asset: T::AssetId,
1933 who: &T::AccountId,
1934 depositor: &T::AccountId,
1935 ) -> DispatchResult {
1936 Self::do_touch(asset, who.clone(), depositor.clone())
1937 }
1938 }
1939
1940 /// Implements [`ContainsPair`] trait for a pair of asset and account IDs.
1941 impl<T: Config<I>, I: 'static> ContainsPair<T::AssetId, T::AccountId> for Pallet<T, I> {
1942 /// Check if an account with the given asset ID and account address exists.
1943 fn contains(asset: &T::AssetId, who: &T::AccountId) -> bool {
1944 Account::<T, I>::contains_key(asset, who)
1945 }
1946 }
1947
1948 /// Implements [`ProvideAssetReserves`] trait for getting the list of trusted reserves for a
1949 /// given asset.
1950 impl<T: Config<I>, I: 'static> ProvideAssetReserves<T::AssetId, T::ReserveData> for Pallet<T, I> {
1951 /// Provide the configured reserves for asset `id`.
1952 fn reserves(id: &T::AssetId) -> Vec<T::ReserveData> {
1953 Reserves::<T, I>::get(id).into_inner()
1954 }
1955 }
1956}
1957
1958#[cfg(any(feature = "try-runtime", test))]
1959impl<T: Config<I>, I: 'static> Pallet<T, I> {
1960 pub fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
1961 for asset_id in Reserves::<T, I>::iter_keys() {
1962 ensure!(Asset::<T, I>::contains_key(asset_id.clone()), "Orphaned Reserves data found");
1963 }
1964
1965 for asset_id in Metadata::<T, I>::iter_keys() {
1966 ensure!(Asset::<T, I>::contains_key(asset_id.clone()), "Orphaned Metadata found");
1967 }
1968
1969 for (asset_id, _, _) in Approvals::<T, I>::iter_keys() {
1970 ensure!(Asset::<T, I>::contains_key(asset_id.clone()), "Orphaned Approval found");
1971 }
1972
1973 for (asset_id, _) in Account::<T, I>::iter_keys() {
1974 ensure!(Asset::<T, I>::contains_key(asset_id.clone()), "Orphaned Account found");
1975 }
1976
1977 for (asset_id, details) in Asset::<T, I>::iter() {
1978 if details.status == AssetStatus::Destroying {
1979 continue;
1980 }
1981
1982 let mut calculated_supply = T::Balance::zero();
1983 let mut calculated_accounts = 0u32;
1984 let mut calculated_sufficients = 0u32;
1985
1986 for (who, account) in Account::<T, I>::iter_prefix(&asset_id) {
1987 let held = T::Holder::balance_on_hold(asset_id.clone(), &who).unwrap_or_default();
1988 calculated_supply =
1989 calculated_supply.saturating_add(account.balance).saturating_add(held);
1990 calculated_accounts += 1;
1991
1992 if matches!(account.reason, ExistenceReason::Sufficient) {
1993 calculated_sufficients += 1;
1994 }
1995
1996 let total_balance = account.balance.saturating_add(held);
1997 if total_balance < details.min_balance {
1998 if !matches!(
1999 account.reason,
2000 ExistenceReason::DepositHeld(_) | ExistenceReason::DepositFrom(_, _)
2001 ) {
2002 log::warn!(
2003 "Account {who:?} for asset {asset_id:?} has total balance below min_balance but no deposit. Balance: {:?}, Held: {:?}, Min balance: {:?}, Reason: {:?}",
2004 account.balance,
2005 held,
2006 details.min_balance,
2007 account.reason,
2008 );
2009 }
2010 }
2011 }
2012
2013 // Using >= instead of == because the provided `do_refund` implementation
2014 // historically destroyed the account balance without decrementing the asset
2015 // supply. Although this has been fixed, existing on-chain state may still
2016 // contain overcounted supply from prior refunds.
2017 // TODO: add a migration to recalculate supply, then tighten this to `==`.
2018 ensure!(details.supply >= calculated_supply, "Asset supply mismatch");
2019 if details.accounts != calculated_accounts {
2020 // Legacy error in Kusama Asset Hub that needs to be cleaned up.
2021 log::error!(
2022 "Asset {asset_id:?} account count mismatch: calculated {calculated_accounts} vs expected {}",
2023 details.accounts,
2024 );
2025 }
2026 ensure!(
2027 details.sufficients == calculated_sufficients,
2028 "Asset sufficients count mismatch"
2029 );
2030
2031 let calculated_approvals = Approvals::<T, I>::iter_prefix((&asset_id,)).count() as u32;
2032
2033 if details.approvals != calculated_approvals {
2034 log::error!(
2035 "Asset {asset_id:?} approvals count mismatch: calculated {calculated_approvals} vs expected {}",
2036 details.approvals,
2037 );
2038
2039 return Err("Asset approvals count mismatch".into());
2040 }
2041 }
2042 Ok(())
2043 }
2044}
2045
2046sp_core::generate_feature_enabled_macro!(runtime_benchmarks_enabled, feature = "runtime-benchmarks", $);