#!/usr/bin/env pyston3 import argparse import json 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="Buys one of the tokens in a Mango Markets group.") mango.Context.add_context_command_line_parameters(parser) parser.add_argument("--id-file", type=str, default="id.json", help="file containing the JSON-formatted wallet private key") parser.add_argument("--token-data-file", type=str, default="solana.tokenlist.json", help="data file that contains token symbols, names, mints and decimals (format is same as https://raw.githubusercontent.com/solana-labs/token-list/main/src/tokens/solana.tokenlist.json)") parser.add_argument("--log-level", default=logging.INFO, type=lambda level: getattr(logging, level), help="level of verbosity to log (possible values: DEBUG, INFO, WARNING, ERROR, CRITICAL)") parser.add_argument("--symbol", type=str, required=True, help="market symbol to buy (e.g. ETH/USDC)") parser.add_argument("--quantity", type=Decimal, required=True, help="quantity of token to buy") parser.add_argument("--adjustment-factor", type=Decimal, default=Decimal("0.05"), help="factor by which to adjust the BUY price (akin to maximum slippage)") parser.add_argument("--wait", action="store_true", default=False, help="wait until the transaction is confirmed") 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: 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 = mango.Wallet.load(id_filename) adjustment_factor = args.adjustment_factor context = mango.Context.from_context_command_line_parameters(args) logging.info(f"Context: {context}") logging.info(f"Wallet address: {wallet.address}") symbol = args.symbol.upper() if args.dry_run: trade_executor: mango.TradeExecutor = mango.NullTradeExecutor() else: with open(args.token_data_file) as json_file: token_data = json.load(json_file) spot_market_lookup = mango.SpotMarketLookup(token_data) trade_executor = mango.SerumImmediateTradeExecutor(context, wallet, spot_market_lookup, adjustment_factor) transaction_id = trade_executor.buy(symbol, args.quantity) if args.wait: logging.info(f"Waiting on {transaction_id}") context.wait_for_confirmation(transaction_id) logging.info("Buy completed.") except Exception as exception: logging.critical(f"Buy stopped because of exception: {exception} - {traceback.format_exc()}") except: logging.critical(f"Buy stopped because of uncatchable error: {traceback.format_exc()}")