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