|
|
@@ -11,6 +11,7 @@ from uuid import uuid4
|
|
|
from fastapi import HTTPException
|
|
|
|
|
|
from .bitstamp import BitstampClient, BitstampError
|
|
|
+from .services_bitstamp import get_bitstamp_client, clear_bitstamp_trading_client
|
|
|
from .bitstamp_metadata import load_market_by_symbol
|
|
|
from .storage import get_connection
|
|
|
|
|
|
@@ -48,14 +49,16 @@ def _utc_now() -> str:
|
|
|
|
|
|
|
|
|
def _get_client(account_id: str) -> BitstampClient:
|
|
|
- from .repo import get_account, get_account_secrets
|
|
|
- account = get_account(account_id)
|
|
|
- secrets = get_account_secrets(account_id)
|
|
|
- return BitstampClient(
|
|
|
- username=account["venue_account_ref"],
|
|
|
- api_key=secrets["api_key"],
|
|
|
- api_secret=secrets["api_secret"],
|
|
|
- )
|
|
|
+ return BitstampClientWrapper(account_id)
|
|
|
+
|
|
|
+
|
|
|
+class BitstampClientWrapper:
|
|
|
+ def __init__(self, account_id: str):
|
|
|
+ self.trading = get_bitstamp_client(account_id).trading
|
|
|
+
|
|
|
+
|
|
|
+def _invalidate_client(account_id: str) -> None:
|
|
|
+ clear_bitstamp_trading_client(account_id)
|
|
|
|
|
|
|
|
|
def _format_decimal(value, decimals: int) -> str:
|
|
|
@@ -149,7 +152,10 @@ def place_order(*, account_id: str, market: str, side: str, order_type: str, amo
|
|
|
else:
|
|
|
raise HTTPException(status_code=400, detail="invalid side")
|
|
|
except BitstampError as exc:
|
|
|
- raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
|
+ detail = str(exc)
|
|
|
+ if _looks_like_auth_failure(detail):
|
|
|
+ _invalidate_client(account_id)
|
|
|
+ raise HTTPException(status_code=400, detail=detail) from exc
|
|
|
|
|
|
bitstamp_order_id = str(result.get("id") or result.get("order_id") or "")
|
|
|
if not bitstamp_order_id:
|
|
|
@@ -266,7 +272,10 @@ def query_order(*, account_id: str, order_id, client_order_id: str | None = None
|
|
|
try:
|
|
|
result = client.trading.order_status_v2(order_id=order_id, client_order_id=client_order_id, omit_transactions=omit_transactions)
|
|
|
except BitstampError as exc:
|
|
|
- raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
|
+ detail = str(exc)
|
|
|
+ if _looks_like_auth_failure(detail):
|
|
|
+ _invalidate_client(account_id)
|
|
|
+ raise HTTPException(status_code=400, detail=detail) from exc
|
|
|
|
|
|
with get_connection() as conn:
|
|
|
conn.execute(
|
|
|
@@ -289,6 +298,7 @@ def cancel_order(*, account_id: str, order_id) -> dict:
|
|
|
detail = str(exc)
|
|
|
if _looks_like_auth_failure(detail):
|
|
|
_cancel_breaker_trip(account_id)
|
|
|
+ _invalidate_client(account_id)
|
|
|
raise HTTPException(status_code=400, detail=detail) from exc
|
|
|
|
|
|
status = "cancelled" if result else "cancel_failed"
|