2021-04-28 10:36:48 -07:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import os, sys
|
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
# Get the full path to this script.
|
|
|
|
|
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.
|
|
|
|
|
startup_directory = notebook_directory / "meta" / "startup"
|
|
|
|
|
sys.path.append(str(startup_directory))
|
|
|
|
|
|
|
|
|
|
import argparse
|
2021-04-29 02:53:01 -07:00
|
|
|
|
import logging
|
2021-04-28 10:36:48 -07:00
|
|
|
|
import os.path
|
|
|
|
|
import projectsetup
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
from solana.publickey import PublicKey
|
|
|
|
|
|
|
|
|
|
from AccountScout import AccountScout
|
2021-05-06 07:27:25 -07:00
|
|
|
|
from AccountLiquidator import AccountLiquidator, ForceCancelOrdersAccountLiquidator, NullAccountLiquidator
|
2021-04-28 10:36:48 -07:00
|
|
|
|
from BaseModel import Group, MarginAccount
|
|
|
|
|
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
|
|
|
|
|
from Wallet import Wallet
|
|
|
|
|
|
|
|
|
|
# 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.')
|
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-04-28 10:36:48 -07:00
|
|
|
|
parser.add_argument('--margin-account-address', type=PublicKey,
|
|
|
|
|
help="Solana address of the Mango Markets margin account to be liquidated")
|
2021-05-06 07:27:25 -07:00
|
|
|
|
parser.add_argument("--dry-run", type=bool,
|
|
|
|
|
help="runs as read-only and does not perform any transactions")
|
2021-04-28 10:36:48 -07:00
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
margin_account_address = args.margin_account_address
|
|
|
|
|
|
|
|
|
|
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}")
|
|
|
|
|
logging.info(f"Margin account address: {margin_account_address}")
|
|
|
|
|
|
2021-04-29 08:33:04 -07:00
|
|
|
|
group = Group.load(context)
|
|
|
|
|
|
|
|
|
|
logging.info("Checking wallet accounts.")
|
|
|
|
|
scout = 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.")
|
|
|
|
|
|
2021-04-28 10:36:48 -07:00
|
|
|
|
print("Wallet Balances Before:")
|
|
|
|
|
print(f" SOL balance: {context.fetch_sol_balance(wallet.address):>18,.8f}")
|
|
|
|
|
for token in group.tokens:
|
|
|
|
|
print(f"{token.name:>7} balance: {context.fetch_token_balance(wallet.address, token.mint):>18,.8f}")
|
|
|
|
|
|
2021-05-06 06:21:40 -07:00
|
|
|
|
prices = group.fetch_token_prices()
|
2021-04-29 08:33:04 -07:00
|
|
|
|
margin_account = MarginAccount.load(context, margin_account_address, group)
|
2021-04-28 10:36:48 -07:00
|
|
|
|
intrinsic_balance_sheets_before = margin_account.get_intrinsic_balance_sheets(group)
|
|
|
|
|
print("Margin Account Before:", intrinsic_balance_sheets_before)
|
2021-05-06 07:27:25 -07:00
|
|
|
|
|
|
|
|
|
if args.dry_run:
|
|
|
|
|
account_liquidator: AccountLiquidator = NullAccountLiquidator()
|
|
|
|
|
else:
|
|
|
|
|
account_liquidator = ForceCancelOrdersAccountLiquidator(context, wallet)
|
|
|
|
|
|
|
|
|
|
transaction_id = account_liquidator.liquidate(group, margin_account, prices)
|
2021-04-28 10:36:48 -07:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
group_after = Group.load(context)
|
|
|
|
|
margin_account_after_liquidation = MarginAccount.load(context, group_after, margin_account.address)
|
|
|
|
|
intrinsic_balance_sheets_after = margin_account_after_liquidation.get_intrinsic_balance_sheets(group_after)
|
|
|
|
|
print("Margin Account After:", intrinsic_balance_sheets_after)
|
|
|
|
|
print("Wallet Balances After:")
|
|
|
|
|
print(f" SOL balance: {context.fetch_sol_balance(wallet.address):>18,.8f}")
|
|
|
|
|
for token in group.tokens:
|
|
|
|
|
print(f"{token.name:>7} balance: {context.fetch_token_balance(wallet.address, token.mint):>18,.8f}")
|
|
|
|
|
|
|
|
|
|
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.")
|