Skip to content

Commit

Permalink
Adds helpers for getting the current time
Browse files Browse the repository at this point in the history
  • Loading branch information
macalinao committed Apr 27, 2022
1 parent 543a75b commit 5289ca8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vipers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ edition = "2021"
readme = "../README.md"
keywords = ["solana", "saber", "anchor"]

[features]
default = []
ata = ["spl-associated-token-account"]

[dependencies]
anchor-lang = ">=0.22, <=0.24"
anchor-spl = ">=0.22, <=0.24"
num-traits = "^0.2.14"
spl-associated-token-account = { version = "^1.0.3", features = [
"no-entrypoint"
], optional = true }
Expand Down
18 changes: 15 additions & 3 deletions vipers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,24 @@ pub fn validate_derived_address(
}
}

/// Helper for getting the current timestamp.
pub fn now_i64() -> Result<i64> {
Ok(Clock::get()?.unix_timestamp)
}

/// Helper for getting the current timestamp as any convertible type.
pub fn now<T: TryFrom<i64>>() -> Result<T> {
now_i64()?
.try_into()
.map_err(|_| ::anchor_lang::prelude::error!(VipersError::IntegerOverflow))
}

pub mod prelude {
//! The prelude contains all commonly used components of the crate. All programs should include it via `use vipers::prelude::*;`.

pub use super::{
assert_is_zero_token_account, assert_keys_eq, assert_keys_neq, invariant, try_or_err,
unwrap_bump, unwrap_checked, unwrap_int, unwrap_opt, unwrap_opt_block, unwrap_or_err,
AsKeyRef, CmpError, IntoCmpError, Validate, VipersError,
assert_is_zero_token_account, assert_keys_eq, assert_keys_neq, invariant, now, now_i64,
try_or_err, unwrap_bump, unwrap_checked, unwrap_int, unwrap_opt, unwrap_opt_block,
unwrap_or_err, AsKeyRef, CmpError, IntoCmpError, Validate, VipersError,
};
}

0 comments on commit 5289ca8

Please sign in to comment.