Create and use a struct in storage
Learn how to write a struct to manage your storage values.
#
GoalCreate a struct in StorageValue
and use it on on_initialize
.
#
Use casesKeep track of different accounts and balances for testing a pallet.
#
OverviewCreating a struct of similarly grouped storage items is a neat way to keep track of them.
They can be easier to reference than keeping individual StorageValue
items separate this way.
In addition, they can be used to ease testing and genesis configuration.
This guide steps through the procedure of creating a struct in storage which:
- 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
#
Steps#
1. Create a your structCall it MetaData
and declare its different types:
#
2. Declare the struct as a storage itemUse StorageValue
to declare the struct as a new single item in storage:
GenesisConfig
and GenesisBuild
#
3. Configure GenesisConfig
#
Use #[pallet::genesis_config]
to declare the admin account that you'll use in your to
initialize values from your MetaData
struct:
GenesisBuild
#
Use #[pallet::genesis_build]
to initialize the values of your struct, using admin
to initialize the values
of type T::AccountId
:
on_initialize()
#
4. Use the struct in Assign an amount to the issuance
field of MetaData
to be initialized when the chain is launched: