use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey,
pubkey::Pubkey,
};
const ATA_PROGRAM_ID: Pubkey = pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
pub(crate) fn get_associated_token_address_and_bump_seed(
wallet_address: &Pubkey,
token_mint_address: &Pubkey,
program_id: &Pubkey,
token_program_id: &Pubkey,
) -> (Pubkey, u8) {
get_associated_token_address_and_bump_seed_internal(
wallet_address,
token_mint_address,
program_id,
token_program_id,
)
}
pub fn get_associated_token_address(
wallet_address: &Pubkey,
token_mint_address: &Pubkey,
) -> Pubkey {
get_associated_token_address_with_program_id(
wallet_address,
token_mint_address,
&spl_token::id(),
)
}
pub fn get_associated_token_address_with_program_id(
wallet_address: &Pubkey,
token_mint_address: &Pubkey,
token_program_id: &Pubkey,
) -> Pubkey {
get_associated_token_address_and_bump_seed(
wallet_address,
token_mint_address,
&ATA_PROGRAM_ID,
token_program_id,
)
.0
}
fn get_associated_token_address_and_bump_seed_internal(
wallet_address: &Pubkey,
token_mint_address: &Pubkey,
program_id: &Pubkey,
token_program_id: &Pubkey,
) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[
&wallet_address.to_bytes(),
&token_program_id.to_bytes(),
&token_mint_address.to_bytes(),
],
program_id,
)
}
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub enum AssociatedTokenAccountInstruction {
Create,
CreateIdempotent,
RecoverNested,
}
fn build_associated_token_account_instruction(
funding_address: &Pubkey,
wallet_address: &Pubkey,
token_mint_address: &Pubkey,
token_program_id: &Pubkey,
instruction: AssociatedTokenAccountInstruction,
) -> Instruction {
let associated_account_address = get_associated_token_address_with_program_id(
wallet_address,
token_mint_address,
token_program_id,
);
assert!(matches!(
instruction,
AssociatedTokenAccountInstruction::Create
| AssociatedTokenAccountInstruction::CreateIdempotent
));
Instruction {
program_id: ATA_PROGRAM_ID,
accounts: vec![
AccountMeta::new(*funding_address, true),
AccountMeta::new(associated_account_address, false),
AccountMeta::new_readonly(*wallet_address, false),
AccountMeta::new_readonly(*token_mint_address, false),
AccountMeta::new_readonly(solana_sdk::system_program::id(), false),
AccountMeta::new_readonly(*token_program_id, false),
],
data: borsh::to_vec(&instruction).unwrap(),
}
}
pub fn create_associated_token_account(
funding_address: &Pubkey,
wallet_address: &Pubkey,
token_mint_address: &Pubkey,
token_program_id: &Pubkey,
) -> Instruction {
build_associated_token_account_instruction(
funding_address,
wallet_address,
token_mint_address,
token_program_id,
AssociatedTokenAccountInstruction::Create,
)
}
pub fn create_associated_token_account_idempotent(
funding_address: &Pubkey,
wallet_address: &Pubkey,
token_mint_address: &Pubkey,
token_program_id: &Pubkey,
) -> Instruction {
build_associated_token_account_instruction(
funding_address,
wallet_address,
token_mint_address,
token_program_id,
AssociatedTokenAccountInstruction::CreateIdempotent,
)
}