-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
108 lines (81 loc) · 3.66 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#![cfg_attr(not(feature = "std"), no_std)]
/// Edit this file to define custom logic or remove it if it is not needed.
/// Learn more about FRAME and the core library of Substrate FRAME pallets:
/// https://substrate.dev/docs/en/knowledgebase/runtime/frame
use frame_support::{decl_module, decl_storage, ensure, decl_event, decl_error, dispatch};
use frame_system::ensure_signed;
use sp_std::prelude::*;
/// Configure the pallet by specifying the parameters and types on which it depends.
pub trait Trait: frame_system::Trait {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
}
// The pallet's runtime storage items.
// https://substrate.dev/docs/en/knowledgebase/runtime/storage
decl_storage! {
// A unique name is used to ensure that the pallet's storage items are isolated.
// This name may be updated, but each pallet in the runtime must use a unique name.
// ---------------------------------vvvvvvvvvvvvvv
trait Store for Module<T: Trait> as PoeModule {
// Learn more about declaring storage items:
Proofs: map hasher(blake2_128_concat) Vec<u8> => (T::AccountId, T::BlockNumber);
}
}
// Pallets use events to inform users when important changes are made.
// https://substrate.dev/docs/en/knowledgebase/runtime/events
decl_event!(
pub enum Event<T> where AccountId = <T as frame_system::Trait>::AccountId {
/// Event documentation should end with an array that provides descriptive names for event
/// parameters. [something, who]
ClaimCreated(AccountId, Vec<u8>),
ClaimRemoved(AccountId, Vec<u8>),
ClaimMoved(AccountId, Vec<u8>),
}
);
// Errors inform users that something went wrong.
decl_error! {
pub enum Error for Module<T: Trait> {
ProofAlreadyExist,
ClaimNotExist,
NotClaimOwner
}
}
// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
// Errors must be initialized if they are used by the pallet.
type Error = Error<T>;
// Events must be initialized if they are used by the pallet.
fn deposit_event() = default;
#[weight = 0]
pub fn create_claim(origin, claim: Vec<u8>) -> dispatch::DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(!Proofs::<T>::contains_key(&claim), Error::<T>::ProofAlreadyExist);
Proofs::<T>::insert(&claim, (sender.clone(), frame_system::Module::<T>::block_number()));
Self::deposit_event(RawEvent::ClaimCreated(sender, claim));
Ok(())
}
#[weight = 0]
pub fn revoke_claim(origin, claim: Vec<u8>) -> dispatch::DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(Proofs::<T>::contains_key(&claim), Error::<T>::ClaimNotExist);
let (owner, _block_number) = Proofs::<T>::get(&claim);
ensure!(owner == sender, Error::<T>::NotClaimOwner);
Proofs::<T>::remove(&claim);
Self::deposit_event(RawEvent::ClaimRemoved(sender, claim));
Ok(())
}
#[weight = 0]
pub fn move_claim(origin, claim: Vec<u8>, account: T::AccountId) -> dispatch::DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(Proofs::<T>::contains_key(&claim), Error::<T>::ClaimNotExist);
let (owner, _block_number) = Proofs::<T>::get(&claim);
ensure!(owner == sender, Error::<T>::NotClaimOwner);
Proofs::<T>::insert(&claim, (account.clone(), frame_system::Module::<T>::block_number()));
Self::deposit_event(RawEvent::ClaimRemoved(account, claim));
Ok(())
}
}
}