diff --git a/src/driftpy/drift_client.py b/src/driftpy/drift_client.py index 3f64f98a..644634ee 100644 --- a/src/driftpy/drift_client.py +++ b/src/driftpy/drift_client.py @@ -790,6 +790,34 @@ def get_cancel_order_ix( ), ) + async def cancel_order_by_user_id( + self, + user_order_id: int, + sub_account_id: int = 0, + ): + return await self.send_ixs( + self.get_cancel_order_by_user_id_ix(user_order_id, sub_account_id), + ) + + def get_cancel_order_by_user_id_ix( + self, user_order_id: int, sub_account_id: int = 0 + ): + remaining_accounts = self.get_remaining_accounts( + user_accounts=[self.get_user_account(sub_account_id)] + ) + + return self.program.instruction["cancel_order_by_user_id"]( + user_order_id, + ctx=Context( + accounts={ + "state": self.get_state_public_key(), + "user": self.get_user_account_public_key(sub_account_id), + "authority": self.authority, + }, + remaining_accounts=remaining_accounts, + ), + ) + async def cancel_orders( self, market_type: MarketType = None, @@ -873,6 +901,76 @@ def get_cancel_and_place_orders_ix( place_orders_ix = self.get_place_orders_ix(place_order_params, sub_account_id) return [cancel_orders_ix, place_orders_ix] + async def modify_order( + self, + order_id: int, + modify_order_params: ModifyOrderParams, + sub_account_id: int = 0, + ): + return await self.send_ixs( + [self.get_modify_order_ix(order_id, modify_order_params, sub_account_id)], + ) + + def get_modify_order_ix( + self, + order_id: int, + modify_order_params: ModifyOrderParams, + sub_account_id: int = 0, + ): + remaining_accounts = self.get_remaining_accounts( + user_accounts=[self.get_user_account(sub_account_id)], + ) + + return self.program.instruction["modify_order"]( + order_id, + modify_order_params, + ctx=Context( + accounts={ + "state": self.get_state_public_key(), + "user": self.get_user_account_public_key(sub_account_id), + "authority": self.authority, + }, + remaining_accounts=remaining_accounts, + ), + ) + + async def modify_order_by_user_id( + self, + user_order_id: int, + modify_order_params: ModifyOrderParams, + sub_account_id: int = 0, + ): + return await self.send_ixs( + [ + self.get_modify_order_by_user_id_ix( + user_order_id, modify_order_params, sub_account_id + ) + ], + ) + + def get_modify_order_by_user_id_ix( + self, + user_order_id: int, + modify_order_params: ModifyOrderParams, + sub_account_id: int = 0, + ): + remaining_accounts = self.get_remaining_accounts( + user_accounts=[self.get_user_account(sub_account_id)], + ) + + return self.program.instruction["modify_order_by_user_id"]( + user_order_id, + modify_order_params, + ctx=Context( + accounts={ + "state": self.get_state_public_key(), + "user": self.get_user_account_public_key(sub_account_id), + "authority": self.authority, + }, + remaining_accounts=remaining_accounts, + ), + ) + async def place_and_take_perp_order( self, order_params: OrderParams, diff --git a/src/driftpy/drift_user.py b/src/driftpy/drift_user.py index 755fe91d..bc1c4122 100644 --- a/src/driftpy/drift_user.py +++ b/src/driftpy/drift_user.py @@ -76,7 +76,12 @@ def get_open_orders( # market_index: int, # position_direction: PositionDirection ): - return self.get_user_account().orders + return list( + filter( + lambda order: "Open" in str(order.status), + self.get_user_account().orders, + ) + ) def get_spot_market_liability( self, diff --git a/src/driftpy/types.py b/src/driftpy/types.py index e9a9fc92..c254d1f5 100644 --- a/src/driftpy/types.py +++ b/src/driftpy/types.py @@ -357,20 +357,20 @@ def check_market_type(self): @dataclass class ModifyOrderParams: - direction: Optional[PositionDirection] - base_asset_amount: Optional[int] - price: Optional[int] - reduce_only: Optional[bool] - post_only: Optional[PostOnlyParams] - immediate_or_cancel: Optional[bool] - max_ts: Optional[int] - trigger_price: Optional[int] - trigger_condition: Optional[OrderTriggerCondition] - oracle_price_offset: Optional[int] - auction_duration: Optional[int] - auction_start_price: Optional[int] - auction_end_price: Optional[int] - policy: Optional[ModifyOrderPolicy] + direction: Optional[PositionDirection] = None + base_asset_amount: Optional[int] = None + price: Optional[int] = None + reduce_only: Optional[bool] = None + post_only: Optional[PostOnlyParams] = None + immediate_or_cancel: Optional[bool] = None + max_ts: Optional[int] = None + trigger_price: Optional[int] = None + trigger_condition: Optional[OrderTriggerCondition] = None + oracle_price_offset: Optional[int] = None + auction_duration: Optional[int] = None + auction_start_price: Optional[int] = None + auction_end_price: Optional[int] = None + policy: Optional[ModifyOrderPolicy] = None @dataclass diff --git a/tests/test.py b/tests/test.py index c5f9327c..16e9866e 100644 --- a/tests/test.py +++ b/tests/test.py @@ -230,11 +230,9 @@ async def test_open_orders( user_account = drift_client.get_user(0).get_user_account() assert len(user_account.orders) == 32 - assert user_account.orders[0].market_index == 0 open_orders = drift_user.get_open_orders() - assert len(open_orders) == 32 - assert open_orders == user_account.orders + assert len(open_orders) == 0 order_params = OrderParams( market_type=MarketType.PERP(), @@ -256,7 +254,7 @@ async def test_open_orders( await drift_client.cancel_order(1, 0) await drift_user.account_subscriber.update_cache() open_orders_after2 = drift_user.get_open_orders() - assert open_orders_after2[0].base_asset_amount == 0 + assert len(open_orders_after2) == 0 @mark.asyncio