Skip to content
Gav Wood edited this page Dec 12, 2018 · 3 revisions

How do I store a values using a struct?

Ensure the struct implements everything needed for Parameter. This basically means putting #[derive(Encode, Decode, Clone, Eq, PartialEq)] #[cfg_attr(feature = "std", derive(Debug)] before it. If you also intend for your struct to be used in a field that can be configured in a JSON chain spec file, then the second part should be need #[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] (and you'll need to use serde_derive;).

See staking/lib.rs and specifically ValidatorPrefs for an example of it working.

How do I work with addresses in a struct (or an appropriate reference that deals with the public address) other than origin (which already has T::Origin)

Ensure they are stored as T::AccountId. In a struct, you'll want to keep it generic and parameterise the type with T::AccountId (where T is your module's configuration trait (which implies system::Trait).

See staking/lib.rs and specifically ValidatorPrefs for an example of it working in a similar way with with T::Balance instead of T::AccountId.

Anything special I need to know about dealing with a hashed value in a struct?

Not really; just ensure it's a generic argument in your struct which is specialised from your module as T::Hash.

How do I store put strings in storage/accept them as parameters to functions?

In general you should not be using, modifying, working with human-readable strings in the runtime, since human-readability and strict consensus rarely go together. Working with hashes of those strings should generally be fine. However, if you're really insistent on placing human-readable data into the (very expensive Merklised) runtime state, then the way to do it is for convert to some particular encoding (e.g. UTF-8) and provide that byte array to the runtime (as a Vec<u8>) to store.

You can’t perform that action at this time.