Skip to content

Instantly share code, notes, and snippets.

@shawntabrizi
Created June 21, 2019 10:19
Show Gist options
  • Save shawntabrizi/58135af3be4531c9d5ec65ce24677e7e to your computer and use it in GitHub Desktop.
Save shawntabrizi/58135af3be4531c9d5ec65ce24677e7e to your computer and use it in GitHub Desktop.
Simple adder for Substrate
use support::{decl_module, decl_storage, decl_event, StorageValue, dispatch::Result, ensure, StorageMap};
use system::ensure_signed;
pub trait Trait: system::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
decl_storage! {
trait Store for Module<T: Trait> as calculator {
FinalResult: u64;
}
}
decl_module! {
pub struct Module <T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default; //This is default deposit_event definition and it is used only if you are using events in your module i.e. decl_events
fn add(origin, a: u64, b: u64) -> Result {
// We use this to make sure that this is a signed message
// and that a user will be charged a transaction fee.
let sender = ensure_signed(origin)?;
let result = a.checked_add(b).ok_or("Numbers are too large to store in a u64")?;
<FinalResult<T>>::put(result);
Self::deposit_event(RawEvent::Addition(sender, a, b, result));
Ok(())
}
}
}
decl_event! (
pub enum Event<T> where <T as system::Trait>::AccountId {
Addition(AccountId, u64, u64, u64),
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment