frame_support/traits/tokens/fungibles/lifetime.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//! Traits for creating, editing and destroying assets.
19//!
20//! See the [`crate::traits::fungibles`] doc for more information about fungibles traits.
21
22use super::Inspect;
23use crate::traits::tokens::{AssetId, Balance};
24use sp_runtime::{DispatchError, DispatchResult};
25
26/// Trait for providing the ability to create new fungible assets.
27pub trait Create<AccountId>: Inspect<AccountId> {
28 /// Create a new fungible asset.
29 fn create(
30 id: Self::AssetId,
31 admin: AccountId,
32 is_sufficient: bool,
33 min_balance: Self::Balance,
34 ) -> DispatchResult;
35}
36
37/// Trait for refunding the existence deposit of a target asset account.
38///
39/// The existence deposit might by necessary and present in cases where the asset held by the
40/// account is insufficient for the required storage, or when the system cannot provide a consumer
41/// reference for any reason.
42pub trait Refund<AccountId> {
43 /// Means of identifying one asset class from another.
44 type AssetId: AssetId;
45 /// Scalar type for representing deposit balance of an account.
46 type Balance: Balance;
47 /// Returns the amount of account deposit and depositor address, if any.
48 fn deposit_held(id: Self::AssetId, who: AccountId) -> Option<(AccountId, Self::Balance)>;
49 /// Return the deposit (if any) of a target asset account.
50 fn refund(id: Self::AssetId, who: AccountId) -> DispatchResult;
51}
52
53/// Trait for providing the ability to destroy existing fungible assets.
54pub trait Destroy<AccountId>: Inspect<AccountId> {
55 /// Start the destruction an existing fungible asset.
56 /// * `id`: The `AssetId` to be destroyed. successfully.
57 /// * `maybe_check_owner`: An optional account id that can be used to authorize the destroy
58 /// command. If not provided, no authorization checks will be performed before destroying
59 /// asset.
60 fn start_destroy(id: Self::AssetId, maybe_check_owner: Option<AccountId>) -> DispatchResult;
61
62 /// Destroy all accounts associated with a given asset.
63 /// `destroy_accounts` should only be called after `start_destroy` has been called, and the
64 /// asset is in a `Destroying` state
65 ///
66 /// * `id`: The identifier of the asset to be destroyed. This must identify an existing asset.
67 /// * `max_items`: The maximum number of accounts to be destroyed for a given call of the
68 /// function. This value should be small enough to allow the operation fit into a logical
69 /// block.
70 ///
71 /// Response:
72 /// * u32: Total number of approvals which were actually destroyed
73 ///
74 /// Due to weight restrictions, this function may need to be called multiple
75 /// times to fully destroy all approvals. It will destroy `max_items` approvals at a
76 /// time.
77 fn destroy_accounts(id: Self::AssetId, max_items: u32) -> Result<u32, DispatchError>;
78 /// Destroy all approvals associated with a given asset up to the `max_items`
79 /// `destroy_approvals` should only be called after `start_destroy` has been called, and the
80 /// asset is in a `Destroying` state
81 ///
82 /// * `id`: The identifier of the asset to be destroyed. This must identify an existing asset.
83 /// * `max_items`: The maximum number of accounts to be destroyed for a given call of the
84 /// function. This value should be small enough to allow the operation fit into a logical
85 /// block.
86 ///
87 /// Response:
88 /// * u32: Total number of approvals which were actually destroyed
89 ///
90 /// Due to weight restrictions, this function may need to be called multiple
91 /// times to fully destroy all approvals. It will destroy `max_items` approvals at a
92 /// time.
93 fn destroy_approvals(id: Self::AssetId, max_items: u32) -> Result<u32, DispatchError>;
94
95 /// Complete destroying asset and unreserve currency.
96 /// `finish_destroy` should only be called after `start_destroy` has been called, and the
97 /// asset is in a `Destroying` state. All accounts or approvals should be destroyed before
98 /// hand.
99 ///
100 /// * `id`: The identifier of the asset to be destroyed. This must identify an existing asset.
101 fn finish_destroy(id: Self::AssetId) -> DispatchResult;
102}