ink_sandbox/api/
timestamp_api.rs

1use crate::Sandbox;
2
3/// Generic Time type.
4type MomentOf<R> = <R as pallet_timestamp::Config>::Moment;
5
6/// Timestamp API used to interact with the timestamp pallet.
7pub trait TimestampAPI {
8    /// The runtime timestamp config.
9    type T: pallet_timestamp::Config;
10
11    /// Return the timestamp of the current block.
12    fn get_timestamp(&mut self) -> MomentOf<Self::T>;
13
14    /// Set the timestamp of the current block.
15    ///
16    /// # Arguments
17    ///
18    /// * `timestamp` - The new timestamp to be set.
19    fn set_timestamp(&mut self, timestamp: MomentOf<Self::T>);
20}
21
22impl<T> TimestampAPI for T
23where
24    T: Sandbox,
25    T::Runtime: pallet_timestamp::Config,
26{
27    type T = T::Runtime;
28
29    fn get_timestamp(&mut self) -> MomentOf<Self::T> {
30        self.execute_with(pallet_timestamp::Pallet::<T::Runtime>::get)
31    }
32
33    fn set_timestamp(&mut self, timestamp: MomentOf<Self::T>) {
34        self.execute_with(|| {
35            pallet_timestamp::Pallet::<T::Runtime>::set_timestamp(timestamp)
36        })
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use crate::{
43        api::prelude::*,
44        DefaultSandbox,
45    };
46
47    #[test]
48    fn getting_and_setting_timestamp_works() {
49        let mut sandbox = DefaultSandbox::default();
50        for timestamp in 0..10 {
51            assert_ne!(sandbox.get_timestamp(), timestamp);
52            sandbox.set_timestamp(timestamp);
53            assert_eq!(sandbox.get_timestamp(), timestamp);
54
55            sandbox.build_block();
56        }
57    }
58}