Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Improved creation of non-existant spot openorders.
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoff Taylor committed Mar 4, 2022
1 parent 89e45e9 commit ba996d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
6 changes: 0 additions & 6 deletions mango/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,15 +675,9 @@ def build_spot_place_order_instructions(
quantity: Decimal,
client_id: int,
fee_discount_address: PublicKey,
create_open_orders: bool,
) -> CombinableInstructions:
instructions: CombinableInstructions = CombinableInstructions.empty()

if create_open_orders:
instructions += build_spot_create_openorders_instructions(
context, wallet, group, account, spot_market, open_orders_address
)

pyserum_market: PySerumMarket = spot_market.underlying_serum_market
serum_order_type: pyserum.enums.OrderType = order_type.to_serum()
serum_side: pyserum.enums.Side = side.to_serum()
Expand Down
30 changes: 20 additions & 10 deletions mango/spotmarket.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,21 +298,38 @@ def build_place_order_instructions(self, order: Order) -> CombinableInstructions
if order.match_limit != Order.DefaultMatchLimit:
self._logger.warning("Ignoring match_limit - not supported on Spot markets")

create_open_orders: bool = False
slot = self.group.slot_by_spot_market_address(self.spot_market.address)
market_index = slot.index
open_orders_address: typing.Optional[
PublicKey
] = self.account.spot_open_orders_by_index[market_index]

instructions = CombinableInstructions.empty()
if open_orders_address is None:
# Spot OpenOrders accounts use a PDA as of v3.3
open_orders_address = self.spot_market.derive_open_orders_address(
self.context, self.account
)
create_open_orders = True
instructions += build_spot_create_openorders_instructions(
self.context,
self.wallet,
self.group,
self.account,
self.spot_market,
open_orders_address,
)

# This line is a little nasty. Now that we know we have an OpenOrders account at
# this address, update the IAccount so that future uses (like later in this
# method) have access to it in the right place.
#
# This might cause problems if this instruction is never sent or the transaction
# fails though.
self.account.update_spot_open_orders_for_market(
market_index, open_orders_address
)

instructions = build_spot_place_order_instructions(
instructions += build_spot_place_order_instructions(
self.context,
self.wallet,
self.group,
Expand All @@ -325,13 +342,6 @@ def build_place_order_instructions(self, order: Order) -> CombinableInstructions
order.quantity,
order.client_id,
self.fee_discount_token_address,
create_open_orders,
)

# This line is a little nasty. Now that we know we have an OpenOrders account at this address, update
# the IAccount so that future uses (like later in this method) have access to it in the right place.
self.account.update_spot_open_orders_for_market(
market_index, open_orders_address
)

return instructions
Expand Down

0 comments on commit ba996d9

Please sign in to comment.