Unverified Commit 0c7e9a31 authored by Michael Müller's avatar Michael Müller Committed by GitHub
Browse files

Fix `env::block_timestamp` example (#937)

* Fix `env::block_timestamp()` example

* Add clarifying note for `env::block_timestamp()`

* Add example for `env::block_number()`
parent 882b4dde
Pipeline #158787 passed with stages
in 34 minutes and 29 seconds
...@@ -286,12 +286,10 @@ where ...@@ -286,12 +286,10 @@ where
/// } /// }
/// } /// }
/// ///
/// /// The function can be executed at most once every 100 blocks. /// /// Records the last time the message was invoked.
/// #[ink(message)] /// #[ink(message)]
/// pub fn execute_me(&mut self) { /// pub fn execute_me(&mut self) {
/// let now = self.env().block_timestamp(); /// self.last_invocation = self.env().block_timestamp();
/// assert!(now - self.last_invocation > 100);
/// self.last_invocation = now;
/// } /// }
/// } /// }
/// } /// }
...@@ -299,6 +297,10 @@ where ...@@ -299,6 +297,10 @@ where
/// ///
/// # Note /// # Note
/// ///
/// The Substrate default for the timestamp type is the milliseconds since the
/// Unix epoch. However, this is not guaranteed: the specific timestamp is
/// defined by the chain environment on which this contract runs.
///
/// For more details visit: [`ink_env::block_timestamp`] /// For more details visit: [`ink_env::block_timestamp`]
pub fn block_timestamp(self) -> T::Timestamp { pub fn block_timestamp(self) -> T::Timestamp {
ink_env::block_timestamp::<T>().expect("couldn't decode block time stamp") ink_env::block_timestamp::<T>().expect("couldn't decode block time stamp")
...@@ -515,25 +517,32 @@ where ...@@ -515,25 +517,32 @@ where
/// # Example /// # Example
/// ///
/// ``` /// ```
/// # use ink_lang as ink; /// use ink_lang as ink;
/// # #[ink::contract] ///
/// # pub mod my_contract { /// #[ink::contract]
/// # #[ink(storage)] /// pub mod my_contract {
/// # pub struct MyContract { } /// #[ink(storage)]
/// # /// pub struct MyContract {
/// # impl MyContract { /// last_invocation: BlockNumber
/// # #[ink(constructor)] /// }
/// # pub fn new() -> Self { ///
/// # Self {} /// impl MyContract {
/// # } /// #[ink(constructor)]
/// # /// pub fn new() -> Self {
/// #[ink(message)] /// Self {
/// pub fn block_number(&self) -> BlockNumber { /// last_invocation: Self::env().block_number()
/// self.env().block_number() /// }
/// }
///
/// /// The function can be executed at most once every 100 blocks.
/// #[ink(message)]
/// pub fn execute_me(&mut self) {
/// let now = self.env().block_number();
/// assert!(now - self.last_invocation > 100);
/// self.last_invocation = now;
/// }
/// }
/// } /// }
/// #
/// # }
/// # }
/// ``` /// ```
/// ///
/// # Note /// # Note
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment