use solana_program::instruction::Instruction;
use crate::anchor_traits::*;
use crate::solana_compat::{hash, pubkey};
use crate::{solana_program, Pubkey};
#[inline(always)]
pub fn is_devnet() -> bool {
cfg!(feature = "devnet") || std::env::var("SB_ENV").unwrap_or_default() == "devnet"
}
pub const DEFAULT_DEVNET_QUEUE: Pubkey = pubkey!("EYiAmGSdsQTuCw413V5BzaruWuCCSDgTPtBGvLkXHbe7");
pub const DEFAULT_MAINNET_QUEUE: Pubkey = pubkey!("A43DyUGA7s8eXPxqEjJY6EBu1KKbNgfxF8h17VAHn13w");
#[inline(always)]
pub fn default_queue() -> Pubkey {
if is_devnet() {
DEFAULT_DEVNET_QUEUE
} else {
DEFAULT_MAINNET_QUEUE
}
}
pub const SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID: Pubkey =
pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
pub const SPL_TOKEN_PROGRAM_ID: Pubkey = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
pub fn find_associated_token_address(owner: &Pubkey, mint: &Pubkey) -> Pubkey {
let (akey, _bump) = Pubkey::find_program_address(
&[owner.as_ref(), SPL_TOKEN_PROGRAM_ID.as_ref(), mint.as_ref()],
&SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID,
);
akey
}
pub fn get_ixn_discriminator(ixn_name: &str) -> [u8; 8] {
let preimage = format!("global:{}", ixn_name);
let mut sighash = [0u8; 8];
sighash.copy_from_slice(&hash::hash(preimage.as_bytes()).to_bytes()[..8]);
sighash
}
pub fn get_account_discriminator(account_name: &str) -> [u8; 8] {
let id = format!("account:{}", account_name);
hash::hash(id.as_bytes()).to_bytes()[..8]
.try_into()
.unwrap()
}
#[inline(always)]
pub unsafe fn read_u64_at(ptr: *const u64, offset: usize) -> u64 {
core::ptr::read_unaligned(ptr.add(offset))
}
#[inline(always)]
pub unsafe fn read(ptr: *const u64, offset: usize) -> u64 {
*ptr.add(offset)
}
#[inline(always)]
pub fn check_pubkey_eq<L: AsRef<[u8]>, R: AsRef<[u8]>>(lhs: L, rhs: R) -> bool {
let lhs_bytes = lhs.as_ref();
let rhs_bytes = rhs.as_ref();
unsafe {
let lhs_ptr = lhs_bytes.as_ptr() as *const u64;
let rhs_ptr = rhs_bytes.as_ptr() as *const u64;
core::ptr::read_unaligned(lhs_ptr) == core::ptr::read_unaligned(rhs_ptr)
&& core::ptr::read_unaligned(lhs_ptr.add(1))
== core::ptr::read_unaligned(rhs_ptr.add(1))
&& core::ptr::read_unaligned(lhs_ptr.add(2))
== core::ptr::read_unaligned(rhs_ptr.add(2))
&& core::ptr::read_unaligned(lhs_ptr.add(3))
== core::ptr::read_unaligned(rhs_ptr.add(3))
}
}
#[inline(always)]
pub unsafe fn check_p64_eq(lhs_ptr: *const u64, rhs_ptr: *const u64) -> bool {
core::ptr::read_unaligned(lhs_ptr) == core::ptr::read_unaligned(rhs_ptr)
&& core::ptr::read_unaligned(lhs_ptr.add(1)) == core::ptr::read_unaligned(rhs_ptr.add(1))
&& core::ptr::read_unaligned(lhs_ptr.add(2)) == core::ptr::read_unaligned(rhs_ptr.add(2))
&& core::ptr::read_unaligned(lhs_ptr.add(3)) == core::ptr::read_unaligned(rhs_ptr.add(3))
}
pub fn build_ix<A: ToAccountMetas, I: InstructionData + Discriminator + std::fmt::Debug>(
program_id: &Pubkey,
accounts: &A,
params: &I,
) -> Instruction {
Instruction {
program_id: *program_id,
accounts: accounts.to_account_metas(None),
data: params.data(),
}
}