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
- 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 withEXECUTION_MODE=live needs …. See Paper & live modes. - Connect to Postgres and apply migrations. With
MIGRATE_ONLY=1it stops here. - If
PAPER_RESET=true(paper only), wipe the paper books. - Create the Elysium client (15 s timeout, 2 retries, multicall batching).
- Start the feeds: Hyperliquid for every market's coin, Montra for the token's pool. See Market data.
- Pick the venues: paper simulators, or live HyperCore and Montra clients.
- Mark every order still
openin the database ascancelled(a restart never trusts old quotes). - Wait for prices: up to 20 s for HYPE and KNTQ marks, then continue with a warning.
- Load the books from the database, or seed a new paper treasury. See Accounting.
- Start the strategies in order:
harvest,hypercore-mm,montra-mm,hedge,arb. - 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.
| Timer | Every | Does |
|---|---|---|
control | 3 s | Reads the kill switch and strategy pauses from engine_state |
heartbeat | 5 s | Writes the heartbeat the API uses to say online |
books | 5 s | Revalues the vault, saves balances, positions and strategy rollups |
snapshot | SNAPSHOT_SECONDS (60 s) | Saves a NAV snapshot, checks the drawdown limit, suggests rebalances |
prune | 1 h | Deletes closed orders older than a day |
sync | 15 s, live only | Reads the real balances from Elysium and HyperCore |
harvest | 30 s | Strategy tick |
hypercore-mm | 1 s | Strategy tick |
montra-mm | 10 s | Strategy tick |
hedge | 5 s | Strategy tick |
arb | 15 s | Strategy 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
| Status | Meaning |
|---|---|
running | Working normally |
paused | Paused by an operator through the admin API |
halted | The kill switch is on (by an operator or the drawdown limit) |
idle | Nothing to do yet (arbitrage before MOTION lists on HyperCore) |
blocked | Waiting on something outside the engine (no token, FeeSplitter not wired) |
offline | Added 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:
{ "ok": true, "mode": "paper", "uptimeSec": 42, "killSwitch": false }GET /status:
{
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.