Use bun for web3proxy and webui

This commit is contained in:
2025-12-13 17:46:25 +07:00
parent c4e444347c
commit 588927678c
16 changed files with 6692 additions and 11326 deletions

View File

@@ -1,189 +0,0 @@
Todo List
Phase 1: Keep TradingBotBase Unchanged (Composition Approach) ✅ COMPLETE
[✅] File: src/Managing.Application/Bots/TradingBotBase.cs
[✅] Keep class as concrete (not abstract)
[✅] No Orleans-specific methods needed
[✅] Preserve all existing functionality
[✅] Ensure it remains reusable for direct instantiation and Orleans composition
Phase 2: Create Orleans Wrapper Grains (Composition)
[✅] File: src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs
[✅] Inherit from Grain<TradingBotGrainState> and implement ITradingBotGrain
[✅] Use composition: private TradingBotBase _tradingBot
[✅] Implement Orleans lifecycle methods (OnActivateAsync, OnDeactivateAsync)
[✅] Delegate trading operations to _tradingBot instance
[✅] Handle Orleans timer management for bot execution
[✅] Implement state persistence between grain state and TradingBotBase
[✅] Add configuration validation for live trading
[✅] Implement all ITradingBotGrain methods as wrappers
[✅] File: src/Managing.Application/Bots/Grains/BacktestTradingBotGrain.cs
[✅] Inherit from Grain<TradingBotGrainState> and implement IBacktestTradingBotGrain
[✅] Use composition: private TradingBotBase _tradingBot
[✅] Implement Orleans lifecycle methods for backtest execution
[✅] Delegate trading operations to _tradingBot instance
[✅] Handle backtest-specific candle processing (no timer)
[✅] Implement state persistence for backtest scenarios
[✅] Add configuration validation for backtesting
[✅] Implement all ITradingBotGrain methods as wrappers
[✅] Add backtest-specific methods: RunBacktestAsync, GetBacktestProgressAsync (following GetBacktestingResult pattern)
[✅] Stateless design - no state persistence, fresh TradingBotBase instance per backtest
[✅] Simplified interface - Start/Stop are no-ops, other methods throw exceptions for backtest mode
[✅] StatelessWorker attribute - grain doesn't inherit from Grain<T> but implements interface
[✅] Config passed as parameter - no state dependency, config passed to RunBacktestAsync method
[✅] **NEW: Orleans Serialization Support**
[✅] Return LightBacktest instead of Backtest for safe Orleans serialization
[✅] Add ConvertToLightBacktest method to map Backtest to LightBacktest
[✅] Handle type conversions (decimal? to double? for SharpeRatio, etc.)
[✅] Ensure all properties are Orleans-serializable
[✅] File: src/Managing.Domain/Backtests/LightBacktest.cs
[✅] **NEW: Add Orleans Serialization Attributes**
[✅] Add [GenerateSerializer] attribute for Orleans serialization
[✅] Add [Id(n)] attributes to all properties for proper serialization
[✅] Add using Orleans; statement
[✅] Ensure all property types are Orleans-serializable
[✅] Match property types with LightBacktestResponse for consistency
[✅] File: src/Managing.Application.Abstractions/Grains/IBacktestTradingBotGrain.cs
[✅] **NEW: Update Interface for LightBacktest**
[✅] Change RunBacktestAsync return type from Backtest to LightBacktest
[✅] Update method documentation to reflect LightBacktest usage
[✅] Ensure interface is Orleans-compatible
[✅] File: src/Managing.Application/Backtesting/Backtester.cs
[✅] Inject IGrainFactory dependency
[✅] Update RunBacktestWithCandles to use Orleans grain instead of direct bot creation
[✅] Remove GetBacktestingResult method (logic moved to grain)
[✅] Remove helper methods (AggregateValues, GetIndicatorsValues) - moved to grain
[✅] Simplified backtesting flow - Backtester orchestrates, grain executes
[✅] Fixed Orleans serialization issue - CreateCleanConfigForOrleans method removes FixedSizeQueue objects
[✅] Created LightIndicator and LightScenario classes for Orleans serialization
[✅] Updated TradingBotConfig to use LightScenario instead of Scenario
[✅] Simplified serialization - no more FixedSizeQueue or User properties in Orleans data
[✅] Updated all application code to use LightScenario conversions
[✅] Main application builds successfully with Orleans integration
[✅] **NEW: Update for LightBacktest Integration**
[✅] Update interface to return LightBacktest instead of Backtest
[✅] Update RunTradingBotBacktest methods to return LightBacktest
[✅] Remove conversion methods (no longer needed)
[✅] Simplify Orleans grain calls to return LightBacktest directly
[✅] Update all dependent services to work with LightBacktest
[✅] File: src/Managing.Application.Abstractions/Services/IBacktester.cs
[✅] **NEW: Update Interface for LightBacktest**
[✅] Change main backtest methods to return LightBacktest
[✅] Keep full Backtest methods for database retrieval
[✅] Update method documentation for LightBacktest usage
[✅] Ensure backward compatibility where needed
[✅] File: src/Managing.Api/Controllers/BacktestController.cs
[✅] **NEW: Update Controller for LightBacktest**
[✅] Update Run method to return LightBacktest instead of Backtest
[✅] Update method documentation to explain LightBacktest usage
[✅] Remove unused notification method (handled in Orleans grain)
[✅] Update variable declarations and return statements
[✅] Ensure API responses are consistent with LightBacktest structure
[✅] File: src/Managing.Application.Workers/StatisticService.cs
[✅] **NEW: Update for LightBacktest Compatibility**
[✅] Update GetSignals method to handle LightBacktest (no signals data)
[✅] Add warning log when signals data is not available
[✅] Return empty list for signals (full data available via database lookup)
[✅] File: src/Managing.Application/GeneticService.cs
[✅] **NEW: Update for LightBacktest Compatibility**
[✅] Update TradingBotFitness.Evaluate to work with LightBacktest
[✅] Update CalculateFitness method to accept LightBacktest
[✅] Ensure genetic algorithm works with lightweight backtest data
[ ] File: src/Managing.Application/Bots/Grains/TradingBotGrainProxy.cs
[ ] Fix remaining test compilation errors (6 scenario conversion errors in BotsTests.cs)
[ ] Create proxy class that implements ITradingBot interface
[ ] Wrap Orleans grain calls for seamless integration
[ ] Maintain compatibility with existing ITradingBot consumers
[ ] Handle async/await conversion between Orleans and synchronous calls
Phase 3: Update BotService for Conditional Instantiation
[ ] File: src/Managing.Application/ManageBot/BotService.cs
[ ] Remove _botTasks dictionary (replaced by Orleans grain management)
[ ] Remove BotTaskWrapper class (no longer needed)
[ ] Inject IGrainFactory for Orleans grain creation
[ ] Update CreateTradingBot() with conditional logic:
[ ] If IsForBacktest: return new TradingBotBase() (direct instantiation)
[ ] If live trading: return new TradingBotGrainProxy(grain) (Orleans wrapper)
[ ] Update CreateBacktestTradingBot() with same conditional logic
[ ] Update all bot management methods to work with both direct and grain instances
[ ] Use Guid for grain identification
Phase 4: Update Orleans Interface and State
[ ] File: src/Managing.Application.Abstractions/Grains/ITradingBotGrain.cs
[ ] Update to use IGrainWithGuidKey
[ ] Add InitializeAsync(TradingBotConfig config) method
[ ] Add RestartAsync() method
[ ] Add DeleteAsync() method
[ ] Add GetBotDataAsync() method
[ ] Ensure all methods are async and Orleans-compatible
[ ] File: src/Managing.Application/Bots/TradingBotGrainState.cs
[ ] Ensure all properties are Orleans-serializable
[ ] Add methods for state synchronization with TradingBotBase
[ ] Implement backup/restore functionality
[ ] Add validation for state consistency
Phase 5: Update Dependencies and Configuration
[ ] File: src/Managing.Bootstrap/ApiBootstrap.cs
[ ] Register Orleans grains (LiveTradingBotGrain, BacktestTradingBotGrain)
[ ] Keep existing bot service registrations for backward compatibility
[ ] Add grain factory registration
[ ] Configure Orleans clustering and persistence
Phase 6: Testing and Validation
[ ] Test direct TradingBotBase instantiation (backtesting)
[ ] Test LiveTradingBotGrain functionality (live trading)
[ ] Test BacktestTradingBotGrain functionality (Orleans backtesting)
[ ] Test BotService conditional instantiation
[ ] Test Orleans reminder functionality
[ ] Test grain lifecycle management
[ ] Test state persistence and recovery
[ ] Test TradingBotGrainProxy compatibility
[✅] **NEW: Test LightBacktest Serialization**
[✅] Verify Orleans serialization works correctly
[✅] Test LightBacktest to Backtest conversion (if needed)
[✅] Verify API responses with LightBacktest data
[✅] Test genetic algorithm with LightBacktest
Benefits of Composition Approach
✅ TradingBotBase remains concrete and reusable
✅ No Orleans-specific code in core trading logic
✅ Backward compatibility maintained
✅ Clean separation of concerns
✅ Easier testing and maintenance
✅ Follows SOLID principles
✅ Flexible architecture for future changes
**NEW: Orleans Serialization Benefits**
✅ LightBacktest provides efficient serialization
✅ Reduced memory footprint for Orleans communication
✅ Safe type serialization with GenerateSerializer attributes
✅ Consistent data structure across Orleans grains and API responses
Implementation Order
Phase 1: Keep TradingBotBase unchanged (preserve existing functionality) ✅ COMPLETE
Phase 2: Create Orleans wrapper grains (composition approach) ✅ COMPLETE
Phase 3: Update BotService for conditional instantiation
Phase 4: Update Orleans interface and state management
Phase 5: Update dependencies and configuration
Phase 6: Testing and validation
Current Status
✅ Orleans infrastructure setup
✅ TradingBotBase contains all core logic (keep as-is)
✅ LiveTradingBot.cs exists (will be replaced by grain)
✅ Phase 1 Complete - TradingBotBase ready for composition approach
✅ Phase 2 Complete - Orleans wrapper grains created and working
**NEW: LightBacktest Orleans Serialization Complete**
✅ BacktestTradingBotGrain returns LightBacktest for safe serialization
✅ All interfaces and services updated to use LightBacktest
✅ API controllers updated for LightBacktest responses
✅ Application builds successfully with Orleans integration
✅ Ready to start Phase 3 (update BotService for conditional instantiation)

View File

@@ -1,5 +1,5 @@
# Use an official Node.js image as the base
FROM node:22.14.0-alpine
# Use an official Bun image as the base
FROM oven/bun:1.3-debian
# Set the working directory in the container
WORKDIR /app
@@ -8,13 +8,13 @@ WORKDIR /app
COPY /src/Managing.Web3Proxy/package.json ./
# Install dependencies with the --legacy-peer-deps flag to bypass peer dependency conflicts
RUN npm install
# Install dependencies
RUN bun install
COPY src/Managing.Web3Proxy/ .
RUN npm run build
RUN bun run build
EXPOSE 4111
CMD ["npm", "run", "start"]
CMD ["bun", "run", "start"]

View File

@@ -2,12 +2,12 @@
The aim of this repository is to provide a concrete example of a Fastify application using what are considered best practices by the Fastify community.
**Prerequisites:** You need to have Node.js version 22 or higher installed.
**Prerequisites:** You need to have Bun version 1.3 or higher installed.
## Getting started
Install the dependencies:
```bash
npm install
bun install
```
### Database
@@ -23,54 +23,54 @@ docker compose up -d
To create and update the database schema, run the migrations:
```bash
npm run db:migrate
bun run db:migrate
```
To populate the database with initial data, run:
```bash
npm run db:seed
bun run db:seed
```
### TypeScript
To build the project:
```bash
npm run build
bun run build
```
### Start the server
In dev mode:
```bash
npm run dev
bun run dev
```
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
In production mode:
```bash
npm run start
bun run start
```
### Testing
To run the tests:
```bash
npm run test
bun run test
```
### Standalone
`dev` and `start` leverage [fastify-cli](https://github.com/fastify/fastify-cli),
but you can run the demo as a standalone executable (see [server.ts](./src/server.ts)):
```bash
npm run standalone
bun run standalone
```
### Linting
To check for linting errors:
```bash
npm run lint
bun run lint
```
To check and automatically fix linting errors:
```bash
npm run lint:fix
bun run lint:fix
```
## Learn More

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -11,14 +11,14 @@
"start": "fastify start -l info dist/app.js",
"build": "tsc",
"watch": "tsc -w",
"dev": "npm run build && concurrently -k -p \"[{name}]\" -n \"TypeScript,App\" -c \"yellow.bold,cyan.bold\" \"npm:watch\" \"npm:dev:start\"",
"dev:start": "npm run build && fastify start -d --ignore-watch=.ts$ -w -l info -P dist/app.js",
"test": "npm run db:seed && c8 npm run test:run",
"dev": "bun run build && concurrently -k -p \"[{name}]\" -n \"TypeScript,App\" -c \"yellow.bold,cyan.bold\" \"bun:watch\" \"bun:dev:start\"",
"dev:start": "bun run build && fastify start -d --ignore-watch=.ts$ -w -l info -P dist/app.js",
"test": "bun run db:seed && c8 bun run test:run",
"test:run": "tsx --test ./test/**/*.ts",
"test:single": "tsc && tsx --test",
"standalone": "npm run build && node --env-file=.env dist/server.js",
"standalone": "bun run build && node --env-file=.env dist/server.js",
"lint": "eslint --ignore-pattern=dist",
"lint:fix": "npm run lint -- --fix",
"lint:fix": "bun run lint -- --fix",
"db:create": "tsx --env-file=.env ./scripts/create-database.ts",
"db:drop": "tsx --env-file=.env ./scripts/drop-database.ts",
"db:migrate": "tsx --env-file=.env ./scripts/migrate.ts",

View File

@@ -55,11 +55,12 @@ export default async function serviceApp (
'Unhandled error occurred'
)
reply.code(err.statusCode ?? 500)
const statusCode = (err as any)?.statusCode ?? 500
reply.code(statusCode)
let message = 'Internal Server Error'
if (err.statusCode && err.statusCode < 500) {
message = err.message
if ((err as any)?.statusCode && (err as any).statusCode < 500) {
message = (err as any).message
}
return { message }

View File

@@ -11,8 +11,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
- uses: oven-sh/setup-bun@v1
with:
node-version: '18.1.0'
- run: yarn install
- run: yarn build
bun-version: '1.3'
- run: bun install
- run: bun run build

View File

@@ -11,8 +11,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
- uses: oven-sh/setup-bun@v1
with:
node-version: '18.1.0'
- run: yarn install
- run: yarn lint
bun-version: '1.3'
- run: bun install
- run: bun run lint

View File

@@ -1,18 +0,0 @@
name: Test
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
with:
node-version: '18.1.0'
- run: yarn install
- run: yarn test

View File

@@ -11,8 +11,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
- uses: oven-sh/setup-bun@v1
with:
node-version: '18.1.0'
- run: yarn install
- run: yarn typecheck
bun-version: '1.3'
- run: bun install
- run: bun run typecheck

View File

@@ -1,5 +1,5 @@
# Use an official Node.js image as the base
FROM node:18-alpine
# Use an official Bun image as the base
FROM oven/bun:1.3-debian
# Set the working directory in the container
WORKDIR /app
@@ -8,26 +8,26 @@ WORKDIR /app
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
# Install git and Python
RUN apk update && apk add --no-cache git python3 make g++
RUN apt-get update && apt-get install -y git python3 make g++ && rm -rf /var/lib/apt/lists/*
# Create a symlink for python3 as python
RUN ln -sf /usr/bin/python3 /usr/bin/python
# Copy package.json and package-lock.json to the container
# Copy package.json and bun.lockb to the container
# COPY package*.json ./
COPY /src/Managing.WebApp/package.json ./
# Install dependencies with the --legacy-peer-deps flag to bypass peer dependency conflicts
RUN npm install --legacy-peer-deps
RUN npm install -g tailwindcss postcss autoprefixer @tailwindcss/typography
# Install dependencies with bun
RUN bun install
RUN bun add -g tailwindcss postcss autoprefixer @tailwindcss/typography
# Copy the rest of the app's source code to the container
# COPY . .
COPY src/Managing.WebApp/ /app/
RUN node --max-old-space-size=8192 ./node_modules/.bin/vite build
RUN bun --bun ./node_modules/.bin/vite build
# Build the app
RUN npm run build
RUN bun run build
# Use NGINX as the web server
FROM nginx:alpine

View File

@@ -1,5 +1,5 @@
# Use an official Node.js image as the base
FROM node:22.14.0-alpine
# Use an official Bun image as the base
FROM oven/bun:1.3-debian
# Add build argument for cache busting
ARG CACHEBUST=1
@@ -11,23 +11,22 @@ WORKDIR /app
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
# Install git and Python
RUN apk update && apk add --no-cache git python3 make g++
RUN apt-get update && apt-get install -y git python3 make g++ && rm -rf /var/lib/apt/lists/*
# Create a symlink for python3 as python
# This might not be strictly necessary for your current issue but good to keep if Python scripts are involved.
# RUN ln -sf /usr/bin/python3 /usr/bin/python
# Copy package.json and package-lock.json to the container
# Copy package.json and bun.lockb to the container
# COPY package*.json ./
COPY /src/Managing.WebApp/package.json ./
# Use cache busting argument to force cache invalidation
RUN echo "Cache bust: $CACHEBUST"
# Install dependencies with the --legacy-peer-deps flag to bypass peer dependency conflicts
# Add --no-cache flag to prevent npm cache usage
RUN npm install --legacy-peer-deps --loglevel verbose --no-cache
RUN npm install -g tailwindcss postcss autoprefixer @tailwindcss/typography
# Install dependencies with bun
RUN bun install
RUN bun add -g tailwindcss postcss autoprefixer @tailwindcss/typography
# Copy the rest of the app's source code to the container
# COPY . .
@@ -35,7 +34,7 @@ RUN ls -la
COPY src/Managing.WebApp/ .
# Build the app
RUN npm run build
RUN bun run build
# Use NGINX as the web server
FROM nginx:alpine

4486
src/Managing.WebApp/bun.lock Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,35 +0,0 @@
const config = {
collectCoverageFrom: ['<rootDir>/src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'],
moduleDirectories: ['node_modules'],
moduleFileExtensions: ['js', 'mjs', 'jsx', 'ts', 'tsx', 'json'],
moduleNameMapper: {
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
},
notify: true,
notifyMode: 'success-change',
resetMocks: true,
roots: ['<rootDir>'],
setupFilesAfterEnv: ['<rootDir>/jest/setupTests.ts'],
testEnvironment: 'jsdom',
testMatch: [
'<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}',
'<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
],
transform: {
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'<rootDir>/jest/fileTransform.js',
'^.+\\.[jt]sx?$': 'esbuild-jest',
'^.+\\.css$': '<rootDir>/jest/cssTransform.js',
},
transformIgnorePatterns: [
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$',
'^.+\\.module\\.(css|sass|scss)$',
],
verbose: true,
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
}
module.exports = config

View File

@@ -3,16 +3,15 @@
"version": "2.0.0",
"license": "MIT",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"dev": "bunx --bun vite",
"build": "bunx --bun vite build",
"preview": "bunx --bun vite preview",
"serve": "serve -s dist -p 3000",
"test": "jest",
"lint": "eslint . --ext .ts,.tsx,.js,jsx",
"lint:fix": "eslint . --ext .ts,.tsx,.js,jsx --fix",
"typecheck": "tsc --noEmit",
"prettier": "prettier --write \"**/*.+(json|yml|css|md|mdx)\"",
"clean": "rimraf node_modules yarn.lock dist",
"clean": "rimraf node_modules bun.lock dist",
"validate": "./scripts/validate"
},
"dependencies": {
@@ -40,7 +39,7 @@
"genetic-js": "^0.1.14",
"jotai": "^1.6.7",
"latest-version": "^9.0.0",
"lightweight-charts": "git+https://github.com/ntf/lightweight-charts.git",
"lightweight-charts": "^4.2.2",
"moment": "^2.29.3",
"plotly.js": "^2.18.1",
"postcss": "^8.4.13",