Skip to main content

pump_rust_client/
token.rs

1//! SPL Token / Token-2022 instruction helpers used by the SDK.
2//!
3//! [`wrap_sol_instructions`] and [`unwrap_sol_instruction`] cover the
4//! wSOL roundtrip every wSOL-quoted buy needs.
5//! [`create_associated_token_account_idempotent`] is re-exported from
6//! `spl-associated-token-account` so all token-instruction imports live
7//! in one place.
8
9use anchor_spl::token::spl_token::instruction::{close_account, sync_native};
10use solana_program::{instruction::Instruction, pubkey::Pubkey, system_instruction};
11
12pub use anchor_spl::associated_token::spl_associated_token_account::instruction::create_associated_token_account_idempotent;
13
14use crate::{constants, pda};
15
16fn user_wsol_ata(user: &Pubkey) -> Pubkey {
17    pda::associated_token(
18        user,
19        &constants::SPL_TOKEN_PROGRAM_ID,
20        &constants::NATIVE_MINT,
21    )
22    .0
23}
24
25/// `system_transfer(user → user_wsol_ata, lamports)` followed by
26/// `sync_native(user_wsol_ata)`. Used before any wSOL-quoted buy.
27pub fn wrap_sol_instructions(user: &Pubkey, lamports: u64) -> [Instruction; 2] {
28    let ata = user_wsol_ata(user);
29    [
30        system_instruction::transfer(user, &ata, lamports),
31        sync_native(&constants::SPL_TOKEN_PROGRAM_ID, &ata)
32            .expect("sync_native: token program id is constant"),
33    ]
34}
35
36/// `close_account(user_wsol_ata)` with destination and owner both `user`.
37/// Unwraps the wSOL balance back to lamports on the user's main account.
38pub fn unwrap_sol_instruction(user: &Pubkey) -> Instruction {
39    let ata = user_wsol_ata(user);
40    close_account(&constants::SPL_TOKEN_PROGRAM_ID, &ata, user, user, &[])
41        .expect("close_account: token program id is constant")
42}