Attribute Macro frame_support::storage_alias
source · #[storage_alias]
Expand description
Generate a new type alias for storage::types::StorageValue
,
storage::types::StorageMap
, storage::types::StorageDoubleMap
and storage::types::StorageNMap
.
Useful for creating a storage-like struct for test and migrations.
use frame_support::codec;
use frame_support::Twox64Concat;
// generate a storage value with type u32.
#[storage_alias]
type StorageName = StorageValue<Prefix, u32>;
// generate a double map from `(u32, u32)` (with hashers `Twox64Concat` for each key)
// to `Vec<u8>`
#[storage_alias]
type OtherStorageName = StorageDoubleMap<
OtherPrefix,
Twox64Concat,
u32,
Twox64Concat,
u32,
Vec<u8>,
>;
// optionally specify the query type
use frame_support::pallet_prelude::{ValueQuery, OptionQuery};
#[storage_alias]
type ValueName = StorageValue<Prefix, u32, OptionQuery>;
#[storage_alias]
type SomeStorageName = StorageMap<
Prefix,
Twox64Concat,
u32,
Vec<u8>,
ValueQuery,
>;
// generate a map from `Config::AccountId` (with hasher `Twox64Concat`) to `Vec<u8>`
trait Config { type AccountId: codec::FullCodec; }
#[storage_alias]
type GenericStorage<T> = StorageMap<Prefix, Twox64Concat, <T as Config>::AccountId, Vec<u8>>;
// It also supports NMap
use frame_support::storage::types::Key as NMapKey;
#[storage_alias]
type SomeNMap = StorageNMap<Prefix, (NMapKey<Twox64Concat, u32>, NMapKey<Twox64Concat, u64>), Vec<u8>>;
// Using pallet name as prefix.
//
// When the first generic argument is taking generic arguments it is expected to be a pallet.
// The prefix will then be the pallet name as configured in the runtime through
// `construct_runtime!`.
#[storage_alias]
type SomeValue<T: Config> = StorageValue<Pallet<T>, u64>;
// Pallet with instance
#[storage_alias]
type SomeValue2<T: Config, I: 'static> = StorageValue<Pallet<T, I>, u64>;