# Direct Raydium CPMM execution

BLOCK CLI builds exact-input swaps and liquidity changes for Raydium's canonical constant-product pools on Solana mainnet. The hosted API prepares and simulates unsigned transactions. Agents inspect, approve and sign those exact bytes with their local wallet, then submit through the generic Solana transaction driver.

The four actions are separate from the older routed `raydium.build_swap` Trade API path, whose provider instructions are still only structurally checked.

## Actions and units

All four actions require `wallet`, `pool`, and positive `maxNetworkFeeLamports`, with optional `priorityFeeMicroLamports` (integer 0–1,000,000; default 0) and `commitment` (`confirmed` or `finalized`; default `confirmed`). Token amounts are canonical positive decimal **raw-unit strings**, bounded to u64.

| Action | Additional required parameters | Behavior |
| --- | --- | --- |
| `build_buy` | `baseMint`, `quoteMint`, `amountIn`, `minAmountOut` | Spend exact quote-token input and receive at least the base-token minimum. |
| `build_sell` | `baseMint`, `quoteMint`, `amountIn`, `minAmountOut` | Spend exact base-token input and receive at least the quote-token minimum. |
| `build_add_liquidity` | `token0Mint`, `token1Mint`, `lpTokenAmount`, `maxAmount0`, `maxAmount1` | Mint exactly the requested raw LP amount while bounding both token deposits. |
| `build_remove_liquidity` | `token0Mint`, `token1Mint`, `lpTokenAmount`, `minAmount0`, `minAmount1` | Burn exactly the requested raw LP amount while enforcing both token withdrawal minima. |

The two liquidity mints must match the pool's token0/token1 order. LP amounts use the verified LP mint's decimals; they are not a percentage or USD value. Both minimum outputs must be positive. Tiny LP changes that round either token amount to zero are rejected.

The wallet must already have canonical classic SPL Token ATAs for both pool tokens. Liquidity operations also require its canonical LP ATA. Wrapped SOL must already be funded in its ATA, and any received WSOL remains wrapped. The builder creates no accounts, charges no rent, and neither wraps nor unwraps SOL.

## Validation and transaction construction

The builder pins Solana mainnet and program `CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C`. It verifies the 637-byte pool and 236-byte config layouts, canonical pool/config/vault/LP/oracle PDAs, global authority, current permissions and opening time, classic token ownership and initialization, unsupported extensions, exact balances, fee reserves and LP supply. Delegated, frozen, externally closable, Token-2022 and unsupported pool/account layouts fail closed.

Quote math follows the official Rust program: protocol, fund and creator balances are excluded from usable reserves; creator fees may apply to input or output; swap fees round upward; proportional LP deposits round upward except when the unrounded quotient is zero; withdrawals round downward. Exact input, minimum output, LP amount and liquidity maxima/minima are encoded in the instruction.

The complete transaction is locally constructed with only compute-budget instructions plus `swap_base_input`, `deposit` or `withdraw`. The wallet is its only signer and fee payer, and there are no address lookup tables, external recipients, provider instructions or automatic setup transactions.

Unsigned simulation must match the calculated token debits/credits, vault changes, fee counters, LP supply and LP account delta. It must preserve account ownership, metadata and rent, and debit the wallet's native SOL only for the estimated network fee. Concurrent pool changes can make this exact check fail; refresh the preparation rather than weakening the check. Blockhash replacement, stale state, excessive compute and expired preparations are rejected.

## Local execution

Save the selected `block-cli call raydium ACTION --params '<JSON>'` preparation result, then follow [the local execution workflow](/guides/local-execution.md): `tx inspect`, independent digest/amount review, `wallet sign`, `tx submit`, and transaction status reconciliation. The preparation returns `transactionBase64`, `lastValidBlockHeight`, exact `constraints`, signed token delta estimates under `quote`, and the verified simulation slot.

Encoded token limits remain enforceable at execution. Network fee ceilings and simulated quotes are snapshots; recheck before signing. Successful simulation is not execution, and RPC submission alone is not confirmation. Preserve the original transaction ID and reconcile it before retrying an uncertain operation.

## Evidence and scope

`raydium-cpmm-live-v0.12.0.json` (evidence record in the source checkout) records all four actual builders passing mainnet simulation: buy, sell and add liquidity on a WSOL/USDC pool, and remove liquidity on a USDC/USDT pool. Public existing accounts were used without possessing or signing their private keys. No transaction was broadcast. Fixture tests cover both token directions, all creator-fee modes, native WSOL accounting, Rust rounding examples, state mutations and the actual generic local-signing/mock-submission workflow.

This release does not directly construct AMM v4 or CLMM operations, noncanonical pool addresses, Token-2022 transfers, new ATAs, pool creation or automatic wrapping. The existing routed quote/build API remains available with its separately stated verification limits.

Sources: [official CPMM program](https://github.com/raydium-io/raydium-cp-swap), [swap and fee calculations](https://github.com/raydium-io/raydium-cp-swap/blob/master/programs/cp-swap/src/curve/calculator.rs), [LP rounding](https://github.com/raydium-io/raydium-cp-swap/blob/master/programs/cp-swap/src/curve/constant_product.rs), [deposit](https://github.com/raydium-io/raydium-cp-swap/blob/master/programs/cp-swap/src/instructions/deposit.rs), [withdraw](https://github.com/raydium-io/raydium-cp-swap/blob/master/programs/cp-swap/src/instructions/withdraw.rs).
