phoenix_sdk_core/ata_utils.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
// Copy-pasted from https://github.com/solana-labs/solana-program-library/blob/master/associated-token-account/program/src/lib.rs
// Solana dependency graph is too complicated. I don't want to talk about it
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,
)
}
/// Derives the associated token account address for the given wallet address
/// and token mint
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(),
)
}
/// Derives the associated token account address for the given wallet address,
/// token mint and token program 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,
)
}
/// Instructions supported by the AssociatedTokenAccount program
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub enum AssociatedTokenAccountInstruction {
/// Creates an associated token account for the given wallet address and
/// token mint Returns an error if the account exists.
///
/// 0. `[writeable,signer]` Funding account (must be a system account)
/// 1. `[writeable]` Associated token account address to be created
/// 2. `[]` Wallet address for the new associated token account
/// 3. `[]` The token mint for the new associated token account
/// 4. `[]` System program
/// 5. `[]` SPL Token program
Create,
/// Creates an associated token account for the given wallet address and
/// token mint, if it doesn't already exist. Returns an error if the
/// account exists, but with a different owner.
///
/// 0. `[writeable,signer]` Funding account (must be a system account)
/// 1. `[writeable]` Associated token account address to be created
/// 2. `[]` Wallet address for the new associated token account
/// 3. `[]` The token mint for the new associated token account
/// 4. `[]` System program
/// 5. `[]` SPL Token program
CreateIdempotent,
/// Transfers from and closes a nested associated token account: an
/// associated token account owned by an associated token account.
///
/// The tokens are moved from the nested associated token account to the
/// wallet's associated token account, and the nested account lamports are
/// moved to the wallet.
///
/// Note: Nested token accounts are an anti-pattern, and almost always
/// created unintentionally, so this instruction should only be used to
/// recover from errors.
///
/// 0. `[writeable]` Nested associated token account, must be owned by `3`
/// 1. `[]` Token mint for the nested associated token account
/// 2. `[writeable]` Wallet's associated token account
/// 3. `[]` Owner associated token account address, must be owned by `5`
/// 4. `[]` Token mint for the owner associated token account
/// 5. `[writeable, signer]` Wallet address for the owner associated token
/// account
/// 6. `[]` SPL Token program
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,
);
// safety check, assert if not a creation instruction
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(),
}
}
/// Creates Create instruction
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,
)
}
/// Creates CreateIdempotent instruction
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,
)
}