> ## 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.

# AMM v4 程式碼示例

> TypeScript 範例：兌換與提供流動性至現有 AMM v4 池。新池建立不在本範圍內 — 請改用 CPMM。

<Info>
  **本頁內容由 AI 自動翻譯，所有內容以英文版本為準。**

  [查看英文版 →](/products/amm-v4/code-demos)
</Info>

<Info>
  **版本標記。** 所有示例針對 `@raydium-io/raydium-sdk-v2@0.2.42-alpha` 在 Solana mainnet-beta 上驗證，驗證時間 2026-04。程式 ID：`675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8`（見 [`reference/program-addresses`](/zh-Hant/reference/program-addresses)）。
</Info>

<Info>
  **本處未展示新池建立。** Raydium UI 不再提供 AMM v4 池建立 — 新交易對預設使用 [CPMM](/zh-Hant/products/cpmm/code-demos)。AMM v4 程式本身仍接受鏈上 `Initialize2`；只是不是建議的路徑。下面的示例涵蓋每個整合者仍需的實時池操作：兌換、存入、提取。
</Info>

## 設定

```ts theme={null}
import { Connection, Keypair, clusterApiUrl } from "@solana/web3.js";
import { Raydium, TxVersion } from "@raydium-io/raydium-sdk-v2";
import fs from "node:fs";

const connection = new Connection(process.env.RPC_URL ?? clusterApiUrl("mainnet-beta"));
const owner = Keypair.fromSecretKey(
  new Uint8Array(JSON.parse(fs.readFileSync(process.env.KEYPAIR!, "utf8"))),
);
const raydium = await Raydium.load({ owner, connection, cluster: "mainnet" });
```

## 按 ID 獲取池

```ts theme={null}
import { PublicKey } from "@solana/web3.js";

const poolId = new PublicKey("<AMM_V4_POOL_ID>");

// 取得 SDK 規範化的池物件。對於 AMM v4，包含指令建構器需要的 OpenBook 帳戶。
const data = await raydium.liquidity.getPoolInfoFromRpc({ poolId });
const { poolInfo, poolKeys, poolRpcData } = data;

console.log("交易對:", poolInfo.mintA.symbol, "/", poolInfo.mintB.symbol);
console.log("版本:", poolInfo.version);       // AMM v4 為 4
console.log("市場:", poolKeys.marketId.toBase58());
```

`poolKeys` 是指令建構器使用的結構。它以程式預期的順序承載每個 AMM v4 與 OpenBook 帳戶。

## 兌換（基礎入）

```ts theme={null}
import BN from "bn.js";

const amountIn = new BN(1_000_000);            // 1 USDC（6 位小數報價）
const inputMint = new PublicKey(poolInfo.mintB.address);  // USDC
const slippage  = 0.005;

const computed = raydium.liquidity.computeAmountOut({
  poolInfo,
  amountIn,
  mintIn: inputMint,
  mintOut: new PublicKey(poolInfo.mintA.address),
  slippage,
});

const { execute } = await raydium.liquidity.swap({
  poolInfo,
  poolKeys,
  amountIn,
  amountOut: computed.minAmountOut,
  fixedSide: "in",
  inputMint,
  txVersion: TxVersion.V0,
});

const { txId } = await execute({ sendAndConfirm: true });
console.log("兌換交易:", txId);
```

SDK 自動新增每個 OpenBook 帳戶。不要嘗試手動替換它們 — 程式驗證每個位置。

## 兌換（基礎出）

```ts theme={null}
const amountOut = new BN(1_000_000_000);       // 1 SOL（9 位小數基礎）
const slippage  = 0.005;

const computed = raydium.liquidity.computeAmountIn({
  poolInfo,
  amountOut,
  mintOut: new PublicKey(poolInfo.mintA.address),
  mintIn: new PublicKey(poolInfo.mintB.address),
  slippage,
});

const { execute } = await raydium.liquidity.swap({
  poolInfo,
  poolKeys,
  amountIn: computed.maxAmountIn,
  amountOut,
  fixedSide: "out",
  inputMint: new PublicKey(poolInfo.mintB.address),
  txVersion: TxVersion.V0,
});

await execute({ sendAndConfirm: true });
```

## 新增流動性

```ts theme={null}
const amountA = new BN(100_000_000);           // 0.1 SOL

const { anotherAmount, maxAnotherAmount } = raydium.liquidity.computePairAmount({
  poolInfo,
  amount: amountA,
  baseIn: true,
  slippage: 0.01,
});

const { execute } = await raydium.liquidity.addLiquidity({
  poolInfo,
  poolKeys,
  amountInA: amountA,
  amountInB: maxAnotherAmount,
  fixedSide: "a",
  txVersion: TxVersion.V0,
});

await execute({ sendAndConfirm: true });
```

`fixedSide: "a"` 告訴 SDK 你提供了確切的 `amountInA`，`amountInB` 應最多為 `maxAnotherAmount`。池的帳面流動性在按比例數學前結算，故存款比例與最新準備金相符。

## 移除流動性

```ts theme={null}
const lpAmount = new BN(50_000);               // 要燒毀的 LP

const { execute } = await raydium.liquidity.removeLiquidity({
  poolInfo,
  poolKeys,
  lpAmount,
  baseAmountMin: new BN(0),
  quoteAmountMin: new BN(0),
  txVersion: TxVersion.V0,
});

await execute({ sendAndConfirm: true });
```

滑點最小值保護池狀態在你預報價與落地時間之間的變化。

## 計算單位 / 優先費調整

AMM v4 兌換因為每個指令驗證完整 OpenBook 狀態而計算成本大。典型兌換根據需多少開放訂單在結算時使用 180k–250k CU。始終傳遞計算單位限制：

```ts theme={null}
import { ComputeBudgetProgram } from "@solana/web3.js";

const { execute, innerTransactions } = await raydium.liquidity.swap({
  /* ...參數... */
  computeBudgetConfig: {
    units: 400_000,
    microLamports: 50_000,       // 優先費
  },
});
```

若你省略 `computeBudgetConfig`，SDK 仍可能使用自己的預設值；檢查 `innerTransactions` 確認。見 [`integration-guides/priority-fee-tuning`](/zh-Hant/integration-guides/priority-fee-tuning)。

## 直接 Rust CPI

若你必須從自己的 Anchor 程式 CPI 進入 AMM v4，你需要逐字建模 `SwapBaseIn` 的帳戶列表。最小草圖：

```rust theme={null}
use anchor_lang::prelude::*;
use anchor_lang::solana_program::program::invoke_signed;
use anchor_lang::solana_program::instruction::Instruction;

const AMM_V4_PROGRAM_ID: Pubkey = pubkey!("675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8");

#[derive(Accounts)]
pub struct ProxyAmmV4Swap<'info> {
    /// CHECK:
    pub token_program: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub amm:          UncheckedAccount<'info>,
    /// CHECK:
    pub amm_authority: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub amm_open_orders: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub amm_target_orders: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub pool_coin_token_account: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub pool_pc_token_account: UncheckedAccount<'info>,
    /// CHECK:
    pub market_program: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub market: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub market_bids: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub market_asks: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub market_event_queue: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub market_coin_vault: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub market_pc_vault: UncheckedAccount<'info>,
    /// CHECK:
    pub market_vault_signer: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub user_source: UncheckedAccount<'info>,
    #[account(mut)] /// CHECK:
    pub user_dest: UncheckedAccount<'info>,
    pub user_owner: Signer<'info>,
}

pub fn proxy_swap(
    ctx: Context<ProxyAmmV4Swap>,
    amount_in: u64,
    minimum_amount_out: u64,
) -> Result<()> {
    // SwapBaseIn 的指令辨別器在 AMM v4 上為 9。
    let mut data = vec![9u8];
    data.extend_from_slice(&amount_in.to_le_bytes());
    data.extend_from_slice(&minimum_amount_out.to_le_bytes());

    let ix = Instruction {
        program_id: AMM_V4_PROGRAM_ID,
        accounts: vec![
            AccountMeta::new_readonly(ctx.accounts.token_program.key(), false),
            AccountMeta::new(ctx.accounts.amm.key(), false),
            AccountMeta::new_readonly(ctx.accounts.amm_authority.key(), false),
            AccountMeta::new(ctx.accounts.amm_open_orders.key(), false),
            AccountMeta::new(ctx.accounts.amm_target_orders.key(), false),
            AccountMeta::new(ctx.accounts.pool_coin_token_account.key(), false),
            AccountMeta::new(ctx.accounts.pool_pc_token_account.key(), false),
            AccountMeta::new_readonly(ctx.accounts.market_program.key(), false),
            AccountMeta::new(ctx.accounts.market.key(), false),
            AccountMeta::new(ctx.accounts.market_bids.key(), false),
            AccountMeta::new(ctx.accounts.market_asks.key(), false),
            AccountMeta::new(ctx.accounts.market_event_queue.key(), false),
            AccountMeta::new(ctx.accounts.market_coin_vault.key(), false),
            AccountMeta::new(ctx.accounts.market_pc_vault.key(), false),
            AccountMeta::new_readonly(ctx.accounts.market_vault_signer.key(), false),
            AccountMeta::new(ctx.accounts.user_source.key(), false),
            AccountMeta::new(ctx.accounts.user_dest.key(), false),
            AccountMeta::new_readonly(ctx.accounts.user_owner.key(), true),
        ],
        data,
    };
    invoke_signed(&ix, &ctx.accounts.to_account_infos(), &[])?;
    Ok(())
}
```

AMM v4 不提供 CPI 的 Anchor crate。上面的草圖使用手動構造的 `Instruction`。

## 陷阱

* **缺少 OpenBook 帳戶。** 每次兌換、存入及提取都需要全部 8 個 OpenBook 端帳戶；SDK 處理此事，手建指令通常不會。
* **讀取原始金庫餘額。** 不反映帳面託管金額或應計損益。使用 SDK 的報價或 `api-v3.raydium.io/pools/info/ids`。
* **OpenBook 事件隊列滿。** 當池的市場事件隊列需要啟動時，池可能以 `SerumOrderError` 還原兌換。啟動是無許可的（市場 OpenBook 帳戶上的 `MonitorStep`）。
* **Token-2022 鑄幣。** 不支援。AMM v4 池無法針對 Token-2022 鑄幣建立；任何 Token-2022 交易對應在 CPMM 或 CLMM 上。

## 接下來去哪裡

* [`products/amm-v4/instructions`](/zh-Hant/products/amm-v4/instructions) — 這些示例背後的指令級表面。
* [`user-flows/migrate-amm-v4-to-cpmm`](/zh-Hant/user-flows/migrate-amm-v4-to-cpmm) — 若你是考慮遷移的 LP。
* [`integration-guides/priority-fee-tuning`](/zh-Hant/integration-guides/priority-fee-tuning) — 重型 AMM v4 兌換的優先費調整。

資源：

* [Raydium SDK v2](https://github.com/raydium-io/raydium-sdk-V2)
* [Raydium AMM 程式](https://github.com/raydium-io/raydium-amm)
