2021-07-12 02:57:05 -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 logging
|
2021-07-12 09:18:56 -07:00
|
|
|
import typing
|
2021-07-12 02:57:05 -07:00
|
|
|
|
2021-08-22 11:48:20 -07:00
|
|
|
from decimal import Decimal
|
2021-09-02 05:11:35 -07:00
|
|
|
from solana.publickey import PublicKey
|
|
|
|
|
2021-10-02 04:28:16 -07:00
|
|
|
from .account import Account
|
|
|
|
from .group import Group
|
|
|
|
from .inventory import Inventory
|
|
|
|
from .market import Market
|
|
|
|
from .oracle import Price
|
2021-10-26 10:45:04 -07:00
|
|
|
from .orders import Order, OrderBook
|
2021-10-02 04:28:16 -07:00
|
|
|
from .placedorder import PlacedOrdersContainer
|
|
|
|
from .watcher import Watcher
|
|
|
|
|
2021-08-02 09:14:00 -07:00
|
|
|
|
2022-01-11 04:24:15 -08:00
|
|
|
# # 🥭 EventQueue protocol
|
|
|
|
#
|
|
|
|
# The `EventQueue` protocol just says the object has a property `accounts_to_crank` that returns a `Sequence` of `PublicKey`s.
|
|
|
|
#
|
|
|
|
# This is to share the interface between a `SerumEventQueue` and a `PerpEventQueue` for cranking, when the
|
|
|
|
# underlying objects are really quite different.
|
|
|
|
#
|
|
|
|
class EventQueue(typing.Protocol):
|
|
|
|
@property
|
|
|
|
def accounts_to_crank(self) -> typing.Sequence[PublicKey]:
|
2022-02-09 11:31:50 -08:00
|
|
|
raise NotImplementedError(
|
|
|
|
"EventQueue.accounts_to_crank is not implemented on the Protocol."
|
|
|
|
)
|
2022-01-11 04:24:15 -08:00
|
|
|
|
|
|
|
|
|
|
|
class NullEventQueue:
|
|
|
|
@property
|
|
|
|
def accounts_to_crank(self) -> typing.Sequence[PublicKey]:
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2021-07-12 02:57:05 -07:00
|
|
|
# # 🥭 ModelState class
|
|
|
|
#
|
|
|
|
# Provides simple access to the latest state of market and account data.
|
|
|
|
#
|
|
|
|
class ModelState:
|
2022-02-09 11:31:50 -08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
order_owner: PublicKey,
|
|
|
|
market: Market,
|
|
|
|
group_watcher: Watcher[Group],
|
|
|
|
account_watcher: Watcher[Account],
|
|
|
|
price_watcher: Watcher[Price],
|
|
|
|
placed_orders_container_watcher: Watcher[PlacedOrdersContainer],
|
|
|
|
inventory_watcher: Watcher[Inventory],
|
|
|
|
orderbook: Watcher[OrderBook],
|
|
|
|
event_queue: Watcher[EventQueue],
|
|
|
|
) -> None:
|
2021-12-13 03:15:24 -08:00
|
|
|
self._logger: logging.Logger = logging.getLogger(self.__class__.__name__)
|
2021-09-02 05:11:35 -07:00
|
|
|
self.order_owner: PublicKey = order_owner
|
2021-10-02 04:28:16 -07:00
|
|
|
self.market: Market = market
|
|
|
|
self.group_watcher: Watcher[Group] = group_watcher
|
|
|
|
self.account_watcher: Watcher[Account] = account_watcher
|
|
|
|
self.price_watcher: Watcher[Price] = price_watcher
|
|
|
|
self.placed_orders_container_watcher: Watcher[
|
2022-02-09 11:31:50 -08:00
|
|
|
PlacedOrdersContainer
|
|
|
|
] = placed_orders_container_watcher
|
2021-10-02 04:28:16 -07:00
|
|
|
self.inventory_watcher: Watcher[Inventory] = inventory_watcher
|
2021-10-26 10:45:04 -07:00
|
|
|
self.orderbook_watcher: Watcher[OrderBook] = orderbook
|
2022-01-11 04:24:15 -08:00
|
|
|
self.event_queue_watcher: Watcher[EventQueue] = event_queue
|
2021-07-12 02:57:05 -07:00
|
|
|
|
2021-08-26 01:19:51 -07:00
|
|
|
self.not_quoting: bool = False
|
|
|
|
self.state: typing.Dict[str, typing.Any] = {}
|
|
|
|
|
2021-07-12 02:57:05 -07:00
|
|
|
@property
|
2021-10-02 04:28:16 -07:00
|
|
|
def group(self) -> Group:
|
2021-07-12 02:57:05 -07:00
|
|
|
return self.group_watcher.latest
|
|
|
|
|
|
|
|
@property
|
2021-10-02 04:28:16 -07:00
|
|
|
def account(self) -> Account:
|
2021-07-12 02:57:05 -07:00
|
|
|
return self.account_watcher.latest
|
|
|
|
|
|
|
|
@property
|
2021-10-02 04:28:16 -07:00
|
|
|
def price(self) -> Price:
|
2021-07-12 02:57:05 -07:00
|
|
|
return self.price_watcher.latest
|
|
|
|
|
2021-08-02 09:14:00 -07:00
|
|
|
@property
|
2021-10-02 04:28:16 -07:00
|
|
|
def placed_orders_container(self) -> PlacedOrdersContainer:
|
2021-08-02 09:14:00 -07:00
|
|
|
return self.placed_orders_container_watcher.latest
|
|
|
|
|
|
|
|
@property
|
2021-10-02 04:28:16 -07:00
|
|
|
def inventory(self) -> Inventory:
|
2021-08-02 09:14:00 -07:00
|
|
|
return self.inventory_watcher.latest
|
|
|
|
|
2021-10-26 10:45:04 -07:00
|
|
|
@property
|
|
|
|
def orderbook(self) -> OrderBook:
|
|
|
|
return self.orderbook_watcher.latest
|
|
|
|
|
2021-08-21 14:06:58 -07:00
|
|
|
@property
|
2021-10-02 04:28:16 -07:00
|
|
|
def bids(self) -> typing.Sequence[Order]:
|
2021-10-26 10:45:04 -07:00
|
|
|
return self.orderbook.bids
|
2021-08-21 14:06:58 -07:00
|
|
|
|
|
|
|
@property
|
2021-10-02 04:28:16 -07:00
|
|
|
def asks(self) -> typing.Sequence[Order]:
|
2021-10-26 10:45:04 -07:00
|
|
|
return self.orderbook.asks
|
2021-08-21 14:06:58 -07:00
|
|
|
|
2021-09-01 10:33:38 -07:00
|
|
|
# The top bid is the highest price someone is willing to pay to BUY
|
2021-08-22 11:48:20 -07:00
|
|
|
@property
|
2021-10-02 04:28:16 -07:00
|
|
|
def top_bid(self) -> typing.Optional[Order]:
|
2021-10-26 10:45:04 -07:00
|
|
|
return self.orderbook.top_bid
|
2021-08-22 11:48:20 -07:00
|
|
|
|
2021-09-01 10:33:38 -07:00
|
|
|
# The top ask is the lowest price someone is willing to pay to SELL
|
2021-08-22 11:48:20 -07:00
|
|
|
@property
|
2021-10-02 04:28:16 -07:00
|
|
|
def top_ask(self) -> typing.Optional[Order]:
|
2021-10-26 10:45:04 -07:00
|
|
|
return self.orderbook.top_ask
|
2021-08-22 11:48:20 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def spread(self) -> Decimal:
|
2021-10-26 10:45:04 -07:00
|
|
|
return self.orderbook.spread
|
2021-08-22 11:48:20 -07:00
|
|
|
|
2022-01-11 04:24:15 -08:00
|
|
|
@property
|
|
|
|
def accounts_to_crank(self) -> typing.Sequence[PublicKey]:
|
|
|
|
return self.event_queue_watcher.latest.accounts_to_crank
|
|
|
|
|
2021-10-02 04:28:16 -07:00
|
|
|
def current_orders(self) -> typing.Sequence[Order]:
|
2021-10-26 10:45:04 -07:00
|
|
|
self.orderbook
|
|
|
|
all_orders = [*self.bids, *self.asks]
|
2021-09-02 05:11:35 -07:00
|
|
|
return list([o for o in all_orders if o.owner == self.order_owner])
|
2021-07-12 09:18:56 -07:00
|
|
|
|
2021-07-12 02:57:05 -07:00
|
|
|
def __str__(self) -> str:
|
2021-12-13 04:06:42 -08:00
|
|
|
return f"""« ModelState for market '{self.market.symbol}'
|
2021-08-24 12:13:44 -07:00
|
|
|
Group: {self.group_watcher.latest.address}
|
|
|
|
Account: {self.account_watcher.latest.address}
|
|
|
|
Price: {self.price_watcher.latest}
|
|
|
|
Inventory: {self.inventory_watcher.latest}
|
|
|
|
Existing Order Count: {len(self.placed_orders_container_watcher.latest.placed_orders)}
|
2021-10-26 10:45:04 -07:00
|
|
|
Bid Count: {len(self.bids)}
|
|
|
|
Ask Count: {len(self.asks)}
|
2021-08-24 12:13:44 -07:00
|
|
|
»"""
|
2021-07-12 02:57:05 -07:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"{self}"
|