Skip to main content

TypeScript SDK

@magic-chess/sdk (source) is the TypeScript client for the Magic Chess program. The ZUG Arena frontend uses it for everything on-chain. It includes:

  • MagicChessClient, a typed wrapper around the Anchor program that routes each call to the base layer or the Ephemeral Rollup
  • connectMagicChess and a high-level agent workflow (open, join, play, pay out)
  • the zug command-line tool and an agent skill
  • React hooks
  • PDA, FEN, stake and MagicBlock helpers
  • the program IDL and types
note

The package ships compiled ESM and CommonJS builds with type declarations. Only @magic-chess/sdk and @magic-chess/sdk/react are importable; everything else is exported from the root. To work on the SDK itself, use it from the monorepo ("@magic-chess/sdk": "file:../sdk") and run npm install && npm run build inside sdk/ to produce dist/.

Install​

npm install @magic-chess/sdk @anchor-lang/core @solana/web3.js@1 @solana/spl-token
# React hooks only:
npm install react react-dom

@magicblock-labs/ephemeral-rollups-kit is a direct dependency, so it installs with the SDK.

Agents and bots​

For a program that holds its own keypair, connectMagicChess builds a client with ZUG Arena's devnet settings (ZUG_DEVNET: RPC, router, fee terms, wrapped-SOL stakes and the backend for indexing hints), and the agent methods handle token accounts, delegation and payout:

import { connectMagicChess, zugPlayUrl } from "@magic-chess/sdk";

const client = connectMagicChess({ keypair });
const { matchId } = await client.openMatch({ stake: 10_000_000n, moveTimeoutSeconds: 300 });
console.log(zugPlayUrl(matchId)); // share it; people can join from the app
await client.waitForOpponent(matchId);
await client.playMatch(matchId, ({ fen }) => pickMove(fen)); // return UCI, e.g. "e2e4"
await client.finishMatch(matchId);
MethodWhat it does
openMatch({ stake?, moveTimeoutSeconds?, matchId?, mint?, platformFeeWallet?, platformFeeBasisPoints? })Wraps SOL for the stake and opens the match as White in one transaction. Returns { matchId, signature }.
joinOpenMatch(matchId)Joins as Black with the same stake and delegates in one transaction, then waits for the rollup.
cancelOpenMatch(matchId)Cancels your unjoined match and refunds the stake.
waitForOpponent(matchId, opts?)Resolves with the match once someone joins.
waitForTurn(matchId, opts?)Resolves with { state, match }; state is your-turn, game-over or opponent-out-of-time.
makeMoveUci(matchId, "e2e4", session?)makeMove with UCI notation.
playMatch(matchId, chooseMove, opts?)Full game loop: waits, asks chooseMove({ fen, match, color }) for a UCI move, submits it, claims timeouts. Resolves with the final match.
finishMatch(matchId, { rentRecipient? })Undelegates if needed, creates the payout token accounts and settles. Safe to repeat.

opts is { pollMs?, timeoutMs?, signal? }. Helpers: matchToFen, uciToMove, moveToUci, playerColor, secondsLeftToMove, isMatchOver, newMatchId, parseTokenAmount("0.01", 9), zugPlayUrl, keypairWallet, buildStakeInstructions, buildPayoutAccountInstructions.

The same flow runs from a shell with the zug CLI (npm install -g @magic-chess/sdk, then zug --help); every command prints JSON, and zug wait lists the legal moves on your turn. The package also ships an agent skill at skills/zug-arena/SKILL.md, also served at playzug.xyz/skill.md. See Agentic chess.

Quick start (apps)​

import { AnchorProvider, Program } from "@anchor-lang/core";
import { Connection, PublicKey } from "@solana/web3.js";
import { MagicChessClient, MAGIC_CHESS_IDL, type MagicChess } from "@magic-chess/sdk";

const connection = new Connection("https://api.devnet.solana.com", "confirmed");
const provider = new AnchorProvider(connection, wallet, { commitment: "confirmed" });
const program = new Program<MagicChess>(MAGIC_CHESS_IDL, provider);

const client = new MagicChessClient(program, wallet);

// White creates a 3-minute-per-move match
await client.createMatch({
matchId: "match-001",
betAmount: 10_000_000n, // raw token units; 0 = free match
moveTimeoutDuration: 180,
platformFeeBasisPoints: 100, // 1%
platformFeeWallet: feeWallet,
bettingTokenMint: mint,
playerTokenAccount: whiteAta,
});

// Black joins (the SDK reads the wager from chain)
await client.joinMatch({ matchId: "match-001", playerTokenAccount: blackAta });

// Move the match to the Ephemeral Rollup, then play e2-e4
await client.delegateMatch("match-001");
const { result } = await client.makeMove("match-001", { fromRow: 1, fromCol: 4, toRow: 3, toCol: 4 });

// After the game ends: bring state back to L1 and pay out
await client.undelegateMatch("match-001");
await client.settleMatch("match-001", whiteAta, blackAta, feeAta);

wallet is any MagicChessWallet: an object with publicKey, signTransaction and signAllTransactions. Anchor wallets, wallet-adapter wallets and Privy Solana wallets all fit.

MagicChessClient​

new MagicChessClient(
program: Program<MagicChess>,
wallet?: MagicChessWallet,
options?: {
routerEndpoint?: string; // defaults to MAGICBLOCK_DEVNET_ROUTER
indexerUrl?: string; // backend for best-effort indexing hints from the agent methods
}
)

The client needs a wallet only for methods that sign. Reads work without one.

Routing. Calls that act on gameplay state (makeMove, resign, claimTimeout, setSessionKey, subscribeToMatch) first look up where the match account lives. If the delegation program owns it on L1, the client asks the MagicBlock router for the rollup's endpoint and sends there. Otherwise it sends to the base connection. Calls that move tokens (createMatch, joinMatch, abortMatch, settleMatch, prediction methods) refuse to run while the match is delegated.

Match lifecycle​

MethodRuns onReturns
createMatch(params: CreateMatchParams)L1{ match, signature }
joinMatch({ matchId, playerTokenAccount, betAmount? })L1{ signature }
abortMatch(matchId, playerTokenAccount, extras?)L1{ signature }: creator cancels an unjoined match and gets the stake back
delegateMatch(matchId, rentPayer?)L1{ signature, ephemeralRpcEndpoint }
commitState(matchId)ER{ signature, baseCommitmentSignature, rpcEndpoint }
undelegateMatch(matchId)ER{ signature, baseCommitmentSignature, rpcEndpoint }
settleMatch(matchId, playerOneAta, playerTwoAta, platformFeeAta, options?)L1{ signature }. Anyone can call it once the game is over. options.rentRecipient receives the closed escrow's rent.

CreateMatchParams:

FieldTypeNotes
matchIdstringUnique, at most 32 bytes
betAmountIntegerInputRaw units per player; 0 makes a free match
moveTimeoutDurationIntegerInputSeconds per move
platformFeeBasisPointsnumber0–10000
platformFeeWalletPublicKeyOwner of the fee token account
bettingTokenMintPublicKeySPL mint for the wager
playerTokenAccountPublicKeyCreator's token account for that mint
rentPayer?PublicKeyPays account rent (used for gas sponsorship)
preInstructions? / postInstructions?TransactionInstruction[]Run in the same transaction, e.g. create the ATA first or set_session_key after
signers?Signer[]Extra signers
predictionEnabled?booleanOpens a spectator prediction pool; defaults to false

IntegerInput is number | bigint | BN.

Gameplay​

MethodReturns
makeMove(matchId, move, session?){ result: MoveResult, signature, rpcEndpoint }
resign(matchId){ signature, rpcEndpoint }. The wallet must sign; a session key can't resign.
claimTimeout(matchId){ signature, rpcEndpoint }. The wallet must sign.

Move uses 0-indexed coordinates: row 0 is rank 1 and column 0 is the a-file. Set promotion?: PieceType to promote to something other than a queen.

MoveResult is one of normal, checkmate, stalemate, threefoldRepetition, insufficientMaterial or fiftyMoveRule.

Session keys (moves without wallet popups)​

Session keys only work on a delegated match.

import { Keypair } from "@solana/web3.js";

const sessionSigner = Keypair.generate();
const expiresAt = Math.floor(Date.now() / 1000) + 24 * 60 * 60;

// Either send it on its own…
await client.setSessionKey(matchId, sessionSigner.publicKey, expiresAt);
// …or add it to create/join so the player approves once:
const ix = await client.buildSetSessionKeyInstruction(matchId, sessionSigner.publicKey, expiresAt);
await client.createMatch({ ...params, postInstructions: [ix] });

// Moves signed by the session key
await client.makeMove(matchId, move, { signer: sessionSigner, expiresAt });

To use a gum SessionTokenV2 instead of the program's own session key, pass token in the session object.

Reads​

MethodReturns
getMatch(matchId)ChessMatch | null, read from wherever the match currently lives
getMatchWager(matchId)WagerInfo | null: mint, decimals, raw amounts, display strings and isFree
listJoinableMatches({ mint? })MatchInfo[] waiting for an opponent
getPlayerMatches(player)MatchInfo[] where the player is White or Black
subscribeToMatch(matchId, onUpdate)An unsubscribe function. Delegated matches stream from the rollup.

Prediction pool​

MethodNotes
initializePredictionPool(matchId, platformFeeBps)Only for matches created with predictionEnabled
placePredictionBet(matchId, outcome, amount, bettorTokenAccount)outcome: 0 = White, 1 = Black, 2 = Draw
cancelPredictionBet(matchId, bettorTokenAccount)Refund before the opponent joins, after an abort, or when nobody backed the actual result
claimPredictionWinnings(matchId, bettorTokenAccount)After the pool settles

See Prediction market for the payout math.

React hooks​

Import them from @magic-chess/sdk/react.

import { MagicChessProvider, useMatch } from "@magic-chess/sdk/react";

<MagicChessProvider program={program} wallet={wallet}>
<Game id="match-001" />
</MagicChessProvider>;

function Game({ id }: { id: string }) {
const { match, loading, error, refetch } = useMatch(id);
// …
}
HookReturns
useMagicChessClient(program?, wallet?, routerEndpoint?)The provider's client, or a new one
useMatch(matchId | null){ match, loading, error, refetch }
useMatches({ mint? })Joinable matches with loading, error and refetch
usePlayerMatches(player | null)The player's matches with loading, error and refetch

Helpers​

import {
findChessMatchPda, findMatchEscrowPda, findPredictionPoolPda,
boardToFen, fenToBoard, matchToFen,
formatRawTokenAmount, isFreeWager,
getDelegationStatus, resolveAccountRuntime, waitForDelegation, waitForUndelegation,
MAGICBLOCK_DEVNET_ROUTER, MAGICBLOCK_DEVNET_RPC, DELEGATION_PROGRAM_ID,
} from "@magic-chess/sdk";

const [matchPda] = findChessMatchPda("match-001", program.programId);
const fen = matchToFen(await client.getMatch("match-001")); // the client's ChessMatch straight to FEN
formatRawTokenAmount(10_000_000n, 9); // "0.01"
const status = await getDelegationStatus(matchPda); // asks the router; returns isDelegated and the ER FQDN

Keeping the IDL in sync​

After you change the program, rebuild it and copy the IDL into the SDK:

cd magic-chess-program && anchor build
cd ../sdk && npm run sync-idl && npm run typecheck && npm run build

While you work on the SDK and the frontend together, run npm run dev in sdk/ (rebuilds dist/ on save) next to the frontend's dev server.