Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing best effort opened order status for dYdX #2068

Merged
merged 3 commits into from
Nov 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/dydx/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def __init__(self) -> None:
DYDXOrderStatus.FILLED: OrderStatus.FILLED,
DYDXOrderStatus.CANCELED: OrderStatus.CANCELED,
DYDXOrderStatus.BEST_EFFORT_CANCELED: OrderStatus.CANCELED,
DYDXOrderStatus.BEST_EFFORT_OPENED: OrderStatus.SUBMITTED,
DYDXOrderStatus.BEST_EFFORT_OPENED: OrderStatus.ACCEPTED,
}

self.dydx_to_nautilus_time_in_force = {
Expand Down
94 changes: 57 additions & 37 deletions nautilus_trader/adapters/dydx/grpc/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,15 @@ async def get_account(self, address: str) -> BaseAccount:
"""
Retrieve the account information for a given address.

Args:
----
address (str): The account address.
Parameters
----------
address : str
The account address.

Returns:
Returns
-------
BaseAccount: The base account information.
BaseAccount
The base account information.

"""
account = BaseAccount()
Expand All @@ -282,13 +284,15 @@ async def get_account_balances(self, address: str) -> bank_query.QueryAllBalance
"""
Retrieve all account balances for a given address.

Args:
----
address (str): The account address.
Parameters
----------
address : str
The account address.

Returns:
Returns
-------
bank_query.QueryAllBalancesResponse: The response containing all account balances.
bank_query.QueryAllBalancesResponse
The response containing all account balances.

"""
stub = bank_query_grpc.QueryStub(self._channel)
Expand All @@ -300,7 +304,8 @@ async def latest_block(self) -> tendermint_query.GetLatestBlockResponse:

Returns
-------
tendermint_query.GetLatestBlockResponse: The response containing the latest block information.
tendermint_query.GetLatestBlockResponse
The response containing the latest block information.

"""
return await tendermint_query_grpc.ServiceStub(self._channel).GetLatestBlock(
Expand All @@ -313,7 +318,8 @@ async def latest_block_height(self) -> int:

Returns
-------
int: The height of the latest block.
int
The height of the latest block.

"""
block = await self.latest_block()
Expand All @@ -325,7 +331,8 @@ async def get_fee_tiers(self) -> fee_tier_query.QueryPerpetualFeeParamsResponse:

Returns
-------
fee_tier_query.QueryPerpetualFeeParamsResponse: The response containing the perpetual fee parameters.
fee_tier_query.QueryPerpetualFeeParamsResponse
The response containing the perpetual fee parameters.

"""
stub = fee_tier_query_grpc.QueryStub(self._channel)
Expand All @@ -335,13 +342,15 @@ async def get_user_fee_tier(self, address: str) -> fee_tier_query.QueryUserFeeTi
"""
Retrieve the user fee tier for a given address.

Args:
----
address (str): The user address.
Parameters
----------
address : str
The user address.

Returns:
Returns
-------
fee_tier_query.QueryUserFeeTierResponse: The response containing the user fee tier.
fee_tier_query.QueryUserFeeTierResponse
The response containing the user fee tier.

"""
stub = fee_tier_query_grpc.QueryStub(self._channel)
Expand All @@ -351,13 +360,16 @@ async def place_order(self, wallet: Wallet, order: Order) -> BroadcastTxResponse
"""
Places an order.

Args:
----
wallet (Wallet): The wallet to use for signing the transaction.
order (Order): The order to place.
Parameters
----------
wallet : Wallet
The wallet to use for signing the transaction.
order : Order
The order to place.

Returns:
Returns
-------
BroadcastTxResponse
The response from the transaction broadcast.

"""
Expand Down Expand Up @@ -468,24 +480,29 @@ async def broadcast_message(
"""
Broadcast a message.

Args:
----
wallet (Wallet): The wallet to use for signing the transaction.
message (Message): The message to broadcast.
mode (BroadcastMode, optional): The broadcast mode. Defaults to BroadcastMode.BROADCAST_MODE_SYNC.
Parameters
----------
wallet : Wallet
The wallet to use for signing the transaction.
message : Message
The message to broadcast.
mode : BroadcastMode, optional
The broadcast mode. Defaults to BroadcastMode.BROADCAST_MODE_SYNC.

Returns:
Returns
-------
The response from the broadcast.

"""
async with self._lock:
response = await self.broadcast(self._transaction_builder.build(wallet, message), mode)
wallet.sequence += 1

if response.tx_response.code == 0:
wallet.sequence += 1

# The sequence number is not correct. Retrieve it from the gRPC channel.
# The retry manager can retry the transaction.
if response.tx_response.code == ACCOUNT_SEQUENCE_MISMATCH_ERROR_CODE:
elif response.tx_response.code == ACCOUNT_SEQUENCE_MISMATCH_ERROR_CODE:
account = await self.get_account(wallet.address)
wallet.sequence = account.sequence

Expand All @@ -499,14 +516,17 @@ async def broadcast(
"""
Broadcast a transaction.

Args:
----
transaction (Tx): The transaction to broadcast.
mode (BroadcastMode, optional): The broadcast mode. Defaults to BroadcastMode.BROADCAST_MODE_SYNC.
Parameters
----------
transaction : Tx
The transaction to broadcast.
mode : BroadcastMode, optional
The broadcast mode. Defaults to BroadcastMode.BROADCAST_MODE_SYNC.

Returns:
Returns
-------
BroadcastTxResponse: The response from the broadcast.
BroadcastTxResponse
The response from the broadcast.

"""
request = BroadcastTxRequest(tx_bytes=transaction.SerializeToString(), mode=mode)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/adapters/dydx/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_parse_nautilus_order_side(
(DYDXOrderStatus.FILLED, OrderStatus.FILLED),
(DYDXOrderStatus.CANCELED, OrderStatus.CANCELED),
(DYDXOrderStatus.BEST_EFFORT_CANCELED, OrderStatus.CANCELED),
(DYDXOrderStatus.BEST_EFFORT_OPENED, OrderStatus.SUBMITTED),
(DYDXOrderStatus.BEST_EFFORT_OPENED, OrderStatus.ACCEPTED),
],
)
def test_parse_order_status(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def test_account_channel_data_new_order_opened() -> None:
order_side=OrderSide.BUY,
order_type=OrderType.LIMIT,
time_in_force=TimeInForce.IOC,
order_status=OrderStatus.SUBMITTED,
order_status=OrderStatus.ACCEPTED,
price=Price(2791.6, 4),
quantity=Quantity(0.002, 5),
filled_qty=Quantity(0, 5),
Expand Down