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