#!/usr/bin/env pyston3 import argparse import logging import os import os.path import sys import traceback from decimal import Decimal from solana.publickey import PublicKey sys.path.insert(0, os.path.abspath( os.path.join(os.path.dirname(__file__), ".."))) import mango # nopep8 # We explicitly want argument parsing to be outside the main try-except block because some arguments # (like --help) will cause an exit, which our except: block traps. parser = argparse.ArgumentParser(description="Liquidate a single margin account.") mango.Context.add_command_line_parameters(parser) mango.Wallet.add_command_line_parameters(parser) parser.add_argument("--name", type=str, default="Mango Markets Liquidator", help="Name of the liquidator (used in reports and alerts)") parser.add_argument("--address", type=PublicKey, help="Solana address of the Mango Markets margin account to be liquidated") parser.add_argument("--notify-liquidations", type=mango.parse_subscription_target, action="append", default=[], help="The notification target for liquidation events") parser.add_argument("--notify-successful-liquidations", type=mango.parse_subscription_target, action="append", default=[], help="The notification target for successful liquidation events") parser.add_argument("--notify-failed-liquidations", type=mango.parse_subscription_target, action="append", default=[], help="The notification target for failed liquidation events") parser.add_argument("--notify-errors", type=mango.parse_subscription_target, action="append", default=[], help="The notification target for error events") parser.add_argument("--dry-run", action="store_true", default=False, help="runs as read-only and does not perform any transactions") args = parser.parse_args() logging.getLogger().setLevel(args.log_level) for notify in args.notify_errors: handler = mango.NotificationHandler(notify) handler.setLevel(logging.ERROR) logging.getLogger().addHandler(handler) logging.warning(mango.WARNING_DISCLAIMER_TEXT) try: context = mango.Context.from_command_line_parameters(args) wallet = mango.Wallet.from_command_line_parameters_or_raise(args) account_address = args.address liquidator_name = args.name logging.info(f"Context: {context}") logging.info(f"Wallet address: {wallet.address}") logging.info(f"Margin account address: {account_address}") group = mango.Group.load(context) logging.info("Checking wallet accounts.") scout = mango.AccountScout() report = scout.verify_account_prepared_for_group(context, group, wallet.address) logging.info(f"Wallet account report: {report}") if report.has_errors: raise Exception(f"Account '{wallet.address}' is not prepared for group '{group.address}'.") logging.info("Wallet accounts OK.") liquidations_publisher = mango.EventSource[mango.LiquidationEvent]() liquidations_publisher.subscribe(on_next=lambda event: logging.info( str(mango.TransactionScout.load(context, event.signature)))) for notification_target in args.notify_liquidations: liquidations_publisher.subscribe(on_next=notification_target.send) for notification_target in args.notify_successful_liquidations: filtering = mango.FilteringNotificationTarget( notification_target, lambda item: isinstance(item, mango.LiquidationEvent) and item.succeeded) liquidations_publisher.subscribe(on_next=filtering.send) for notification_target in args.notify_failed_liquidations: filtering = mango.FilteringNotificationTarget(notification_target, lambda item: isinstance( item, mango.LiquidationEvent) and not item.succeeded) liquidations_publisher.subscribe(on_next=filtering.send) # TODO: Add proper liquidator classes here when they're written for V3 if args.dry_run: account_liquidator: mango.AccountLiquidator = mango.NullAccountLiquidator() else: account_liquidator = mango.NullAccountLiquidator() # TODO - fetch prices when available for V3. # prices = group.fetch_token_prices(context) account = mango.Account.load(context, account_address, group) worthwhile_threshold = Decimal(0) # No threshold - don't take this into account. liquidatable_report = mango.LiquidatableReport.build(group, [], account, worthwhile_threshold) transaction_id = account_liquidator.liquidate(liquidatable_report) if transaction_id is None: print("No transaction sent.") else: print("Transaction ID:", transaction_id) print("Waiting for confirmation...") context.wait_for_confirmation(transaction_id) transaction_scout = mango.TransactionScout.load(context, transaction_id) print(str(transaction_scout)) except Exception as exception: logging.critical(f"Liquidator stopped because of exception: {exception} - {traceback.format_exc()}") except: logging.critical(f"Liquidator stopped because of uncatchable error: {traceback.format_exc()}") finally: logging.info("Liquidation complete.")