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 abc
|
|
|
|
|
import logging
|
|
|
|
|
import typing
|
|
|
|
|
|
|
|
|
|
from decimal import Decimal
|
2021-08-19 03:00:39 -07:00
|
|
|
|
from solana.publickey import PublicKey
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
2021-11-08 04:43:11 -08:00
|
|
|
|
from .combinableinstructions import CombinableInstructions
|
2021-08-19 03:00:39 -07:00
|
|
|
|
from .constants import SYSTEM_PROGRAM_ADDRESS
|
2021-09-30 10:53:33 -07:00
|
|
|
|
from .market import Market, DryRunMarket
|
2021-10-26 10:45:04 -07:00
|
|
|
|
from .orders import Order, OrderBook
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # 🥭 MarketOperations
|
|
|
|
|
#
|
|
|
|
|
# This file deals with placing orders. We want the interface to be simple and basic:
|
|
|
|
|
# ```
|
2021-07-28 07:29:24 -07:00
|
|
|
|
# market_operations.cancel_order(old_order)
|
|
|
|
|
# market_operations.place_order(new_order)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
# ```
|
|
|
|
|
# This requires the `MarketOperations` already know a bit about the market it is placing the
|
|
|
|
|
# order on, and the code in the `MarketOperations` be specialised for that market platform.
|
|
|
|
|
#
|
|
|
|
|
|
2021-11-08 04:43:11 -08:00
|
|
|
|
|
|
|
|
|
# # 🥭 MarketInstructionBuilder class
|
|
|
|
|
#
|
|
|
|
|
# This abstracts the process of buiding instructions for placing orders and cancelling orders.
|
|
|
|
|
#
|
|
|
|
|
# It's abstracted because we may want to have different implementations for different market types.
|
|
|
|
|
#
|
|
|
|
|
# Whichever choice is made, the calling code shouldn't have to care. It should be able to
|
|
|
|
|
# use its `MarketInstructionBuilder` class as simply as:
|
|
|
|
|
# ```
|
|
|
|
|
# instruction_builder.build_cancel_order_instructions(order)
|
|
|
|
|
# ```
|
|
|
|
|
#
|
|
|
|
|
# As a matter of policy for all InstructionBuidlers, construction and build_* methods should all work with
|
|
|
|
|
# existing data, requiring no fetches from Solana or other sources. All necessary data should all be loaded
|
|
|
|
|
# on initial setup in the `load()` method.
|
|
|
|
|
#
|
|
|
|
|
class MarketInstructionBuilder(metaclass=abc.ABCMeta):
|
2021-11-09 05:23:36 -08:00
|
|
|
|
def __init__(self) -> None:
|
2021-11-08 04:43:11 -08:00
|
|
|
|
self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def build_cancel_order_instructions(self, order: Order, ok_if_missing: bool = False) -> CombinableInstructions:
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
|
"MarketInstructionBuilder.build_cancel_order_instructions() is not implemented on the base type.")
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def build_place_order_instructions(self, order: Order) -> CombinableInstructions:
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
|
"MarketInstructionBuilder.build_place_order_instructions() is not implemented on the base type.")
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def build_settle_instructions(self) -> CombinableInstructions:
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
|
"MarketInstructionBuilder.build_settle_instructions() is not implemented on the base type.")
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def build_crank_instructions(self, open_orders_addresses: typing.Sequence[PublicKey], limit: Decimal = Decimal(32)) -> CombinableInstructions:
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
|
"MarketInstructionBuilder.build_crank_instructions() is not implemented on the base type.")
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def build_redeem_instructions(self) -> CombinableInstructions:
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
|
"MarketInstructionBuilder.build_redeem_instructions() is not implemented on the base type.")
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"{self}"
|
|
|
|
|
|
|
|
|
|
|
2021-06-25 02:33:40 -07:00
|
|
|
|
# # 🥭 MarketOperations class
|
|
|
|
|
#
|
|
|
|
|
# This abstracts the process of placing orders, providing a base class for specialised operations.
|
|
|
|
|
#
|
|
|
|
|
# It's abstracted because we may want to have different approaches to placing these
|
|
|
|
|
# orders - do we want to run them against the Serum orderbook? Do we want to run them against
|
|
|
|
|
# Mango groups?
|
|
|
|
|
#
|
|
|
|
|
# Whichever choice is made, the calling code shouldn't have to care. It should be able to
|
|
|
|
|
# use its `MarketOperations` class as simply as:
|
|
|
|
|
# ```
|
2021-07-28 07:29:24 -07:00
|
|
|
|
# market_operations.place_order(order)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
# ```
|
|
|
|
|
#
|
|
|
|
|
class MarketOperations(metaclass=abc.ABCMeta):
|
2021-11-09 05:23:36 -08:00
|
|
|
|
def __init__(self, market: Market) -> None:
|
2021-06-25 02:33:40 -07:00
|
|
|
|
self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)
|
2021-09-30 10:53:33 -07:00
|
|
|
|
self.market: Market = market
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2021-08-17 11:42:11 -07:00
|
|
|
|
def cancel_order(self, order: Order, ok_if_missing: bool = False) -> typing.Sequence[str]:
|
2021-06-25 02:33:40 -07:00
|
|
|
|
raise NotImplementedError("MarketOperations.cancel_order() is not implemented on the base type.")
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2021-07-28 07:29:24 -07:00
|
|
|
|
def place_order(self, order: Order) -> Order:
|
2021-06-25 02:33:40 -07:00
|
|
|
|
raise NotImplementedError("MarketOperations.place_order() is not implemented on the base type.")
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2021-10-26 10:45:04 -07:00
|
|
|
|
def load_orderbook(self) -> OrderBook:
|
2021-06-25 02:33:40 -07:00
|
|
|
|
raise NotImplementedError("MarketOperations.load_orders() is not implemented on the base type.")
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2021-06-25 07:50:37 -07:00
|
|
|
|
def load_my_orders(self) -> typing.Sequence[Order]:
|
2021-06-25 02:33:40 -07:00
|
|
|
|
raise NotImplementedError("MarketOperations.load_my_orders() is not implemented on the base type.")
|
|
|
|
|
|
2021-07-19 07:04:53 -07:00
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def settle(self) -> typing.Sequence[str]:
|
|
|
|
|
raise NotImplementedError("MarketOperations.settle() is not implemented on the base type.")
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def crank(self, limit: Decimal = Decimal(32)) -> typing.Sequence[str]:
|
|
|
|
|
raise NotImplementedError("MarketOperations.crank() is not implemented on the base type.")
|
|
|
|
|
|
2021-08-19 03:00:39 -07:00
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def create_openorders(self) -> PublicKey:
|
|
|
|
|
raise NotImplementedError("MarketOperations.create_openorders() is not implemented on the base type.")
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def ensure_openorders(self) -> PublicKey:
|
|
|
|
|
raise NotImplementedError("MarketOperations.ensure_openorders() is not implemented on the base type.")
|
|
|
|
|
|
2021-06-25 02:33:40 -07:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"{self}"
|
|
|
|
|
|
|
|
|
|
|
2021-11-08 04:43:11 -08:00
|
|
|
|
# # 🥭 NullMarketInstructionBuilder class
|
|
|
|
|
#
|
|
|
|
|
# A null, no-op, dry-run trade executor that can be plugged in anywhere a `MarketInstructionBuilder`
|
|
|
|
|
# is expected, but which will not actually trade.
|
|
|
|
|
#
|
|
|
|
|
class NullMarketInstructionBuilder(MarketInstructionBuilder):
|
2021-11-09 05:23:36 -08:00
|
|
|
|
def __init__(self, symbol: str) -> None:
|
2021-11-08 04:43:11 -08:00
|
|
|
|
super().__init__()
|
|
|
|
|
self.symbol: str = symbol
|
|
|
|
|
|
|
|
|
|
def build_cancel_order_instructions(self, order: Order, ok_if_missing: bool = False) -> CombinableInstructions:
|
|
|
|
|
return CombinableInstructions.empty()
|
|
|
|
|
|
|
|
|
|
def build_place_order_instructions(self, order: Order) -> CombinableInstructions:
|
|
|
|
|
return CombinableInstructions.empty()
|
|
|
|
|
|
|
|
|
|
def build_settle_instructions(self) -> CombinableInstructions:
|
|
|
|
|
return CombinableInstructions.empty()
|
|
|
|
|
|
|
|
|
|
def build_crank_instructions(self, addresses_to_crank: typing.Sequence[PublicKey], limit: Decimal = Decimal(32)) -> CombinableInstructions:
|
|
|
|
|
return CombinableInstructions.empty()
|
|
|
|
|
|
|
|
|
|
def build_redeem_instructions(self) -> CombinableInstructions:
|
|
|
|
|
return CombinableInstructions.empty()
|
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
|
return f"« 𝙽𝚞𝚕𝚕𝙼𝚊𝚛𝚔𝚎𝚝𝙸𝚗𝚜𝚝𝚛𝚞𝚌𝚝𝚒𝚘𝚗𝙱𝚞𝚒𝚕𝚍𝚎𝚛 {self.symbol} »"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # 🥭 NullMarketOperations class
|
2021-06-25 02:33:40 -07:00
|
|
|
|
#
|
|
|
|
|
# A null, no-op, dry-run trade executor that can be plugged in anywhere a `MarketOperations`
|
|
|
|
|
# is expected, but which will not actually trade.
|
|
|
|
|
#
|
2021-11-08 04:43:11 -08:00
|
|
|
|
class NullMarketOperations(MarketOperations):
|
2021-11-09 05:23:36 -08:00
|
|
|
|
def __init__(self, market_name: str) -> None:
|
2021-09-30 10:53:33 -07:00
|
|
|
|
super().__init__(DryRunMarket(market_name))
|
2021-06-25 02:33:40 -07:00
|
|
|
|
self.market_name: str = market_name
|
|
|
|
|
|
2021-08-17 11:42:11 -07:00
|
|
|
|
def cancel_order(self, order: Order, ok_if_missing: bool = False) -> typing.Sequence[str]:
|
2021-08-23 07:36:55 -07:00
|
|
|
|
self.logger.info(f"[Dry Run] Not cancelling order {order}.")
|
2021-07-14 06:03:51 -07:00
|
|
|
|
return [""]
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
2021-07-28 07:29:24 -07:00
|
|
|
|
def place_order(self, order: Order) -> Order:
|
2021-08-23 07:36:55 -07:00
|
|
|
|
self.logger.info(f"[Dry Run] Not placing order {order}.")
|
2021-07-22 10:44:23 -07:00
|
|
|
|
return order
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
2021-10-26 10:45:04 -07:00
|
|
|
|
def load_orderbook(self) -> OrderBook:
|
|
|
|
|
return OrderBook(self.market_name, [], [])
|
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-06-25 02:33:40 -07:00
|
|
|
|
return []
|
|
|
|
|
|
2021-07-19 07:04:53 -07:00
|
|
|
|
def settle(self) -> typing.Sequence[str]:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
def crank(self, limit: Decimal = Decimal(32)) -> typing.Sequence[str]:
|
|
|
|
|
return []
|
|
|
|
|
|
2021-08-19 03:00:39 -07:00
|
|
|
|
def create_openorders(self) -> PublicKey:
|
|
|
|
|
return SYSTEM_PROGRAM_ADDRESS
|
|
|
|
|
|
|
|
|
|
def ensure_openorders(self) -> PublicKey:
|
|
|
|
|
return SYSTEM_PROGRAM_ADDRESS
|
|
|
|
|
|
2021-06-25 02:33:40 -07:00
|
|
|
|
def __str__(self) -> str:
|
2021-08-23 07:36:55 -07:00
|
|
|
|
return f"""« 𝙳𝚛𝚢𝚁𝚞𝚗𝙼𝚊𝚛𝚔𝚎𝚝𝙾𝚙𝚎𝚛𝚊𝚝𝚒𝚘𝚗𝚜 [{self.market_name}] »"""
|