In this step, we will initialize the pallet project, where we can start building simple logic for the DEX pallet.
git clone --branch template git@github.com:shawntabrizi/polkadot-sdk-tutorial-dex.git pallet-dex
Cargo.toml
:
lib.rs
: This file serves as the main entry point for the Substrate pallet. It defines the pallet's structure,
configuration, storage, events, errors, and dispatchable functions. By organizing the pallet's code in this manner, it
becomes easier to understand, test, and maintain the pallet's functionality within the Substrate framework. The lib.rs
file in a Substrate pallet heavily relies on the use of macros to organize and simplify the code. Macros are a powerful feature in Rust that allow you to define reusable code templates and generate code based on those templates.mock.rs
used to create a mock runtime environment for testing the functionality of the pallet_dex
pallet. It sets up a minimal runtime configuration that includes the necessary modules and types required for testing the pallet.
By creating a mock runtime, developers can write unit tests for the pallet's functionality, ensuring that it behaves as expected and catching potential issues early in the development process. The new_test_ext
function is typically used in the test cases to initialize the mock runtime's storage before running the tests.tests.rs
contains the unit tests for the pallet_dex
pallet. It imports the necessary modules and types from the mock.rs
file and the pallet itself, and defines test functions to verify the pallet's behavior.In the context of a Substrate pallet, macros provided by the FRAME framework are extensively used to define and structure various aspects of the pallet. Here are the primary macro usages that you'll see often:
#[frame_support::pallet]
macro is used to declare the pallet module, indicating that it represents a FRAME pallet.#[pallet::config]
macro is used to define the pallet's configuration trait, specifying the required types and constants for the pallet.#[pallet::storage]
macro is used to define the pallet's storage items, such as storage values and storage maps.#[pallet::event]
macro is used to define the events that the pallet can emit.#[pallet::generate_deposit]
macro automatically generates the deposit_event function for emitting events, reducing boilerplate code.#[pallet::error]
macro is used to define the errors that the pallet can return.#[pallet::call]
macro is used to define the pallet's dispatchable functions (or Calls).By leveraging these macros, the Substrate pallet's code becomes more organized, readable, and maintainable. The macros abstract away many of the low-level details and provide a high-level, declarative way to define the pallet's components.
Furthermore, the use of macros helps in enforcing a consistent structure across different pallets. This consistency makes it easier for developers to understand and work with multiple pallets within a Substrate runtime.
Now that we've familiarized ourselves with the starter pallet code, let's dive into specifying the generic types necessary for our project.