Create a Storage Structure
Creating a struct of similarly grouped storage items is an orderly way to keep track of them. When grouped in this way, it is easier to reference them than it is if individual StorageValue
items are kept separately. This can make testing and genesis configuration easier.
This guide shows you how to create a StorageValue
storage item that holds a struct and is used in on_initialize
. This struct does the following:
Keeps track of an initial amount (
issuance
)Keeps track of the account that receives that amount (
minter
)Keeps track of an account that can burn some amount (
burner
)Is (partially) used in
on_initialize
Before you begin
Make sure you have a working pallet to build your struct for. Use the template pallet if you don't have one you're already working on.
Create a struct
Name the struct
MetaData
and declare its different types:Declare the struct as a storage item
Use
StorageValue
to declare the struct as a new single item in storage:Configure
GenesisConfig
Use the
#[pallet::genesis_config]
attribute to initialize values from yourMetaData
struct.This
admin
variable must correspond with the variable used in thenode/chainspec.rs
file insidefn testnet_genesis
.Configure
GenesisBuild
Use the
#[pallet::genesis_build]
attribute to initialize the values of your struct, usingadmin
to initialize the values of typeT::AccountId
:Use the struct in
on_initialize()
Assign an amount to the
issuance
field ofMetaData
to be initialized when the chain is launched:
The on_initialize
function ensures that the values for the specified items are written to storage when the chain is launched.
Examples
reward-coin
example pallet
Resources
Last updated