Engine

Engine overview

The always-on process that runs the strategies, keeps the books and enforces the limits.

The engine (engine/) is a single Node 22 process. It reads live markets, runs five strategies, keeps the vault's books in Postgres and obeys the kill switch. It must run 24/7, which is why it lives on Render or any Docker host rather than on Vercel.

It is bundled by esbuild into one file, dist/engine.mjs, with the database migrations copied next to it. Main dependencies: @nktkas/hyperliquid, viem, drizzle-orm, postgres, zod.

Startup

  1. Validate the configuration with zod. A missing or malformed variable stops the process with Engine configuration is invalid: and the failing keys. Live mode without its keys stops with EXECUTION_MODE=live needs …. See Paper & live modes.
  2. Connect to Postgres and apply migrations. With MIGRATE_ONLY=1 it stops here.
  3. If PAPER_RESET=true (paper only), wipe the paper books.
  4. Create the Elysium client (15 s timeout, 2 retries, multicall batching).
  5. Start the feeds: Hyperliquid for every market's coin, Montra for the token's pool. See Market data.
  6. Pick the venues: paper simulators, or live HyperCore and Montra clients.
  7. Mark every order still open in the database as cancelled (a restart never trusts old quotes).
  8. Wait for prices: up to 20 s for HYPE and KNTQ marks, then continue with a warning.
  9. Load the books from the database, or seed a new paper treasury. See Accounting.
  10. Start the strategies in order: harvest, hypercore-mm, montra-mm, hedge, arb.
  11. Log Engine started in paper mode (or LIVE), write the first heartbeat, start the timers and the HTTP server.

On SIGTERM or SIGINT it stops the timers, makes every strategy stand down (cancel its resting orders), saves the books, logs Engine stopped; resting orders cancelled, closes the feeds and the database, and exits.

Timers

Each timer skips a tick if its previous run is still busy, and logs errors without stopping.

TimerEveryDoes
control3 sReads the kill switch and strategy pauses from engine_state
heartbeat5 sWrites the heartbeat the API uses to say online
books5 sRevalues the vault, saves balances, positions and strategy rollups
snapshotSNAPSHOT_SECONDS (60 s)Saves a NAV snapshot, checks the drawdown limit, suggests rebalances
prune1 hDeletes closed orders older than a day
sync15 s, live onlyReads the real balances from Elysium and HyperCore
harvest30 sStrategy tick
hypercore-mm1 sStrategy tick
montra-mm10 sStrategy tick
hedge5 sStrategy tick
arb15 sStrategy tick

Before every strategy tick the engine checks the controls: if the kill switch is on the strategy is marked halted and skipped; if it is paused it is marked paused and skipped; otherwise the vault is revalued and the strategy runs.

Strategy statuses

StatusMeaning
runningWorking normally
pausedPaused by an operator through the admin API
haltedThe kill switch is on (by an operator or the drawdown limit)
idleNothing to do yet (arbitrage before MOTION lists on HyperCore)
blockedWaiting on something outside the engine (no token, FeeSplitter not wired)
offlineAdded by the API when the engine has stopped reporting

HTTP endpoints

The engine listens on PORT (default 8080).

GET /health, used by Render and by the Docker health check:

json
{ "ok": true, "mode": "paper", "uptimeSec": 42, "killSwitch": false }

GET /status:

ts
{
  mode: "paper" | "live",
  navUsd: number | null,
  startingNavUsd: number | null,
  killSwitch: boolean,
  strategies: Array<{ id, status, note, lastActionAt }>,
  holdings: Record<"<venue>:<asset>", number>   // e.g. "hypercore:USDC"
}

With ENGINE_SERVES_API=true the engine also serves the full API at /v1/* and /api/v1/*.

Logs and tests

Logs are one JSON line per event ({t, level, scope, msg, …}) when LOG_FORMAT=json or when not on a terminal, readable lines otherwise. LOG_LEVEL is debug, info (default), warn or error.

cd engine && npm test runs 19 tests: paper fills and quoting, inventory limits, stale-book pull-back, arbitrage maths, Hyperliquid price rounding, average-cost accounting and a real database round trip on in-process Postgres.