Module Fundamentals


Clone the substrate module template:

git clone

At the top of the src/ file, import the following from srml-support:

use support::{decl_module, decl_event, decl_storage, StorageValue, StorageMap};
use system::ensure_signed;

The blockchain's runtime storage is configured in decl_storage.

decl_storage! {
    trait Store for Module<T: Trait> as HelloWorld {
        pub LastValue get(fn last_value): u64;
        pub UserValue get(fn user_value): map T::AccountId => u64;

Defined in decl_module, the runtime methods specify acceptable interaction with runtime storage.

decl_module! {
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        fn deposit_event() = default;

        pub fn set_value(origin, value: u64) {
            let sender = ensure_signed(origin)?;
            UserValue::<T>::insert(&sender, value);
            Self::deposit_event(RawEvent::ValueSet(sender, value));

Events are declared in decl_event. The emission of events is used to determine successful execution of the logic in the body of runtime methods.

    pub enum Event<T> where
        AccountId = <T as system::Trait>::AccountId,
        ValueSet(AccountId, u64),

It is also possible to declare an error type for runtime modules with decl_error