This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathlib.rs
More file actions
135 lines (110 loc) · 3.7 KB
/
Copy pathlib.rs
File metadata and controls
135 lines (110 loc) · 3.7 KB
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
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use frame_support::{dispatch::DispatchResult, traits::Task};
// Re-export pallet items so that they can be accessed from the crate namespace.
pub use pallet::*;
use sp_runtime::DispatchError;
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
pub enum ExampleTask {
Increment,
Decrement,
}
pub trait PalletTask<T: Config>: Task {
type Config: frame_system::Config;
fn is_valid(&self, config: &Self::Config) -> bool;
fn run(&self, config: &Self::Config) -> Result<(), DispatchError>;
}
impl Task for ExampleTask {
type Enumeration = std::vec::IntoIter<ExampleTask>;
const TASK_INDEX: usize = 0;
fn enumerate() -> Self::Enumeration {
vec![ExampleTask::Increment, ExampleTask::Decrement].into_iter()
}
fn is_valid(&self) -> bool {
unimplemented!()
}
fn run(&self) -> Result<(), DispatchError> {
unimplemented!()
}
}
impl<T: Config> PalletTask<T> for ExampleTask {
type Config = T;
fn is_valid(&self, _config: &Self::Config) -> bool {
let value = Value::<T>::get().unwrap();
match self {
ExampleTask::Increment => value < 255,
ExampleTask::Decrement => value > 0,
}
}
fn run(&self, _config: &Self::Config) -> Result<(), DispatchError> {
match self {
ExampleTask::Increment => {
// Increment the value and emit an event
let new_val = Value::<T>::get().unwrap().checked_add(1).ok_or("Value overflow")?;
Value::<T>::put(new_val);
Pallet::<T>::deposit_event(Event::Incremented { new_val });
},
ExampleTask::Decrement => {
// Decrement the value and emit an event
let new_val = Value::<T>::get().unwrap().checked_sub(1).ok_or("Value underflow")?;
Value::<T>::put(new_val);
Pallet::<T>::deposit_event(Event::Decremented { new_val });
},
}
Ok(())
}
}
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::storage]
#[pallet::getter(fn value)]
pub type Value<T: Config> = StorageValue<_, u8>;
#[pallet::call]
impl<T: Config> Pallet<T> {
pub fn increment(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
// Increment the value and emit an event
let new_val = Value::<T>::get().unwrap().checked_add(1).ok_or("Value overflow")?;
Value::<T>::put(new_val);
Self::deposit_event(Event::Incremented { new_val });
Ok(())
}
pub fn decrement(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
// Decrement the value and emit an event
let new_val = Value::<T>::get().unwrap().checked_sub(1).ok_or("Value underflow")?;
Value::<T>::put(new_val);
Self::deposit_event(Event::Decremented { new_val });
Ok(())
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Incremented { new_val: u8 },
Decremented { new_val: u8 },
}
}