Skip to content

I'm trying to create CPMM pool using the sdk v2 #143

@yasilvalmeida

Description

@yasilvalmeida

Dear I'm trying to create a CPMM pool on devnet from a created token follow this code
https://github.com/raydium-io/raydium-sdk-V2-demo/blob/master/src/cpmm/createCpmmPool.ts

export const createLiquidityPool = async (
walletPublicKey: string,
tokenA: string,
tokenB: string,
amountA: number,
amountB: number,
) => {
try {
const raydium = await getRaydium(walletPublicKey);
const owner = new PublicKey(walletPublicKey);
const connection = raydium.connection;

// Verify token addresses are valid
const verifyMint = (address: string) => {
  try {
    return new PublicKey(address);
  } catch {
    throw new Error(`Invalid token address: ${address}`);
  }
};

// Get token info and verify mints
const tokenInfoA = await raydium.token.getTokenInfo(tokenA);
const tokenInfoB = await raydium.token.getTokenInfo(tokenB);

const mintA = verifyMint(tokenInfoA.address);
const mintB = verifyMint(tokenInfoB.address);

console.log('Verified Token A:', { mint: mintA.toString(), ...tokenInfoA });
console.log('Verified Token B:', { mint: mintB.toString(), ...tokenInfoB });

// Check and create token accounts if needed
try {
  // First try with associated token accounts
  const [tokenAAccounts, tokenBAccounts] = await Promise.all([
    connection.getTokenAccountsByOwner(owner, { mint: mintA }),
    connection.getTokenAccountsByOwner(owner, { mint: mintB }),
  ]);

  console.log('Token A Accounts:', tokenAAccounts.value.length);
  console.log('Token B Accounts:', tokenBAccounts.value.length);

  // Create ATAs if they don't exist
  if (tokenAAccounts.value.length === 0) {
    const { pubKey: pubKeyA } = await raydium.account.checkOrCreateAta({
      mint: mintA,
    });
    console.log('Created token A ATA:', pubKeyA);
  }

  if (tokenBAccounts.value.length === 0) {
    const { pubKey: pubKeyB } = await raydium.account.checkOrCreateAta({
      mint: mintB,
    });
    console.log('Created token B ATA:', pubKeyB);
  }
} catch (error) {
  console.error('Error checking/creating token accounts:', error);
  throw new Error('Failed to setup token accounts');
}

// Get fee configs
const feeConfigs = await raydium.api.getCpmmConfigs();
const programId = raydium.cluster === 'devnet' 
  ? DEVNET_PROGRAM_ID.CREATE_CPMM_POOL_PROGRAM 
  : CREATE_CPMM_POOL_PROGRAM;
const poolFeeAccount = raydium.cluster === 'devnet'
  ? DEVNET_PROGRAM_ID.CREATE_CPMM_POOL_FEE_ACC
  : CREATE_CPMM_POOL_FEE_ACC;

// Update config IDs for devnet
if (raydium.cluster === 'devnet') {
  feeConfigs.forEach((config) => {
    config.id = getCpmmPdaAmmConfigId(programId, config.index).publicKey.toBase58();
  });
}

// Create pool with properly formatted mint objects
const { execute } = await raydium.cpmm.createPool({
  programId,
  poolFeeAccount,
  mintA: tokenInfoA,
  mintB: tokenInfoB,
  mintAAmount: new BN(amountA),
  mintBAmount: new BN(amountB),
  startTime: new BN(Math.floor(Date.now() / 1000)),
  feeConfig: feeConfigs[0],
  associatedOnly: true, // Use associated token accounts
  ownerInfo: {
    useSOLBalance: true,
    feePayer: owner,
  },
  txVersion,
});

const { txId } = await execute({ sendAndConfirm: true });
return txId;

} catch (error) {
console.error('Error creating liquidity pool:', error);
toast.error(Error creating liquidity pool: ${error instanceof Error ? error.message : JSON.stringify(error)});
throw error;
}
};

Getting this Error checking/creating token accounts: SolanaJSONRPCError: failed to get token accounts owned by account 8hEXUjNMD1UoLfUxGrjXJ2Boqb8Phc4eReCoA7qB46i7: Invalid param: Token mint could not be unpacked

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions