Mira

Professional on-chain trading strategies.

·4 min read·mirafinance.xyz

Mira copies professional traders on Hyperliquid into each investor's own account, as a balanced portfolio. I'm the co-founder and CTO: I designed the system and wrote almost all of it, from the copy engine to the web app.

336 architecture decisions written down in eight months.

Mira

The engineering problem

Copy trading sounds simple: a trader opens a position, the followers open the same one. In practice every trade has to reach many isolated accounts in the right order, at the right size, within seconds, and survive crashes, deploys, partial fills and an exchange that sometimes doesn't answer. And it has to do all of that without Mira ever being able to move anyone's money.

The rule I built around: a wrong trade is worse than a late one. Nothing is retried blindly, and anything uncertain is reconciled against the exchange.

Architecture

A Bun and TypeScript monorepo with 8 deployed services: an Elysia API, the copy-engine worker, a notification worker, the React web app, a Next.js site, docs and two Telegram bots, sharing 17 internal packages.

  • Postgres (Drizzle, 100+ tables) is the source of truth. Every trade, order attempt and plan step is a durable row before anything touches the exchange.
  • RabbitMQ carries background work, with dead-letter queues and stepped retry delays. Every message ends as acknowledged, retried, deferred or discarded, never lost.
  • Redis holds prices, market metadata, rate limits and proxy health, so cooldowns survive a deploy.
  • OpenTelemetry traces a trade end to end, from the exchange websocket to the order acknowledgement, into Grafana, Tempo, Loki and Prometheus, with 9 dashboards and around 35 alerts.
  • Infrastructure as code on Railway: a merge to main deploys staging, a release deploys production.

The copy engine

Detect. One websocket per trader streams fills, grouped in a 100 ms window. A polling job runs behind it every minute, so a dropped socket never means a missed trade.

Turn fills into events. Fills become one event with a deterministic id, so the same trade is recognised whether it arrives live or through recovery. After a restart, a drain barrier holds new events until the gap from the downtime is filled, so a live trade is never copied ahead of an older one.

Size. Each follower's order is scaled from the trader's position, the follower's equity and a per-strategy factor, then fitted to the exchange's minimums, leverage limits and the follower's free margin.

Execute without guessing. Each order is saved before it's sent, gets a deterministic client id and expires on the exchange 20 seconds after submission. If the answer is lost, the exact same signed bytes are replayed through another network route. The exchange accepts a signature only once, so a replay can never fill twice.

Reconcile. Every account keeps a target position and an actual one. Partial fills and rejections leave a gap that a separate job closes, with backoff, instead of re-running the original trade.

Never holding the money

Each follower gets a dedicated wallet. Trading goes through a separate exchange key that can open and close positions but can't withdraw, encrypted at rest and rotated before it expires. Withdrawals are locked by wallet policies to the user's own addresses, and a new address only becomes trusted after a 24-hour delay and an explicit action by the user.

Problems worth telling

One stuck order froze a whole strategy. Copies ran in strict order per strategy, so a single slow follower delayed everyone. I split each event in two: the trader's side is computed once and frozen, and every follower then copies in its own order. A late copy still sizes from the trader as of that moment, and nobody else waits.

Lost orders that never existed. Timeouts held a strategy for about 100 seconds, and tracing showed 31 of 32 "unanswered" orders had never reached the exchange. Signing once and replaying the same bytes made the retry safe and the wait short.

Database connections that went silent. A few queries hung for over 15 minutes because a connection pooler dropped sockets without telling anyone. Sockets now get destroyed after 60 seconds of silence, since the same pool also serves order execution.

Plans that take days. Rebalancing a portfolio can mean closing positions today and investing the money once it settles. Plans run as a state machine in Postgres: the API starts each step, a worker sweep only observes and advances, and no transaction is ever held open across a wallet or exchange call.

How I work

Decisions in writing. 336 decision records, each with context, the decision, consequences and the alternatives considered. About a hundred of them were later superseded, and that history is the point.

Specs as the source of truth. 71 feature contracts describe what each part must do. Code that disagrees with them is a bug.

Tested at every level. Around 6,500 tests, including integration tests against real Postgres, Redis and RabbitMQ, plus 118 Storybook stories for the interface.

AI as part of the team. About 60% of the 6,400+ commits were written with Claude, guided by 25 project skills and per-folder agent instructions. Every pull request gets a fresh AI review, and anything that touches money, the copy engine, auth, wallets or migrations stops for my review before merging.

/projects/mira/portfolio-2.webp-image-0/projects/mira/a.webp-image-1/projects/mira/trades.webp-image-2/projects/mira/c.webp-image-3

Stack: Bun, TypeScript, Elysia, React, Next.js, Postgres, Drizzle, RabbitMQ, Redis, Hyperliquid, Privy, OpenTelemetry, Grafana, Railway.

Outcome

Live for three months and bootstrapped: $4.38M in traded volume for 35 investors, on an engine built so it never has to guess what happened to an order.