pallet_time_release/
types.rs1#![cfg_attr(not(feature = "std"), no_std)]
3
4use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode, HasCompact, MaxEncodedLen};
5use sp_runtime::{traits::AtLeast32Bit, DispatchError, RuntimeDebug};
6extern crate alloc;
7use alloc::boxed::Box;
8use core::cmp::{Eq, PartialEq};
9
10use scale_info::TypeInfo;
11
12pub type ScheduleName = [u8; 32];
14
15#[derive(
20 Clone,
21 Encode,
22 Decode,
23 DecodeWithMemTracking,
24 PartialEq,
25 Eq,
26 RuntimeDebug,
27 MaxEncodedLen,
28 TypeInfo,
29)]
30pub struct ReleaseSchedule<BlockNumber, Balance>
31where
32 Balance: MaxEncodedLen + HasCompact,
33{
34 pub start: BlockNumber,
36 pub period: BlockNumber,
38 pub period_count: u32,
40 #[codec(compact)]
42 pub per_period: Balance,
43}
44
45impl<BlockNumber: AtLeast32Bit + Copy, Balance: AtLeast32Bit + MaxEncodedLen + Copy>
46 ReleaseSchedule<BlockNumber, Balance>
47{
48 pub fn end(&self) -> Option<BlockNumber> {
50 self.period.checked_mul(&self.period_count.into())?.checked_add(&self.start)
52 }
53
54 pub fn total_amount(&self) -> Option<Balance> {
56 self.per_period.checked_mul(&self.period_count.into())
57 }
58
59 #[allow(clippy::expect_used)]
64 pub fn frozen_amount(&self, time: BlockNumber) -> Balance {
65 let full = time
69 .saturating_sub(self.start)
70 .checked_div(&self.period)
71 .expect("ensured non-zero period; qed");
72 let unrealized = self.period_count.saturating_sub(full.unique_saturated_into());
73 self.per_period
74 .checked_mul(&unrealized.into())
75 .expect("ensured non-overflow total amount; qed")
76 }
77}
78
79pub trait SchedulerProviderTrait<Origin, BlockNumber, Call> {
81 fn schedule(
93 origin: Origin,
94 id: ScheduleName,
95 when: BlockNumber,
96 call: Box<Call>,
97 ) -> Result<(), DispatchError>;
98
99 fn cancel(origin: Origin, id: ScheduleName) -> Result<(), DispatchError>;
105}