-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tutorials > Polkadot SDK > Parachains > Build Custom Pallet > Build t…
…he Pallet (#232) * wip: build-pallet * Page ready * Improvements * Extract code snippets * Description and grammarly * fix: typo * Add suggestions * Update tutorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet.md Co-authored-by: Nicolás Hussein <[email protected]> * Add root origin reference * Apply suggestions from code review Co-authored-by: Erin Shaben <[email protected]> * Apply fmt * Apply suggestions from code review Co-authored-by: Erin Shaben <[email protected]> * Apply suggestions from code review Co-authored-by: Nicolás Hussein <[email protected]> * Apply fixes --------- Co-authored-by: nhussein11 <[email protected]> Co-authored-by: Nicolás Hussein <[email protected]> Co-authored-by: Erin Shaben <[email protected]>
- Loading branch information
1 parent
8df580b
commit 215e49a
Showing
10 changed files
with
548 additions
and
2 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
.snippets/code/tutorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "custom-pallet" | ||
version = "0.1.0" | ||
license.workspace = true | ||
authors.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
edition.workspace = true | ||
|
||
[dependencies] | ||
codec = { features = ["derive"], workspace = true } | ||
scale-info = { features = ["derive"], workspace = true } | ||
frame-support.workspace = true | ||
frame-system.workspace = true | ||
|
||
[features] | ||
default = ["std"] | ||
std = ["codec/std", "frame-support/std", "frame-system/std", "scale-info/std"] |
14 changes: 14 additions & 0 deletions
14
...code/tutorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet/call_structure.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#[pallet::call] | ||
impl<T: Config> Pallet<T> { | ||
#[pallet::call_index(0)] | ||
#[pallet::weight(0)] | ||
pub fn set_counter_value(origin: OriginFor<T>, new_value: u32) -> DispatchResult {} | ||
|
||
#[pallet::call_index(1)] | ||
#[pallet::weight(0)] | ||
pub fn increment(origin: OriginFor<T>, amount_to_increment: u32) -> DispatchResult {} | ||
|
||
#[pallet::call_index(2)] | ||
#[pallet::weight(0)] | ||
pub fn decrement(origin: OriginFor<T>, amount_to_decrement: u32) -> DispatchResult {} | ||
} |
5 changes: 5 additions & 0 deletions
5
...utorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet/compilation-output.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div id="termynal" data-termynal> | ||
<span data-ty="input"><span class="file-path"></span>cargo build --release</span> | ||
<span data-ty>Compiling solochain-template-node</span> | ||
<span data-ty>Finished `release` profile [optimized] target(s) in 27.12s</span> | ||
</div> |
184 changes: 184 additions & 0 deletions
184
.snippets/code/tutorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
pub use pallet::*; | ||
|
||
#[frame_support::pallet(dev_mode)] | ||
pub mod pallet { | ||
use super::*; | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::*; | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
||
// Configuration trait for the pallet | ||
#[pallet::config] | ||
pub trait Config: frame_system::Config { | ||
// Defines the event type for the pallet | ||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; | ||
|
||
// Defines the maximum value the counter can hold | ||
#[pallet::constant] | ||
type CounterMaxValue: Get<u32>; | ||
} | ||
|
||
#[pallet::event] | ||
#[pallet::generate_deposit(pub(super) fn deposit_event)] | ||
pub enum Event<T: Config> { | ||
/// The counter value has been set to a new value by Root. | ||
CounterValueSet { | ||
/// The new value set. | ||
counter_value: u32, | ||
}, | ||
/// A user has successfully incremented the counter. | ||
CounterIncremented { | ||
/// The new value set. | ||
counter_value: u32, | ||
/// The account who incremented the counter. | ||
who: T::AccountId, | ||
/// The amount by which the counter was incremented. | ||
incremented_amount: u32, | ||
}, | ||
/// A user has successfully decremented the counter. | ||
CounterDecremented { | ||
/// The new value set. | ||
counter_value: u32, | ||
/// The account who decremented the counter. | ||
who: T::AccountId, | ||
/// The amount by which the counter was decremented. | ||
decremented_amount: u32, | ||
}, | ||
} | ||
|
||
/// Storage for the current value of the counter. | ||
#[pallet::storage] | ||
pub type CounterValue<T> = StorageValue<_, u32>; | ||
|
||
/// Storage map to track the number of interactions performed by each account. | ||
#[pallet::storage] | ||
pub type UserInteractions<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, u32>; | ||
|
||
#[pallet::error] | ||
pub enum Error<T> { | ||
/// The counter value exceeds the maximum allowed value. | ||
CounterValueExceedsMax, | ||
/// The counter value cannot be decremented below zero. | ||
CounterValueBelowZero, | ||
/// Overflow occurred in the counter. | ||
CounterOverflow, | ||
/// Overflow occurred in user interactions. | ||
UserInteractionOverflow, | ||
} | ||
|
||
#[pallet::call] | ||
impl<T: Config> Pallet<T> { | ||
/// Set the value of the counter. | ||
/// | ||
/// The dispatch origin of this call must be _Root_. | ||
/// | ||
/// - `new_value`: The new value to set for the counter. | ||
/// | ||
/// Emits `CounterValueSet` event when successful. | ||
#[pallet::call_index(0)] | ||
#[pallet::weight(0)] | ||
pub fn set_counter_value(origin: OriginFor<T>, new_value: u32) -> DispatchResult { | ||
ensure_root(origin)?; | ||
|
||
ensure!( | ||
new_value <= T::CounterMaxValue::get(), | ||
Error::<T>::CounterValueExceedsMax | ||
); | ||
|
||
CounterValue::<T>::put(new_value); | ||
|
||
Self::deposit_event(Event::<T>::CounterValueSet { | ||
counter_value: new_value, | ||
}); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Increment the counter by a specified amount. | ||
/// | ||
/// This function can be called by any signed account. | ||
/// | ||
/// - `amount_to_increment`: The amount by which to increment the counter. | ||
/// | ||
/// Emits `CounterIncremented` event when successful. | ||
#[pallet::call_index(1)] | ||
#[pallet::weight(0)] | ||
pub fn increment(origin: OriginFor<T>, amount_to_increment: u32) -> DispatchResult { | ||
let who = ensure_signed(origin)?; | ||
|
||
let current_value = CounterValue::<T>::get().unwrap_or(0); | ||
|
||
let new_value = current_value | ||
.checked_add(amount_to_increment) | ||
.ok_or(Error::<T>::CounterOverflow)?; | ||
|
||
ensure!( | ||
new_value <= T::CounterMaxValue::get(), | ||
Error::<T>::CounterValueExceedsMax | ||
); | ||
|
||
CounterValue::<T>::put(new_value); | ||
|
||
UserInteractions::<T>::try_mutate(&who, |interactions| -> Result<_, Error<T>> { | ||
let new_interactions = interactions | ||
.unwrap_or(0) | ||
.checked_add(1) | ||
.ok_or(Error::<T>::UserInteractionOverflow)?; | ||
*interactions = Some(new_interactions); // Store the new value | ||
|
||
Ok(()) | ||
})?; | ||
|
||
Self::deposit_event(Event::<T>::CounterIncremented { | ||
counter_value: new_value, | ||
who, | ||
incremented_amount: amount_to_increment, | ||
}); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Decrement the counter by a specified amount. | ||
/// | ||
/// This function can be called by any signed account. | ||
/// | ||
/// - `amount_to_decrement`: The amount by which to decrement the counter. | ||
/// | ||
/// Emits `CounterDecremented` event when successful. | ||
#[pallet::call_index(2)] | ||
#[pallet::weight(0)] | ||
pub fn decrement(origin: OriginFor<T>, amount_to_decrement: u32) -> DispatchResult { | ||
let who = ensure_signed(origin)?; | ||
|
||
let current_value = CounterValue::<T>::get().unwrap_or(0); | ||
|
||
let new_value = current_value | ||
.checked_sub(amount_to_decrement) | ||
.ok_or(Error::<T>::CounterValueBelowZero)?; | ||
|
||
CounterValue::<T>::put(new_value); | ||
|
||
UserInteractions::<T>::try_mutate(&who, |interactions| -> Result<_, Error<T>> { | ||
let new_interactions = interactions | ||
.unwrap_or(0) | ||
.checked_add(1) | ||
.ok_or(Error::<T>::UserInteractionOverflow)?; | ||
*interactions = Some(new_interactions); // Store the new value | ||
|
||
Ok(()) | ||
})?; | ||
|
||
Self::deposit_event(Event::<T>::CounterDecremented { | ||
counter_value: new_value, | ||
who, | ||
decremented_amount: amount_to_decrement, | ||
}); | ||
|
||
Ok(()) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ppets/code/tutorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet/scaffold.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
pub use pallet::*; | ||
|
||
#[frame_support::pallet(dev_mode)] | ||
pub mod pallet { | ||
use super::*; | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::*; | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
||
#[pallet::config] | ||
pub trait Config: frame_system::Config {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
title: Build Custom Pallet | ||
nav: | ||
- index.md | ||
- 'Build Pallet': build-pallet.md |
Oops, something went wrong.