2021-06-25 02:33:40 -07:00
|
|
|
# # ⚠ Warning
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
|
|
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
#
|
|
|
|
# [🥭 Mango Markets](https://mango.markets/) support is available at:
|
|
|
|
# [Docs](https://docs.mango.markets/)
|
|
|
|
# [Discord](https://discord.gg/67jySBhxrg)
|
|
|
|
# [Twitter](https://twitter.com/mangomarkets)
|
|
|
|
# [Github](https://github.com/blockworks-foundation)
|
|
|
|
# [Email](mailto:hello@blockworks.foundation)
|
|
|
|
|
|
|
|
|
|
|
|
import typing
|
|
|
|
|
|
|
|
from decimal import Decimal
|
2021-07-21 06:31:04 -07:00
|
|
|
from solana.publickey import PublicKey
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-07-12 10:26:35 -07:00
|
|
|
from .combinableinstructions import CombinableInstructions
|
2021-07-14 06:03:51 -07:00
|
|
|
from .constants import SYSTEM_PROGRAM_ADDRESS
|
2021-06-25 02:33:40 -07:00
|
|
|
from .context import Context
|
|
|
|
from .marketoperations import MarketOperations
|
2021-07-28 07:29:24 -07:00
|
|
|
from .orders import Order
|
2021-06-30 06:08:37 -07:00
|
|
|
from .serummarket import SerumMarket
|
2021-07-12 10:26:35 -07:00
|
|
|
from .serummarketinstructionbuilder import SerumMarketInstructionBuilder
|
2021-06-25 02:33:40 -07:00
|
|
|
from .wallet import Wallet
|
|
|
|
|
|
|
|
|
|
|
|
# # 🥭 SerumMarketOperations class
|
|
|
|
#
|
2021-08-19 03:00:39 -07:00
|
|
|
# This class performs standard operations on the Serum orderbook.
|
2021-06-25 02:33:40 -07:00
|
|
|
#
|
|
|
|
class SerumMarketOperations(MarketOperations):
|
2021-07-14 06:03:51 -07:00
|
|
|
def __init__(self, context: Context, wallet: Wallet, serum_market: SerumMarket, market_instruction_builder: SerumMarketInstructionBuilder):
|
2021-06-25 02:33:40 -07:00
|
|
|
super().__init__()
|
|
|
|
self.context: Context = context
|
|
|
|
self.wallet: Wallet = wallet
|
2021-06-30 06:08:37 -07:00
|
|
|
self.serum_market: SerumMarket = serum_market
|
2021-07-12 10:26:35 -07:00
|
|
|
self.market_instruction_builder: SerumMarketInstructionBuilder = market_instruction_builder
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-08-17 11:42:11 -07:00
|
|
|
def cancel_order(self, order: Order, ok_if_missing: bool = False) -> typing.Sequence[str]:
|
2021-07-22 10:44:23 -07:00
|
|
|
self.logger.info(f"Cancelling {self.serum_market.symbol} order {order}.")
|
2021-07-12 10:26:35 -07:00
|
|
|
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
|
2021-08-17 11:42:11 -07:00
|
|
|
cancel: CombinableInstructions = self.market_instruction_builder.build_cancel_order_instructions(
|
|
|
|
order, ok_if_missing=ok_if_missing)
|
2021-08-01 09:47:45 -07:00
|
|
|
crank: CombinableInstructions = self._build_crank()
|
2021-07-22 10:44:23 -07:00
|
|
|
settle: CombinableInstructions = self.market_instruction_builder.build_settle_instructions()
|
2021-08-07 07:07:19 -07:00
|
|
|
return (signers + cancel + crank + settle).execute(self.context)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-07-28 07:29:24 -07:00
|
|
|
def place_order(self, order: Order) -> Order:
|
2021-06-25 02:33:40 -07:00
|
|
|
client_id: int = self.context.random_client_id()
|
2021-07-12 10:26:35 -07:00
|
|
|
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
|
2021-07-15 13:03:22 -07:00
|
|
|
open_orders_address = self.market_instruction_builder.open_orders_address or SYSTEM_PROGRAM_ADDRESS
|
2021-07-28 07:29:24 -07:00
|
|
|
order_with_client_id: Order = Order(id=0, client_id=client_id, side=order.side, price=order.price,
|
|
|
|
quantity=order.quantity, owner=open_orders_address,
|
|
|
|
order_type=order.order_type)
|
|
|
|
self.logger.info(f"Placing {self.serum_market.symbol} order {order_with_client_id}.")
|
|
|
|
place: CombinableInstructions = self.market_instruction_builder.build_place_order_instructions(
|
|
|
|
order_with_client_id)
|
2021-07-12 12:29:41 -07:00
|
|
|
|
2021-08-01 09:47:45 -07:00
|
|
|
crank: CombinableInstructions = self._build_crank()
|
2021-07-22 10:44:23 -07:00
|
|
|
settle: CombinableInstructions = self.market_instruction_builder.build_settle_instructions()
|
2021-07-12 12:29:41 -07:00
|
|
|
|
|
|
|
(signers + place + crank + settle).execute(self.context)
|
2021-07-15 13:03:22 -07:00
|
|
|
return order
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-07-19 07:04:53 -07:00
|
|
|
def settle(self) -> typing.Sequence[str]:
|
2021-07-21 06:31:04 -07:00
|
|
|
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
|
|
|
|
settle = self.market_instruction_builder.build_settle_instructions()
|
|
|
|
return (signers + settle).execute(self.context)
|
2021-07-19 07:04:53 -07:00
|
|
|
|
|
|
|
def crank(self, limit: Decimal = Decimal(32)) -> typing.Sequence[str]:
|
2021-07-21 06:31:04 -07:00
|
|
|
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
|
2021-08-01 09:47:45 -07:00
|
|
|
crank = self._build_crank(limit)
|
2021-07-21 06:31:04 -07:00
|
|
|
return (signers + crank).execute(self.context)
|
2021-07-19 07:04:53 -07:00
|
|
|
|
2021-08-19 03:00:39 -07:00
|
|
|
def create_openorders(self) -> PublicKey:
|
|
|
|
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
|
|
|
|
create_open_orders = self.market_instruction_builder.build_create_openorders_instructions()
|
|
|
|
open_orders_address = create_open_orders.signers[0].public_key()
|
|
|
|
(signers + create_open_orders).execute(self.context)
|
|
|
|
|
|
|
|
return open_orders_address
|
|
|
|
|
|
|
|
def ensure_openorders(self) -> PublicKey:
|
|
|
|
if self.market_instruction_builder.open_orders_address is not None:
|
|
|
|
return self.market_instruction_builder.open_orders_address
|
|
|
|
return self.create_openorders()
|
|
|
|
|
2021-06-25 07:50:37 -07:00
|
|
|
def load_orders(self) -> typing.Sequence[Order]:
|
2021-07-23 06:18:26 -07:00
|
|
|
return self.serum_market.orders(self.context)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-06-25 07:50:37 -07:00
|
|
|
def load_my_orders(self) -> typing.Sequence[Order]:
|
2021-07-14 06:03:51 -07:00
|
|
|
open_orders_address = self.market_instruction_builder.open_orders_address
|
|
|
|
if not open_orders_address:
|
|
|
|
return []
|
|
|
|
|
2021-07-23 02:20:44 -07:00
|
|
|
all_orders = self.serum_market.orders(self.context)
|
2021-07-23 06:18:26 -07:00
|
|
|
return list([o for o in all_orders if o.owner == open_orders_address])
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-08-01 09:47:45 -07:00
|
|
|
def _build_crank(self, limit: Decimal = Decimal(32)) -> CombinableInstructions:
|
|
|
|
open_orders_to_crank: typing.List[PublicKey] = []
|
|
|
|
for event in self.serum_market.unprocessed_events(self.context):
|
|
|
|
open_orders_to_crank += [event.public_key]
|
|
|
|
|
|
|
|
if len(open_orders_to_crank) == 0:
|
|
|
|
return CombinableInstructions.empty()
|
|
|
|
|
|
|
|
return self.market_instruction_builder.build_crank_instructions(open_orders_to_crank, limit)
|
|
|
|
|
2021-06-25 02:33:40 -07:00
|
|
|
def __str__(self) -> str:
|
2021-06-30 06:08:37 -07:00
|
|
|
return f"""« 𝚂𝚎𝚛𝚞𝚖𝙼𝚊𝚛𝚔𝚎𝚝𝙾𝚙𝚎𝚛𝚊𝚝𝚒𝚘𝚗𝚜 [{self.serum_market.symbol}] »"""
|