|
|
@@ -26,9 +26,10 @@ import type { EngineStatus, EngineState, RebalanceResult } from './types'
|
|
|
const GAS_RESERVE_MON = 0.5
|
|
|
const ERROR_COOLDOWN_MS = 30_000 // 30s cooldown after error
|
|
|
|
|
|
-export class RebalancerEngine {
|
|
|
- private static instance: RebalancerEngine | null = null
|
|
|
+// Use globalThis to survive HMR in dev mode
|
|
|
+const GLOBAL_KEY = '__lfj_rebalancer_engine__' as const
|
|
|
|
|
|
+export class RebalancerEngine {
|
|
|
private status: EngineStatus = 'idle'
|
|
|
private cooldownUntil = 0
|
|
|
private errors: string[] = []
|
|
|
@@ -36,13 +37,20 @@ export class RebalancerEngine {
|
|
|
private running = false
|
|
|
private stopRequested = false
|
|
|
|
|
|
- private constructor() {}
|
|
|
+ private constructor() {
|
|
|
+ // Restore status from DB on creation
|
|
|
+ const saved = getEngineState('status')
|
|
|
+ if (saved === 'running' || saved === 'paused') {
|
|
|
+ this.status = saved as EngineStatus
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
static getInstance(): RebalancerEngine {
|
|
|
- if (!RebalancerEngine.instance) {
|
|
|
- RebalancerEngine.instance = new RebalancerEngine()
|
|
|
+ const g = globalThis as unknown as Record<string, RebalancerEngine>
|
|
|
+ if (!g[GLOBAL_KEY]) {
|
|
|
+ g[GLOBAL_KEY] = new RebalancerEngine()
|
|
|
}
|
|
|
- return RebalancerEngine.instance
|
|
|
+ return g[GLOBAL_KEY]
|
|
|
}
|
|
|
|
|
|
start(): void {
|
|
|
@@ -126,7 +134,7 @@ export class RebalancerEngine {
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
- status: this.status,
|
|
|
+ status: this.running ? 'running' : this.status,
|
|
|
currentActiveId,
|
|
|
currentPrice,
|
|
|
positionMinBin: position?.minBin ?? null,
|