referrerpolicy=no-referrer-when-downgrade

chain_spec_guide_runtime/
pallets.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
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//! Pallets for the chain-spec demo runtime.
19
20use alloc::vec::Vec;
21use frame::prelude::*;
22
23#[docify::export]
24#[frame::pallet(dev_mode)]
25pub mod pallet_bar {
26	use super::*;
27
28	#[pallet::config]
29	pub trait Config: frame_system::Config {}
30
31	#[pallet::pallet]
32	pub struct Pallet<T>(_);
33
34	#[pallet::storage]
35	pub(super) type InitialAccount<T: Config> = StorageValue<Value = T::AccountId>;
36
37	/// Simple `GenesisConfig`.
38	#[pallet::genesis_config]
39	#[derive(DefaultNoBound)]
40	#[docify::export(pallet_bar_GenesisConfig)]
41	pub struct GenesisConfig<T: Config> {
42		pub initial_account: Option<T::AccountId>,
43	}
44
45	#[pallet::genesis_build]
46	#[docify::export(pallet_bar_build)]
47	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
48		/// The storage building function that presents a direct mapping of the initial config
49		/// values to the storage items.
50		fn build(&self) {
51			InitialAccount::<T>::set(self.initial_account.clone());
52		}
53	}
54}
55
56/// The sample structure used in `GenesisConfig`.
57///
58/// This structure does not deny unknown fields. This may lead to some problems.
59#[derive(Default, serde::Serialize, serde::Deserialize)]
60#[serde(rename_all = "camelCase")]
61pub struct FooStruct {
62	pub field_a: u8,
63	pub field_b: u8,
64}
65
66/// The sample structure used in `GenesisConfig`.
67///
68/// This structure does not deny unknown fields. This may lead to some problems.
69#[derive(Default, serde::Serialize, serde::Deserialize)]
70#[serde(deny_unknown_fields, rename_all = "camelCase")]
71pub struct SomeFooData1 {
72	pub a: u8,
73	pub b: u8,
74}
75
76/// Another sample structure used in `GenesisConfig`.
77///
78/// The user defined serialization is used.
79#[derive(Default, serde::Serialize, serde::Deserialize)]
80#[docify::export]
81#[serde(deny_unknown_fields, rename_all = "camelCase")]
82pub struct SomeFooData2 {
83	#[serde(default, with = "sp_core::bytes")]
84	pub values: Vec<u8>,
85}
86
87/// Sample enum used in `GenesisConfig`.
88#[derive(Default, serde::Serialize, serde::Deserialize)]
89pub enum FooEnum {
90	#[default]
91	Data0,
92	Data1(SomeFooData1),
93	Data2(SomeFooData2),
94}
95
96#[docify::export]
97#[frame::pallet(dev_mode)]
98pub mod pallet_foo {
99	use super::*;
100
101	#[pallet::config]
102	pub trait Config: frame_system::Config {}
103
104	#[pallet::pallet]
105	pub struct Pallet<T>(_);
106
107	#[pallet::storage]
108	pub type ProcessedEnumValue<T> = StorageValue<Value = u64>;
109	#[pallet::storage]
110	pub type SomeInteger<T> = StorageValue<Value = u32>;
111
112	/// The more sophisticated structure for conveying initial state.
113	#[docify::export(pallet_foo_GenesisConfig)]
114	#[pallet::genesis_config]
115	#[derive(DefaultNoBound)]
116	pub struct GenesisConfig<T: Config> {
117		pub some_integer: u32,
118		pub some_enum: FooEnum,
119		pub some_struct: FooStruct,
120		#[serde(skip)]
121		pub _phantom: PhantomData<T>,
122	}
123
124	#[pallet::genesis_build]
125	#[docify::export(pallet_foo_build)]
126	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
127		/// The build method that indirectly maps an initial config values into the storage items.
128		fn build(&self) {
129			let processed_value: u64 = match &self.some_enum {
130				FooEnum::Data0 => 0,
131				FooEnum::Data1(v) => (v.a + v.b).into(),
132				FooEnum::Data2(v) => v.values.iter().map(|v| *v as u64).sum(),
133			};
134			ProcessedEnumValue::<T>::set(Some(processed_value));
135			SomeInteger::<T>::set(Some(self.some_integer));
136		}
137	}
138}