Skip to content

About

Polkadot Cookbook recipe: Custom pallet example with Polkadot SDK

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

title Pallet Example
description Replace with a short description.
pathway pallets

Pallet Example

A standalone FRAME pallet template for focused pallet development.

Overview

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

Prerequisites

Before starting, ensure you have:

  • Rust 1.91+ - The Polkadot SDK requires a recent Rust toolchain
  • Basic knowledge of Rust and blockchain concepts

Project Structure

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

Getting Started

1. Build the Pallet

cd recipes/pallet-example
cargo build --release

2. Run Tests

cargo test

3. Run Benchmarks (Optional)

cargo test --features runtime-benchmarks

Development Workflow

Understanding the Pallet Structure

The 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
    }
}

Testing Your Pallet

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
    });
}

Customizing the Pallet

  1. Modify Storage: Add or change storage items in lib.rs
  2. Add Events: Define new events for important state changes
  3. Implement Logic: Write your pallet's business logic in extrinsics
  4. Write Tests: Cover edge cases in tests.rs
  5. Add Benchmarks: Measure computational costs in benchmarking.rs

Integration into a Runtime

Once your pallet is ready, integrate it into a full runtime:

  1. Add your pallet as a dependency in the runtime's Cargo.toml
  2. Configure the pallet in runtime/src/lib.rs
  3. Add it to the construct_runtime! macro
  4. Run runtime tests to verify integration

Resources

Next Steps

  • Implement your custom storage and logic
  • Write comprehensive unit tests
  • Add benchmarking for weight calculations
  • Consider integrating into a full parachain runtime

Support

Having issues? Check out:

About

Polkadot Cookbook recipe: Custom pallet example with Polkadot SDK

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages