This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Deriv Bot is a Progressive Web Application (PWA) for building, testing, and running automated trading strategies on the Deriv platform. Users create trading bots using a visual block-based programming interface powered by Blockly, without needing to write code.
- Repository: https://github.com/deriv-com/bot
- License: MIT License (Copyright 2024 deriv.com)
- Node.js Version: 20.x (required)
- React 18.2.0 with TypeScript 5.5.3
- MobX 6.12.3 for state management (with mobx-react-lite)
- Rsbuild 1.0.1-beta.1 for builds (replacing webpack, though webpack config still exists)
- Blockly 10.4.3 for visual programming blocks
- @deriv/deriv-api 1.0.15 for trading API (WebSocket-based)
- @deriv/js-interpreter 3.0.0 for safe bot code execution
- @deriv/deriv-charts 2.9.1 for trading charts (SmartCharts)
- @deriv-com/auth-client 1.5.1 for OIDC authentication
- @tanstack/react-query 5.29.2 for server state management
npm run start # Start dev server on https://localhost:8443 (auto-opens browser)
npm run build # Production build to dist/
npm run watch # Build in watch mode
npm run serve # Serve production build locally
npm run build:analyze # Build with bundle analyzernpm test # Run Jest tests
npm run coverage # Run tests with coverage report
npm run test:lint # Run ESLint and Prettier checks
npm run test:fix # Auto-fix linting issues with ESLint and Prettiernpm run start:webpack # Start webpack dev server
npm run build:webpack # Build with webpackThe application uses a centralized RootStore pattern that orchestrates 22 domain-specific stores:
// Core stores
- RootStore (src/stores/root-store.ts) - Main orchestrator
- AppStore - Application-level state
- ClientStore - User/account state
- UiStore - UI state
- CommonStore - Shared state
// Feature stores
- BlocklyStore - Blockly workspace state
- RunPanelStore - Bot execution controls
- QuickStrategyStore - Quick strategy builder
- JournalStore - Trading journal/logs
- TransactionsStore - Transaction history
- ChartStore - Trading chart state
- DashboardStore - Dashboard/bot list
- LoadModalStore, SaveModalStore - Bot file operations
- GoogleDriveStore - Google Drive integration
- ToolbarStore, ToolboxStore, FlyoutStore, FlyoutHelpStore - Blockly UI
- SummaryCardStore - Trade summary
- SelfExclusionStore - Self-exclusion limits
- DataCollectionStore - Analytics dataKey patterns:
- Stores are dependency-injected via React Context (use
useStore()hook) - Components observe stores via
observer()from mobx-react-lite - RootStore receives
dbotinstance (the bot engine) at construction - Stores receive both
root_storeandcore(shortcut to ui/client/common)
Located in src/external/bot-skeleton/, this is the core trading bot engine:
DBot Class (scratch/dbot.js):
- Main bot controller that initializes and manages the Blockly workspace
- Holds reference to
interpreter(JS-Interpreter instance) - Manages bot execution state (
is_bot_running) - Handles workspace initialization, XML loading, and bot lifecycle
Blockly Integration (scratch/):
- Custom block definitions in
scratch/blocks/(Binary, Logic, Math, Text, Advanced, etc.) - Block XML templates in
scratch/xml/ - Workspace utilities in
scratch/utils/ - Blockly hooks in
scratch/hooks/ - Custom
loadBlockly()function initializes Blockly with custom blocks
Trade Engine (services/tradeEngine/):
- Separate execution engine that runs bot strategies
- Handles contract proposals, purchases, and trade execution
- Integrates with Deriv API via WebSocket
- Manages tick subscriptions and real-time market data
API Services (services/api/):
api-base.js- Core API service managementApiHelpers- Helper methods for API operations- Observer pattern for API event handling
- Tick service for real-time data streaming
Strategy Templates:
- Pre-built strategies (Martingale, D'Alembert, Oscar's Grind)
- Quick strategy builder in
src/pages/bot-builder/quick-strategy/ - XML-based strategy definitions in
src/xml/
Lazy Loading:
- Pages and heavy components use
React.lazy()for code-splitting - Suspense boundaries with custom
ChunkLoaderfor loading states - Global
ErrorBoundarycomponent for error handling
Key Pages (src/pages/):
main/- Main pagedashboard/- Bot list and managementbot-builder/- Visual bot builder (Blockly workspace)quick-strategy/- Quick strategy buildertoolbar/- Workspace toolbartoolbox/- Block toolbox
chart/- Trading charts (SmartCharts integration)tutorials/- Tutorial pagesendpoint/- Endpoint configurationcallback/- OAuth callback handler
Major Component Groups (src/components/):
layout/- Header, footer, body layout componentsjournal/- Trading journal and logstransactions/- Transaction history UIrun-panel/- Bot run controlsload-modal/- Load bot modaltrade-animation/- Trade execution animationsshared/andshared_ui/- Reusable components
- OIDC/OAuth2 via
@deriv-com/auth-client - Token stored in LocalStorage
AuthWrappercomponent handles authentication stateuseInvalidTokenHandlerhook auto-re-authenticates on expiry- Multi-account support with currency validation
- TMB (Trading Management Backend) integration for feature flags
- Service worker:
public/sw.js - Browser-specific behavior (Chrome vs Firefox/Safari)
- Offline detection via
useOfflineDetectionhook - Cache-first for assets, network-first for API
- Manifest:
public/manifest.jsonwith full installability support - PWA utilities in
src/utils/pwa-utils.ts
- Modern routing with
createBrowserRouter - Nested routes with
Layoutcomponent andOutlet - Protected routes with authentication checks
- Currency validation and account checks
@/external → src/external
@/components → src/components
@/hooks → src/hooks
@/utils → src/utils
@/constants → src/constants
@/stores → src/stores
@/pages → src/pages- Primary: Rsbuild (modern, faster)
- Legacy: Webpack (still available, being phased out)
- Entry point:
src/main.tsx - Output:
dist/ - Dev server: Port 8443 with HTTPS enabled
- Asset copying: SmartCharts assets, fonts, shaders, public files
Configure via .env or CI/CD (15+ variables for feature flags and API keys):
TRANSLATIONS_CDN_URL,R2_PROJECT_NAME,CROWDIN_BRANCH_NAME- TranslationsTRACKJS_TOKEN,APP_ENV,REF_NAME- Environment/trackingREMOTE_CONFIG_URL- Remote configGD_CLIENT_ID,GD_APP_ID,GD_API_KEY- Google DriveDATADOG_*- Datadog RUM configurationRUDDERSTACK_KEY- AnalyticsGROWTHBOOK_CLIENT_KEY,GROWTHBOOK_DECRYPTION_KEY- Feature flags
- Commit convention: Conventional Commits enforced by commitlint
- Allowed types: feat, fix, chore, docs, test, refactor, perf, ci, build, hotfix, revert, style, redeploy, translations
- Git hooks (via Husky):
commit-msg- Validates commit messagespost-merge- Post-merge actions
- Lint-staged: Auto-formats files on commit
- TypeScript: Strict mode enabled
- ESLint: React, React Hooks, TypeScript rules
- Prettier: 120 char width, single quotes, 4-space indent
- Stylelint: BEM pattern, browser compatibility checks
- Jest 29.7.0 with ts-jest
- @testing-library/react for component testing
- jsdom environment for browser-like tests
- Coverage collected automatically
- Path aliases configured in
jest.config.ts - Transform patterns for TypeScript, JavaScript, and XML files
- CDN-based via Crowdin (
@deriv-com/translations) - Branch-specific translations per environment
- Loaded dynamically at runtime
- Sync workflow via GitHub Actions
- Primary hosting: Cloudflare Pages
- Disaster Recovery: Vercel
- Environments: production, staging, test
- Production trigger: Tags matching
production_v* - Staging/Test trigger: Branch pushes
- Blockly for Visual Programming: Enables no-code bot creation for non-technical traders
- MobX over Redux: Simpler, more flexible for complex trading logic and real-time updates
- Rsbuild over Webpack: Modern, faster build tool (migration in progress)
- PWA-first: Offline support critical for trading application reliability
- Separate Bot Engine:
bot-skeletonas independent trading engine with own architecture - WebSocket Real-time: Live market data and trade execution require persistent connections
- Multi-environment: Strict separation with feature flags and environment-specific configs
- OIDC Authentication: Standards-based auth with multi-account support
- The codebase is in transition from Webpack to Rsbuild (both configs exist)
- Some stores and utilities are JavaScript (
.js) not TypeScript yet __webpack_public_path__global assignment exists for dynamic loading- Service worker behavior differs between Chrome and Firefox/Safari
- XML files for blocks are loaded via
raw-loadercustom transform - SmartCharts assets require special copy configuration
- Development requires HTTPS (self-signed cert via
@rsbuild/plugin-basic-ssl)