Use loose pallet coupling
This guide steps through how to reuse a function or type from another pallet using a technique called loose coupling.
Loose coupling is a technique that enables re-using logic from another pallet inside a pallet. In this guide, we show the simple pattern of using a type from an outside pallet in our working pallet, by using trait bounds in our pallet's configuration trait. We will loosely couple a pallet to make use of the `Currency` trait from `frame_support`.
Configure your workspace
In the Cargo.toml file of the pallet in your working directory, make sure you specify the
pallet you want to couple to accordingly:
[dependencies]
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.26"}
# -- snip
[features]
default = ['std']
std = [
'frame-support/std',
# -- snip
]Import the trait you want to use
In this example, we want to use the Currency trait from frame_support so that we can give our pallet access to the its methods.
Import the trait in your pallet:
use frame_support::traits::Currency;Create a type for your pallet's Config trait
-
In your configuration trait, create a type that is bound by the type you want to expose to your pallet (in
this-pallet/src/lib.rs):pub trait Config: frame_system::Config { // --snip-- /// A type that is accessing our loosely coupled pallet `my-pallet` type LocalCurrency: Currency<Self::AccountId>; } -
Use the method that the type of your loosely coupled pallet provides (in
this-pallet/src/lib.rs):// Use the getter from `my-pallet` let total_balance = T::LocalCurrency::total_issuance();
In the above snippet, we're using total_issuance
that the Currency trait exposes from frame_support.
Provide the implementation in runtime configuration
In our runtime configuration, usually runtime/src/lib.rs, we specify the LocalCurrency to be
Balances, which is defined inside construct_runtime! macro and has a type of pallet_balances
that implements the Currency trait.
impl my_pallet::Config for Runtime {
type LocalCurrency = Balances;
}
construct_runtime! (
pub enum Runtime where
Block = Block,
NodeBlock = opaque::Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
}
)Examples
try_originfrom theEnsureOrigintrait in FRAME's Democracy pallet- the use of
WeightInfoin all FRAME pallets, such as the the Identity pallet and its use of its specific weighting methods - the
KeyOwnerProofSystemtrait used inpallet_grandpa
