Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions services/accounts_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from fastapi import HTTPException
from hummingbot.client.config.config_crypt import ETHKeyFileSecretManger
from hummingbot.core.data_type.common import OrderType, PositionAction, PositionMode, TradeType
from hummingbot.core.data_type.in_flight_order import OrderState

from config import settings
from database import AccountRepository, AsyncDatabaseManager, IdempotencyRepository
Expand Down Expand Up @@ -912,6 +913,83 @@ def get_account_distribution(self) -> Dict[str, Any]:
"""
return self.portfolio_analytics_service.get_account_distribution(self.accounts_state)

# How long ``place_trade`` waits for the venue to accept or refuse an order
# before answering. The submission is asynchronous, so this is the window in
# which a refusal can still turn a "submitted" into an error.
SUBMISSION_TIMEOUT_SEC = 15.0
SUBMISSION_POLL_SEC = 0.25

@staticmethod
def _tracked_order(connector, client_order_id: str):
"""The in-flight order for ``client_order_id``, wherever the tracker keeps it.

``in_flight_orders`` only holds orders that are still live: the tracker
drops an order the moment it reaches a terminal state, so a *refused*
order is precisely the one missing from it. Reading only that map turns
every refusal into "not tracked" -- which is why the first version of
this check still answered 201. ``all_orders`` keeps them.
"""
tracker = getattr(connector, "_order_tracker", None)
for source in (
getattr(tracker, "all_orders", None),
getattr(connector, "in_flight_orders", None),
):
if source is None:
continue
try:
order = source.get(client_order_id)
except Exception:
continue
if order is not None:
return order
return None

async def _await_submission(
self, connector, client_order_id: str, timeout: Optional[float] = None
) -> Optional[str]:
"""Wait for an order's submission to resolve; return why it failed, if it did.

``connector.buy``/``sell`` return a **client** order id immediately --
the venue call happens in the connector's own task, so it lands on the
in-flight order after the method has already returned. Answering 201 off
the back of that id reports a submission nothing has confirmed yet: a
Derive order refused for a risk-universe mismatch came back as
``status: submitted`` with an id the venue had never seen, and the only
trace was a warning in the container log.

Resolves to a failure reason when the venue refused, ``None`` when it
was accepted -- or when it is still unconfirmed at the timeout, which
stays a success because the order may well be live.
"""
limit = self.SUBMISSION_TIMEOUT_SEC if timeout is None else timeout
loop = asyncio.get_running_loop()
deadline = loop.time() + limit
while True:
order = self._tracked_order(connector, client_order_id)
# Absence is not an answer. A connector registers an order with its
# tracker asynchronously, and a refused one lands there *after* the
# refusal -- returning early on the first miss is exactly what let a
# refusal through as a success.
if order is not None:
state = order.current_state
if state == OrderState.FAILED:
# InFlightOrder does not keep the venue's text (no
# ``last_failure_reason`` on this version), so read it when
# a connector does expose one and otherwise say plainly that
# the venue refused. The connector has already logged the
# detail at its own level.
reason = getattr(order, "last_failure_reason", None) or getattr(
order, "error_message", None
)
return reason or "the venue rejected the order"
if state != OrderState.PENDING_CREATE:
return None
remaining = deadline - loop.time()
if remaining <= 0:
# Never tracked: the caller's id is all there is to go on.
return None
await asyncio.sleep(min(self.SUBMISSION_POLL_SEC, remaining))

async def place_trade(self, account_name: str, connector_name: str, trading_pair: str,
trade_type: TradeType, amount: Decimal, order_type: OrderType = OrderType.LIMIT,
price: Optional[Decimal] = None, position_action: PositionAction = PositionAction.OPEN) -> str:
Expand Down Expand Up @@ -1024,6 +1102,20 @@ async def place_trade(self, account_name: str, connector_name: str, trading_pair
position_action=position_action
)

failure = await self._await_submission(connector, order_id)
if failure:
logger.error(
f"{connector_name} refused the {trade_type.name} order for "
f"{quantized_amount} {trading_pair} (Account: {account_name}): {failure}"
)
raise HTTPException(
status_code=502,
detail=(
f"{connector_name} refused the {trade_type.name} order for "
f"{trading_pair}: {failure}"
),
)

logger.info(
f"Placed {trade_type} order for {amount} {trading_pair} on {connector_name} "
f"(Account: {account_name}). Order ID: {order_id}"
Expand Down
253 changes: 253 additions & 0 deletions test/test_place_order_refusal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
"""A refused order must not come back as ``201 submitted``.

``connector.buy``/``sell`` return a *client* order id immediately; the venue
call happens in the connector's own task. Answering 201 from that id reported a
submission nothing had confirmed: a Derive order refused for a risk-universe
mismatch returned ``status: submitted`` with an id the venue never saw, and only
the container log carried the refusal.

These tests pin the outcome: refused -> 502 with the venue's reason, accepted ->
the id, still-unconfirmed -> the id (the order may well be live).
"""

from decimal import Decimal
from types import SimpleNamespace
from unittest.mock import AsyncMock

import pytest
from fastapi import HTTPException
from hummingbot.core.data_type.common import OrderType, PositionAction, TradeType
from hummingbot.core.data_type.in_flight_order import OrderState

from services.accounts_service import AccountsService

PAIR = "ETH-USDC"


class _Order:
"""In-flight order whose state resolves after ``pending_reads`` polls."""

def __init__(self, final_state, reason=None, pending_reads=0):
self._final = final_state
self._pending = pending_reads
self.current_state = (
OrderState.PENDING_CREATE if pending_reads else final_state
)
self.last_failure_reason = reason

def poll(self):
if self._pending > 0:
self._pending -= 1
if self._pending == 0:
self.current_state = self._final


class _TrackingDict(dict):
"""``in_flight_orders`` that advances the fake order on every read."""

def get(self, key, default=None):
order = super().get(key, default)
if isinstance(order, _Order):
order.poll()
# One read is the current state; hand back what it was before the
# poll so a test can drive PENDING -> OPEN across two reads.
return order


def _connector(order, order_id="cid-1"):
returns = {}

def _buy(**kwargs):
returns.update(kwargs)
return order_id

def _sell(**kwargs):
returns.update(kwargs)
return order_id

return SimpleNamespace(
trading_rules={
PAIR: SimpleNamespace(
min_order_size=Decimal("0.1"), min_notional_size=Decimal("0")
)
},
supported_order_types=lambda: [OrderType.LIMIT],
quantize_order_amount=lambda pair, amount: Decimal(amount),
quantize_order_price=lambda pair, price: Decimal(price),
in_flight_orders=_TrackingDict({order_id: order} if order else {}),
buy=_buy,
sell=_sell,
calls=returns,
)


def _service(connector):
svc = AccountsService.__new__(AccountsService)
svc.list_accounts = lambda: ["master_account"]
svc._connector_service = SimpleNamespace(
get_trading_connector=AsyncMock(return_value=connector)
)
svc.SUBMISSION_TIMEOUT_SEC = 0.05
svc.SUBMISSION_POLL_SEC = 0.01
return svc


async def _place(svc, trade_type=TradeType.BUY):
return await svc.place_trade(
account_name="master_account",
connector_name="derive_perpetual",
trading_pair=PAIR,
trade_type=trade_type,
amount=Decimal("0.1"),
order_type=OrderType.LIMIT,
price=Decimal("1660.88"),
position_action=PositionAction.OPEN,
)


@pytest.mark.asyncio
async def test_accepted_order_returns_its_id():
order = _Order(OrderState.OPEN)
svc = _service(_connector(order))
assert await _place(svc) == "cid-1"


@pytest.mark.asyncio
async def test_refused_order_raises_with_the_venue_reason():
"""The whole point: a venue refusal is an error, not a successful submit."""
reason = (
"code=-32602 Invalid params (subaccount 121338 is in risk universe 1 but "
"instrument SOL-PERP is in risk universe 3)"
)
order = _Order(OrderState.FAILED, reason=reason, pending_reads=2)
svc = _service(_connector(order))

with pytest.raises(HTTPException) as exc:
await _place(svc)

assert exc.value.status_code == 502
assert "risk universe" in exc.value.detail
assert "refused" in exc.value.detail


@pytest.mark.asyncio
async def test_refusal_without_a_reason_still_fails():
order = _Order(OrderState.FAILED, reason=None)
svc = _service(_connector(order))

with pytest.raises(HTTPException) as exc:
await _place(svc)

assert exc.value.status_code == 502
assert "rejected" in exc.value.detail


@pytest.mark.asyncio
async def test_unconfirmed_order_is_still_a_success():
"""Still PENDING_CREATE at the timeout: the order may well be live."""
order = _Order(OrderState.OPEN, pending_reads=10_000)
svc = _service(_connector(order))
assert await _place(svc) == "cid-1"


@pytest.mark.asyncio
async def test_untracked_order_is_not_treated_as_a_failure():
"""A connector that does not track its orders must not start failing."""
svc = _service(_connector(order=None))
assert await _place(svc) == "cid-1"


class _LateTracker:
"""Tracker that reports nothing for the first ``missing_polls`` reads."""

def __init__(self, order, missing_polls=2):
self._order = order
self._missing = missing_polls
self._map = {}

def poll_once(self):
if self._missing > 0:
self._missing -= 1
if self._missing == 0:
self._map = {"cid-1": self._order}

@property
def all_orders(self):
# `_tracked_order` reads this attribute before calling .get(), so the
# advance has to happen here for a test clock to work at all.
self.poll_once()
return self

def get(self, key, default=None):
return self._map.get(key, default)


class _LateConnector:
"""Connector whose tracker only learns about the order a few polls later."""

def __init__(self, order):
self.trading_rules = {
PAIR: SimpleNamespace(
min_order_size=Decimal("0.1"), min_notional_size=Decimal("0")
)
}
self.supported_order_types = lambda: [OrderType.LIMIT]
self.quantize_order_amount = lambda pair, amount: Decimal(amount)
self.quantize_order_price = lambda pair, price: Decimal(price)
self.in_flight_orders = {}
self._order_tracker = _LateTracker(order)

def buy(self, **kwargs):
return "cid-1"

def sell(self, **kwargs):
return "cid-1"


@pytest.mark.asyncio
async def test_order_registered_late_is_still_checked():
"""The order is not tracked at submit time; its refusal must still be caught.

This is the production shape: Derive registers the order with its tracker
after the venue answers, so a first-poll miss used to be read as success.
"""
order = _Order(OrderState.FAILED, reason="risk universe mismatch")
svc = _service(_LateConnector(order))

with pytest.raises(HTTPException) as exc:
await _place(svc)

assert exc.value.status_code == 502
assert "risk universe" in exc.value.detail


@pytest.mark.asyncio
async def test_failed_order_that_left_the_in_flight_map_is_still_caught():
"""The tracker drops a failed order from ``in_flight_orders``; catch it anyway.

This is the shape that matters in production: by the time the refusal is
processed, the order is no longer "in flight", so a check that reads only
that map sees nothing and reports success.
"""
order = _Order(OrderState.FAILED, reason="risk universe mismatch")
conn = _connector(order=None) # nothing in in_flight_orders
conn._order_tracker = SimpleNamespace(all_orders={"cid-1": order})
svc = _service(conn)

with pytest.raises(HTTPException) as exc:
await _place(svc)

assert exc.value.status_code == 502
assert "risk universe" in exc.value.detail


@pytest.mark.asyncio
async def test_sell_path_checks_the_outcome_too():
order = _Order(OrderState.FAILED, reason="margin insufficient")
svc = _service(_connector(order))

with pytest.raises(HTTPException) as exc:
await _place(svc, trade_type=TradeType.SELL)

assert exc.value.status_code == 502
assert "margin insufficient" in exc.value.detail
Loading