From a1a3930463e3c04552d427a8a9b400dc04671385 Mon Sep 17 00:00:00 2001 From: David Blom Date: Fri, 22 Nov 2024 17:19:16 +0100 Subject: [PATCH 1/3] Fix parsing best effort opened order status for dYdX --- nautilus_trader/adapters/dydx/common/enums.py | 2 +- tests/integration_tests/adapters/dydx/test_parsing.py | 2 +- tests/integration_tests/adapters/dydx/test_websocket_schema.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nautilus_trader/adapters/dydx/common/enums.py b/nautilus_trader/adapters/dydx/common/enums.py index 19303887951..f7cda6cf60b 100644 --- a/nautilus_trader/adapters/dydx/common/enums.py +++ b/nautilus_trader/adapters/dydx/common/enums.py @@ -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 = { diff --git a/tests/integration_tests/adapters/dydx/test_parsing.py b/tests/integration_tests/adapters/dydx/test_parsing.py index f4f48d9098b..f03b76fd267 100644 --- a/tests/integration_tests/adapters/dydx/test_parsing.py +++ b/tests/integration_tests/adapters/dydx/test_parsing.py @@ -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( diff --git a/tests/integration_tests/adapters/dydx/test_websocket_schema.py b/tests/integration_tests/adapters/dydx/test_websocket_schema.py index 4515ffbe8a6..b2f8cbf1b3e 100644 --- a/tests/integration_tests/adapters/dydx/test_websocket_schema.py +++ b/tests/integration_tests/adapters/dydx/test_websocket_schema.py @@ -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), From 8d8d81b4acdee50bb7a9f0cf7299c74cc6deee11 Mon Sep 17 00:00:00 2001 From: David Blom Date: Fri, 22 Nov 2024 19:06:01 +0100 Subject: [PATCH 2/3] Only update the sequence if the grpc message is success --- nautilus_trader/adapters/dydx/grpc/account.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nautilus_trader/adapters/dydx/grpc/account.py b/nautilus_trader/adapters/dydx/grpc/account.py index d61966e63c8..5ad7feb11ed 100644 --- a/nautilus_trader/adapters/dydx/grpc/account.py +++ b/nautilus_trader/adapters/dydx/grpc/account.py @@ -481,11 +481,13 @@ async def broadcast_message( """ 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 From 0a31557ff8f31042c59aaa8ae38fd181c6840b29 Mon Sep 17 00:00:00 2001 From: David Blom Date: Fri, 22 Nov 2024 19:24:38 +0100 Subject: [PATCH 3/3] Format doc strings --- nautilus_trader/adapters/dydx/grpc/account.py | 88 +++++++++++-------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/nautilus_trader/adapters/dydx/grpc/account.py b/nautilus_trader/adapters/dydx/grpc/account.py index 5ad7feb11ed..7cce3cd1a32 100644 --- a/nautilus_trader/adapters/dydx/grpc/account.py +++ b/nautilus_trader/adapters/dydx/grpc/account.py @@ -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() @@ -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) @@ -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( @@ -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() @@ -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) @@ -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) @@ -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. """ @@ -468,13 +480,16 @@ 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. @@ -501,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)