How marketcalls/opencase works
1. Purpose and Runtime
OpenCase is a self-hosted stock-basket platform: users authenticate, connect Zerodha or Angel One accounts, build weighted baskets, place/rebalance investments, and inspect broker/OpenCase holdings. It runs as a TypeScript Hono application targeted at Cloudflare Pages/Workers, with a Vite-built vanilla-JS UI.
• src/index.tsx
• src/renderer.tsx
• wrangler.jsonc
2. Application Entry and HTTP Composition
src/index.tsx is the main application entry: it serves HTML/UI routes and mounts the domain API handlers. Route modules form the backend boundary for setup, identity, broker accounts, baskets, instruments, investments, portfolio, SIPs, and alerts. The browser client consumes these APIs rather than using a separate SPA backend.
• src/index.tsx
• src/routes/baskets.ts
• public/static/app.js
3. Identity, Sessions, and Platform Setup
User signup/login/profile handling is separated from legacy broker-oriented auth. The first user may become admin; sessions are stored in Cloudflare KV. Setup endpoints manage platform-level broker configuration/default selection, while broker-account endpoints manage per-user linked accounts and encrypted credentials.
• src/routes/user.ts
• src/routes/setup.ts
• src/routes/broker-accounts.ts
4. Broker Abstraction and External Execution Boundary
A broker factory selects a common broker interface for Zerodha and Angel One. Adapter implementations normalize authentication, holdings, market data, and order-facing behavior despite different OAuth/TOTP and API protocols. This is the principal boundary between OpenCase’s unified domain model and external broker services.
• src/brokers/base.ts
• src/brokers/factory.ts
• src/brokers/zerodha.ts
• src/brokers/angelone.ts
5. Instrument Master and Market Data Flow
Instrument routes download broker catalogs, merge them into unified symbols while retaining broker-specific tokens, and support stock/index search and LTP lookup. Broker API clients provide the vendor-specific transport layer. Unified instruments enable basket composition, benchmarks, and portable holdings mapping.
• src/routes/instruments.ts
• src/lib/kite.ts
• src/lib/angelone.ts
• migrations/0002_master_instruments.sql
• migrations/0003_unified_symbols.sql
6. Basket-to-Investment and Portfolio Flow
Users define baskets with constituent weights; investment operations translate a basket into broker order actions through the active connected account. Portfolio routes aggregate persisted OpenCase investments with normalized live broker holdings for P&L, comparison, and rebalancing. SIPs and alerts are adjacent lifecycle workflows; scheduled execution is isolated from request handling.
• src/routes/baskets.ts
• src/routes/investments.ts
• src/routes/portfolio.ts
• src/scheduled.ts
7. Persistence and Deployment Boundaries
Cloudflare D1 (SQLite) persists users, broker links, instruments, baskets, investments, and related records; migrations establish the evolving schema and seed data supplies templates. Cloudflare KV holds sessions. Docker Compose provides a local/container deployment path, while Wrangler configures production Cloudflare bindings.
• migrations/0001_initial_schema.sql
• migrations/0004_user_auth.sql
• migrations/0005_investment_broker_link.sql
• docker-compose.yml
• wrangler.jsonc
Main entry points: index.tsx Hono app entry · 0001_initial_schema.sql Cloudflare D1 · factory.ts Broker factory · user.ts User & session APIs
Components 19
Browser UI
- Vanilla JS UI · Consumes the application APIs.
public/static/app.js
Cloudflare Application
- Hono app entry · Serves UI routes and mounts domain APIs.
src/index.tsx - HTML renderer
src/renderer.tsx - User & session APIs · Handles signup, login, profiles, and initial admin assignment.
src/routes/user.ts - Platform setup APIs · Manages broker configuration and defaults.
src/routes/setup.ts - Broker account APIs · Manages per-user broker links and encrypted credentials.
src/routes/broker-accounts.ts - Basket APIs · Defines weighted basket constituents.
src/routes/baskets.ts - Investment APIs · Turns basket operations into broker order actions.
src/routes/investments.ts - Portfolio APIs · Combines persisted investments with live normalized holdings.
src/routes/portfolio.ts - Instrument APIs · Imports catalogs, resolves unified symbols, searches, and fetches LTP.
src/routes/instruments.ts - Scheduled lifecycle jobs · Runs SIP and alert workflows outside requests.
src/scheduled.ts
Broker Integration
- Broker factory · Selects the common interface for the active linked broker.
src/brokers/factory.ts - Common broker interface · Normalizes authentication, holdings, market data, and orders.
src/brokers/base.ts - Zerodha adapter
src/brokers/zerodha.ts - Angel One adapter
src/brokers/angelone.ts - Broker API clients · Implements vendor-specific catalog and market-data transport.
src/lib/kite.ts
Persistence & Deployment
- Cloudflare D1 · Stores users, links, instruments, baskets, investments, and records.
migrations/0001_initial_schema.sql - Cloudflare KV
- Cloudflare bindings · Configures production Worker and D1/KV bindings.
wrangler.jsonc
Connections 26
- Vanilla JS UI → Hono app entry: HTTP routes
- Hono app entry → HTML renderer: renders HTML
- Hono app entry → User & session APIs: mounts
- Hono app entry → Platform setup APIs: mounts
- Hono app entry → Broker account APIs: mounts
- Hono app entry → Basket APIs: mounts
- Hono app entry → Investment APIs: mounts
- Hono app entry → Portfolio APIs: mounts
- Hono app entry → Instrument APIs: mounts
- User & session APIs → Cloudflare KV: stores sessions
- User & session APIs → Cloudflare D1: persists users
- Platform setup APIs → Cloudflare D1: persists configuration
- Broker account APIs → Cloudflare D1: persists broker links
- Basket APIs → Cloudflare D1: persists baskets
- Investment APIs → Broker factory: executes through
- Investment APIs → Cloudflare D1: persists investments
- Portfolio APIs → Cloudflare D1: reads investments
- Portfolio APIs → Broker factory: fetches live holdings
- Instrument APIs → Broker API clients: downloads catalogs and LTP
- Instrument APIs → Cloudflare D1: merges unified instruments
- Broker factory → Common broker interface: returns contract
- Common broker interface → Zerodha adapter: implemented by
- Common broker interface → Angel One adapter: implemented by
- Scheduled lifecycle jobs → Cloudflare D1: processes lifecycle data
- Scheduled lifecycle jobs → Broker factory: executes scheduled actions
- Cloudflare bindings → Hono app entry: deploys and binds