Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

Commit 23da1c3

Browse files
author
Dan Forbes
authored
New Add a Pallet Tutorial with Nicks Pallet (#627)
1 parent 7278cdf commit 23da1c3

29 files changed

+835
-247
lines changed
Loading
Loading
Loading
Loading
49.2 KB
Loading

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Substrate is designed to be used in one of three ways:
9595

9696
- Follow our [tutorial to start a private network with Substrate](tutorials/start-a-private-network/).
9797

98-
- Follow our [tutorial to add a runtime module to your Substrate runtime](tutorials/add-a-pallet-to-your-runtime/).
98+
- Follow our [tutorial to add a pallet to your FRAME runtime](tutorials/add-a-pallet/).
9999

100100
### References
101101

docs/knowledgebase/runtime/frame.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ amount for any reason other than transaction fee payment.
410410
### Examples
411411

412412
- Follow a
413-
[tutorial to add a pallet to your Substrate runtime](../../tutorials/add-a-pallet-to-your-runtime/).
413+
[tutorial to add a pallet to your FRAME runtime](../../tutorials/add-a-pallet/).
414414

415415
### References
416416

docs/knowledgebase/runtime/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ can be upgraded without a hard fork.
3232
- Follow a
3333
[tutorial to develop your first Substrate chain](../../tutorials/create-your-first-substrate-chain/).
3434
- Follow a
35-
[tutorial to add a pallet to your Substrate runtime](../../tutorials/add-a-pallet-to-your-runtime/).
35+
[tutorial to add a pallet to your Substrate runtime](../../tutorials/add-a-pallet/).

docs/knowledgebase/smart-contracts/contracts-pallet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ or upgradeable contracts on a Substrate based blockchain.
113113
### Examples
114114

115115
- Follow a
116-
[tutorial to add this Contracts pallet to your Substrate runtime](https://substrate.dev/docs/en/tutorials/add-a-pallet-to-your-runtime/).
116+
[tutorial to add this Contracts pallet to your Substrate runtime](../../tutorials/add-contracts-pallet/).
117117

118118
### References
119119

docs/knowledgebase/smart-contracts/evm-pallet.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ to support earlier hard fork configurations.
5151

5252
### Examples
5353

54-
- Follow a
55-
[tutorial to add a runtime module to your Substrate runtime](https://substrate.dev/docs/en/tutorials/add-a-pallet-to-your-runtime/).
54+
- Follow a [tutorial to add a pallet to your FRAME runtime](../../tutorials/add-a-pallet/).
5655

5756
### References
5857

docs/knowledgebase/smart-contracts/faq.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ cargo +nightly test
4141
### How do I add the Contracts module to my custom chain?
4242

4343
You can follow
44-
[our guide here](https://substrate.dev/docs/en/tutorials/add-a-pallet-to-your-runtime/) for
45-
instructions to add the Contracts pallet and other FRAME pallets to your blockchain runtime.
44+
[our guide here](../../tutorials/add-contracts-pallet/) for instructions to add the Contracts pallet
45+
and other FRAME pallets to your blockchain runtime.

docs/knowledgebase/smart-contracts/ink-development.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Join the growing community of ink! smart contract developers:
9595
### Examples
9696

9797
- Follow a
98-
[tutorial to add this Contracts pallet to your Substrate runtime](https://substrate.dev/docs/en/tutorials/add-a-pallet-to-your-runtime/).
98+
[tutorial to add this Contracts pallet to your Substrate runtime](../../tutorials/add-contracts-pallet/).
9999

100100
### References
101101

docs/knowledgebase/smart-contracts/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ influence your decision on the kinds of situations you may want to use these dif
177177
### Examples
178178

179179
- Follow a
180-
[tutorial to add a runtime module to your Substrate runtime](../../tutorials/add-a-pallet-to-your-runtime/).
180+
[tutorial to add the Contracts pallet to your FRAME runtime](../../tutorials/add-contracts-pallet/).
181181
- Learn how to
182182
[start developing with the Contracts pallet and ink!](ink-development).
183183

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
title: Configure the Nicks Pallet
3+
---
4+
5+
Every pallet has a component called `Trait` that is used for configuration. This component is a
6+
[Rust "trait"](https://doc.rust-lang.org/book/ch10-02-traits.html); traits in Rust are similar to
7+
interfaces in languages such as C++, Java and Go. FRAME developers must implement this trait for
8+
each pallet they would like to include in a runtime in order to configure that pallet with the
9+
parameters and types that it needs from the outer runtime. For instance, in the template pallet that
10+
is included in the Node Template, you will see the following `Trait` configuration trait:
11+
12+
```rust
13+
/// Configure the pallet by specifying the parameters and types on which it depends.
14+
pub trait Trait: frame_system::Trait {
15+
/// Because this pallet emits events, it depends on the runtime's definition of an event.
16+
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
17+
}
18+
```
19+
20+
As the comment states, we are using the `Event` type of the `Trait` configuration trait in order to
21+
allow the `TemplateModule` pallet to emit a type of event that is compatible with the outer runtime.
22+
The `Trait` configuration trait may also be used to tune parameters that control the resources
23+
required to interact with a pallet or even to limit the resources of the runtime that the pallet may
24+
consume. You will see examples of both such cases below when you implement the `Trait` configuration
25+
trait for the Nicks pallet.
26+
27+
To figure out what you need to implement for the Nicks pallet specifically, you can take a look at
28+
the
29+
[`pallet_nicks::Trait` documentation](https://substrate.dev/rustdocs/v2.0.0-rc6/pallet_nicks/trait.Trait.html)
30+
or the definition of the trait itself in
31+
[the source code](https://github.com/paritytech/substrate/blob/v2.0.0-rc6/frame/nicks/src/lib.rs) of
32+
the Nicks pallet. We have annotated the source code below with new comments that expand on those
33+
already included in the documentation so be sure to read them:
34+
35+
```rust
36+
pub trait Trait: frame_system::Trait {
37+
// The runtime must supply this pallet with an Event type that satisfies the pallet's requirements.
38+
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
39+
40+
// The currency type that will be used to place deposits on nicks.
41+
// It must implement ReservableCurrency.
42+
// https://substrate.dev/rustdocs/v2.0.0-rc6/frame_support/traits/trait.ReservableCurrency.html
43+
type Currency: ReservableCurrency<Self::AccountId>;
44+
45+
// The amount required to reserve a nick.
46+
type ReservationFee: Get<BalanceOf<Self>>;
47+
48+
// A callback that will be invoked when a deposit is forfeited.
49+
type Slashed: OnUnbalanced<NegativeImbalanceOf<Self>>;
50+
51+
// Origins are used to identify network participants and control access.
52+
// This is used to identify the pallet's admin.
53+
// https://substrate.dev/docs/en/knowledgebase/runtime/origin
54+
type ForceOrigin: EnsureOrigin<Self::Origin>;
55+
56+
// This parameter is used to configure a nick's minimum length.
57+
type MinLength: Get<usize>;
58+
59+
// This parameter is used to configure a nick's maximum length.
60+
// https://substrate.dev/docs/en/knowledgebase/runtime/storage#create-bounds
61+
type MaxLength: Get<usize>;
62+
}
63+
```
64+
65+
Just like we used the Balances pallet as a template for importing the Nicks pallet, let's use the
66+
Balances pallet as an example to help us understand how can implement the `Trait` interface for the
67+
Nicks pallet. You will notice that this implementation consists of two parts: a `parameter_types!`
68+
block where constant values are defined and an `impl` block where the types and values defined by
69+
the `Trait` interface are configured. This code block has also been annotated with additional
70+
comments that you should be sure to read:
71+
72+
**`runtime/src/lib.rs`**
73+
74+
```rust
75+
parameter_types! {
76+
// The u128 constant value 500 is aliased to a type named ExistentialDeposit.
77+
pub const ExistentialDeposit: u128 = 500;
78+
}
79+
80+
impl pallet_balances::Trait for Runtime {
81+
// The "Balance" that appears after the equal sign is an alias for the u128 type.
82+
type Balance = Balance;
83+
84+
// The empty value, (), is used to specify a no-op callback function.
85+
type DustRemoval = ();
86+
87+
// The previously defined parameter_type is used as a configuration parameter.
88+
type ExistentialDeposit = ExistentialDeposit;
89+
90+
// The FRAME runtime system is used to track the accounts that hold balances.
91+
type AccountStore = System;
92+
93+
// No weight information is supplied to the Balances pallet by the Node Template's runtime.
94+
type WeightInfo = ();
95+
96+
// The ubiquitous event type.
97+
type Event = Event;
98+
}
99+
```
100+
101+
The `impl balances::Trait` block allows runtime developers that are including the Balances pallet in
102+
their runtime to configure the types and parameters that are specified by the Balances pallet
103+
`Trait` configuration trait. For example, the `impl` block above configures the Balances pallet to
104+
use the `u128` type to track balances. If you were developing a chain where it was important to
105+
optimize storage, you could use any unsigned integer type that was at least 32-bits in size; this is
106+
because
107+
[the `Balance` type](https://substrate.dev/rustdocs/v2.0.0-rc6/pallet_balances/trait.Trait.html#associatedtype.Balance)
108+
for the Balances pallet `Trait` configuration trait specifies
109+
[the `AtLeast32BitUnsigned` trait](https://substrate.dev/rustdocs/v2.0.0-rc6/sp_arithmetic/traits/trait.AtLeast32BitUnsigned.html)
110+
(or "trait" in Rust terminology).
111+
112+
Now that you have an idea of the purpose behind the `Trait` configuration trait and how you can
113+
implement a FRAME pallet's `Trait` interface for your runtime, let's implement the `Trait`
114+
configuration trait for the Nicks pallet. Add the following code to `runtime/src/lib.rs`:
115+
116+
```rust
117+
parameter_types! {
118+
// Choose a fee that incentivizes desireable behavior.
119+
pub const NickReservationFee: u128 = 100;
120+
pub const MinNickLength: usize = 8;
121+
// Maximum bounds on storage are important to secure your chain.
122+
pub const MaxNickLength: usize = 32;
123+
}
124+
125+
impl pallet_nicks::Trait for Runtime {
126+
// The Balances pallet implements the ReservableCurrency trait.
127+
// https://substrate.dev/rustdocs/v2.0.0-rc6/pallet_balances/index.html#implementations-2
128+
type Currency = pallet_balances::Module<Runtime>;
129+
130+
// Use the NickReservationFee from the parameter_types block.
131+
type ReservationFee = NickReservationFee;
132+
133+
// No action is taken when deposits are forfeited.
134+
type Slashed = ();
135+
136+
// Configure the FRAME System Root origin as the Nick pallet admin.
137+
// https://substrate.dev/rustdocs/v2.0.0-rc6/frame_system/enum.RawOrigin.html#variant.Root
138+
type ForceOrigin = EnsureRoot<AccountId>;
139+
140+
// Use the MinNickLength from the parameter_types block.
141+
type MinLength = MinNickLength;
142+
143+
// Use the MaxNickLength from the parameter_types block.
144+
type MaxLength = MaxNickLength;
145+
146+
// The ubiquitous event type.
147+
type Event = Event;
148+
}
149+
```
150+
151+
In order to use the `EnsureRoot<AccountId>` type as the `ForceOrigin` for the Nicks pallet, you will
152+
need to add the following line along with the other `import` statements at the top of
153+
`runtime/src/lib.rs`:
154+
155+
```rust
156+
use frame_system::EnsureRoot;
157+
```
158+
159+
### Adding Nicks to the `construct_runtime!` Macro
160+
161+
Next, we need to add the Nicks pallet to the `construct_runtime!` macro. For this, we need to
162+
determine the types that the pallet exposes so that we can tell the our runtime that they exist. The
163+
complete list of possible types can be found in the
164+
[`construct_runtime!` macro documentation](https://substrate.dev/rustdocs/v2.0.0-rc6/frame_support/macro.construct_runtime.html).
165+
166+
If we look at the Nicks pallet in detail, we know it has:
167+
168+
- Module **Storage**: Because it uses the `decl_storage!` macro.
169+
- Module **Event**s: Because it uses the `decl_event!` macro. You will notice that in the case of
170+
the Nicks pallet, the `Event` keyword is parameterized with respect to a type, `T`; this is
171+
because at least one of the events defined by the Nicks pallet depends on a type that is
172+
configured with the `Trait` configuration trait.
173+
- **Call**able Functions: Because it has dispatchable functions in the `decl_module!` macro.
174+
- The **Module** type from the `decl_module!` macro.
175+
176+
Thus, when we add the pallet, it will look like this:
177+
178+
**`runtime/src/lib.rs`**
179+
180+
```rust
181+
construct_runtime!(
182+
pub enum Runtime where
183+
Block = Block,
184+
NodeBlock = opaque::Block,
185+
UncheckedExtrinsic = UncheckedExtrinsic
186+
{
187+
/* --snip-- */
188+
189+
/*** Add This Line ***/
190+
Nicks: pallet_nicks::{Module, Call, Storage, Event<T>},
191+
}
192+
);
193+
```
194+
195+
Note that not all pallets will expose all of these runtime types, and some may expose more! You
196+
should always look at the documentation or source code of a pallet to determine which of these types
197+
you need to expose.
198+
199+
This is another good time to check that your runtime compiles correctly so far. Use this command to
200+
check just the runtime.
201+
202+
```bash
203+
cargo check -p node-template-runtime
204+
```

0 commit comments

Comments
 (0)