-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
173 lines (147 loc) · 6.2 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#![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, decl_event, decl_error, dispatch, traits::Get, traits::OnInitialize, ensure, StorageMap
};
use frame_system::ensure_signed;
use sp_std::vec::Vec;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
/// 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 TemplateModule {
// Learn more about declaring storage items:
// https://substrate.dev/docs/en/knowledgebase/runtime/storage#declaring-storage-items
Something get(fn something): Option<u32>;
// leku
// Storage item for our proofs (1st ups)
// Maps a proof to the user who made the claim and when they made it.
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]
SomethingStored(u32, AccountId),
// leku
// Event emitted when a proof has been claimed [who, claim]
ClaimCreated(AccountId, Vec<u8>),
// Event emitted when a claim is revoked by the owner [who, claim]
ClaimRevoked(AccountId, Vec<u8>),
}
);
// Errors inform users that something went wrong.
decl_error! {
pub enum Error for Module<T: Trait> {
/// Error names should be descriptive.
NoneValue,
/// Errors should have helpful documentation associated with them.
StorageOverflow,
// leku
// Proof has already been claimed
ProofAlreadyClaimed,
// Proof does not exist, cannot be revoked
NoSuchProof,
// Proof claimed by another account, caller cant revoke it
NotProofOwner,
}
}
// 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.
// leku - this is where most of the magic will happen
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;
/// An example dispatchable that takes a singles value as a parameter, writes the value to
/// storage and emits an event. This function must be dispatched by a signed extrinsic.
#[weight = 10_000 + T::DbWeight::get().writes(1)]
pub fn do_something(origin, something: u32) -> dispatch::DispatchResult {
// Check that the extrinsic was signed and get the signer.
// This function will return an error if the extrinsic is not signed.
// https://substrate.dev/docs/en/knowledgebase/runtime/origin
let who = ensure_signed(origin)?;
// Update storage.
Something::put(something);
// Emit an event.
Self::deposit_event(RawEvent::SomethingStored(something, who));
// Return a successful DispatchResult
Ok(())
}
/// An example dispatchable that may throw a custom error.
#[weight = 10_000 + T::DbWeight::get().reads_writes(1,1)]
pub fn cause_error(origin) -> dispatch::DispatchResult {
let _who = ensure_signed(origin)?;
// Read a value from storage.
match Something::get() {
// Return an error if the value has not been set.
None => Err(Error::<T>::NoneValue)?,
Some(old) => {
// Increment the value read from storage; will error in the event of overflow.
let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
// Update the value in storage with the incremented result.
Something::put(new);
Ok(())
},
}
}
// leku - 1stup
// run a job every 24 hours
fn on_initialize(_n: u64) {
// assumes block time is 6 seconds / polkadot
if (_n % 14400 == 0) {
// create_claim(origin)
}
}
// Allow a user to to claim ownership (1st up) of an unclaimed proof
#[weight = 10_000]
fn create_claim(origin, proof: Vec<u8>) {
// check that the extrinsic was signed and get the signer
// this function will return an error if the extrinsic is not signed.
let sender = ensure_signed(origin)?;
// Verify that the specified proof (1st up) has not already been claimed
ensure!(!Proofs::<T>::contains_key(&proof), Error::<T>::ProofAlreadyClaimed);
// Get the block number from the FRAME system module
let current_block = <frame_system::Module<T>>::block_number();
// Store proof w/ sender and block number
Proofs::<T>::insert(&proof, (&sender, current_block));
}
// allow the owner to revoke their claim
// we might remove this - leku
#[weight = 10_000]
fn revoke_claim(origin, proof: Vec <u8>) {
// check that the extrinsic was signed and get the signer
// will return error if extrinsic is not signed.
let sender = ensure_signed(origin)?;
// verify the specified proof has been claimed
ensure!(Proofs::<T>::contains_key(&proof), Error::<T>::NoSuchProof);
// get the owner of the claim
let (owner, _) = Proofs::<T>::get(&proof);
// verify the sender of the current call is the claim owner
ensure!(sender == owner, Error::<T>::NotProofOwner);
// Remove claim from storage
Proofs::<T>::remove(&proof);
// Emit event that the claim was erased
Self::deposit_event(RawEvent::ClaimRevoked(sender, proof));
}
}
}