|
|
|
@@ -21,20 +21,20 @@
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
|
|
|
//! # Kitchensink Example Pallet
|
|
|
|
|
//! # Kitchensink Example Pezpallet
|
|
|
|
|
//!
|
|
|
|
|
//! **This pallet serves as an example and is not meant to be used in production.**
|
|
|
|
|
//! **This pezpallet serves as an example and is not meant to be used in production.**
|
|
|
|
|
//!
|
|
|
|
|
//! The kitchen-sink catalog of the the FRAME macros and their various syntax options.
|
|
|
|
|
//!
|
|
|
|
|
//! This example does not focus on pallet instancing, `dev_mode`, and does nto include any 'where'
|
|
|
|
|
//! This example does not focus on pezpallet instancing, `dev_mode`, and does nto include any 'where'
|
|
|
|
|
//! clauses on `T`. These will both incur additional complexity to the syntax, but are not discussed
|
|
|
|
|
//! here.
|
|
|
|
|
|
|
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
|
|
|
|
|
|
|
|
|
// Re-export pallet items so that they can be accessed from the crate namespace.
|
|
|
|
|
pub use pallet::*;
|
|
|
|
|
// Re-export pezpallet items so that they can be accessed from the crate namespace.
|
|
|
|
|
pub use pezpallet::*;
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
@@ -50,32 +50,32 @@ pub use weights::*;
|
|
|
|
|
|
|
|
|
|
extern crate alloc;
|
|
|
|
|
|
|
|
|
|
#[pezframe_support::pallet]
|
|
|
|
|
pub mod pallet {
|
|
|
|
|
#[pezframe_support::pezpallet]
|
|
|
|
|
pub mod pezpallet {
|
|
|
|
|
use super::*;
|
|
|
|
|
use pezframe_support::pezpallet_prelude::*;
|
|
|
|
|
use pezframe_system::pezpallet_prelude::*;
|
|
|
|
|
|
|
|
|
|
/// The config trait of the pallet. You can basically do anything with the config trait that you
|
|
|
|
|
/// The config trait of the pezpallet. You can basically do anything with the config trait that you
|
|
|
|
|
/// can do with a normal rust trait: import items consisting of types, constants and functions.
|
|
|
|
|
///
|
|
|
|
|
/// A very common pattern is for a pallet to import implementations of traits such as
|
|
|
|
|
/// A very common pattern is for a pezpallet to import implementations of traits such as
|
|
|
|
|
/// [`pezframe_support::traits::Currency`], [`pezframe_support::traits::fungibles::Inspect`] and
|
|
|
|
|
/// [`pezframe_support::traits::Get`]. These are all types that the pallet is delegating to the top
|
|
|
|
|
/// [`pezframe_support::traits::Get`]. These are all types that the pezpallet is delegating to the top
|
|
|
|
|
/// level runtime to provide to it.
|
|
|
|
|
///
|
|
|
|
|
/// The `FRAME`-specific syntax are:
|
|
|
|
|
///
|
|
|
|
|
/// * the use of `#[pallet::constant]`([`pezframe_support::procedural`]), which places a `Get`
|
|
|
|
|
/// * the use of `#[pezpallet::constant]`([`pezframe_support::procedural`]), which places a `Get`
|
|
|
|
|
/// implementation in the metadata.
|
|
|
|
|
/// * `type RuntimeEvent`, which is mandatory if your pallet has events. See TODO.
|
|
|
|
|
/// * `type RuntimeEvent`, which is mandatory if your pezpallet has events. See TODO.
|
|
|
|
|
/// * Needless to say, because [`Config`] is bounded by [`pezframe_system::Config`], you can use
|
|
|
|
|
/// all the items from [`pezframe_system::Config`] as well, such as `AccountId`.
|
|
|
|
|
/// * `#[pallet::disable_pezframe_system_supertrait_check]` would remove the need for
|
|
|
|
|
/// * `#[pezpallet::disable_pezframe_system_supertrait_check]` would remove the need for
|
|
|
|
|
/// `pezframe_system::Config` to exist, which you should almost never need.
|
|
|
|
|
#[pallet::config]
|
|
|
|
|
#[pezpallet::config]
|
|
|
|
|
pub trait Config: pezframe_system::Config {
|
|
|
|
|
/// Type representing the weight of this pallet
|
|
|
|
|
/// Type representing the weight of this pezpallet
|
|
|
|
|
type WeightInfo: WeightInfo;
|
|
|
|
|
|
|
|
|
|
/// This is a normal Rust type, nothing specific to FRAME here.
|
|
|
|
@@ -87,21 +87,21 @@ pub mod pallet {
|
|
|
|
|
/// And this
|
|
|
|
|
const FOO: u32;
|
|
|
|
|
|
|
|
|
|
/// This is a FRAME-specific item. It will be placed in the metadata of the pallet, and
|
|
|
|
|
/// This is a FRAME-specific item. It will be placed in the metadata of the pezpallet, and
|
|
|
|
|
/// therefore can be queried by offchain applications.
|
|
|
|
|
#[pallet::constant]
|
|
|
|
|
#[pezpallet::constant]
|
|
|
|
|
type InMetadata: Get<u32>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Allows you to define some extra constants to be added into constant metadata.
|
|
|
|
|
#[pallet::extra_constants]
|
|
|
|
|
impl<T: Config> Pallet<T> {
|
|
|
|
|
#[pezpallet::extra_constants]
|
|
|
|
|
impl<T: Config> Pezpallet<T> {
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
fn SomeValue() -> u32 {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::constant_name(OtherValue)]
|
|
|
|
|
#[pezpallet::constant_name(OtherValue)]
|
|
|
|
|
fn arbitrary_name() -> u32 {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
@@ -109,15 +109,15 @@ pub mod pallet {
|
|
|
|
|
|
|
|
|
|
const STORAGE_VERSION: pezframe_support::traits::StorageVersion = StorageVersion::new(1);
|
|
|
|
|
|
|
|
|
|
/// The pallet struct. There's nothing special to FRAME about this; it can implement functions
|
|
|
|
|
/// The pezpallet struct. There's nothing special to FRAME about this; it can implement functions
|
|
|
|
|
/// in an impl blocks, traits and so on.
|
|
|
|
|
#[pallet::pallet]
|
|
|
|
|
#[pallet::without_storage_info]
|
|
|
|
|
#[pallet::storage_version(STORAGE_VERSION)]
|
|
|
|
|
pub struct Pallet<T>(_);
|
|
|
|
|
#[pezpallet::pezpallet]
|
|
|
|
|
#[pezpallet::without_storage_info]
|
|
|
|
|
#[pezpallet::storage_version(STORAGE_VERSION)]
|
|
|
|
|
pub struct Pezpallet<T>(_);
|
|
|
|
|
|
|
|
|
|
/// Allows you to define some origin for the pallet.
|
|
|
|
|
#[pallet::origin]
|
|
|
|
|
/// Allows you to define some origin for the pezpallet.
|
|
|
|
|
#[pezpallet::origin]
|
|
|
|
|
pub type Origin<T> = pezframe_system::RawOrigin<<T as pezframe_system::Config>::AccountId>;
|
|
|
|
|
|
|
|
|
|
// first, we showcase all the possible storage types, with most of their details.
|
|
|
|
@@ -127,17 +127,17 @@ pub mod pallet {
|
|
|
|
|
///
|
|
|
|
|
/// The value is stored a single trie node, and therefore can be retrieved with a single
|
|
|
|
|
/// database access.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pallet::unbounded] // optional
|
|
|
|
|
#[pallet::storage_prefix = "OtherFoo"] // optional
|
|
|
|
|
#[pezpallet::storage]
|
|
|
|
|
#[pezpallet::unbounded] // optional
|
|
|
|
|
#[pezpallet::storage_prefix = "OtherFoo"] // optional
|
|
|
|
|
pub type Foo<T> = StorageValue<Value = u32>;
|
|
|
|
|
|
|
|
|
|
#[pallet::type_value]
|
|
|
|
|
#[pezpallet::type_value]
|
|
|
|
|
pub fn DefaultForFoo() -> u32 {
|
|
|
|
|
1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pezpallet::storage]
|
|
|
|
|
pub type FooWithDefault<T> =
|
|
|
|
|
StorageValue<Value = u32, QueryKind = ValueQuery, OnEmpty = DefaultForFoo>;
|
|
|
|
|
|
|
|
|
@@ -145,12 +145,12 @@ pub mod pallet {
|
|
|
|
|
///
|
|
|
|
|
/// Keys and values can be iterated, albeit each value is stored under a unique trie key,
|
|
|
|
|
/// meaning that an iteration consists of many database accesses.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pezpallet::storage]
|
|
|
|
|
pub type Bar<T> = StorageMap<Hasher = Blake2_128Concat, Key = u32, Value = u32>;
|
|
|
|
|
|
|
|
|
|
/// Conceptually same as `StorageMap<>` where the key is a tuple of `(u32, u32)`. On top, it
|
|
|
|
|
/// provides some functions to iterate or remove items based on only the first key.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pezpallet::storage]
|
|
|
|
|
pub type Qux<T> = StorageDoubleMap<
|
|
|
|
|
Hasher1 = Blake2_128Concat,
|
|
|
|
|
Key1 = u32,
|
|
|
|
@@ -160,7 +160,7 @@ pub mod pallet {
|
|
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
/// Same as `StorageDoubleMap`, but with arbitrary number of keys.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pezpallet::storage]
|
|
|
|
|
pub type Quux<T> = StorageNMap<
|
|
|
|
|
Key = (
|
|
|
|
|
NMapKey<Blake2_128Concat, u8>,
|
|
|
|
@@ -172,19 +172,19 @@ pub mod pallet {
|
|
|
|
|
|
|
|
|
|
/// In all of these examples, we chose a syntax where the storage item is defined using the
|
|
|
|
|
/// explicit generic syntax (`X = Y`). Alternatively:
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pezpallet::storage]
|
|
|
|
|
pub type AlternativeSyntax<T> = StorageMap<_, Blake2_128Concat, u32, u32>;
|
|
|
|
|
|
|
|
|
|
/// Lastly, all storage items, as you saw, had to be generic over `T`. If they want to use an
|
|
|
|
|
/// item from `Config`, `<T: Config>` should be used.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pezpallet::storage]
|
|
|
|
|
pub type AlternativeSyntax2<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, u32>;
|
|
|
|
|
|
|
|
|
|
/// The genesis config type. This allows the pallet to define how it should initialized upon
|
|
|
|
|
/// The genesis config type. This allows the pezpallet to define how it should initialized upon
|
|
|
|
|
/// genesis.
|
|
|
|
|
///
|
|
|
|
|
/// It can be generic over `T` or not, depending on whether it is or not.
|
|
|
|
|
#[pallet::genesis_config]
|
|
|
|
|
#[pezpallet::genesis_config]
|
|
|
|
|
pub struct GenesisConfig<T: Config> {
|
|
|
|
|
pub foo: u32,
|
|
|
|
|
pub bar: BlockNumberFor<T>,
|
|
|
|
@@ -197,7 +197,7 @@ pub mod pallet {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Allows you to define how `genesis_configuration is built.
|
|
|
|
|
#[pallet::genesis_build]
|
|
|
|
|
#[pezpallet::genesis_build]
|
|
|
|
|
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
|
|
|
|
|
fn build(&self) {
|
|
|
|
|
Foo::<T>::put(self.foo);
|
|
|
|
@@ -206,18 +206,18 @@ pub mod pallet {
|
|
|
|
|
|
|
|
|
|
/// The call declaration. This states the entry points that we handle. The
|
|
|
|
|
/// macro takes care of the marshalling of arguments and dispatch.
|
|
|
|
|
#[pallet::call]
|
|
|
|
|
impl<T: Config> Pallet<T> {
|
|
|
|
|
#[pallet::call_index(0)]
|
|
|
|
|
#[pallet::weight(T::WeightInfo::set_foo_benchmark())]
|
|
|
|
|
#[pezpallet::call]
|
|
|
|
|
impl<T: Config> Pezpallet<T> {
|
|
|
|
|
#[pezpallet::call_index(0)]
|
|
|
|
|
#[pezpallet::weight(T::WeightInfo::set_foo_benchmark())]
|
|
|
|
|
/// Marks this call as feeless if `new_foo` is zero.
|
|
|
|
|
#[pallet::feeless_if(|_origin: &OriginFor<T>, new_foo: &u32, _other_compact: &u128| -> bool {
|
|
|
|
|
#[pezpallet::feeless_if(|_origin: &OriginFor<T>, new_foo: &u32, _other_compact: &u128| -> bool {
|
|
|
|
|
*new_foo == 0
|
|
|
|
|
})]
|
|
|
|
|
pub fn set_foo(
|
|
|
|
|
_: OriginFor<T>,
|
|
|
|
|
new_foo: u32,
|
|
|
|
|
#[pallet::compact] _other_compact: u128,
|
|
|
|
|
#[pezpallet::compact] _other_compact: u128,
|
|
|
|
|
) -> DispatchResult {
|
|
|
|
|
Foo::<T>::set(Some(new_foo));
|
|
|
|
|
|
|
|
|
@@ -226,8 +226,8 @@ pub mod pallet {
|
|
|
|
|
|
|
|
|
|
/// A call that is specially authorized.
|
|
|
|
|
/// Authorized call can be dispatched by anybody without requiring any signature or fee.
|
|
|
|
|
#[pallet::call_index(1)]
|
|
|
|
|
#[pallet::authorize(|
|
|
|
|
|
#[pezpallet::call_index(1)]
|
|
|
|
|
#[pezpallet::authorize(|
|
|
|
|
|
_source: TransactionSource,
|
|
|
|
|
new_foo: &u32,
|
|
|
|
|
| -> TransactionValidityWithRefund {
|
|
|
|
@@ -244,11 +244,11 @@ pub mod pallet {
|
|
|
|
|
Err(InvalidTransaction::Call.into())
|
|
|
|
|
}
|
|
|
|
|
})]
|
|
|
|
|
#[pallet::weight(T::WeightInfo::set_foo_using_authorize())]
|
|
|
|
|
#[pallet::weight_of_authorize(T::WeightInfo::authorize_set_foo_using_authorize())]
|
|
|
|
|
#[pezpallet::weight(T::WeightInfo::set_foo_using_authorize())]
|
|
|
|
|
#[pezpallet::weight_of_authorize(T::WeightInfo::authorize_set_foo_using_authorize())]
|
|
|
|
|
pub fn set_foo_using_authorize(origin: OriginFor<T>, new_foo: u32) -> DispatchResult {
|
|
|
|
|
// We only dispatch if it comes from the authorized origin. Meaning that the closure
|
|
|
|
|
// passed in `pallet::authorize` has successfully authorized the call.
|
|
|
|
|
// passed in `pezpallet::authorize` has successfully authorized the call.
|
|
|
|
|
ensure_authorized(origin)?;
|
|
|
|
|
Foo::<T>::set(Some(new_foo));
|
|
|
|
|
|
|
|
|
@@ -262,12 +262,12 @@ pub mod pallet {
|
|
|
|
|
/// the variants actually use `<T: Config>`, the macro will generate a hidden `PhantomData`
|
|
|
|
|
/// variant.
|
|
|
|
|
///
|
|
|
|
|
/// The `generate_deposit` macro generates a function on `Pallet` called `deposit_event` which
|
|
|
|
|
/// will properly convert the error type of your pallet into `RuntimeEvent` (recall `type
|
|
|
|
|
/// The `generate_deposit` macro generates a function on `Pezpallet` called `deposit_event` which
|
|
|
|
|
/// will properly convert the error type of your pezpallet into `RuntimeEvent` (recall `type
|
|
|
|
|
/// RuntimeEvent: From<Event<Self>>`, so it can be converted) and deposit it via
|
|
|
|
|
/// `pezframe_system::Pallet::deposit_event`.
|
|
|
|
|
#[pallet::event]
|
|
|
|
|
#[pallet::generate_deposit(pub fn deposit_event)]
|
|
|
|
|
/// `pezframe_system::Pezpallet::deposit_event`.
|
|
|
|
|
#[pezpallet::event]
|
|
|
|
|
#[pezpallet::generate_deposit(pub fn deposit_event)]
|
|
|
|
|
pub enum Event<T: Config> {
|
|
|
|
|
/// A simple tuple style variant.
|
|
|
|
|
SomethingHappened(u32),
|
|
|
|
@@ -279,16 +279,16 @@ pub mod pallet {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The error enum. Must always be generic over `<T>`, which is expanded to `<T: Config>`.
|
|
|
|
|
#[pallet::error]
|
|
|
|
|
#[pezpallet::error]
|
|
|
|
|
pub enum Error<T> {
|
|
|
|
|
SomethingWentWrong,
|
|
|
|
|
SomethingBroke,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// All the possible hooks that a pallet can have. See [`pezframe_support::traits::Hooks`] for more
|
|
|
|
|
/// All the possible hooks that a pezpallet can have. See [`pezframe_support::traits::Hooks`] for more
|
|
|
|
|
/// info.
|
|
|
|
|
#[pallet::hooks]
|
|
|
|
|
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
|
|
|
|
#[pezpallet::hooks]
|
|
|
|
|
impl<T: Config> Hooks<BlockNumberFor<T>> for Pezpallet<T> {
|
|
|
|
|
fn integrity_test() {}
|
|
|
|
|
|
|
|
|
|
fn offchain_worker(_n: BlockNumberFor<T>) {
|
|
|
|
@@ -327,17 +327,17 @@ pub mod pallet {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Allows you to define an enum on the pallet which will then instruct `construct_runtime` to
|
|
|
|
|
/// Allows you to define an enum on the pezpallet which will then instruct `construct_runtime` to
|
|
|
|
|
/// amalgamate all similarly-named enums from other pallets into an aggregate enum.
|
|
|
|
|
#[pallet::composite_enum]
|
|
|
|
|
#[pezpallet::composite_enum]
|
|
|
|
|
pub enum HoldReason {
|
|
|
|
|
Staking,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Allows the pallet to provide some inherent. See [`pezframe_support::inherent::ProvideInherent`]
|
|
|
|
|
/// Allows the pezpallet to provide some inherent. See [`pezframe_support::inherent::ProvideInherent`]
|
|
|
|
|
/// for more info.
|
|
|
|
|
#[pallet::inherent]
|
|
|
|
|
impl<T: Config> ProvideInherent for Pallet<T> {
|
|
|
|
|
#[pezpallet::inherent]
|
|
|
|
|
impl<T: Config> ProvideInherent for Pezpallet<T> {
|
|
|
|
|
type Call = Call<T>;
|
|
|
|
|
type Error = MakeFatalError<()>;
|
|
|
|
|
|
|
|
|
|