#!/usr/bin/env pyston3 import argparse import logging import os import os.path import sys import time import traceback 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="Run a single pass of the liquidator for a Mango Markets group.") 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("--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) liquidator_name = args.name logging.info(f"Context: {context}") logging.info(f"Wallet address: {wallet.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) if args.dry_run: account_liquidator: mango.AccountLiquidator = mango.NullAccountLiquidator() else: intermediate = mango.ForceCancelOrdersAccountLiquidator(context, wallet) account_liquidator = mango.ReportingAccountLiquidator(intermediate, context, wallet, liquidations_publisher, liquidator_name) wallet_balancer = mango.NullWalletBalancer() liquidation_processor = mango.LiquidationProcessor(context, account_liquidator, wallet_balancer) started_at = time.time() ripe = group.load_ripe_margin_accounts() liquidation_processor.update_margin_accounts(ripe) group = mango.Group.load(context) # Refresh group data prices = group.fetch_token_prices(context) liquidation_processor.update_prices(group, prices) time_taken = time.time() - started_at logging.info(f"Check of all margin accounts complete. Time taken: {time_taken:.2f} seconds.") 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("Liquidator completed.")