Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
8a4b955
wip
liamaharon Dec 23, 2023
4aa41fd
Merge branch 'master' into liam-tokens-docs
liamaharon Feb 28, 2024
8e24dbc
update cargo.lock
liamaharon Feb 28, 2024
ef7c3dd
wip
liamaharon Feb 28, 2024
8a9a95a
Merge branch 'master' into liam-tokens-docs
liamaharon Feb 29, 2024
1204191
Merge branch 'master' into liam-tokens-docs
liamaharon Mar 1, 2024
1d1b71e
wip fungible docs
liamaharon Mar 13, 2024
db18a5f
Merge branch 'master' into liam-tokens-docs
liamaharon Mar 13, 2024
96c2993
Merge branch 'master' into liam-tokens-docs
liamaharon Mar 13, 2024
3a0c39c
document pallets and traits
liamaharon Mar 15, 2024
c34c01d
Merge branch 'master' into liam-tokens-docs
liamaharon Mar 15, 2024
5d4e2d4
Update docs/sdk/src/reference_docs/tokens_in_substrate.rs
liamaharon Mar 15, 2024
f0e9ce5
Update substrate/frame/support/src/traits/tokens/fungible/mod.rs
liamaharon Mar 15, 2024
304b6a7
Update substrate/frame/support/src/traits/tokens/fungible/mod.rs
liamaharon Mar 15, 2024
bacaa72
Update substrate/frame/support/src/traits/tokens/fungible/mod.rs
liamaharon Mar 15, 2024
25c1cb3
Update substrate/frame/support/src/traits/tokens/fungible/mod.rs
liamaharon Mar 15, 2024
e86c7a9
Update substrate/frame/support/src/traits/tokens/fungible/mod.rs
liamaharon Mar 15, 2024
41a3cb8
Update substrate/frame/support/src/traits/tokens/fungible/mod.rs
liamaharon Mar 15, 2024
f389aa7
Update docs/sdk/src/reference_docs/mod.rs
liamaharon Mar 15, 2024
9ff81e1
Update docs/sdk/src/reference_docs/tokens_in_substrate.rs
liamaharon Mar 15, 2024
c8d04a9
Update docs/sdk/src/reference_docs/tokens_in_substrate.rs
liamaharon Mar 15, 2024
34d33a3
Update docs/sdk/src/reference_docs/tokens_in_substrate.rs
liamaharon Mar 15, 2024
39227f2
Update docs/sdk/src/reference_docs/tokens_in_substrate.rs
liamaharon Mar 15, 2024
fc10027
Update docs/sdk/src/reference_docs/tokens_in_substrate.rs
liamaharon Mar 15, 2024
4ad31f5
Update docs/sdk/src/reference_docs/tokens_in_substrate.rs
liamaharon Mar 15, 2024
e56bcbf
Update substrate/frame/balances/src/lib.rs
liamaharon Mar 15, 2024
58aca97
Update substrate/frame/support/src/traits/tokens/fungible/mod.rs
liamaharon Mar 15, 2024
ca849d0
Update substrate/frame/support/src/traits/tokens/fungible/mod.rs
liamaharon Mar 15, 2024
6101352
Update docs/sdk/src/reference_docs/tokens_in_substrate.rs
liamaharon Mar 15, 2024
44e22cd
Merge branch 'master' into liam-tokens-docs
liamaharon Mar 19, 2024
566dd5d
rename frame tokens
liamaharon Mar 19, 2024
c0eab83
rustfmt
liamaharon Mar 19, 2024
b884b43
address comments
liamaharon Mar 19, 2024
32a86a0
Update substrate/frame/assets/src/lib.rs
liamaharon Mar 19, 2024
1b27d16
Update substrate/frame/balances/src/lib.rs
liamaharon Mar 19, 2024
7ac73c5
link back to parent doc
liamaharon Mar 20, 2024
af05963
add note regarding ItemOf
liamaharon Mar 20, 2024
28ef429
remove frame_currency
liamaharon Mar 20, 2024
d59f203
add additional notes on holds and freezes
liamaharon Mar 21, 2024
cbc8752
further freeze vs hold discussion
liamaharon Mar 21, 2024
463eb3e
Merge branch 'master' into liam-tokens-docs
liamaharon Mar 21, 2024
dd14974
Update substrate/frame/balances/src/lib.rs
liamaharon Mar 22, 2024
1986c08
Update docs/sdk/src/reference_docs/frame_tokens.rs
liamaharon Mar 26, 2024
b6924b2
Update docs/sdk/src/reference_docs/frame_tokens.rs
liamaharon Mar 26, 2024
193ae2b
address comments
liamaharon Mar 26, 2024
2248dfb
Update substrate/frame/support/src/traits/tokens/fungible/mod.rs
liamaharon Mar 26, 2024
7894ca0
Update substrate/frame/support/src/traits/tokens/fungible/mod.rs
liamaharon Mar 26, 2024
a4fd664
address comments
liamaharon Mar 26, 2024
1817539
merge
liamaharon Mar 26, 2024
11ebcfa
Merge branch 'master' into liam-tokens-docs
liamaharon Mar 26, 2024
ed3bfff
fmt
liamaharon Mar 26, 2024
3521ea2
add note about removing hold/freeze reasons
liamaharon Mar 26, 2024
5654274
Update docs/sdk/src/reference_docs/frame_tokens.rs
liamaharon Mar 26, 2024
1774c51
Update docs/sdk/src/reference_docs/frame_tokens.rs
liamaharon Mar 26, 2024
1e67f42
Update docs/sdk/src/reference_docs/frame_tokens.rs
liamaharon Mar 26, 2024
77d9fc9
Update docs/sdk/src/reference_docs/frame_tokens.rs
liamaharon Mar 26, 2024
c7cca8a
Update docs/sdk/src/reference_docs/frame_tokens.rs
liamaharon Mar 26, 2024
0cb5e77
fix nft docs
liamaharon Mar 26, 2024
0f33d96
Update docs/sdk/src/reference_docs/frame_tokens.rs
liamaharon Mar 29, 2024
d4fd9e0
Merge branch 'master' into liam-tokens-docs
liamaharon Mar 29, 2024
3a8f2b2
Merge branch 'master' into liam-tokens-docs
kianenigma Mar 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@ pallet-aura = { path = "../../substrate/frame/aura", default-features = false }
pallet-timestamp = { path = "../../substrate/frame/timestamp" }
pallet-balances = { path = "../../substrate/frame/balances" }
pallet-assets = { path = "../../substrate/frame/assets" }
pallet-preimage = { path = "../../substrate/frame/preimage" }
pallet-transaction-payment = { path = "../../substrate/frame/transaction-payment" }
pallet-utility = { path = "../../substrate/frame/utility" }
pallet-multisig = { path = "../../substrate/frame/multisig" }
pallet-proxy = { path = "../../substrate/frame/proxy" }
pallet-authorship = { path = "../../substrate/frame/authorship" }
pallet-collective = { path = "../../substrate/frame/collective" }
pallet-democracy = { path = "../../substrate/frame/democracy" }
pallet-uniques = { path = "../../substrate/frame/uniques" }
pallet-nfts = { path = "../../substrate/frame/nfts" }
pallet-scheduler = { path = "../../substrate/frame/scheduler" }

# Primitives
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@
//!
//! TODO: Link to multi block migration example/s once PR is merged (<https://github.com/paritytech/polkadot-sdk/pull/2119>).
//!
//! [`GetStorageVersion`]: frame_support::traits::GetStorageVersion
//! [`OnRuntimeUpgrade`]: frame_support::traits::OnRuntimeUpgrade
//! [`StorageVersion`]: frame_support::traits::StorageVersion
//! [`set_code`]: frame_system::Call::set_code
Expand Down
3 changes: 3 additions & 0 deletions docs/sdk/src/reference_docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub mod glossary;
/// Learn about the WASM meta-protocol of all Substrate-based chains.
pub mod wasm_meta_protocol;

/// Learn about the token-related logic in Substrate and how to apply it to your usecase.
pub mod tokens_in_substrate;

/// Learn about the differences between smart contracts and a FRAME-based runtime. They are both
/// "code stored onchain", but how do they differ?
pub mod runtime_vs_smart_contract;
Expand Down
97 changes: 97 additions & 0 deletions docs/sdk/src/reference_docs/tokens_in_substrate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// This file is part of polkadot-sdk.
//
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! # Tokens in Substrate
//!
//! This reference doc serves as a high-level overview of the token-related logic in Substrate and
//! how to properly apply it to your usecase.
//!
//! On completion of reading this doc, you should have a good understanding of
//! - The distinction between token traits and trait implementations in Substrate, and why this
//! distinction is helpful
//! - Token-related traits avaliable in Substrate
//! - Token-related trait implementations in Substrate
//! - How to choose the right trait or trait implementation for your use case
//! - Where to go next
//!
//! ## Traits and Trait Implementations
//!
//! Broardly speaking, token logic in Substrate can be divided into two categories: traits and
//! trait implementations.
//!
//! **Traits** define common interfaces that types of token should implement. For example, the
//! [`frame_support::traits::fungible::Inspect`] trait specifies an interface for *inspecting*
//! token state such as the total issuance of the token, the balance of individual accounts, etc.
//!
//! **Trait implementations** are concrete implementations of these traits. For example, one of the
//! many traits [`pallet_balances`] implements is [`frame_support::traits::fungible::Inspect`].
//!
//! The distinction between traits and trait implementations is helpful because it allows pallets
//! and other logic to be generic over their dependencies, avoiding cumbersome pallet tight
//! coupling.
//!
//! To illustrate this with an example let's consider [`pallet_preimage`]. This pallet takes a
//! deposit in exchange for storing some preimage for use later. A naive implementation of the
//! pallet may use [`pallet_balances`] as a dependency, and directly call the methods exposed by
//! [`pallet_balances`] to reserve and unreserve deposits. This approach works well, until someone
//! has a usecase requiring that an asset from a different pallet such as [`pallet_assets`] is
//! used for the deposit. Rather than tightly couple [`pallet_preimage`] to [`pallet_balances`] AND
//! [`pallet_assets`], along with every other token type pallet a user could possibly specify,
//! [`pallet_preimage`] does not specify a concrete pallet as a dependency but instead accepts any
//! dependency which implements the [`frame_support::traits::tokens::currency::ReservableCurrency`]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably migrate pallet_preimage to use fungible so we don't have to use the old traits as the example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.. There's an open PR for it, waiting for MBM to write a migration.

//! trait. This allows [`pallet_preimage`] to support any arbitrary pallet implementing this trait,
//! without needing any knowledge of what those pallets may be or requiring changes to support new
//! pallets which may be written in the future.
//!
//! ## Fungible Token Traits in Substrate
//!
//! The [`frame_support::traits::fungible`] crate contains the latest set of Substrate
//! fungible token traits, and is recommended to use for all new logic requiring a fungible tokens.
//! See the crate documentation for more info about these fungible traits.
//!
//! [`frame_support::traits::fungibles`] provides very similar functionality to
//! [`frame_support::traits::fungible`], except it supports managing multiple tokens.
//!
//! You may notice the trait [`frame_support::traits::Currency`] with similar functionality is also
//! used in the codebase, however this trait is deprecated and existing logic is in the process of
//! being migrated to [`frame_support::traits::fungible`] ([tracking issue](https://github.com/paritytech/polkadot-sdk/issues/226)).
//!
//! ## Fungible Token Trait Implementations in Substrate
//!
//! [`pallet_balances`] implements [`frame_support::traits::fungible`], and is the most commonly
//! used fungible implementation in Substrate. Most of the time, it's used for managing the native
//! token of the blockchain network.
//!
//! [`pallet_assets`] implements [`frame_support::traits::fungibles`], and is another popular
//! fungible token implementation. It supports the creation and management of multiple assets in a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! fungible token implementation. It supports the creation and management of multiple assets in a
//! fungible tokens implementation. It supports the creation and management of multiple assets in a

//! single crate, making it a good choice when a network requires more assets in
//! addition to its native token.
//!
//! ## Non-Fungible Tokens in Substrate
//!
//! [`pallet_uniques`] is recommended to use for all NFT use cases in Substrate.
//! See the crate documentation for more info about this pallet.
//!
//! [`pallet_nfts`] is deprecated and should not be used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually the other way around 😅

//!
//! # What Next?
//!
//! - If you are interested in implementing a single fungible token, continue reading the
//! [`frame_support::traits::fungible`] and [`pallet_balances`] docs.
//! - If you are interested in implementing a set of fungible tokens, continue reading the
//! [`frame_support::traits::fungibles`] trait and [`pallet_assets`] docs.
//! - If you are interested in implementing an NFT, continue reading the [`pallet_uniques`] docs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! - If you are interested in implementing an NFT, continue reading the [`pallet_uniques`] docs.
//! - If you are interested in implementing an NFT, continue reading the [`pallet_nfts`] docs.

13 changes: 12 additions & 1 deletion substrate/frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@

//! # Assets Pallet
//!
//! A simple, secure module for dealing with fungible assets.
//! A simple, secure module for dealing with sets of assets implementing
//! [`fungible`](frame_support::traits::fungible) traits, via
//! [`fungibles`](frame_support::traits::fungibles) traits.
//!
//! The pallet makes heavy use of concepts such as Holds and Freezes from the
//! [`frame_support::traits::fungible`] traits, therefore you should read and understand those docs
//! as a prerequisite to understanding this pallet.
//!
//! See the [`tokens_in_substrate`] reference docs for more information about the place of the
//! Assets pallet in Substrate.
//!
//! ## Overview
//!
Expand Down Expand Up @@ -133,6 +142,8 @@
//!
//! * [`System`](../frame_system/index.html)
//! * [`Support`](../frame_support/index.html)
//!
//! [`tokens_in_substrate`]: ../polkadot_sdk_docs/reference_docs/tokens_in_substrate/index.html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know you could link like that, sweet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's nice for when clippy would otherwise auto-wrap the link.

// This recursion limit is needed because we have too many benchmarks and benchmarking will fail if
// we add more without this limit.
Expand Down
81 changes: 38 additions & 43 deletions substrate/frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@

//! # Balances Pallet
//!
//! The Balances pallet provides functionality for handling accounts and balances.
//! The Balances pallet provides functionality for handling accounts and balances for a single
//! token.
//!
//! - [`Config`]
//! - [`Call`]
//! - [`Pallet`]
//! It makes heavy use of concepts such as Holds and Freezes from the
//! [`frame_support::traits::fungible`] traits, therefore you should read and understand those docs
//! as a prerequisite to understanding this pallet.
//!
//! Also see the [`tokens_in_substrate`] reference docs for higher level information regarding the
//! place of this palet in Substrate.
//!
//! ## Overview
//!
Expand All @@ -38,42 +42,30 @@
//!
//! ### Terminology
//!
//! - **Existential Deposit:** The minimum balance required to create or keep an account open. This
//! prevents "dust accounts" from filling storage. When the free plus the reserved balance (i.e.
//! the total balance) fall below this, then the account is said to be dead; and it loses its
//! functionality as well as any prior history and all information on it is removed from the
//! chain's state. No account should ever have a total balance that is strictly between 0 and the
//! existential deposit (exclusive). If this ever happens, it indicates either a bug in this
//! pallet or an erroneous raw mutation of storage.
//!
//! - **Total Issuance:** The total number of units in existence in a system.
//!
//! - **Reaping an account:** The act of removing an account by resetting its nonce. Happens after
//! its total balance has become zero (or, strictly speaking, less than the Existential Deposit).
//!
//! - **Free Balance:** The portion of a balance that is not reserved. The free balance is the only
//! balance that matters for most operations.
//!
//! - **Reserved Balance:** Reserved balance still belongs to the account holder, but is suspended.
//! Reserved balance can still be slashed, but only after all the free balance has been slashed.
//!
//! - **Imbalance:** A condition when some funds were credited or debited without equal and opposite
//! accounting (i.e. a difference between total issuance and account balances). Functions that
//! result in an imbalance will return an object of the `Imbalance` trait that can be managed within
//! your runtime logic. (If an imbalance is simply dropped, it should automatically maintain any
//! book-keeping such as total issuance.)
//! ### Implementations
//!
//! - **Lock:** A freeze on a specified amount of an account's free balance until a specified block
//! number. Multiple locks always operate over the same funds, so they "overlay" rather than
//! "stack".
//! The Balances pallet provides implementations for the following [`fungible`] traits. If these
//! traits provide the functionality that you need, then you can avoid coupling with the Balances
//! pallet.
//!
//! ### Implementations
//! - [`fungible::Inspect`]
//! - [`fungible::Mutate`]
//! - [`fungible::Unbalanced`]
//! - [`fungible::Balanced`]
//! - [`fungible::BalancedHold`]
//! - [`fungible::InspectHold`]
//! - [`fungible::MutateHold`]
//! - [`fungible::InspectFreeze`]
//! - [`fungible::MutateFreeze`]
//! - [`fungible::Imbalance`]
//!
//! The Balances pallet provides implementations for the following traits. If these traits provide
//! the functionality that you need, then you can avoid coupling with the Balances pallet.
//! It also implements the following [`Currency`] related traits, however they are deprecated and
//! will eventually be removed.
//!
//! - [`Currency`]: Functions for dealing with a
//! fungible assets system.
//! - [`Currency`]: Functions for dealing with a fungible assets system.
//! - [`ReservableCurrency`]
//! - [`NamedReservableCurrency`](frame_support::traits::NamedReservableCurrency):
//! Functions for dealing with assets that can be reserved from an account.
Expand All @@ -83,14 +75,6 @@
//! imbalances between total issuance in the system and account balances. Must be used when a
//! function creates new funds (e.g. a reward) or destroys some funds (e.g. a system fee).
//!
//! ## Interface
//!
//! ### Dispatchable Functions
//!
//! - `transfer_allow_death` - Transfer some liquid free balance to another account.
//! - `force_set_balance` - Set the balances of a given account. The origin of this call must be
//! root.
//!
//! ## Usage
//!
//! The following examples show how to use the Balances pallet in your custom pallet.
Expand Down Expand Up @@ -151,8 +135,11 @@
//! * Total issued balanced of all accounts should be less than `Config::Balance::max_value()`.
//! * Existential Deposit is set to a value greater than zero.
//!
//! Note, you may find the Balances pallet still functions with an ED of zero in some circumstances,
//! however this is not a configuration which is generally supported, nor will it be.
//! Note, you may find the Balances pallet still functions with an ED of zero when the
//! `insecure_zero_ed` cargo feature is enabled. However this is not a configuration which is
//! generally supported, nor will it be.
//!
//! [`tokens_in_substrate`]: ../polkadot_sdk_docs/reference_docs/tokens_in_substrate/index.html

#![cfg_attr(not(feature = "std"), no_std)]
mod benchmarking;
Expand Down Expand Up @@ -308,10 +295,14 @@ pub mod pallet {

/// The maximum number of locks that should exist on an account.
/// Not strictly enforced, but used for weight estimation.
///
/// Use of locks is deprecated in favour of freezes. See `https://github.com/paritytech/substrate/pull/12951/`
#[pallet::constant]
type MaxLocks: Get<u32>;

/// The maximum number of named reserves that can exist on an account.
///
/// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/`
#[pallet::constant]
type MaxReserves: Get<u32>;

Expand Down Expand Up @@ -455,6 +446,8 @@ pub mod pallet {

/// Any liquidity locks on some account balances.
/// NOTE: Should only be accessed when setting, changing and freeing a lock.
///
/// Use of locks is deprecated in favour of freezes. See `https://github.com/paritytech/substrate/pull/12951/`
#[pallet::storage]
#[pallet::getter(fn locks)]
pub type Locks<T: Config<I>, I: 'static = ()> = StorageMap<
Expand All @@ -466,6 +459,8 @@ pub mod pallet {
>;

/// Named reserves on some account balances.
///
/// Use of reserves is deprecated in favour of holds. See `https://github.com/paritytech/substrate/pull/12951/`
#[pallet::storage]
#[pallet::getter(fn reserves)]
pub type Reserves<T: Config<I>, I: 'static = ()> = StorageMap<
Expand Down
3 changes: 3 additions & 0 deletions substrate/frame/support/src/traits/tokens/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
// limitations under the License.

//! The Currency trait and associated types.
//!
//! Note Currency and related traits are deprecated, instead
//! [`fungible`](frame_support::traits::fungible) traits should be used.
use super::{
imbalance::{Imbalance, SignedImbalance},
Expand Down
Loading