Docs · Guides

Run Mesa on a local network

Run the whole Mesa stack on your machine with one command, open a funded account, move the oracle price, and point scripts at it.

The local network is Mesa with every part running on your machine: a Solana validator with the Privacy Rings programs and the Mesa program, the shielded indexer and prover, a drand beacon, an oracle you set by hand, and the API, indexer and keeper. Batches run, positions renew and lapse, and liquidations land, the same as on mainnet. Use it to test a script before it touches real funds, or to make a position lapse on purpose.

What you need

  • Linux, macOS, or Windows with WSL2. Run every command below in that shell.
  • Node 22 and Docker.
  • The zolana CLI, release cli-v0.10.0, at ~/mesa-dev/bin/zolana, or with ZOLANA_CLI_BIN set to its path.
  • Rust 1.98.1 and Solana platform tools v1.54, to build the program.
  • A checkout of the Mesa repository, with npm install run in backend/.

Build the program

Once, and again after the program changes:

bash
bash backend/program/scripts/build.sh

It compiles the circuits and their keys, the browser and Node WASM packages, and the program, to ~/mesa-dev/target/deploy/mesa_program.so.

Start it

From the repository root:

bash
bash dev/localnet/up.sh

It starts each part in order and waits for each to answer:

  1. Postgres

    A container named mesa-postgres on 127.0.0.1:23720.

  2. drand beacon

    A local quicknet beacon on 127.0.0.1:23712, so batches open without waiting on the public network.

  3. Validator, indexer and prover

    Surfpool on 127.0.0.1:23730 (WebSocket 23731), Photon on 23734 and the Rings prover on 23735, with the Privacy Rings programs and the Mesa program loaded.

  4. Seed

    Mints SOL, USDC and JITOSOL, writes Config, creates the SOL/USDC market and vault, the three lending pools, SOL-PERP and its vault, and runs two batches so every market has history. It funds a test account. Running it again on a seeded chain sends nothing.

  5. Oracle

    Writes the prices in dev/localnet/prices.json on chain every 10 seconds.

  6. API, indexer and keeper

    Syncs the circuits, builds the backend and starts all three. The API answers on 127.0.0.1:23714.

When it finishes, it prints each address and where the logs are. GET http://127.0.0.1:23714/health reads live.

Logs are in ~/mesa-dev/localnet/logs/ and dev/live/. Set MESA_LOCALNET_DIR before up.sh to keep the chain state somewhere else.

Open the funded account

The seed funds one test account with SOL, USDC and JITOSOL in its wallet, its Rings private balance and its Mesa balance. The local API hands out its key:

local.ts
import { createClient } from '@mesa/contract'
import { openMesaScript } from '@mesa/sdk/node'

const apiUrl = 'http://127.0.0.1:23714'
const dev = await createClient(apiUrl).devWallet()

const mesa = await openMesaScript({ apiUrl, wallet: dev.secretKeyBase58 })

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

The script finds the validator, indexer and prover from the API's config. GET /v1/dev/wallet exists only on a local network. Every other call in this guide works the same against https://api.mesa.markets with your own key file.

Move the price

Edit dev/localnet/prices.json:

dev/localnet/prices.json
{
  "SOL": 120.0,
  "USDC": 1.0,
  "JITOSOL": 140.0
}

Within 10 seconds the oracle writes the new prices and the cross rates between the assets. A drop in SOL makes SOL-collateral borrows less healthy. Push it far enough and a position can no longer renew.

Make a position lapse

The local Config runs short timings, so you can watch a whole cycle in minutes. Read them from GET /v1/config: batchLengthSeconds, heartbeatSeconds.

  1. Open a borrow with SOL collateral from the funded account, near the collateral factor.
  2. Do not run a renew loop, and do not enrol the watcher.
  3. Lower SOL in prices.json.
  4. Wait for its deadline, the end of the heartbeat after the one it was opened in. The position lapses and shows in lapsedPositions().
  5. Liquidate it from a second account. See Run a liquidator.

The keeper on the local network liquidates lapsed positions too, so your liquidator races it.

Stop it

bash
bash dev/localnet/down.sh

It stops only what up.sh started, read from the PID files up.sh wrote, and stops the Postgres container without removing its data.