Skip to content

Instantly share code, notes, and snippets.

@Swader
Created Mar 2, 2020
Embed
What would you like to do?
use frame_support::{decl_module, decl_storage, decl_event, decl_error, ensure};
use sp_std::vec::Vec;
use system::ensure_signed;
pub trait Trait: system::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
decl_event! {
pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {
// Events
ClaimCreated(AccountId, Vec<u8>),
ClaimRevoked(AccountId, Vec<u8>), // This is a temporary event to make things compile :)
}
}
decl_storage! {
trait Store for Module<T: Trait> as TemplateModule {
// Storage
Proofs: map Vec<u8> => (T::AccountId, T::BlockNumber);
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
// Functions
fn deposit_event() = default;
fn create_claim(origin, proof: Vec<u8>) {
let sender = ensure_signed(origin)?;
ensure!(!Proofs::<T>::exists(&proof), Error::<T>::AlreadyClaimed);
let current_block = <system::Module<T>>::block_number();
Proofs::<T>::insert(&proof, (&sender, current_block));
Self::deposit_event(RawEvent::ClaimCreated(sender, proof));
}
}
}
decl_error! {
pub enum Error for Module<T: Trait> {
/// This proof is already claimed.
AlreadyClaimed,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment