forked from hummingbot/hummingbot-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
395 lines (331 loc) · 15.4 KB
/
main.py
File metadata and controls
395 lines (331 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import logging
import secrets
from contextlib import asynccontextmanager
from typing import Annotated
from urllib.parse import urlparse
import logfire
from dotenv import load_dotenv
# Apply the patch before importing hummingbot components
from hummingbot.client.config import config_helpers
# Load environment variables early
load_dotenv()
VERSION = "1.0.1"
# Monkey patch save_to_yml to prevent writes to library directory
def patched_save_to_yml(yml_path, cm):
"""Patched version of save_to_yml that prevents writes to library directory"""
import logging
logger = logging.getLogger(__name__)
logger.debug(f"Skipping config write to {yml_path} (patched for API mode)")
# Do nothing - this prevents the original function from trying to write to the library directory
config_helpers.save_to_yml = patched_save_to_yml
from fastapi import Depends, FastAPI, HTTPException, Request, status # noqa: E402
from fastapi.exceptions import RequestValidationError # noqa: E402
from fastapi.middleware.cors import CORSMiddleware # noqa: E402
from fastapi.responses import JSONResponse # noqa: E402
from fastapi.security import HTTPBasic, HTTPBasicCredentials # noqa: E402
from hummingbot.client.config.client_config_map import GatewayConfigMap # noqa: E402
from hummingbot.client.config.config_crypt import ETHKeyFileSecretManger # noqa: E402
from hummingbot.core.gateway.gateway_http_client import GatewayHttpClient # noqa: E402
from hummingbot.core.rate_oracle.rate_oracle import RATE_ORACLE_SOURCES, RateOracle # noqa: E402
from config import settings # noqa: E402
from database import AsyncDatabaseManager # noqa: E402
from routers import ( # noqa: E402
accounts,
archived_bots,
backtesting,
bot_orchestration,
connectors,
controllers,
docker,
executors,
gateway,
gateway_clmm,
gateway_proxy,
gateway_swap,
market_data,
portfolio,
rate_oracle,
scripts,
trading,
)
from services.accounts_service import AccountsService # noqa: E402
from services.bots_orchestrator import BotsOrchestrator # noqa: E402
from services.docker_service import DockerService # noqa: E402
from services.executor_service import ExecutorService # noqa: E402
from services.gateway_service import GatewayService # noqa: E402
from services.market_data_service import MarketDataService # noqa: E402
from services.trading_service import TradingService # noqa: E402
from services.unified_connector_service import UnifiedConnectorService # noqa: E402
from utils.bot_archiver import BotArchiver # noqa: E402
from utils.security import BackendAPISecurity # noqa: E402
# Set up logging configuration
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Enable info logging for MQTT manager
logging.getLogger('services.mqtt_manager').setLevel(logging.INFO)
# Get settings from Pydantic Settings
username = settings.security.username
password = settings.security.password
debug_mode = settings.security.debug_mode
# Security setup
security = HTTPBasic()
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Lifespan context manager for the FastAPI application.
Handles startup and shutdown events.
"""
# Ensure password verification file exists
if BackendAPISecurity.new_password_required():
# Create secrets manager with CONFIG_PASSWORD
secrets_manager = ETHKeyFileSecretManger(password=settings.security.config_password)
BackendAPISecurity.store_password_verification(secrets_manager)
logging.info("Created password verification file for master_account")
# =========================================================================
# 1. Infrastructure Setup
# =========================================================================
# Initialize GatewayHttpClient singleton
parsed_gateway_url = urlparse(settings.gateway.url)
gateway_config = GatewayConfigMap(
gateway_api_host=parsed_gateway_url.hostname or "localhost",
gateway_api_port=str(parsed_gateway_url.port or 15888),
gateway_use_ssl=parsed_gateway_url.scheme == "https"
)
GatewayHttpClient.get_instance(gateway_config)
logging.info(f"Initialized GatewayHttpClient with URL: {settings.gateway.url}")
# Initialize secrets manager and database
secrets_manager = ETHKeyFileSecretManger(password=settings.security.config_password)
db_manager = AsyncDatabaseManager(settings.database.url)
await db_manager.create_tables()
logging.info("Database initialized")
# Read rate oracle configuration from conf_client.yml
from utils.file_system import FileSystemUtil
fs_util = FileSystemUtil()
try:
conf_client_path = "credentials/master_account/conf_client.yml"
config_data = fs_util.read_yaml_file(conf_client_path)
# Get rate_oracle_source configuration
rate_oracle_source_data = config_data.get("rate_oracle_source", {})
source_name = rate_oracle_source_data.get("name", "binance")
# Get global_token configuration
global_token_data = config_data.get("global_token", {})
quote_token = global_token_data.get("global_token_name", "USDT")
# Create rate source instance
if source_name in RATE_ORACLE_SOURCES:
rate_source = RATE_ORACLE_SOURCES[source_name]()
logging.info(f"Configured RateOracle with source: {source_name}, quote_token: {quote_token}")
else:
logging.warning(f"Unknown rate oracle source '{source_name}', defaulting to binance")
rate_source = RATE_ORACLE_SOURCES["binance"]()
source_name = "binance"
# Initialize RateOracle with configured source and quote token
rate_oracle = RateOracle.get_instance()
rate_oracle.source = rate_source
rate_oracle.quote_token = quote_token
except FileNotFoundError:
logging.warning("conf_client.yml not found, using default RateOracle configuration (binance, USDT)")
rate_oracle = RateOracle.get_instance()
except Exception as e:
logging.warning(f"Error reading conf_client.yml: {e}, using default RateOracle configuration")
rate_oracle = RateOracle.get_instance()
# =========================================================================
# 2. UnifiedConnectorService - Single source of truth for all connectors
# =========================================================================
connector_service = UnifiedConnectorService(
secrets_manager=secrets_manager,
db_manager=db_manager
)
logging.info("UnifiedConnectorService initialized")
# =========================================================================
# 3. Services that depend on connector_service
# =========================================================================
# MarketDataService - candles, order books, prices
market_data_service = MarketDataService(
connector_service=connector_service,
rate_oracle=rate_oracle,
cleanup_interval=settings.market_data.cleanup_interval,
feed_timeout=settings.market_data.feed_timeout
)
logging.info("MarketDataService initialized")
# TradingService - order placement, positions, trading interfaces
trading_service = TradingService(
connector_service=connector_service,
market_data_service=market_data_service
)
logging.info("TradingService initialized")
# AccountsService - account management, balances, portfolio (simplified)
accounts_service = AccountsService(
account_update_interval=settings.app.account_update_interval,
gateway_url=settings.gateway.url
)
# Inject services into AccountsService
accounts_service._connector_service = connector_service
accounts_service._market_data_service = market_data_service
accounts_service._trading_service = trading_service
logging.info("AccountsService initialized")
# =========================================================================
# 4. ExecutorService - depends on TradingService (NO circular dependency)
# =========================================================================
executor_service = ExecutorService(
trading_service=trading_service,
db_manager=db_manager,
default_account="master_account",
update_interval=1.0,
max_retries=10
)
logging.info("ExecutorService initialized")
# Ensure lp_executor is in the registry (workspace hummingbot may load after class definition)
try:
from hummingbot.strategy_v2.executors.lp_executor.data_types import LPExecutorConfig
from hummingbot.strategy_v2.executors.lp_executor.lp_executor import LPExecutor
print(f"[LP-FIX] imports OK. Registry before: {list(ExecutorService.EXECUTOR_REGISTRY.keys())}", flush=True)
ExecutorService.EXECUTOR_REGISTRY["lp_executor"] = (LPExecutor, LPExecutorConfig)
print(f"[LP-FIX] Registry after: {list(ExecutorService.EXECUTOR_REGISTRY.keys())}", flush=True)
except Exception as e:
import traceback
print(f"[LP-FIX] FAILED: {e}", flush=True)
traceback.print_exc()
# =========================================================================
# 5. Other Services
# =========================================================================
bots_orchestrator = BotsOrchestrator(
broker_host=settings.broker.host,
broker_port=settings.broker.port,
broker_username=settings.broker.username,
broker_password=settings.broker.password
)
docker_service = DockerService()
gateway_service = GatewayService()
bot_archiver = BotArchiver(
settings.aws.api_key,
settings.aws.secret_key,
settings.aws.s3_default_bucket_name
)
# =========================================================================
# 6. Start services
# =========================================================================
# Initialize all trading connectors FIRST (before any service that might use them)
# This ensures OrdersRecorder is properly attached before any concurrent access
logging.info("Initializing all trading connectors...")
await connector_service.initialize_all_trading_connectors()
bots_orchestrator.start()
market_data_service.start()
await market_data_service.warmup_rate_oracle()
executor_service.start()
await executor_service.cleanup_orphaned_executors()
await executor_service.recover_positions_from_db()
accounts_service.start()
# =========================================================================
# 7. Store services in app state
# =========================================================================
app.state.db_manager = db_manager
app.state.connector_service = connector_service
app.state.market_data_service = market_data_service
app.state.trading_service = trading_service
app.state.accounts_service = accounts_service
app.state.executor_service = executor_service
app.state.bots_orchestrator = bots_orchestrator
app.state.docker_service = docker_service
app.state.gateway_service = gateway_service
app.state.bot_archiver = bot_archiver
logging.info("All services started successfully")
yield
# =========================================================================
# Shutdown services
# =========================================================================
logging.info("Shutting down services...")
bots_orchestrator.stop()
await accounts_service.stop()
await executor_service.stop()
market_data_service.stop()
await connector_service.stop_all()
docker_service.cleanup()
await db_manager.close()
logging.info("All services stopped")
# Initialize FastAPI with metadata and lifespan
app = FastAPI(
title="Hummingbot API",
description="API for managing Hummingbot trading instances",
version=VERSION,
lifespan=lifespan,
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Modify in production to specific origins
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
"""
Custom handler for validation errors to log detailed error messages.
"""
# Build a readable error message from validation errors
error_messages = []
for error in exc.errors():
loc = " -> ".join(str(part) for part in error.get("loc", []))
msg = error.get("msg", "Validation error")
error_messages.append(f"{loc}: {msg}")
# Log the validation error with details
logging.warning(
f"Validation error on {request.method} {request.url.path}: {'; '.join(error_messages)}"
)
# Return standard FastAPI validation error response
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content={"detail": exc.errors()},
)
logfire.configure(send_to_logfire="if-token-present", environment=settings.app.logfire_environment,
service_name="hummingbot-api")
logfire.instrument_fastapi(app)
def auth_user(
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
):
"""Authenticate user using HTTP Basic Auth"""
current_username_bytes = credentials.username.encode("utf8")
correct_username_bytes = f"{username}".encode("utf8")
is_correct_username = secrets.compare_digest(
current_username_bytes, correct_username_bytes
)
current_password_bytes = credentials.password.encode("utf8")
correct_password_bytes = f"{password}".encode("utf8")
is_correct_password = secrets.compare_digest(
current_password_bytes, correct_password_bytes
)
if not (is_correct_username and is_correct_password) and not debug_mode:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Basic"},
)
return credentials.username
# Include all routers with authentication
app.include_router(docker.router, dependencies=[Depends(auth_user)])
app.include_router(gateway.router, dependencies=[Depends(auth_user)])
app.include_router(accounts.router, dependencies=[Depends(auth_user)])
app.include_router(connectors.router, dependencies=[Depends(auth_user)])
app.include_router(portfolio.router, dependencies=[Depends(auth_user)])
app.include_router(trading.router, dependencies=[Depends(auth_user)])
app.include_router(gateway_swap.router, dependencies=[Depends(auth_user)])
app.include_router(gateway_clmm.router, dependencies=[Depends(auth_user)])
app.include_router(bot_orchestration.router, dependencies=[Depends(auth_user)])
app.include_router(controllers.router, dependencies=[Depends(auth_user)])
app.include_router(scripts.router, dependencies=[Depends(auth_user)])
app.include_router(market_data.router, dependencies=[Depends(auth_user)])
app.include_router(rate_oracle.router, dependencies=[Depends(auth_user)])
app.include_router(backtesting.router, dependencies=[Depends(auth_user)])
app.include_router(archived_bots.router, dependencies=[Depends(auth_user)])
app.include_router(executors.router, dependencies=[Depends(auth_user)])
app.include_router(gateway_proxy.router, dependencies=[Depends(auth_user)])
@app.get("/")
async def root():
"""API root endpoint returning basic information."""
return {
"name": "Hummingbot API",
"version": VERSION,
"status": "running",
}