68 lines
2.8 KiB
Plaintext
Executable File
68 lines
2.8 KiB
Plaintext
Executable File
#!/usr/bin/env pyston3
|
|
|
|
import argparse
|
|
import logging
|
|
import os
|
|
import os.path
|
|
import sys
|
|
import traceback
|
|
|
|
from decimal import Decimal
|
|
|
|
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="Balance the value of tokens in a Mango Markets group to specific values or percentages.")
|
|
mango.Context.add_command_line_parameters(parser)
|
|
mango.Wallet.add_command_line_parameters(parser)
|
|
parser.add_argument("--target", type=str, action="append", required=True,
|
|
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)")
|
|
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)
|
|
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)
|
|
action_threshold = args.action_threshold
|
|
adjustment_factor = args.adjustment_factor
|
|
|
|
logging.info(f"Context: {context}")
|
|
logging.info(f"Wallet address: {wallet.address}")
|
|
|
|
group = mango.Group.load(context)
|
|
tokens = [basket_token.token for basket_token in group.basket_tokens]
|
|
|
|
balance_parser = mango.TargetBalanceParser(tokens)
|
|
targets = list(map(balance_parser.parse, args.target))
|
|
logging.info(f"Targets: {targets}")
|
|
|
|
prices = group.fetch_token_prices(context)
|
|
logging.info(f"Prices: {prices}")
|
|
|
|
if args.dry_run:
|
|
trade_executor: mango.TradeExecutor = mango.NullTradeExecutor(print)
|
|
else:
|
|
trade_executor = mango.SerumImmediateTradeExecutor(context, wallet, adjustment_factor, print)
|
|
|
|
wallet_balancer = mango.LiveWalletBalancer(
|
|
context, wallet, group, trade_executor, action_threshold, tokens, targets)
|
|
wallet_balancer.balance(prices)
|
|
|
|
logging.info("Balancing completed.")
|
|
except Exception as exception:
|
|
logging.critical(f"Balancing stopped because of exception: {exception} - {traceback.format_exc()}")
|
|
except:
|
|
logging.critical(f"Balancing stopped because of uncatchable error: {traceback.format_exc()}")
|