-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.rs
112 lines (101 loc) · 3.37 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
#![cfg_attr(not(feature = "std"), no_std)]
use ink::prelude::vec::Vec;
use ink::env::{DefaultEnvironment, Environment};
use scale::{Decode, Encode};
type Balance = <DefaultEnvironment as Environment>::Balance;
type AccountId = <DefaultEnvironment as Environment>::AccountId;
type BlockNumber = <DefaultEnvironment as Environment>::BlockNumber;
pub struct SchedulerExtension;
impl SchedulerExtension {
pub fn schedule(
origin: Origin,
when: BlockNumber,
maybe_periodic: Option<(BlockNumber, u32)>,
priority: u8,
call_input: ContractCallInput,
) -> Result<(), SchedulerError> {
::ink::env::chain_extension::ChainExtensionMethod::build(0x30001)
.input::<(
Origin,
BlockNumber,
Option<(BlockNumber, u32)>,
u8,
ContractCallInput,
)>()
.output::<Result<(), SchedulerError>, true>()
.handle_error_code::<SchedulerError>()
.call(&(origin, when, maybe_periodic, priority, call_input))
}
pub fn cancel(origin: Origin, when: BlockNumber, index: u32) -> Result<(), SchedulerError> {
::ink::env::chain_extension::ChainExtensionMethod::build(0x30002)
.input::<(Origin, BlockNumber, u32)>()
.output::<Result<(), SchedulerError>, true>()
.handle_error_code::<SchedulerError>()
.call(&(origin, when, index))
}
}
#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, Debug)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum SchedulerError {
/// Failed to schedule a call
FailedToSchedule = 1,
/// Cannot find the scheduled call.
NotFound = 2,
/// Given target block number is in the past.
TargetBlockNumberInPast = 3,
/// Reschedule failed because it does not change scheduled time.
RescheduleNoChange = 4,
/// Attempt to use a non-named function on a named task.
Named = 5,
/// Origin Caller is not supported
OriginCannotBeCaller = 98,
/// Unknown error
RuntimeError = 99,
/// Unknow status code
UnknownStatusCode,
/// Encountered unexpected invalid SCALE encoding
InvalidScaleEncoding,
}
impl ink::env::chain_extension::FromStatusCode for SchedulerError {
fn from_status_code(status_code: u32) -> Result<(), Self> {
match status_code {
0 => Ok(()),
1 => Err(Self::FailedToSchedule),
2 => Err(Self::NotFound),
3 => Err(Self::TargetBlockNumberInPast),
4 => Err(Self::RescheduleNoChange),
5 => Err(Self::Named),
98 => Err(Self::OriginCannotBeCaller),
99 => Err(Self::RuntimeError),
_ => Err(Self::UnknownStatusCode),
}
}
}
impl From<scale::Error> for SchedulerError {
fn from(_: scale::Error) -> Self {
SchedulerError::InvalidScaleEncoding
}
}
#[derive(Clone, Copy, Decode, Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub enum Origin {
Caller,
Address,
}
impl Default for Origin {
fn default() -> Self {
Self::Address
}
}
#[derive(Encode, Decode)]
pub struct ContractCallInput {
pub dest: AccountId,
pub data: Vec<u8>,
pub gas_limit: (u64, u64),
pub storage_deposit_limit: Option<Balance>,
pub value: Balance,
pub max_weight: u64,
}