| title | Pallet Example |
|---|---|
| description | Replace with a short description. |
| pathway | pallets |
A standalone FRAME pallet template for focused pallet development.
This recipe provides a minimal setup for developing and testing FRAME pallets in isolation. Perfect for learning pallet development or creating standalone pallets that will be integrated into existing runtimes.
What you'll build:
- A standalone FRAME pallet with storage, events, and extrinsics
- Unit tests with mocked runtime
- Benchmarking setup (optional)
Who this is for:
- Developers learning FRAME pallet development
- Teams creating reusable pallet libraries
- Anyone wanting to test pallets without full runtime complexity
Before starting, ensure you have:
- Rust 1.91+ - The Polkadot SDK requires a recent Rust toolchain
- Basic knowledge of Rust and blockchain concepts
recipes/pallet-example/
├── Cargo.toml # Workspace configuration
├── rust-toolchain.toml # Rust version specification
└── pallets/
└── template/
├── Cargo.toml # Pallet dependencies
└── src/
├── lib.rs # Main pallet logic
├── mock.rs # Mock runtime for tests
├── tests.rs # Unit tests
├── benchmarking.rs # Benchmarks (optional)
└── weights.rs # Weight calculations
cd recipes/pallet-example
cargo build --releasecargo testcargo test --features runtime-benchmarksThe template pallet demonstrates core FRAME concepts:
Storage: Persistent state stored on-chain
#[pallet::storage]
pub type Something<T> = StorageValue<_, u32>;Events: Notifications of state changes
#[pallet::event]
pub enum Event<T> {
SomethingStored { value: u32, who: T::AccountId },
}Errors: Typed error handling
#[pallet::error]
pub enum Error<T> {
NoneValue,
StorageOverflow,
}Extrinsics: Functions users can call
#[pallet::call]
impl<T: Config> Pallet<T> {
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
// Implementation
}
}The mock runtime (src/mock.rs) provides a lightweight environment for testing:
#[test]
fn it_works_for_default_value() {
new_test_ext().execute_with(|| {
// Test your pallet here
});
}- Modify Storage: Add or change storage items in
lib.rs - Add Events: Define new events for important state changes
- Implement Logic: Write your pallet's business logic in extrinsics
- Write Tests: Cover edge cases in
tests.rs - Add Benchmarks: Measure computational costs in
benchmarking.rs
Once your pallet is ready, integrate it into a full runtime:
- Add your pallet as a dependency in the runtime's
Cargo.toml - Configure the pallet in
runtime/src/lib.rs - Add it to the
construct_runtime!macro - Run runtime tests to verify integration
- Implement your custom storage and logic
- Write comprehensive unit tests
- Add benchmarking for weight calculations
- Consider integrating into a full parachain runtime
Having issues? Check out: