> ## Documentation Index
> Fetch the complete documentation index at: https://docs.raydium.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 部署 CPMM 池

> 即插即用的 Node 脚本，创建新的 CPMM 池。通过环境变量配置 mint 和初始流动性金额。

<Info>
  **本页内容由 AI 自动翻译，所有内容以英文版本为准。**

  [查看英文版 →](/quick-start/deploy-cpmm-pool)
</Info>

<Info>
  **这个脚本的作用**：为你指定的两个 mint 创建全新的 CPMM 池，选择 0.25% 费用级别，根据初始金额隐含的价格设定初始流动性，并输出新池的 ID 和交易签名。
</Info>

## 准备工作

确保你已阅读 [快速开始前置条件](/zh/quick-start) 并已安装 `RPC_URL`、`KEYPAIR` 和相关依赖。

你还需要用两个 mint 的初始金额**为钱包充值**，加上足够的 SOL 覆盖一次性池创建费用（主网约 0.15 SOL，参考 [`reference/program-addresses`](/zh/reference/program-addresses) 了解最新费用）。

## 脚本

保存为 `create-cpmm.mjs`：

```ts theme={null}
// create-cpmm.mjs
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
import {
  Raydium,
  TxVersion,
  CREATE_CPMM_POOL_PROGRAM,
  CREATE_CPMM_POOL_FEE_ACC,
} from "@raydium-io/raydium-sdk-v2";
import BN from "bn.js";
import fs from "node:fs";

// ── Config from env ──────────────────────────────────────────────
const RPC_URL  = process.env.RPC_URL  ?? "https://api.mainnet-beta.solana.com";
const KEYPAIR  = process.env.KEYPAIR  ?? `${process.env.HOME}/.config/solana/id.json`;
const MINT_A   = process.env.MINT_A;   // required, base58
const MINT_B   = process.env.MINT_B;   // required, base58
const AMOUNT_A = process.env.AMOUNT_A; // required, integer raw units
const AMOUNT_B = process.env.AMOUNT_B; // required, integer raw units

if (!MINT_A || !MINT_B || !AMOUNT_A || !AMOUNT_B) {
  console.error("Set MINT_A, MINT_B, AMOUNT_A, AMOUNT_B env vars.");
  process.exit(1);
}

// ── Setup ────────────────────────────────────────────────────────
const connection = new Connection(RPC_URL, "confirmed");
const owner = Keypair.fromSecretKey(
  new Uint8Array(JSON.parse(fs.readFileSync(KEYPAIR, "utf8"))),
);
const raydium = await Raydium.load({
  owner,
  connection,
  cluster: "mainnet",
  disableFeatureCheck: true,
  blockhashCommitment: "finalized",
});

// ── Pick the 0.25% fee tier ─────────────────────────────────────
const feeConfigs = await raydium.api.getCpmmConfigs();
const feeConfig  = feeConfigs.find((c) => c.index === 0);
if (!feeConfig) throw new Error("0.25% fee tier not found in CPMM configs.");

// ── Resolve mint metadata (Token-2022-aware) ────────────────────
const mintA = await raydium.token.getTokenInfo(new PublicKey(MINT_A));
const mintB = await raydium.token.getTokenInfo(new PublicKey(MINT_B));

// ── Build and execute ───────────────────────────────────────────
const { execute, extInfo } = await raydium.cpmm.createPool({
  programId:       CREATE_CPMM_POOL_PROGRAM,
  poolFeeAccount:  CREATE_CPMM_POOL_FEE_ACC,
  mintA,
  mintB,
  mintAAmount:     new BN(AMOUNT_A),
  mintBAmount:     new BN(AMOUNT_B),
  startTime:       new BN(0), // open immediately
  feeConfig,
  associatedOnly:  false,
  ownerInfo:       { useSOLBalance: true },
  txVersion:       TxVersion.V0,
  computeBudgetConfig: {
    units: 600_000,
    microLamports: 100_000,
  },
});

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

console.log(`Pool ID:  ${extInfo.address.poolId.toBase58()}`);
console.log(`LP mint:  ${extInfo.address.lpMint.toBase58()}`);
console.log(`Vault A:  ${extInfo.address.vaultA.toBase58()}`);
console.log(`Vault B:  ${extInfo.address.vaultB.toBase58()}`);
console.log(`Tx:       https://solscan.io/tx/${txId}`);
```

## 运行

示例：用 1 SOL 和 160 USDC 作为初始流动性创建 SOL/USDC 池：

```bash theme={null}
export MINT_A="So11111111111111111111111111111111111111112"   # wSOL
export MINT_B="EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"   # USDC
export AMOUNT_A="1000000000"   # 1 SOL (9 decimals)
export AMOUNT_B="160000000"    # 160 USDC (6 decimals)

node create-cpmm.mjs
```

预期输出：

```
Pool ID:  HgC5...3kXb
LP mint:  4ZAS...9rkV
Vault A:  J9Mu...mP2k
Vault B:  AaJq...8wxx
Tx:       https://solscan.io/tx/5dQ...
```

## 刚才发生了什么

1. **`getCpmmConfigs`** 从 `api-v3.raydium.io` 获取实时费用级别列表，选择索引 0（0.25% 级别 — 参考 [`reference/fee-comparison`](/zh/reference/fee-comparison) 了解全部费用级别）。
2. **`getTokenInfo`** 解析每个 mint 的元数据，包括拥有它的代币程序。CPMM 同时接受 SPL Token 和 Token-2022 mint；SDK 会自动路由。
3. **`createPool`** 构建单笔交易，执行以下操作：
   * 将 mint 排序为规范顺序，
   * 派生池 PDA、金库、LP mint 和权限账户，
   * 向 `CREATE_CPMM_POOL_FEE_ACC` 支付一次性 `create_pool_fee`，
   * 创建调用者缺失的 ATA，
   * 用 `AMOUNT_A` 和 `AMOUNT_B` 初始化金库。
4. **初始价格**由初始金额比确定：`price = AMOUNT_B / AMOUNT_A`（经小数调整）。请仔细选择 — 机器人会在池开放交易后数秒内套利任何错误的定价。
5. **`startTime: new BN(0)`** 立即开放交易。要在向公众开放前暂存流动性，请设置未来的 Unix 时间戳。

## 常见错误

* **`pool already exists`** — 这个 mint 对已经存在该费用级别的池。创建前先查询。
* **`insufficient funds`** — 钱包没有足够的 `MINT_A`、`MINT_B` 或 SOL（用于创建池费用 + 租约）。
* **`Token-2022 extension not supported`** — 其中一个 mint 使用了 CPMM 不支持的扩展。参考 [`reference/token-2022-support`](/zh/reference/token-2022-support)。

## 部署后

你可以立即与新池交易 — [CLI 交易](/zh/quick-start/swap-from-cli) 脚本直接接受你的新 `POOL_ID`。聚合器（Jupiter 等）会在数分钟内索引新池。

## 下一步

* [`products/cpmm/overview`](/zh/products/cpmm/overview) — CPMM 是什么，何时选择它。
* [`user-flows/create-cpmm-pool`](/zh/user-flows/create-cpmm-pool) — 同样的流程（带截图），通过 Raydium UI 操作。
* [`user-flows/choosing-a-pool-type`](/zh/user-flows/choosing-a-pool-type) — 你应该用 CLMM 吗？
