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
16/// `system_transfer(user → user_wsol_ata, lamports)` followed by
17/// `sync_native(user_wsol_ata)`. Used before any wSOL-quoted buy.
18pub fn wrap_sol_instructions(user: &Pubkey, lamports: u64) -> [Instruction; 2] {
19    let user_wsol_ata = pda::associated_token(
20        user,
21        &constants::SPL_TOKEN_PROGRAM_ID,
22        &constants::NATIVE_MINT,
23    )
24    .0;
25    [
26        system_instruction::transfer(user, &user_wsol_ata, lamports),
27        sync_native(&constants::SPL_TOKEN_PROGRAM_ID, &user_wsol_ata)
28            .expect("sync_native: token program id is constant"),
29    ]
30}
31
32/// `close_account(user_wsol_ata)` with destination and owner both `user`.
33/// Unwraps the wSOL balance back to lamports on the user's main account.
34pub fn unwrap_sol_instruction(user: &Pubkey) -> Instruction {
35    let user_wsol_ata = pda::associated_token(
36        user,
37        &constants::SPL_TOKEN_PROGRAM_ID,
38        &constants::NATIVE_MINT,
39    )
40    .0;
41    close_account(
42        &constants::SPL_TOKEN_PROGRAM_ID,
43        &user_wsol_ata,
44        user,
45        user,
46        &[],
47    )
48    .expect("close_account: token program id is constant")
49}