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
62 // Simple declaration of the `Pallet` type. It is placeholder we use to implement traits and
63 // method.
64 #[pallet::pallet]
65 pub struct Pallet<T>(_);
66
67 #[pallet::call]
68 impl<T: Config> Pallet<T> {
69 // No need to define a `call_index` attribute here because of `dev_mode`.
70 // No need to define a `weight` attribute here because of `dev_mode`.
71 pub fn add_dummy(origin: OriginFor<T>, id: T::AccountId) -> DispatchResult {
72 ensure_root(origin)?;
73
74 if let Some(mut dummies) = Dummy::<T>::get() {
75 dummies.push(id.clone());
76 Dummy::<T>::set(Some(dummies));
77 } else {
78 Dummy::<T>::set(Some(vec![id.clone()]));
79 }
80
81 // Let's deposit an event to let the outside world know this happened.
82 Self::deposit_event(Event::AddDummy { account: id });
83
84 Ok(())
85 }
86
87 // No need to define a `call_index` attribute here because of `dev_mode`.
88 // No need to define a `weight` attribute here because of `dev_mode`.
89 pub fn set_bar(
90 origin: OriginFor<T>,
91 #[pallet::compact] new_value: T::Balance,
92 ) -> DispatchResult {
93 let sender = ensure_signed(origin)?;
94
95 // Put the new value into storage.
96 <Bar<T>>::insert(&sender, new_value);
97
98 Self::deposit_event(Event::SetBar { account: sender, balance: new_value });
99
100 Ok(())
101 }
102 }
103
104 #[pallet::event]
105 #[pallet::generate_deposit(pub(super) fn deposit_event)]
106 pub enum Event<T: Config> {
107 AddDummy { account: T::AccountId },
108 SetBar { account: T::AccountId, balance: BalanceOf<T> },
109 }
110
111 /// The MEL requirement for bounded pallets is skipped by `dev_mode`.
112 /// This means that all storages are marked as unbounded.
113 /// This is equivalent to specifying `#[pallet::unbounded]` on this type definitions.
114 /// When the dev_mode is removed, we would need to implement implement `MaxEncodedLen`.
115 #[pallet::storage]
116 pub type Dummy<T: Config> = StorageValue<_, Vec<T::AccountId>>;
117
118 /// The Hasher requirement is skipped by `dev_mode`. So, second parameter can be `_`
119 /// and `Blake2_128Concat` is used as a default.
120 /// When the dev_mode is removed, we would need to specify the hasher like so:
121 /// `pub type Bar<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, T::Balance>;`.
122 #[pallet::storage]
123 pub type Bar<T: Config> = StorageMap<_, _, T::AccountId, T::Balance>;
124}