Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix env::block_timestamp example #937

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 31 additions & 22 deletions crates/lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,21 @@ 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();
/// }
/// }
/// }
/// ```
///
/// # 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")
Expand Down Expand Up @@ -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
Expand Down