feat: enterprise multi-account vault - eliminate all hardcoded accounts, persistent vault api, 1-click account switcher drawer
This commit is contained in:
@@ -19,6 +19,8 @@ from app.ws.routes import ws_router
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s")
|
||||
logger = logging.getLogger("AuraTrade.Main")
|
||||
|
||||
from app.broker.account_vault import account_vault
|
||||
|
||||
# Global Engine Singletons
|
||||
connector = ExnessConnector(
|
||||
account_id=settings.EXNESS_ACCOUNT_ID,
|
||||
@@ -111,6 +113,16 @@ class ConnectRequest(BaseModel):
|
||||
password: str
|
||||
server: str
|
||||
|
||||
class AddAccountRequest(BaseModel):
|
||||
name: str = "My MT5 Account"
|
||||
account_id: int
|
||||
password: str = ""
|
||||
server: str = "Exness-MT5Real15"
|
||||
auto_connect: bool = True
|
||||
|
||||
class SwitchAccountRequest(BaseModel):
|
||||
account_id: int
|
||||
|
||||
class TradeRequest(BaseModel):
|
||||
symbol: str = "XAUUSD"
|
||||
order_type: str = "BUY"
|
||||
@@ -147,8 +159,66 @@ def connect_broker(payload: ConnectRequest):
|
||||
res = connector.connect(payload.account_id, payload.password, payload.server)
|
||||
if res.get("status") == "error":
|
||||
raise HTTPException(status_code=400, detail=res)
|
||||
# Also save to vault
|
||||
account_vault.add_account(f"MT5 #{payload.account_id}", payload.account_id, payload.server, payload.password)
|
||||
account_vault.set_active(payload.account_id)
|
||||
return res
|
||||
|
||||
@app.get("/api/v1/broker/accounts")
|
||||
def get_accounts():
|
||||
"""Returns list of registered accounts in the vault and current active account."""
|
||||
accounts = account_vault.list_accounts()
|
||||
active = account_vault.get_active_account()
|
||||
return {
|
||||
"accounts": accounts,
|
||||
"active_account": active,
|
||||
"total": len(accounts)
|
||||
}
|
||||
|
||||
@app.post("/api/v1/broker/accounts")
|
||||
def add_account(payload: AddAccountRequest):
|
||||
"""Registers a new MT5 account to the multi-account vault."""
|
||||
acc = account_vault.add_account(payload.name, payload.account_id, payload.server, payload.password)
|
||||
if payload.auto_connect:
|
||||
account_vault.set_active(payload.account_id)
|
||||
if payload.password:
|
||||
try:
|
||||
connector.connect(payload.account_id, payload.password, payload.server)
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not immediately connect to added account: {e}")
|
||||
return {"status": "success", "account": acc}
|
||||
|
||||
@app.post("/api/v1/broker/accounts/switch")
|
||||
def switch_account(payload: SwitchAccountRequest):
|
||||
"""Hot-switches the active account across all trading engines."""
|
||||
target = account_vault.set_active(payload.account_id)
|
||||
if not target:
|
||||
raise HTTPException(status_code=404, detail="Account not found in vault")
|
||||
|
||||
# Reconnect connector if password exists
|
||||
if target.get("password"):
|
||||
res = connector.connect(target["account_id"], target["password"], target["server"])
|
||||
if res.get("status") == "error":
|
||||
logger.warning(f"Error connecting during account switch: {res}")
|
||||
|
||||
return {
|
||||
"status": "switched",
|
||||
"active_account": {
|
||||
"id": target.get("id"),
|
||||
"name": target.get("name"),
|
||||
"account_id": target.get("account_id"),
|
||||
"server": target.get("server")
|
||||
}
|
||||
}
|
||||
|
||||
@app.delete("/api/v1/broker/accounts/{account_id}")
|
||||
def delete_account(account_id: int):
|
||||
"""Removes an account from the multi-account vault."""
|
||||
success = account_vault.delete_account(account_id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=400, detail="Cannot delete the primary/only active account")
|
||||
return {"status": "deleted", "account_id": account_id}
|
||||
|
||||
@app.get("/api/v1/broker/telemetry")
|
||||
def get_telemetry():
|
||||
"""Returns live account financial vitals."""
|
||||
|
||||
Reference in New Issue
Block a user