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 pyserum.market import Market
|
|
|
|
|
2021-07-12 10:26:35 -07:00
|
|
|
from .combinableinstructions import CombinableInstructions
|
2021-06-25 02:33:40 -07:00
|
|
|
from .context import Context
|
|
|
|
from .marketoperations import MarketOperations
|
|
|
|
from .orders import Order, OrderType, Side
|
2021-06-30 06:08:37 -07:00
|
|
|
from .serummarket import SerumMarket
|
2021-07-12 10:26:35 -07:00
|
|
|
from .serummarketinstructionbuilder import SerumMarketInstructionBuilder
|
2021-06-25 02:33:40 -07:00
|
|
|
from .wallet import Wallet
|
|
|
|
|
|
|
|
|
|
|
|
# # 🥭 SerumMarketOperations class
|
|
|
|
#
|
|
|
|
# This class puts trades on the Serum orderbook. It doesn't do anything complicated.
|
|
|
|
#
|
|
|
|
|
|
|
|
class SerumMarketOperations(MarketOperations):
|
2021-07-12 10:26:35 -07:00
|
|
|
def __init__(self, context: Context, wallet: Wallet, serum_market: SerumMarket, market_instruction_builder: SerumMarketInstructionBuilder, reporter: typing.Callable[[str], None] = None):
|
2021-06-25 02:33:40 -07:00
|
|
|
super().__init__()
|
|
|
|
self.context: Context = context
|
|
|
|
self.wallet: Wallet = wallet
|
2021-06-30 06:08:37 -07:00
|
|
|
self.serum_market: SerumMarket = serum_market
|
|
|
|
self.market: Market = Market.load(context.client, serum_market.address, context.dex_program_id)
|
2021-07-12 10:26:35 -07:00
|
|
|
self.market_instruction_builder: SerumMarketInstructionBuilder = market_instruction_builder
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
def report(text):
|
|
|
|
self.logger.info(text)
|
|
|
|
reporter(text)
|
|
|
|
|
|
|
|
def just_log(text):
|
|
|
|
self.logger.info(text)
|
|
|
|
|
|
|
|
if reporter is not None:
|
|
|
|
self.reporter = report
|
|
|
|
else:
|
|
|
|
self.reporter = just_log
|
|
|
|
|
2021-07-12 09:18:56 -07:00
|
|
|
def cancel_order(self, order: Order) -> typing.Sequence[str]:
|
2021-07-12 10:26:35 -07:00
|
|
|
self.reporter(f"Cancelling order {order.id} on market {self.serum_market.symbol}.")
|
|
|
|
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, size: Decimal) -> Order:
|
|
|
|
client_id: int = self.context.random_client_id()
|
2021-06-30 06:08:37 -07:00
|
|
|
report: str = f"Placing {order_type} {side} order for size {size} at price {price} on market {self.serum_market.symbol} with ID {client_id}."
|
2021-06-25 02:33:40 -07:00
|
|
|
self.logger.info(report)
|
|
|
|
self.reporter(report)
|
2021-07-12 10:26:35 -07:00
|
|
|
|
|
|
|
signers: CombinableInstructions = CombinableInstructions.from_wallet(self.wallet)
|
|
|
|
place = self.market_instruction_builder.build_place_order_instructions(
|
|
|
|
side, order_type, price, size, client_id)
|
2021-07-12 12:29:41 -07:00
|
|
|
|
|
|
|
crank = self.market_instruction_builder.build_crank_instructions()
|
|
|
|
|
|
|
|
settle = self.market_instruction_builder.build_settle_instructions()
|
|
|
|
|
|
|
|
(signers + place + crank + settle).execute(self.context)
|
2021-07-12 10:26:35 -07:00
|
|
|
return Order(id=0, side=side, price=price, size=size, client_id=client_id, owner=self.market_instruction_builder.open_orders.address)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-06-25 07:50:37 -07:00
|
|
|
def load_orders(self) -> typing.Sequence[Order]:
|
2021-06-25 02:33:40 -07:00
|
|
|
asks = self.market.load_asks()
|
|
|
|
orders: typing.List[Order] = []
|
|
|
|
for serum_order in asks:
|
2021-06-30 06:08:37 -07:00
|
|
|
orders += [Order.from_serum_order(serum_order)]
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
bids = self.market.load_bids()
|
|
|
|
for serum_order in bids:
|
2021-06-30 06:08:37 -07:00
|
|
|
orders += [Order.from_serum_order(serum_order)]
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
return orders
|
|
|
|
|
2021-06-25 07:50:37 -07:00
|
|
|
def load_my_orders(self) -> typing.Sequence[Order]:
|
2021-06-25 02:33:40 -07:00
|
|
|
serum_orders = self.market.load_orders_for_owner(self.wallet.address)
|
|
|
|
orders: typing.List[Order] = []
|
|
|
|
for serum_order in serum_orders:
|
2021-06-30 06:08:37 -07:00
|
|
|
orders += [Order.from_serum_order(serum_order)]
|
2021-06-25 02:33:40 -07:00
|
|
|
|
|
|
|
return orders
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2021-06-30 06:08:37 -07:00
|
|
|
return f"""« 𝚂𝚎𝚛𝚞𝚖𝙼𝚊𝚛𝚔𝚎𝚝𝙾𝚙𝚎𝚛𝚊𝚝𝚒𝚘𝚗𝚜 [{self.serum_market.symbol}] »"""
|