Docs · Start

Quickstart: trade from code

Install the SDK, open an account from a key file, deposit, place a sealed order and read its fill, in one script.

The SDK does from a script what the app does from a browser: one call per action, each returning when it is done. This page goes from an empty folder to a filled order.

Before you start

  • Node 22.
  • A Solana keypair file whose wallet has a Rings private balance of the asset you will trade with. The first quickstart sets one up in the app, and Deposit and withdraw does it from code.

Install

bash
npm install @mesa/sdk

Open the account

trade.ts
import { openMesaScript } from '@mesa/sdk/node'

const mesa = await openMesaScript({
  apiUrl: 'https://api.mesa.markets',
  keyFile: './wallet.json',
  onProgress: (p) => console.log(p.stage, p.batchNumber ?? ''),
})

openMesaScript signs one message with the keypair, derives the account's keys from the signature, and reads the account by decrypting its notes. It is the same account the app opens for that wallet.

Deposit into Mesa

trade.ts
const config = await mesa.mesa.client.config()
const usdc = config.assets.find((a) => a.symbol === 'USDC')!

await mesa.deposit({ mint: usdc.mint, amount: 2_000_000_000n })

Amounts are integers in the asset's smallest unit, so this deposits 2,000 USDC. The deposit is the one action in this script that the keypair signs as a transaction.

Place a sealed order

trade.ts
const { order, batchNumber } = await mesa.swap({
  marketId: 'SOL-USDC',
  side: 'buy',
  amount: 1_850_000_000n,
  limitPrice: '185.00',
})

This locks 1,850 USDC in an order to buy SOL at 185.00 or better. The SDK proves the order in your process, seals its terms to the batch's close and relays it. onProgress reports signing, proving, relaying, then placed with the batch number.

The call resolves when that batch has settled.

Read the fill

trade.ts
if (order) {
  console.log(order.state)          // 'filled', 'partially_filled' or 'unfilled'
  console.log(order.filledBase)     // SOL received, in lamports
  console.log(order.filledQuote)    // USDC spent
  console.log(order.clearingPrice)  // the one price of the batch
}

console.log(mesa.snapshot().balances)
mesa.destroy()

Every order in the batch filled at clearingPrice. The USDC the order did not spend is back in the balance.

Read the batch

The batch is public. Fetch its record with no key at all:

bash
curl "https://api.mesa.markets/v1/batches/<batchNumber>"

It holds the market's clearing price and every opened order, without owners. Read a batch explains each field.

Run it

bash
npx tsx trade.ts

Next