switchboard_solana/
utils.rs

1pub use crate::error::SwitchboardError;
2pub use crate::prelude::*;
3use lazy_static::lazy_static;
4use std::str::FromStr;
5
6lazy_static! {
7    pub static ref ATOKEN_PID: Pubkey =
8        Pubkey::from_str("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL").unwrap();
9    pub static ref TOKEN_PID: Pubkey =
10        Pubkey::from_str("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA").unwrap();
11}
12
13pub fn find_associated_token_address(owner: &Pubkey, mint: &Pubkey) -> Pubkey {
14    let (akey, _bump) = Pubkey::find_program_address(
15        &[owner.as_ref(), TOKEN_PID.as_ref(), mint.as_ref()],
16        &ATOKEN_PID,
17    );
18    akey
19}
20
21pub fn get_ixn_discriminator(ixn_name: &str) -> [u8; 8] {
22    let preimage = format!("global:{}", ixn_name);
23    let mut sighash = [0u8; 8];
24    sighash.copy_from_slice(
25        &anchor_lang::solana_program::hash::hash(preimage.as_bytes()).to_bytes()[..8],
26    );
27    sighash
28}
29
30pub fn build_ix<A: ToAccountMetas, I: InstructionData + Discriminator + std::fmt::Debug>(
31    program_id: &Pubkey,
32    accounts: &A,
33    params: &I,
34) -> Instruction {
35    Instruction {
36        program_id: *program_id,
37        accounts: accounts.to_account_metas(None),
38        data: params.data(),
39    }
40}