Skip to content

Commit

Permalink
add cancel_order_by_user_id, modify_order and modify_order_by_user_id
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Nov 27, 2023
1 parent 5008c89 commit a169322
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 19 deletions.
98 changes: 98 additions & 0 deletions src/driftpy/drift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion src/driftpy/drift_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 14 additions & 14 deletions src/driftpy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand Down

0 comments on commit a169322

Please sign in to comment.