mango-explorer/mango/perpmarketoperations.py

97 lines
4.2 KiB
Python
Raw Normal View History

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
from solana.publickey import PublicKey
from .account import Account
2021-06-25 02:33:40 -07:00
from .accountinfo import AccountInfo
from .combinableinstructions import CombinableInstructions
2021-06-25 02:33:40 -07:00
from .context import Context
from .marketoperations import MarketOperations
from .orderbookside import OrderBookSide
from .orders import Order, OrderType, Side
from .perpmarket import PerpMarket
from .perpmarketinstructionbuilder import PerpMarketInstructionBuilder
2021-06-25 02:33:40 -07:00
from .wallet import Wallet
# # 🥭 PerpMarketOperations
#
# This file deals with placing orders for Perps.
#
class PerpMarketOperations(MarketOperations):
def __init__(self, market_name: str, context: Context, wallet: Wallet,
market_instruction_builder: PerpMarketInstructionBuilder,
account: Account, perp_market: PerpMarket):
2021-06-25 02:33:40 -07:00
super().__init__()
self.market_name: str = market_name
self.context: Context = context
self.wallet: Wallet = wallet
self.market_instruction_builder: PerpMarketInstructionBuilder = market_instruction_builder
self.account: Account = account
2021-06-25 02:33:40 -07:00
self.perp_market: PerpMarket = perp_market
def cancel_order(self, order: Order) -> typing.Sequence[str]:
self.logger.info(
f"Cancelling order {order.id} for quantity {order.quantity} at price {order.price} on market {self.market_name} with client ID {order.client_id}.")
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
cancel = self.market_instruction_builder.build_cancel_order_instructions(order)
return (signers + cancel).execute_and_unwrap_transaction_ids(self.context)
2021-06-25 02:33:40 -07:00
def place_order(self, side: Side, order_type: OrderType, price: Decimal, quantity: Decimal) -> Order:
client_id: int = self.context.random_client_id()
self.logger.info(
f"Placing {order_type} {side} order for quantity {quantity} at price {price} on market {self.market_name} with ID {client_id}.")
2021-06-25 02:33:40 -07:00
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
order = Order(id=0, client_id=client_id, owner=self.account.address,
side=side, price=price, quantity=quantity, order_type=order_type)
place = self.market_instruction_builder.build_place_order_instructions(order)
(signers + place).execute(self.context)
return order
2021-06-25 02:33:40 -07:00
def settle(self) -> typing.Sequence[str]:
return []
def crank(self, limit: Decimal = Decimal(32)) -> typing.Sequence[str]:
return []
def load_orders(self) -> typing.Sequence[Order]:
2021-06-25 02:33:40 -07:00
bids_address: PublicKey = self.perp_market.bids
asks_address: PublicKey = self.perp_market.asks
[bids, asks] = AccountInfo.load_multiple(self.context, [bids_address, asks_address])
bid_side = OrderBookSide.parse(self.context, bids, self.perp_market)
ask_side = OrderBookSide.parse(self.context, asks, self.perp_market)
return [*bid_side.orders(), *ask_side.orders()]
def load_my_orders(self) -> typing.Sequence[Order]:
2021-06-25 02:33:40 -07:00
orders = self.load_orders()
mine = []
for order in orders:
if order.owner == self.account.address:
2021-06-25 02:33:40 -07:00
mine += [order]
return mine
def __str__(self) -> str:
return f"""« 𝙿𝚎𝚛𝚙𝚜𝙾𝚛𝚍𝚎𝚛𝙿𝚕𝚊𝚌𝚎𝚛 [{self.market_name}] »"""