Module Fundamentals

kitchen/modules/hello-substrate

Clone the substrate module template:

git clone https://github.com/substrate-developer-hub/substrate-module-template

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


# #![allow(unused_variables)]
#fn main() {
use support::{decl_module, decl_event, decl_storage, StorageValue, StorageMap};
use system::ensure_signed;
#}

The blockchain's runtime storage is configured in decl_storage.


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

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


# #![allow(unused_variables)]
#fn main() {
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)?;
            LastValue::put(value);
            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.


# #![allow(unused_variables)]
#fn main() {
decl_event!{
    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