pallet_dev_mode/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: MIT-0
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy of
7// this software and associated documentation files (the "Software"), to deal in
8// the Software without restriction, including without limitation the rights to
9// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10// of the Software, and to permit persons to whom the Software is furnished to do
11// so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23
24//! <!-- markdown-link-check-disable -->
25//! # Dev Mode Example Pallet
26//!
27//! A simple example of a FRAME pallet demonstrating
28//! the ease of requirements for a pallet in dev mode.
29//!
30//! Run `cargo doc --package pallet-dev-mode --open` to view this pallet's documentation.
31//!
32//! **Dev mode is not meant to be used in production.**
33
34// Ensure we're `no_std` when compiling for Wasm.
35#![cfg_attr(not(feature = "std"), no_std)]
36
37extern crate alloc;
38
39use alloc::{vec, vec::Vec};
40use frame_support::dispatch::DispatchResult;
41use frame_system::ensure_signed;
42
43// Re-export pallet items so that they can be accessed from the crate namespace.
44pub use pallet::*;
45
46#[cfg(test)]
47mod tests;
48
49/// A type alias for the balance type from this pallet's point of view.
50type BalanceOf<T> = <T as pallet_balances::Config>::Balance;
51
52/// Enable `dev_mode` for this pallet.
53#[frame_support::pallet(dev_mode)]
54pub mod pallet {
55	use super::*;
56	use frame_support::pallet_prelude::*;
57	use frame_system::pallet_prelude::*;
58
59	#[pallet::config]
60	pub trait Config: pallet_balances::Config + frame_system::Config {
61		/// The overarching event type.
62		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
63	}
64
65	// Simple declaration of the `Pallet` type. It is placeholder we use to implement traits and
66	// method.
67	#[pallet::pallet]
68	pub struct Pallet<T>(_);
69
70	#[pallet::call]
71	impl<T: Config> Pallet<T> {
72		// No need to define a `call_index` attribute here because of `dev_mode`.
73		// No need to define a `weight` attribute here because of `dev_mode`.
74		pub fn add_dummy(origin: OriginFor<T>, id: T::AccountId) -> DispatchResult {
75			ensure_root(origin)?;
76
77			if let Some(mut dummies) = Dummy::<T>::get() {
78				dummies.push(id.clone());
79				Dummy::<T>::set(Some(dummies));
80			} else {
81				Dummy::<T>::set(Some(vec![id.clone()]));
82			}
83
84			// Let's deposit an event to let the outside world know this happened.
85			Self::deposit_event(Event::AddDummy { account: id });
86
87			Ok(())
88		}
89
90		// No need to define a `call_index` attribute here because of `dev_mode`.
91		// No need to define a `weight` attribute here because of `dev_mode`.
92		pub fn set_bar(
93			origin: OriginFor<T>,
94			#[pallet::compact] new_value: T::Balance,
95		) -> DispatchResult {
96			let sender = ensure_signed(origin)?;
97
98			// Put the new value into storage.
99			<Bar<T>>::insert(&sender, new_value);
100
101			Self::deposit_event(Event::SetBar { account: sender, balance: new_value });
102
103			Ok(())
104		}
105	}
106
107	#[pallet::event]
108	#[pallet::generate_deposit(pub(super) fn deposit_event)]
109	pub enum Event<T: Config> {
110		AddDummy { account: T::AccountId },
111		SetBar { account: T::AccountId, balance: BalanceOf<T> },
112	}
113
114	/// The MEL requirement for bounded pallets is skipped by `dev_mode`.
115	/// This means that all storages are marked as unbounded.
116	/// This is equivalent to specifying `#[pallet::unbounded]` on this type definitions.
117	/// When the dev_mode is removed, we would need to implement implement `MaxEncodedLen`.
118	#[pallet::storage]
119	pub type Dummy<T: Config> = StorageValue<_, Vec<T::AccountId>>;
120
121	/// The Hasher requirement is skipped by `dev_mode`. So, second parameter can be `_`
122	/// and `Blake2_128Concat` is used as a default.
123	/// When the dev_mode is removed, we would need to specify the hasher like so:
124	/// `pub type Bar<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, T::Balance>;`.
125	#[pallet::storage]
126	pub type Bar<T: Config> = StorageMap<_, _, T::AccountId, T::Balance>;
127}