2021-07-29 08:19:52 -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
|
|
|
|
|
2022-01-12 06:04:54 -08:00
|
|
|
from dataclasses import dataclass
|
2021-07-29 08:19:52 -07:00
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
from .orders import Side
|
|
|
|
|
2021-11-09 05:23:36 -08:00
|
|
|
|
2021-07-29 08:19:52 -07:00
|
|
|
# # 🥭 PlacedOrder tuple
|
|
|
|
#
|
|
|
|
# A `PlacedOrder` is a representation of all the data available from an Open Orders account pertaining to a
|
|
|
|
# particular order.
|
|
|
|
#
|
|
|
|
# The information is usually split across 3 collections - 'is bid', 'orders' and 'client ID's. That can be a
|
|
|
|
# little awkward to use, so this tuple packages it all together, per order.
|
|
|
|
#
|
2022-01-12 06:04:54 -08:00
|
|
|
@dataclass
|
|
|
|
class PlacedOrder:
|
2021-07-29 08:19:52 -07:00
|
|
|
id: int
|
|
|
|
client_id: int
|
|
|
|
side: Side
|
|
|
|
|
|
|
|
@staticmethod
|
2022-02-09 11:31:50 -08:00
|
|
|
def build_from_open_orders_data(
|
|
|
|
free_slot_bits: Decimal,
|
|
|
|
is_bid_bits: Decimal,
|
|
|
|
order_ids: typing.Sequence[Decimal],
|
|
|
|
client_order_ids: typing.Sequence[Decimal],
|
|
|
|
) -> typing.Sequence["PlacedOrder"]:
|
2021-07-29 08:19:52 -07:00
|
|
|
int_free_slot_bits = int(free_slot_bits)
|
|
|
|
int_is_bid_bits = int(is_bid_bits)
|
|
|
|
placed_orders: typing.List[PlacedOrder] = []
|
|
|
|
for index in range(len(order_ids)):
|
|
|
|
if not (int_free_slot_bits & (1 << index)):
|
|
|
|
order_id = int(order_ids[index])
|
|
|
|
client_id = int(client_order_ids[index])
|
|
|
|
side = Side.BUY if int_is_bid_bits & (1 << index) else Side.SELL
|
2022-02-09 11:31:50 -08:00
|
|
|
placed_orders += [
|
|
|
|
PlacedOrder(id=order_id, client_id=client_id, side=side)
|
|
|
|
]
|
2021-07-29 08:19:52 -07:00
|
|
|
return placed_orders
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"{self}"
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2021-12-13 04:06:42 -08:00
|
|
|
return f"« PlacedOrder {self.side} [{self.id}] {self.client_id} »"
|
2021-07-29 08:19:52 -07:00
|
|
|
|
|
|
|
|
|
|
|
# # 🥭 PlacedOrdersContainer protocol
|
|
|
|
#
|
|
|
|
# The `PlacedOrdersContainer` protocol exposes commonality between the regular Serum `OpenOrders` class and the
|
|
|
|
# internally-different `PerpOpenOrders` class. Both have their own `placed_orders` member, but are otherwise
|
|
|
|
# different enough that a common abstract base class would be a bit kludgy.
|
|
|
|
#
|
|
|
|
class PlacedOrdersContainer(typing.Protocol):
|
|
|
|
placed_orders: typing.Sequence[PlacedOrder]
|