withdraw_to_wsol_cpmm

This is the pseudo-code for swapping transfer fees into WSOL using @solana /web3.js in pure JavaScript.

This instruction is public, and you can invoke it on any token that you did not create yourself.

import { PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY, TransactionInstruction } from '@solana/web3.js';
import { Buffer } from 'buffer';
import { PROGRAM_ID, RAYDIUM_CPMM_CONFIG, RAYDIUM_CPMM_PROGRAM } from '../utils/constants';
import {
    ASSOCIATED_TOKEN_PROGRAM_ID,
    getAssociatedTokenAddressSync,
    NATIVE_MINT,
    TOKEN_2022_PROGRAM_ID,
    TOKEN_PROGRAM_ID
} from '@solana/spl-token';
import { sendAndConfirmTx } from '../utils/conn';
import { getAmmAuthAddress, getClaimAuthAddress, getConfigAddress, getObservationAddress, getPoolAddress, getPoolVaultAddress } from '../utils/pda';

const discriminator = Buffer.from([197, 233, 157, 247, 171, 145, 76, 248]);

export const withdrawToWsolCPMM = async (connection, wallet, mintAddress) => {
    const instructionData = discriminator;
    const mint = new PublicKey(mintAddress);

    // accounts
    const claimAuthority = getClaimAuthAddress(mint);
    const mintVault = getAssociatedTokenAddressSync(mint, claimAuthority, true, TOKEN_2022_PROGRAM_ID);
    const wsolVault = getAssociatedTokenAddressSync(NATIVE_MINT, claimAuthority, true, TOKEN_PROGRAM_ID);
    const pool = getPoolAddress(mint, NATIVE_MINT);

    const accounts = [
        { pubkey: wallet.publicKey, isSigner: true, isWritable: true },
        { pubkey: getConfigAddress(), isSigner: false, isWritable: false },
        { pubkey: claimAuthority, isSigner: false, isWritable: true },
        { pubkey: mint, isSigner: false, isWritable: true },
        { pubkey: NATIVE_MINT, isSigner: false, isWritable: false },
        { pubkey: mintVault, isSigner: false, isWritable: true },
        { pubkey: wsolVault, isSigner: false, isWritable: true },
        { pubkey: RAYDIUM_CPMM_CONFIG, isSigner: false, isWritable: false },
        { pubkey: getAmmAuthAddress(), isSigner: false, isWritable: false },
        { pubkey: pool, isSigner: false, isWritable: true },
        { pubkey: getPoolVaultAddress(pool, mint), isSigner: false, isWritable: true },
        { pubkey: getPoolVaultAddress(pool, NATIVE_MINT), isSigner: false, isWritable: true },
        { pubkey: getObservationAddress(pool), isSigner: false, isWritable: true },
        { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
        { pubkey: TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false },
        { pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
        { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
        { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
        { pubkey: RAYDIUM_CPMM_PROGRAM, isSigner: false, isWritable: false },
    ];

    // withdraw_to_wsol_cpmm instruction
    const instruction = new TransactionInstruction({
        keys: accounts,
        programId: PROGRAM_ID,
        data: instructionData,
    });

    // send and confirm transaction
    return sendAndConfirmTx(connection, wallet, [instruction], []);
};

Last updated