For the complete documentation index, see llms.txt. This page is also available as Markdown.

Recipes

Working snippets for common NorthStar patterns. Each recipe extends the Hello World shape; copy-paste ready.

1. Always-on agent loop

A Bun service that opens a persistent session, runs a deterministic policy each tick, and re-emits each decision over Server-Sent Events. This is the shape the Mach Sandbox runs in production.

import { AgentLoop, currentRatioBps, DEFAULT_POLICY } from "@mach/agent";

const loop = new AgentLoop({
  manifest, agent, owner, mintA, mintB,
  rpc: ER_RPC,
  intervalMs: 1_000,
  onCycle: (event) => {
    eventBus.publishTx({
      tSendUnixMs: event.timestampMs,
      signature: event.signature,
      direction: event.decision.kind === "swap" ? event.decision.direction : null,
      //
    });
  },
});
loop.start();

The onCycle callback fires per tick with the policy decision, vault snapshot, and any submitted signature. Wire it to whatever consumer you need — SSE, NDJSON file, OpenTelemetry.

2. Confirm-status reconciliation

ER confirmation can be faster than the L1-tuned confirmTransaction timeout. Use a separate getSignatureStatuses poller to upgrade pending rows once the chain catches up:

3. Settle on TTL approach

Sessions auto-expire when their TTL elapses. To rotate gracefully, open the next session in parallel a few minutes before expiry, warm it (delegate accounts), then swap your traffic over and close the old one:

4. Custom-fee grid (USDC)

See Programmable Economics for the full schema.

5. Subscribe to live activity

The Portal's session account is just a Solana account; subscribe to it like any other:

For per-tx feeds, use the program's instruction accounts:

See also

Last updated