2021-09-06 09:07:26 -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 solana.publickey import PublicKey
|
|
|
|
|
2021-10-26 10:45:04 -07:00
|
|
|
from .accountinfo import AccountInfo
|
2021-09-06 09:07:26 -07:00
|
|
|
from .context import Context
|
|
|
|
from .lotsizeconverter import LotSizeConverter
|
|
|
|
from .market import Market, InventorySource
|
2021-10-26 10:45:04 -07:00
|
|
|
from .orders import Order, OrderBook
|
2021-11-08 03:39:09 -08:00
|
|
|
from .token import Instrument, Token
|
2021-09-06 09:07:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
# # 🥭 LoadedMarket class
|
|
|
|
#
|
|
|
|
# This class describes a crypto market. It *must* have an address, a base token and a quote token.
|
|
|
|
#
|
|
|
|
class LoadedMarket(Market):
|
2022-02-09 11:31:50 -08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
program_address: PublicKey,
|
|
|
|
address: PublicKey,
|
|
|
|
inventory_source: InventorySource,
|
|
|
|
base: Instrument,
|
|
|
|
quote: Token,
|
|
|
|
lot_size_converter: LotSizeConverter,
|
|
|
|
) -> None:
|
|
|
|
super().__init__(
|
|
|
|
program_address, address, inventory_source, base, quote, lot_size_converter
|
|
|
|
)
|
2021-09-06 09:07:26 -07:00
|
|
|
|
2021-10-26 10:45:04 -07:00
|
|
|
@property
|
|
|
|
def bids_address(self) -> PublicKey:
|
2022-02-09 11:31:50 -08:00
|
|
|
raise NotImplementedError(
|
|
|
|
"LoadedMarket.bids_address() is not implemented on the base type."
|
|
|
|
)
|
2021-10-26 10:45:04 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def asks_address(self) -> PublicKey:
|
2022-02-09 11:31:50 -08:00
|
|
|
raise NotImplementedError(
|
|
|
|
"LoadedMarket.asks_address() is not implemented on the base type."
|
|
|
|
)
|
2021-10-26 10:45:04 -07:00
|
|
|
|
2022-02-09 11:31:50 -08:00
|
|
|
def parse_account_info_to_orders(
|
|
|
|
self, account_info: AccountInfo
|
|
|
|
) -> typing.Sequence[Order]:
|
|
|
|
raise NotImplementedError(
|
|
|
|
"LoadedMarket.parse_account_info_to_orders() is not implemented on the base type."
|
|
|
|
)
|
2021-10-26 10:45:04 -07:00
|
|
|
|
2022-02-09 11:31:50 -08:00
|
|
|
def parse_account_infos_to_orderbook(
|
|
|
|
self, bids_account_info: AccountInfo, asks_account_info: AccountInfo
|
|
|
|
) -> OrderBook:
|
2021-10-26 10:45:04 -07:00
|
|
|
bids_orderbook = self.parse_account_info_to_orders(bids_account_info)
|
|
|
|
asks_orderbook = self.parse_account_info_to_orders(asks_account_info)
|
2022-02-09 11:31:50 -08:00
|
|
|
return OrderBook(
|
|
|
|
self.symbol, self.lot_size_converter, bids_orderbook, asks_orderbook
|
|
|
|
)
|
2021-10-26 10:45:04 -07:00
|
|
|
|
|
|
|
def fetch_orderbook(self, context: Context) -> OrderBook:
|
2022-02-09 11:31:50 -08:00
|
|
|
[bids_info, asks_info] = AccountInfo.load_multiple(
|
|
|
|
context, [self.bids_address, self.asks_address]
|
|
|
|
)
|
2021-10-26 10:45:04 -07:00
|
|
|
return self.parse_account_infos_to_orderbook(bids_info, asks_info)
|