2021-05-06 11:06:47 -07:00
|
|
|
#!/usr/bin/env pyston3
|
2021-04-26 09:09:35 -07:00
|
|
|
|
2021-05-11 11:24:02 -07:00
|
|
|
import os
|
|
|
|
import sys
|
2021-04-26 09:09:35 -07:00
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
2021-05-11 11:24:02 -07:00
|
|
|
# Get the full path to this script.
|
2021-04-26 09:09:35 -07:00
|
|
|
script_path = Path(os.path.realpath(__file__))
|
|
|
|
|
|
|
|
# The parent of the script is the bin directory.
|
|
|
|
# The parent of the bin directory is the notebook directory.
|
|
|
|
# It's this notebook directory we want.
|
|
|
|
notebook_directory = script_path.parent.parent
|
|
|
|
|
|
|
|
# Add the notebook directory to our import path.
|
|
|
|
sys.path.append(str(notebook_directory))
|
|
|
|
|
|
|
|
# Add the startup directory to our import path.
|
2021-04-28 10:36:48 -07:00
|
|
|
startup_directory = notebook_directory / "meta" / "startup"
|
2021-04-26 09:09:35 -07:00
|
|
|
sys.path.append(str(startup_directory))
|
|
|
|
|
2021-04-27 02:53:43 -07:00
|
|
|
import argparse
|
2021-04-29 02:53:01 -07:00
|
|
|
import logging
|
2021-04-26 09:09:35 -07:00
|
|
|
import os.path
|
2021-05-11 11:24:02 -07:00
|
|
|
import projectsetup # noqa: F401
|
2021-04-28 10:36:48 -07:00
|
|
|
import time
|
2021-04-26 09:09:35 -07:00
|
|
|
import traceback
|
|
|
|
|
2021-05-06 06:21:40 -07:00
|
|
|
from decimal import Decimal
|
|
|
|
|
2021-04-26 09:09:35 -07:00
|
|
|
from AccountScout import AccountScout
|
2021-05-06 07:27:25 -07:00
|
|
|
from AccountLiquidator import AccountLiquidator, ForceCancelOrdersAccountLiquidator, NullAccountLiquidator
|
2021-04-27 01:41:22 -07:00
|
|
|
from BaseModel import Group
|
2021-04-28 10:36:48 -07:00
|
|
|
from Constants import WARNING_DISCLAIMER_TEXT
|
|
|
|
from Context import Context, default_cluster, default_cluster_url, default_program_id, default_dex_program_id, default_group_name, default_group_id
|
2021-05-10 06:48:19 -07:00
|
|
|
from PollingLiquidator import PollingLiquidator
|
2021-05-06 06:21:40 -07:00
|
|
|
from TradeExecutor import SerumImmediateTradeExecutor
|
2021-04-26 09:09:35 -07:00
|
|
|
from Wallet import Wallet
|
2021-05-11 11:24:02 -07:00
|
|
|
from WalletBalancer import LiveWalletBalancer, NullWalletBalancer, TargetBalanceParser, WalletBalancer
|
2021-04-26 09:09:35 -07:00
|
|
|
|
2021-04-28 10:36:48 -07:00
|
|
|
# We explicitly want argument parsing to be outside the main try-except block because some arguments
|
2021-05-11 11:24:02 -07:00
|
|
|
# (like --help) will cause an exit, which our except: block traps.
|
2021-05-07 02:47:01 -07:00
|
|
|
parser = argparse.ArgumentParser(description="Run a liquidator for a Mango Markets group.")
|
2021-04-29 02:53:01 -07:00
|
|
|
parser.add_argument("--cluster", type=str, default=default_cluster,
|
2021-04-28 10:36:48 -07:00
|
|
|
help="Solana RPC cluster name")
|
2021-04-29 02:53:01 -07:00
|
|
|
parser.add_argument("--cluster-url", type=str, default=default_cluster_url,
|
2021-04-28 10:36:48 -07:00
|
|
|
help="Solana RPC cluster URL")
|
2021-04-29 02:53:01 -07:00
|
|
|
parser.add_argument("--program-id", type=str, default=default_program_id,
|
2021-04-28 10:36:48 -07:00
|
|
|
help="Mango program ID/address")
|
2021-04-29 02:53:01 -07:00
|
|
|
parser.add_argument("--dex-program-id", type=str, default=default_dex_program_id,
|
2021-04-28 10:36:48 -07:00
|
|
|
help="DEX program ID/address")
|
2021-04-29 02:53:01 -07:00
|
|
|
parser.add_argument("--group-name", type=str, default=default_group_name,
|
2021-04-28 10:36:48 -07:00
|
|
|
help="Mango group name")
|
2021-04-29 02:53:01 -07:00
|
|
|
parser.add_argument("--group-id", type=str, default=default_group_id,
|
2021-04-28 10:36:48 -07:00
|
|
|
help="Mango group ID/address")
|
2021-04-29 02:53:01 -07:00
|
|
|
parser.add_argument("--id-file", type=str, default="id.json",
|
2021-04-28 10:36:48 -07:00
|
|
|
help="file containing the JSON-formatted wallet private key")
|
2021-05-06 06:21:40 -07:00
|
|
|
parser.add_argument("--log-level", default=logging.INFO, type=lambda level: getattr(logging, level),
|
2021-04-29 02:53:01 -07:00
|
|
|
help="level of verbosity to log (possible values: DEBUG, INFO, WARNING, ERROR, CRITICAL)")
|
2021-05-12 02:52:26 -07:00
|
|
|
parser.add_argument("--throttle-reload-to-seconds", type=int, default=60,
|
|
|
|
help="minimum number of seconds between each full margin account reload loop (including time taken processing accounts)")
|
|
|
|
parser.add_argument("--throttle-ripe-update-to-seconds", type=int, default=5,
|
|
|
|
help="minimum number of seconds between each ripe update loop (including time taken processing accounts)")
|
|
|
|
parser.add_argument("--ripe-update-iterations", type=int, default=10,
|
|
|
|
help="number of iterations of ripe updates before performing a full reload of all margin accounts")
|
2021-05-07 02:47:01 -07:00
|
|
|
parser.add_argument("--target", type=str, action="append",
|
2021-05-06 06:21:40 -07:00
|
|
|
help="token symbol plus target value or percentage, separated by a colon (e.g. 'ETH:2.5' or 'ETH:33%')")
|
|
|
|
parser.add_argument("--action-threshold", type=Decimal, default=Decimal("0.01"),
|
|
|
|
help="fraction of total wallet value a trade must be above to be carried out")
|
|
|
|
parser.add_argument("--adjustment-factor", type=Decimal, default=Decimal("0.05"),
|
|
|
|
help="factor by which to adjust the SELL price (akin to maximum slippage)")
|
2021-05-07 02:47:01 -07:00
|
|
|
parser.add_argument("--dry-run", action="store_true", default=False,
|
2021-05-06 07:27:25 -07:00
|
|
|
help="runs as read-only and does not perform any transactions")
|
2021-04-28 10:36:48 -07:00
|
|
|
args = parser.parse_args()
|
2021-04-26 09:09:35 -07:00
|
|
|
|
2021-05-06 06:21:40 -07:00
|
|
|
logging.getLogger().setLevel(args.log_level)
|
2021-04-29 04:11:28 -07:00
|
|
|
logging.warning(WARNING_DISCLAIMER_TEXT)
|
2021-04-29 02:53:01 -07:00
|
|
|
|
2021-04-28 10:36:48 -07:00
|
|
|
try:
|
|
|
|
id_filename = args.id_file
|
|
|
|
if not os.path.isfile(id_filename):
|
|
|
|
logging.error(f"Wallet file '{id_filename}' is not present.")
|
|
|
|
sys.exit(1)
|
|
|
|
wallet = Wallet.load(id_filename)
|
2021-05-06 06:21:40 -07:00
|
|
|
action_threshold = args.action_threshold
|
|
|
|
adjustment_factor = args.adjustment_factor
|
2021-04-28 10:36:48 -07:00
|
|
|
|
2021-05-12 02:52:26 -07:00
|
|
|
throttle_reload_to_seconds = args.throttle_reload_to_seconds
|
|
|
|
throttle_ripe_update_to_seconds = args.throttle_ripe_update_to_seconds
|
|
|
|
ripe_update_iterations = args.ripe_update_iterations
|
2021-04-28 10:36:48 -07:00
|
|
|
|
|
|
|
context = Context(args.cluster, args.cluster_url, args.program_id, args.dex_program_id, args.group_name,
|
|
|
|
args.group_id)
|
|
|
|
|
2021-04-29 08:33:04 -07:00
|
|
|
logging.info(f"Context: {context}")
|
2021-04-28 10:36:48 -07:00
|
|
|
logging.info(f"Wallet address: {wallet.address}")
|
|
|
|
|
|
|
|
group = Group.load(context)
|
2021-05-11 12:32:20 -07:00
|
|
|
tokens = [basket_token.token for basket_token in group.basket_tokens]
|
2021-04-28 10:36:48 -07:00
|
|
|
|
|
|
|
logging.info("Checking wallet accounts.")
|
|
|
|
scout = AccountScout()
|
2021-04-29 08:33:04 -07:00
|
|
|
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}'.")
|
|
|
|
|
2021-04-28 10:36:48 -07:00
|
|
|
logging.info("Wallet accounts OK.")
|
|
|
|
|
2021-05-06 07:27:25 -07:00
|
|
|
if args.dry_run:
|
|
|
|
account_liquidator: AccountLiquidator = NullAccountLiquidator()
|
|
|
|
else:
|
|
|
|
account_liquidator = ForceCancelOrdersAccountLiquidator(context, wallet)
|
|
|
|
|
|
|
|
if args.dry_run or (args.target is None) or (len(args.target) == 0):
|
2021-05-11 11:24:02 -07:00
|
|
|
wallet_balancer: WalletBalancer = NullWalletBalancer()
|
2021-05-06 06:21:40 -07:00
|
|
|
else:
|
2021-05-11 12:32:20 -07:00
|
|
|
balance_parser = TargetBalanceParser(tokens)
|
2021-05-06 06:21:40 -07:00
|
|
|
targets = list(map(balance_parser.parse, args.target))
|
|
|
|
trade_executor = SerumImmediateTradeExecutor(context, wallet, group, adjustment_factor)
|
2021-05-11 12:32:20 -07:00
|
|
|
wallet_balancer = LiveWalletBalancer(context, wallet, trade_executor, action_threshold, tokens, targets)
|
2021-05-06 06:21:40 -07:00
|
|
|
|
2021-04-28 10:36:48 -07:00
|
|
|
stop = False
|
2021-05-12 02:52:26 -07:00
|
|
|
liquidator = PollingLiquidator(context, wallet, account_liquidator, wallet_balancer,
|
|
|
|
throttle_to_seconds=throttle_ripe_update_to_seconds,
|
|
|
|
ripe_iterations=ripe_update_iterations)
|
2021-04-28 10:36:48 -07:00
|
|
|
while not stop:
|
|
|
|
started_at = time.time()
|
|
|
|
try:
|
|
|
|
liquidator.run()
|
|
|
|
|
|
|
|
time_taken = time.time() - started_at
|
2021-05-12 02:52:26 -07:00
|
|
|
should_sleep_for = throttle_reload_to_seconds - int(time_taken)
|
2021-04-28 10:36:48 -07:00
|
|
|
sleep_for = max(should_sleep_for, 0)
|
|
|
|
logging.info(f"Check of all margin accounts complete. Time taken: {time_taken:.2f} seconds, sleeping for {sleep_for} seconds...")
|
|
|
|
time.sleep(sleep_for)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
stop = True
|
2021-05-06 06:21:40 -07:00
|
|
|
logging.info("Stopping...")
|
2021-04-28 10:36:48 -07:00
|
|
|
except Exception as exception:
|
|
|
|
logging.critical(f"Iteration failed because of exception: {exception}")
|
2021-04-26 09:09:35 -07:00
|
|
|
|
|
|
|
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.")
|