Created
March 2, 2020 17:58
-
-
Save Swader/d8d794d93dfdb35328a0d760e7ac67a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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