pezkuwi_sdk_docs/reference_docs/frame_tokens.rs
1// This file is part of pezkuwi-sdk.
2//
3// Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute
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//! # FRAME Tokens
19//!
20//! This reference doc serves as a high-level overview of the token-related logic in FRAME, and
21//! how to properly apply it to your use case.
22//!
23//! On completion of reading this doc, you should have a good understanding of:
24//! - The distinction between token traits and trait implementations in FRAME, and why this
25//! distinction is helpful.
26//! - Token-related traits available in FRAME.
27//! - Token-related trait implementations in FRAME.
28//! - How to choose the right trait or trait implementation for your use case.
29//! - Where to go next.
30//!
31//! ## Getting Started
32//!
33//! The most ubiquitous way to add a token to a FRAME runtime is [`pezpallet_balances`]. Read
34//! more about pallets [here](crate::pezkuwi_sdk::frame_runtime#pallets).
35//!
36//! You may then write custom pallets that interact with [`pezpallet_balances`]. The fastest way to
37//! get started with that is by
38//! [tightly coupling](crate::reference_docs::frame_pallet_coupling#tight-coupling-pallets) your
39//! custom pezpallet to [`pezpallet_balances`].
40//!
41//! However, to keep pallets flexible and modular, it is often preferred to
42//! [loosely couple](crate::reference_docs::frame_pallet_coupling#loosely--coupling-pallets).
43//!
44//! To achieve loose coupling,
45//! we separate token logic into traits and trait implementations.
46//!
47//! ## Traits and Trait Implementations
48//!
49//! Broadly speaking, token logic in FRAME can be divided into two categories: traits and
50//! trait implementations.
51//!
52//! **Traits** define common interfaces that types of tokens should implement. For example, the
53//! [`fungible::Inspect`](`pezframe_support::traits::fungible::Inspect`) trait specifies an
54//! interface for *inspecting* token state such as the total issuance of the token, the balance of
55//! individual accounts, etc.
56//!
57//! **Trait implementations** are concrete implementations of these traits. For example, one of the
58//! many traits [`pezpallet_balances`] implements is
59//! [`fungible::Inspect`](`pezframe_support::traits::fungible::Inspect`)[^1]. It provides the
60//! concrete way of inspecting the total issuance, balance of accounts, etc. There can be many
61//! implementations of the same traits.
62//!
63//! [^1]: Rust Advanced Tip: The knowledge that [`pezpallet_balances`] implements
64//! [`fungible::Inspect`](`pezframe_support::traits::fungible::Inspect`) is not some arcane
65//! knowledge that you have to know by heart or memorize. One can simply look at the list of the
66//! implementors of any trait in the Rust Doc to find all implementors (e.g.
67//! [Mutate trait implementors](https://docs.pezkuwichain.io/sdk/master/pezframe_support/traits/tokens/fungible/trait.Mutate.html#implementors)),
68//! or use the `rust-analyzer`'s `Implementations` action.
69//!
70//! The distinction between traits and trait implementations is helpful because it allows pallets
71//! and other logic to be generic over their dependencies, avoiding tight coupling.
72//!
73//! To illustrate this with an example let's consider [`pezpallet_preimage`]. This pezpallet takes a
74//! deposit in exchange for storing a preimage for later use. A naive implementation of the
75//! pezpallet may use [`pezpallet_balances`] in a tightly coupled manner, directly calling methods
76//! on the pezpallet to reserve and unreserve deposits. This approach works well,
77//! until someone has a use case requiring that an asset from a different pezpallet such as
78//! [`pezpallet_assets`] is used for the deposit. Rather than tightly coupling
79//! [`pezpallet_preimage`] to [`pezpallet_balances`], [`pezpallet_assets`], and every other
80//! token-handling pezpallet, a user could possibly specify that [`pezpallet_preimage`] does not
81//! specify a concrete pezpallet as a dependency, but instead accepts any dependency which
82//! implements the
83//! [`currency::ReservableCurrency`](`pezframe_support::traits::tokens::currency::ReservableCurrency`)
84//! trait, namely via its [`Config::Currency`](`pezpallet_preimage::pezpallet::Config::Currency`)
85//! associated type. This allows [`pezpallet_preimage`] to support any arbitrary pezpallet
86//! implementing this trait, without needing any knowledge of what those pallets may be or requiring
87//! changes to support new pallets which may be written in the future.
88//!
89//! Read more about coupling, and the benefits of loose coupling
90//! [here](crate::reference_docs::frame_pallet_coupling).
91//!
92//! ## Fungible Token Traits in FRAME
93//!
94//! The [`fungible`](`pezframe_support::traits::fungible`) crate contains the latest set of FRAME
95//! fungible token traits, and is recommended to use for all new logic requiring a fungible token.
96//! See the crate documentation for more info about these fungible traits.
97//!
98//! [`fungibles`](`pezframe_support::traits::fungibles`) provides very similar functionality to
99//! [`fungible`](`pezframe_support::traits::fungible`), except it supports managing multiple tokens.
100//!
101//! You may notice the trait [`Currency`](`pezframe_support::traits::Currency`) with similar
102//! functionality is also used in the codebase, however this trait is deprecated and existing logic
103//! is in the process of being migrated to [`fungible`](`pezframe_support::traits::fungible`) ([tracking issue](https://github.com/pezkuwichain/pezkuwi-sdk/issues/248)).
104//!
105//! ## Fungible Token Trait Implementations in FRAME
106//!
107//! [`pezpallet_balances`] implements [`fungible`](`pezframe_support::traits::fungible`), and is the
108//! most commonly used fungible implementation in FRAME. Most of the time, it's used for managing
109//! the native token of the blockchain network it's used in.
110//!
111//! [`pezpallet_assets`] implements [`fungibles`](`pezframe_support::traits::fungibles`), and is
112//! another popular fungible token implementation. It supports the creation and management of
113//! multiple assets in a single crate, making it a good choice when a network requires more assets
114//! in addition to its native token.
115//!
116//! ## Non-Fungible Tokens in FRAME
117//!
118//! [`pezpallet_nfts`] is recommended to use for all NFT use cases in FRAME.
119//! See the crate documentation for more info about this pezpallet.
120//!
121//! [`pezpallet_uniques`] is deprecated and should not be used.
122//!
123//!
124//! # What Next?
125//!
126//! - If you are interested in implementing a single fungible token, continue reading the
127//! [`fungible`](`pezframe_support::traits::fungible`) and [`pezpallet_balances`] docs.
128//! - If you are interested in implementing a set of fungible tokens, continue reading the
129//! [`fungibles`](`pezframe_support::traits::fungibles`) trait and [`pezpallet_assets`] docs.
130//! - If you are interested in implementing an NFT, continue reading the [`pezpallet_nfts`] docs.