Create a Storage Structure
Last updated
Last updated
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 storage item that holds a struct and is used in . 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
Make sure you have a working pallet to build your struct for. Use the 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 your MetaData
struct.
This admin
variable must correspond with the variable used in the node/chainspec.rs
file inside fn testnet_genesis
.
Configure GenesisBuild
Use the #[pallet::genesis_build]
attribute to initialize the values of your struct, using admin
to initialize the values of type T::AccountId
:
Use the struct in on_initialize()
Assign an amount to the issuance
field of MetaData
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.
example pallet