tensor_vipers/
lib.rs

1//! Library for writing safer Solana programs.
2#![deny(missing_docs)]
3#![allow(clippy::all)]
4
5pub mod assert;
6mod error;
7mod keyref;
8pub mod validate;
9
10use anchor_lang::prelude::*;
11pub use error::*;
12pub use keyref::AsKeyRef;
13#[cfg(feature = "spl-associated-token-account")]
14pub use spl_associated_token_account as ata;
15pub use validate::Validate;
16
17declare_id!("VipersTest111111111111111111111111111111111");
18
19/// Validates a derived program address.
20///
21/// # Example
22///
23/// ```
24/// use tensor_vipers::validate_derived_address;
25/// use anchor_lang::solana_program;
26/// let random = solana_program::system_program::ID;
27/// let seeds: &[&[u8]] = &["test".as_ref() as &[u8], &random.to_bytes()];
28/// let expected = static_pubkey::static_pubkey!("HjTCk2QYVrDPH1emJyrKBjtnooGqTvHfxa8ResZg3Kb4");
29/// assert!(validate_derived_address(
30///   &expected, &tensor_vipers::ID, seeds
31/// ));
32/// assert!(!validate_derived_address(
33///   &solana_program::system_program::ID, &tensor_vipers::ID, seeds
34/// ));
35/// ```
36pub fn validate_derived_address(
37    derived_address: &Pubkey,
38    program_id: &Pubkey,
39    seeds: &[&[u8]],
40) -> bool {
41    match Pubkey::create_program_address(seeds, program_id) {
42        Ok(ref key) => derived_address == key,
43        _ => false,
44    }
45}
46
47/// Helper for getting the current timestamp.
48pub fn now_i64() -> Result<i64> {
49    Ok(Clock::get()?.unix_timestamp)
50}
51
52/// Helper for getting the current timestamp as any convertible type.
53pub fn now<T: TryFrom<i64>>() -> Result<T> {
54    now_i64()?
55        .try_into()
56        .map_err(|_| ::anchor_lang::prelude::error!(VipersError::IntegerOverflow))
57}
58
59pub mod prelude {
60    //! The prelude contains all commonly used components of the crate. All programs should include it via `use tensor_vipers::prelude::*;`.
61
62    pub use super::{
63        assert_is_zero_token_account, assert_keys_eq, assert_keys_neq, invariant, now, now_i64,
64        try_or_err, unwrap_bump, unwrap_checked, unwrap_int, unwrap_opt, unwrap_opt_block,
65        unwrap_or_err, AsKeyRef, CmpError, IntoCmpError, Validate, VipersError,
66    };
67}