create_token_cpmm

This is the pseudo-code for creating a token with initialize and lock liquidity using @solana /web3.js in pure JavaScript.

import {
    ComputeBudgetProgram,
    Keypair,
    PublicKey,
    SystemProgram,
    SYSVAR_INSTRUCTIONS_PUBKEY,
    SYSVAR_RENT_PUBKEY,
    TransactionInstruction
} from '@solana/web3.js';
import { Buffer } from 'buffer';
import * as borsh from '@coral-xyz/borsh';
import {
    CPMM_LOOKUP_TABLE_ADDRESS,
    FEE_RECEIVER,
    METADATA_PROGRAM_ID,
    PROGRAM_ID,
    RAYDIUM_CPMM_CONFIG,
    RAYDIUM_CPMM_FEE_ACCOUNT,
    RAYDIUM_CPMM_LOCK_PROGRAM,
    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 { sendLookupAndConfirmTx } from '../utils/conn';
import { generateRandomSeed } from '../utils/tools';
import {
    getAmmAuthAddress,
    getClaimAuthAddress,
    getConfigAddress,
    getFeedAddress,
    getLockAuthAddress,
    getLockLiquidityAddress,
    getLpMintAddress,
    getMetadataAddress,
    getObservationAddress,
    getPoolAddress,
    getPoolVaultAddress
} from '../utils/pda';

const discriminator = Buffer.from([161, 36, 211, 14, 9, 88, 19, 92]);

const createTokenLayout = borsh.struct([
    borsh.str('seed'),
    borsh.str('name'),
    borsh.str('symbol'),
    borsh.str('uri'),
    borsh.u16('transfer_fee_basis_points'),
    borsh.u8('fee_ratio'),
    borsh.u32('claim_interval'),
    borsh.u8('claim_type'),
    borsh.u64('quote_amount'),
]);

export const createTokenCPMM = async (connection, wallet, mintKeypair, claimMint, otherParams) => {
    // encode params
    const seed = generateRandomSeed(24);
    const updatedParams = {
        seed: seed,
        ...otherParams
    };

    const buffer = Buffer.alloc(1000);
    createTokenLayout.encode(updatedParams, buffer);
    const serializedData = buffer.slice(0, createTokenLayout.getSpan(buffer));
    // @ts-ignore
    const instructionData = Buffer.concat([discriminator, serializedData]);

    const wsolAccount = await PublicKey.createWithSeed(wallet.publicKey, seed, TOKEN_PROGRAM_ID);

    // Instruction: create_token_cpmm instruction
    const claimAuthority = getClaimAuthAddress(mintKeypair.publicKey);
    const mintAta = getAssociatedTokenAddressSync(mintKeypair.publicKey, wallet.publicKey, false, TOKEN_2022_PROGRAM_ID);
    const feed = getFeedAddress(claimMint);

    const pool = getPoolAddress(mintKeypair.publicKey, NATIVE_MINT);
    const lpMint = getLpMintAddress(pool);
    const lpTokenAccount = getAssociatedTokenAddressSync(lpMint, wallet.publicKey);

    const lockNftKeypair = Keypair.generate();
    const lockNftAccount = getAssociatedTokenAddressSync(lockNftKeypair.publicKey, wallet.publicKey);
    let lockAuthority = getLockAuthAddress();
    const locklpVault = getAssociatedTokenAddressSync(lpMint, lockAuthority, true);

    const accounts = [
        { pubkey: wallet.publicKey, isSigner: true, isWritable: true },
        { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true },
        { pubkey: lockNftKeypair.publicKey, isSigner: true, isWritable: true },
        { pubkey: getConfigAddress(), isSigner: false, isWritable: false },
        { pubkey: claimAuthority, isSigner: false, isWritable: true },
        { pubkey: mintAta, isSigner: false, isWritable: true },
        { pubkey: feed, isSigner: false, isWritable: false },
        { pubkey: getMetadataAddress(mintKeypair.publicKey), isSigner: false, isWritable: true },
        { pubkey: FEE_RECEIVER, 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: NATIVE_MINT, isSigner: false, isWritable: false },
        { pubkey: lpMint, isSigner: false, isWritable: true },
        { pubkey: wsolAccount, isSigner: false, isWritable: true },
        { pubkey: lpTokenAccount, isSigner: false, isWritable: true },
        { pubkey: getPoolVaultAddress(pool, mintKeypair.publicKey), isSigner: false, isWritable: true },
        { pubkey: getPoolVaultAddress(pool, NATIVE_MINT), isSigner: false, isWritable: true },
        { pubkey: RAYDIUM_CPMM_FEE_ACCOUNT, isSigner: false, isWritable: true },
        { pubkey: getObservationAddress(pool), isSigner: false, isWritable: true },
        { pubkey: lockAuthority, isSigner: false, isWritable: false },
        { pubkey: lockNftAccount, isSigner: false, isWritable: true },
        { pubkey: getLockLiquidityAddress(lockNftKeypair.publicKey), isSigner: false, isWritable: true },
        { pubkey: locklpVault, isSigner: false, isWritable: true },
        { pubkey: getMetadataAddress(lockNftKeypair.publicKey), 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 },
        { pubkey: METADATA_PROGRAM_ID, isSigner: false, isWritable: false },
        { pubkey: RAYDIUM_CPMM_LOCK_PROGRAM, isSigner: false, isWritable: false },
        { pubkey: SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false },
    ];

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

    const limitInstruction = ComputeBudgetProgram.setComputeUnitLimit({
        units: 600_000
    });

    const priceInstruction = ComputeBudgetProgram.setComputeUnitPrice({
        microLamports: 10000
    });

    const instructions = [limitInstruction, priceInstruction, createTokenCpmmIx]

    // send and confirm transaction
    return sendLookupAndConfirmTx(connection, wallet, instructions, [mintKeypair, lockNftKeypair], [CPMM_LOOKUP_TABLE_ADDRESS]);
};

Last updated