pallet_dev_mode/
lib.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//! <!-- markdown-link-check-disable -->
19//! # Dev Mode Example Pallet
20//!
21//! A simple example of a FRAME pallet demonstrating
22//! the ease of requirements for a pallet in dev mode.
23//!
24//! Run `cargo doc --package pallet-dev-mode --open` to view this pallet's documentation.
25//!
26//! **Dev mode is not meant to be used in production.**
27
28// Ensure we're `no_std` when compiling for Wasm.
29#![cfg_attr(not(feature = "std"), no_std)]
30
31extern crate alloc;
32
33use alloc::{vec, vec::Vec};
34use frame_support::dispatch::DispatchResult;
35use frame_system::ensure_signed;
36
37// Re-export pallet items so that they can be accessed from the crate namespace.
38pub use pallet::*;
39
40#[cfg(test)]
41mod tests;
42
43/// A type alias for the balance type from this pallet's point of view.
44type BalanceOf<T> = <T as pallet_balances::Config>::Balance;
45
46/// Enable `dev_mode` for this pallet.
47#[frame_support::pallet(dev_mode)]
48pub mod pallet {
49	use super::*;
50	use frame_support::pallet_prelude::*;
51	use frame_system::pallet_prelude::*;
52
53	#[pallet::config]
54	pub trait Config: pallet_balances::Config + frame_system::Config {
55		/// The overarching event type.
56		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
57	}
58
59	// Simple declaration of the `Pallet` type. It is placeholder we use to implement traits and
60	// method.
61	#[pallet::pallet]
62	pub struct Pallet<T>(_);
63
64	#[pallet::call]
65	impl<T: Config> Pallet<T> {
66		// No need to define a `call_index` attribute here because of `dev_mode`.
67		// No need to define a `weight` attribute here because of `dev_mode`.
68		pub fn add_dummy(origin: OriginFor<T>, id: T::AccountId) -> DispatchResult {
69			ensure_root(origin)?;
70
71			if let Some(mut dummies) = Dummy::<T>::get() {
72				dummies.push(id.clone());
73				Dummy::<T>::set(Some(dummies));
74			} else {
75				Dummy::<T>::set(Some(vec![id.clone()]));
76			}
77
78			// Let's deposit an event to let the outside world know this happened.
79			Self::deposit_event(Event::AddDummy { account: id });
80
81			Ok(())
82		}
83
84		// No need to define a `call_index` attribute here because of `dev_mode`.
85		// No need to define a `weight` attribute here because of `dev_mode`.
86		pub fn set_bar(
87			origin: OriginFor<T>,
88			#[pallet::compact] new_value: T::Balance,
89		) -> DispatchResult {
90			let sender = ensure_signed(origin)?;
91
92			// Put the new value into storage.
93			<Bar<T>>::insert(&sender, new_value);
94
95			Self::deposit_event(Event::SetBar { account: sender, balance: new_value });
96
97			Ok(())
98		}
99	}
100
101	#[pallet::event]
102	#[pallet::generate_deposit(pub(super) fn deposit_event)]
103	pub enum Event<T: Config> {
104		AddDummy { account: T::AccountId },
105		SetBar { account: T::AccountId, balance: BalanceOf<T> },
106	}
107
108	/// The MEL requirement for bounded pallets is skipped by `dev_mode`.
109	/// This means that all storages are marked as unbounded.
110	/// This is equivalent to specifying `#[pallet::unbounded]` on this type definitions.
111	/// When the dev_mode is removed, we would need to implement implement `MaxEncodedLen`.
112	#[pallet::storage]
113	pub type Dummy<T: Config> = StorageValue<_, Vec<T::AccountId>>;
114
115	/// The Hasher requirement is skipped by `dev_mode`. So, second parameter can be `_`
116	/// and `Blake2_128Concat` is used as a default.
117	/// When the dev_mode is removed, we would need to specify the hasher like so:
118	/// `pub type Bar<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, T::Balance>;`.
119	#[pallet::storage]
120	pub type Bar<T: Config> = StorageMap<_, _, T::AccountId, T::Balance>;
121}