Attribute Macro frame_support::pallet_macros::storage

source ·
#[storage]
Expand description

The #[pallet::storage] attribute lets you define some abstract storage inside of runtime storage and also set its metadata. This attribute can be used multiple times.

Item should be defined as:

#[pallet::storage]
#[pallet::getter(fn $getter_name)] // optional
$vis type $StorageName<$some_generic> $optional_where_clause
	= $StorageType<$generic_name = $some_generics, $other_name = $some_other, ...>;

or with unnamed generic:

#[pallet::storage]
#[pallet::getter(fn $getter_name)] // optional
$vis type $StorageName<$some_generic> $optional_where_clause
	= $StorageType<_, $some_generics, ...>;

I.e. it must be a type alias, with generics: T or T: Config. The aliased type must be one of StorageValue, StorageMap or StorageDoubleMap. The generic arguments of the storage type can be given in two manners: named and unnamed. For named generic arguments, the name for each argument should match the name defined for it on the storage struct:

  • StorageValue expects Value and optionally QueryKind and OnEmpty,
  • StorageMap expects Hasher, Key, Value and optionally QueryKind and OnEmpty,
  • CountedStorageMap expects Hasher, Key, Value and optionally QueryKind and OnEmpty,
  • StorageDoubleMap expects Hasher1, Key1, Hasher2, Key2, Value and optionally QueryKind and OnEmpty.

For unnamed generic arguments: Their first generic must be _ as it is replaced by the macro and other generic must declared as a normal generic type declaration.

The Prefix generic written by the macro is generated using PalletInfo::name::<Pallet<..>>() and the name of the storage type. E.g. if runtime names the pallet “MyExample” then the storage type Foo<T> = ... should use the prefix: Twox128(b"MyExample") ++ Twox128(b"Foo").

For the CountedStorageMap variant, the Prefix also implements CountedStorageMapInstance. It also associates a CounterPrefix, which is implemented the same as above, but the storage prefix is prepend with "CounterFor". E.g. if runtime names the pallet “MyExample” then the storage type Foo<T> = CountedStorageaMap<...> will store its counter at the prefix: Twox128(b"MyExample") ++ Twox128(b"CounterForFoo").

E.g:

#[pallet::storage]
pub(super) type MyStorage<T> = StorageMap<Hasher = Blake2_128Concat, Key = u32, Value = u32>;

In this case the final prefix used by the map is Twox128(b"MyExample") ++ Twox128(b"OtherName").