Trading Tools v2
Repo: /opt/tradingtools-v2 (pnpm monorepo, package tradingtools-v2)
Server: Cherry (84.32.176.16), co-located with insiders-api · PM2 user: root
Control API: tradingv2.polyinsiders.com → 127.0.0.1:9110 (token-gated)
Trading Tools v2 is a headless internal system and the production copy-trading engine
as of the 2026-05-31 cutover — it replaced the old /opt/copytrading stack
(see Copy Trading (Legacy)). There is no public-facing trading API;
all user interaction goes through the Insiders backend as a proxy.
Design principles
- Built from scratch. The only thing carried over from
/opt/copytradingis the websocket listener. - Execution is orchestration. All buys/sells/redeems go through the insiders-api HTTP layer — no Polymarket SDK in v2.
- Crash-isolated. One process per layer (PM2), queue-backed (Redis Streams + BullMQ), idempotent, circuit-broken.
- Speed. Target
ws_recv → submit < 250ms, end-to-end fill 1–2s (old listener was 6–7s). - One TP/SL for both products — shared by the trading terminal and the copy-trade bot.
Pipeline
Polymarket WS → tt-ingest → tt-router → tt-executor → tt-confirm → register TP/SL (tt-tpsl / GUARD)
└── idempotency: (leaderTx, followerWallet, assetId)PM2 processes (Cherry, root)
Defined in ops/pm2/ecosystem.config.cjs — one process per layer, independent crash domains.
| Process | Package | Role |
|---|---|---|
tt-ingest | packages/ingest | Carried-over WS → normalized LeaderTradeEvent |
tt-bookfeed | packages/ingest (bookfeed.cjs) | CLOB book → mark.prices + bid/ask snapshot |
tt-router | packages/router | Match + sizing + parallel fanout + idempotency |
tt-executor | packages/executor | Execution workers over insiders-api |
tt-confirm | packages/confirm | Fill confirmation + TP/SL registration |
tt-tpsl | packages/tpsl (GUARD) | Standalone price-reactive rule engine (TP/SL, ladders, trailing) |
tt-enrichment | packages/enrichment | Read-only ClickHouse → Redis cache builder |
tt-perfmon | packages/perfmon | Performance-triggered rules (loss-stop / profit-scale) |
tt-control-api | apps/control-api | HTTP control surface :9110 |
packages/shared | — | Shared types, insiders-api client, db (prisma), redis, config, logger |
control-api · :9110
Source: /opt/tradingtools-v2/apps/control-api/src/index.ts
Exposed at tradingv2.polyinsiders.com via nginx (enabled 2026-06-01). Token-gated:
- All routes require
Authorization: Bearer <CONTROL_API_TOKEN>(64-hex, set in/opt/tradingtools-v2/.env). /health+/readystay unauthed for nginx/pm2 probes.- The two internal endpoints (
/subscriptions/funding,/subscriptions/:id/perf-action) are edge-denied (404) on top of the token. - Identity is body-asserted (caller states
followerUserId/WalletId/Proxy), so token + network-reach = full impersonation — only the Insiders backend proxy should call it.
Full route docs:
- Copy Trading Routes — subscription management
- TPSL Routes — TP/SL triggers (GUARD)
Quick reference
| Method | Path | Category |
|---|---|---|
| POST | /subscriptions | Subscription |
| GET | /subscriptions?followerProxy=0x… | Subscription |
| GET | /subscriptions/:id | Subscription |
| GET | /subscriptions/:id/fills | Subscription |
| PATCH | /subscriptions/:id | Subscription |
| POST | /subscriptions/:id/pause | Subscription |
| POST | /subscriptions/:id/resume | Subscription |
| DELETE | /subscriptions/:id | Subscription |
| POST | /subscriptions/funding | Internal (edge-denied) |
| POST | /subscriptions/:id/perf-action | Internal (edge-denied) |
| POST | /triggers | TPSL (GUARD) |
| PATCH | /triggers/:id | TPSL (GUARD) |
| DELETE | /triggers/:id | TPSL (GUARD) |
| GET | /triggers?wallet=0x… | TPSL (GUARD) |
| GET | /triggers/:id | TPSL (GUARD) |
| GET | /health | Infra |
| GET | /ready | Infra |
Data stores
- PostgreSQL (
db/schema.prisma) — durable: subscriptions, copy_fills, tpsl_triggers, audit_log - Redis — hot subscription index, BullMQ queues, dedupe (SETNX), trigger set, breaker state
- No SQLite.
Build & ops
| Command | Purpose |
|---|---|
pnpm build | esbuild bundle → dist/main.cjs per layer (see scripts/build.mjs) |
pnpm typecheck | tsc -b gate |
pnpm pm2:start / pnpm pm2:stop | pm2 start/delete ops/pm2/ecosystem.config.cjs |
Key docs in codebase (/opt/tradingtools-v2/docs)
| File | Contents |
|---|---|
control_api_wire_spec_v1.md | Full wire-true API spec (captured live 2026-05-30) |
control_api_nginx_handoff_v1.md | Edge-exposure / TLS / IP-allowlist handoff |
tpsl_control_api_v1.md | GUARD TPSL full schema |
migration_dryrun_2026-05-30/cutover_report_2026-05-31.md | v1→v2 cutover (212 subs) |
v1_subs_migration_plan_v1.md | V1→V2 subscription migration plan |
TRADINGTOOLS_V2_ARCHITECTURE.md | System architecture overview (repo root) |