Skip to main content

gpl_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 instruction;
7pub mod processor;
8pub mod tools;
9
10// Export current SDK types for downstream users building with a different SDK version
11pub use gemachain_program;
12use gemachain_program::{
13    instruction::{AccountMeta, Instruction},
14    program_pack::Pack,
15    pubkey::Pubkey,
16    sysvar,
17};
18
19gemachain_program::declare_id!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
20
21pub(crate) fn get_associated_token_address_and_bump_seed(
22    wallet_address: &Pubkey,
23    gpl_token_mint_address: &Pubkey,
24    program_id: &Pubkey,
25) -> (Pubkey, u8) {
26    get_associated_token_address_and_bump_seed_internal(
27        wallet_address,
28        gpl_token_mint_address,
29        program_id,
30        &gpl_token::id(),
31    )
32}
33
34/// Derives the associated token account address for the given wallet address and token mint
35pub fn get_associated_token_address(
36    wallet_address: &Pubkey,
37    gpl_token_mint_address: &Pubkey,
38) -> Pubkey {
39    get_associated_token_address_and_bump_seed(wallet_address, gpl_token_mint_address, &id()).0
40}
41
42fn get_associated_token_address_and_bump_seed_internal(
43    wallet_address: &Pubkey,
44    gpl_token_mint_address: &Pubkey,
45    program_id: &Pubkey,
46    token_program_id: &Pubkey,
47) -> (Pubkey, u8) {
48    Pubkey::find_program_address(
49        &[
50            &wallet_address.to_bytes(),
51            &token_program_id.to_bytes(),
52            &gpl_token_mint_address.to_bytes(),
53        ],
54        program_id,
55    )
56}
57
58/// Create an associated token account for the given wallet address and token mint
59///
60/// Accounts expected by this instruction:
61///
62///   0. `[writeable,signer]` Funding account (must be a system account)
63///   1. `[writeable]` Associated token account address to be created
64///   2. `[]` Wallet address for the new associated token account
65///   3. `[]` The token mint for the new associated token account
66///   4. `[]` System program
67///   5. `[]` SPL Token program
68///   6. `[]` Rent sysvar
69///
70// TODO: Uncomment after 1.0.4 is released
71// #[deprecated(
72//     since = "1.0.4",
73//     note = "please use `instruction::create_associated_token_account` instead"
74// )]
75pub fn create_associated_token_account(
76    funding_address: &Pubkey,
77    wallet_address: &Pubkey,
78    gpl_token_mint_address: &Pubkey,
79) -> Instruction {
80    let mut instruction = instruction::create_associated_token_account(
81        funding_address,
82        wallet_address,
83        gpl_token_mint_address,
84    );
85
86    // TODO: Remove after ATA 1.0.4 and Token  >3.2.0 are released (Token::InitializeAccount3 is required if rent account is not provided)
87    instruction
88        .accounts
89        .push(AccountMeta::new_readonly(sysvar::rent::id(), false));
90
91    instruction
92}