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
/// }
/// }
///
/// /// The function can be executed at most once every 100 blocks.
/// /// Records the last time the message was invoked.
/// #[ink(message)]
/// pub fn execute_me(&mut self) {
/// let now = self.env().block_timestamp();
/// assert!(now - self.last_invocation > 100);
/// self.last_invocation = now;
/// self.last_invocation = self.env().block_timestamp();
/// }
/// }
/// }
......@@ -299,6 +297,10 @@ where
///
/// # 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`]
pub fn block_timestamp(self) -> T::Timestamp {
ink_env::block_timestamp::<T>().expect("couldn't decode block time stamp")
......@@ -515,25 +517,32 @@ where
/// # Example
///
/// ```
/// # use ink_lang as ink;
/// # #[ink::contract]
/// # pub mod my_contract {
/// # #[ink(storage)]
/// # pub struct MyContract { }
/// #
/// # impl MyContract {
/// # #[ink(constructor)]
/// # pub fn new() -> Self {
/// # Self {}
/// # }
/// #
/// #[ink(message)]
/// pub fn block_number(&self) -> BlockNumber {
/// self.env().block_number()
/// use ink_lang as ink;
///
/// #[ink::contract]
/// pub mod my_contract {
/// #[ink(storage)]
/// pub struct MyContract {
/// last_invocation: BlockNumber
/// }
///
/// impl MyContract {
/// #[ink(constructor)]
/// pub fn new() -> Self {
/// Self {
/// last_invocation: 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
......
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