> ## 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 Code-Demos

> TypeScript-Beispiele für Swaps gegen und Liquiditätsbereitstellung für einen bestehenden AMM v4 Pool. Neue Pool-Erstellung ist außerhalb des Umfangs – verwenden Sie stattdessen CPMM.

<Info>
  **Diese Seite wurde mit KI automatisch übersetzt. Maßgeblich ist stets die englische Version.**

  [Englische Version ansehen →](/products/amm-v4/code-demos)
</Info>

<Info>
  **Versionsbanner.** Alle Demos zielen auf `@raydium-io/raydium-sdk-v2@0.2.42-alpha` gegen Solana mainnet-beta ab, verifiziert 2026-04. Program ID: `675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8` (siehe [`reference/program-addresses`](/de/reference/program-addresses)).
</Info>

<Info>
  **Neue Pool-Erstellung wird hier nicht gezeigt.** Die Raydium-UI bietet nicht mehr die Erstellung von AMM v4 Pools an – neue Paare nutzen standardmäßig [CPMM](/de/products/cpmm/code-demos). Das AMM v4 Programm selbst akzeptiert On-Chain noch `Initialize2`; dies ist jedoch nicht der empfohlene Weg. Die Demo unten behandelt die Live-Pool-Operationen, die jeder Integrator benötigt: Swap, Deposit, Withdraw.
</Info>

## Setup

```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" });
```

## Pool nach ID abrufen

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

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

// Holen Sie sich das normalisierte Pool-Objekt des SDK. Für AMM v4 enthält dies
// auch die OpenBook-Konten, die die Instruction Builder benötigen.
const data = await raydium.liquidity.getPoolInfoFromRpc({ poolId });
const { poolInfo, poolKeys, poolRpcData } = data;

console.log("Paar:", poolInfo.mintA.symbol, "/", poolInfo.mintB.symbol);
console.log("Version:", poolInfo.version);       // 4 für AMM v4
console.log("Market:", poolKeys.marketId.toBase58());
```

`poolKeys` ist die Struktur, die die Instruction Builder nutzen. Sie enthält jeden AMM v4 und OpenBook-Account in der Reihenfolge, die das Programm erwartet.

## Swap (Base-In)

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

const amountIn = new BN(1_000_000);            // 1 USDC (6-dezimal Quote)
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("Swap Tx:", txId);
```

Das SDK fügt alle OpenBook-Accounts automatisch hinzu. Versuchen Sie nicht, sie von Hand zu ersetzen – das Programm validiert jeden Slot.

## Swap (Base-Out)

```ts theme={null}
const amountOut = new BN(1_000_000_000);       // 1 SOL (9-dezimal Base)
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 });
```

## Liquidität hinzufügen

```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"` sagt dem SDK, dass Sie den exakten `amountInA` bereitgestellt haben und `amountInB` höchstens `maxAnotherAmount` sein sollte. Die On-Book-Liquidität des Pools wird vor der proportionalen Mathematik abgewickelt, damit das Einzahlungsverhältnis den neuesten Reserven entspricht.

## Liquidität entfernen

```ts theme={null}
const lpAmount = new BN(50_000);               // LP zum Verbrennen

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

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

Slippage-Limits schützen davor, dass sich der Pool-Status zwischen Ihrem Pre-Quote und der Landzeit verschiebt.

## Compute-Unit / Priority-Fee-Abstimmung

AMM v4 Swaps sind rechenintensiv, da jede Instruction den vollständigen OpenBook-Status validiert. Ein typischer Swap nutzt 180k–250k CU, abhängig davon, wie viele offene Orders während des Prozesses abgewickelt werden müssen. Übergeben Sie immer ein Compute-Unit-Limit:

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

const { execute, innerTransactions } = await raydium.liquidity.swap({
  /* ...params... */
  computeBudgetConfig: {
    units: 400_000,
    microLamports: 50_000,       // Priority Fee
  },
});
```

Wenn Sie `computeBudgetConfig` auslassen, kann das SDK dennoch sein eigenes Standard nutzen; überprüfen Sie `innerTransactions`, um dies zu bestätigen. Siehe [`integration-guides/priority-fee-tuning`](/de/integration-guides/priority-fee-tuning).

## Direkter Rust CPI

Wenn Sie von Ihrem eigenen Anchor-Programm aus in AMM v4 CPIen müssen, müssen Sie die Account-Liste von `SwapBaseIn` wörtlich modellieren. Eine minimale Skizze:

```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<()> {
    // Instruction Discriminator für SwapBaseIn ist 9 auf AMM v4.
    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 wird nicht mit einer Anchor-Crate für CPI ausgeliefert. Die Skizze oben verwendet eine manuell erstellte `Instruction`.

## Fallstricke

* **Ein OpenBook-Account fehlt.** Alle 8 OpenBook-seitigen Accounts sind bei jedem Swap, Deposit und Withdraw erforderlich; das SDK handhabt dies, manuell erstellte Instructions oft nicht.
* **Rohe Vault-Salden auslesen.** Spiegelt nicht On-Book-hinterlegte Beträge oder aufgelaufene PnL wider. Verwenden Sie das SDK-Quote oder `api-v3.raydium.io/pools/info/ids`.
* **OpenBook Event Queue voll.** Ein Pool kann Swaps mit `SerumOrderError` ablehnen, wenn die Event Queue seines Markets gekurbelt werden muss. Kurbeln ist erlaubnisfrei (`MonitorStep` auf den OpenBook-Accounts des Markets).
* **Token-2022 Mints.** Nicht unterstützt. Ein AMM v4 Pool kann nicht gegen einen Token-2022 Mint erstellt werden; jedes Token-2022 Paar sollte auf CPMM oder CLMM sein.

## Nächste Schritte

* [`products/amm-v4/instructions`](/de/products/amm-v4/instructions) – die Instruction-Ebene hinter diesen Demos.
* [`user-flows/migrate-amm-v4-to-cpmm`](/de/user-flows/migrate-amm-v4-to-cpmm) – wenn Sie ein LP sind und eine Migration erwägen.
* [`integration-guides/priority-fee-tuning`](/de/integration-guides/priority-fee-tuning) – Priority-Fee-Sizing für intensive AMM v4 Swaps.

Quellen:

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