From ba996d9c94443ebca0abe299dd2019bdddddc912 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Fri, 4 Mar 2022 09:45:32 +0000 Subject: [PATCH] Improved creation of non-existant spot openorders. --- mango/instructions.py | 6 ------ mango/spotmarket.py | 30 ++++++++++++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/mango/instructions.py b/mango/instructions.py index 98c2127..aa8746f 100644 --- a/mango/instructions.py +++ b/mango/instructions.py @@ -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() diff --git a/mango/spotmarket.py b/mango/spotmarket.py index d16f0a9..ca5ec61 100644 --- a/mango/spotmarket.py +++ b/mango/spotmarket.py @@ -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, @@ -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