Macro frame_support::decl_storage[][src]

decl_storage!() { /* proc-macro */ }

Declares strongly-typed wrappers around codec-compatible types in storage.

Example

decl_storage! {
	trait Store for Module<T: Trait> as Example {
		Foo get(fn foo) config(): u32=12;
		Bar: map hasher(identity) u32 => u32;
		pub Zed build(|config| vec![(0, 0)]): map hasher(identity) u32 => u32;
	}
}

Declaration is set with the header (pub) trait Store for Module<T: Trait> as Example, with Store a (pub) trait generated associating each storage item to the Module and as Example setting the prefix used for storage items of this module. Example must be unique: another module with the same name and the same inner storage item name will conflict. Example is called the module prefix.

note: For instantiable modules the module prefix is prepended with instance prefix. Instance prefix is “” for default instance and “Instance$n” for instance number $n. Thus, instance 3 of module Example has a module prefix of Instance3Example

Basic storage consists of a name and a type; supported types are:

Supported hashers (ordered from least to best security):

Deprecated hashers, which do not support iteration over keys include:

Basic storage can be extended as such:

#vis #name get(fn #getter) config(#field_name) build(#closure): #type = #default;

Storage items are accessible in multiple ways:

GenesisConfig

An optional GenesisConfig struct for storage initialization can be defined, either when at least one storage field requires default initialization (both get and config or build), or specifically as in:

decl_storage! {
	trait Store for Module<T: Trait> as Example {

		// Your storage items
	}
	add_extra_genesis {
		config(genesis_field): GenesisFieldType;
		config(genesis_field2): GenesisFieldType;
		...
		build(|_: &Self| {
			// Modification of storage
		})
	}
}

This struct can be exposed as ExampleConfig by the construct_runtime! macro like follows:

construct_runtime!(
	pub enum Runtime with ... {
        ...,
        Example: example::{Module, Storage, ..., Config<T>},
        ...,
}
);

Module with Instances

The decl_storage! macro supports building modules with instances with the following syntax (DefaultInstance type is optional):

trait Store for Module<T: Trait<I>, I: Instance=DefaultInstance> as Example {}

Accessing the structure no requires the instance as generic parameter:

Where clause

This macro supports a where clause which will be replicated to all generated types.

trait Store for Module<T: Trait> as Example where T::AccountId: std::fmt::Display {}

Limitations

Instancing and generic GenesisConfig

If your module supports instancing and you see an error like parameter I is never used for your decl_storage!, you are hitting a limitation of the current implementation. You probably try to use an associated type of a non-instantiable trait. To solve this, add the following to your macro call:

add_extra_genesis {
	config(phantom): std::marker::PhantomData<I>,
}
...

This adds a field to your `GenesisConfig` with the name `phantom` that you can initialize with
`Default::default()`.