safe_associated_token_account/
lib.rs

1//! Convention for associating token accounts with a user wallet
2#![deny(missing_docs)]
3#![forbid(unsafe_code)]
4
5mod entrypoint;
6pub mod error;
7pub mod instruction;
8pub mod processor;
9pub mod tools;
10
11// Export current SDK types for downstream users building with a different SDK version
12pub use solana_program;
13use solana_program::{
14    instruction::{AccountMeta, Instruction},
15    pubkey::Pubkey,
16    sysvar,
17};
18
19solana_program::declare_id!("AToD9iqHSc2fhEP9Jp7UYA6mRjHQ4CTWyzCsw8X3tH7K");
20
21pub(crate) fn get_associated_token_address_and_bump_seed(
22    wallet_address: &Pubkey,
23    token_mint_address: &Pubkey,
24    program_id: &Pubkey,
25    token_program_id: &Pubkey,
26) -> (Pubkey, u8) {
27    get_associated_token_address_and_bump_seed_internal(
28        wallet_address,
29        token_mint_address,
30        program_id,
31        token_program_id,
32    )
33}
34
35/// Derives the associated token account address for the given wallet address and token mint
36pub fn get_associated_token_address(
37    wallet_address: &Pubkey,
38    token_mint_address: &Pubkey,
39) -> Pubkey {
40    get_associated_token_address_with_program_id(
41        wallet_address,
42        token_mint_address,
43        &safe_token::id(),
44    )
45}
46
47/// Derives the associated token account address for the given wallet address, token mint and token program id
48pub fn get_associated_token_address_with_program_id(
49    wallet_address: &Pubkey,
50    token_mint_address: &Pubkey,
51    token_program_id: &Pubkey,
52) -> Pubkey {
53    get_associated_token_address_and_bump_seed(
54        wallet_address,
55        token_mint_address,
56        &id(),
57        token_program_id,
58    )
59    .0
60}
61
62fn get_associated_token_address_and_bump_seed_internal(
63    wallet_address: &Pubkey,
64    token_mint_address: &Pubkey,
65    program_id: &Pubkey,
66    token_program_id: &Pubkey,
67) -> (Pubkey, u8) {
68    Pubkey::find_program_address(
69        &[
70            &wallet_address.to_bytes(),
71            &token_program_id.to_bytes(),
72            &token_mint_address.to_bytes(),
73        ],
74        program_id,
75    )
76}
77
78/// Create an associated token account for the given wallet address and token mint
79///
80/// Accounts expected by this instruction:
81///
82///   0. `[writeable,signer]` Funding account (must be a system account)
83///   1. `[writeable]` Associated token account address to be created
84///   2. `[]` Wallet address for the new associated token account
85///   3. `[]` The token mint for the new associated token account
86///   4. `[]` System program
87///   5. `[]` SPL Token program
88///
89#[deprecated(
90    since = "1.0.5",
91    note = "please use `instruction::create_associated_token_account` instead"
92)]
93pub fn create_associated_token_account(
94    funding_address: &Pubkey,
95    wallet_address: &Pubkey,
96    token_mint_address: &Pubkey,
97) -> Instruction {
98    let associated_account_address =
99        get_associated_token_address(wallet_address, token_mint_address);
100
101    Instruction {
102        program_id: id(),
103        accounts: vec![
104            AccountMeta::new(*funding_address, true),
105            AccountMeta::new(associated_account_address, false),
106            AccountMeta::new_readonly(*wallet_address, false),
107            AccountMeta::new_readonly(*token_mint_address, false),
108            AccountMeta::new_readonly(solana_program::system_program::id(), false),
109            AccountMeta::new_readonly(safe_token::id(), false),
110            AccountMeta::new_readonly(sysvar::rent::id(), false),
111        ],
112        data: vec![],
113    }
114}