2021-05-06 11:06:47 -07:00
|
|
|
#!/usr/bin/env pyston3
|
2021-05-04 09:18:09 -07:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
2021-06-07 07:10:18 -07:00
|
|
|
import os
|
2021-05-04 09:18:09 -07:00
|
|
|
import os.path
|
2021-06-07 07:10:18 -07:00
|
|
|
import sys
|
2021-05-04 09:18:09 -07:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
2021-06-07 07:10:18 -07:00
|
|
|
sys.path.insert(0, os.path.abspath(
|
2021-06-25 02:33:40 -07:00
|
|
|
os.path.join(os.path.dirname(__file__), "..")))
|
2021-06-07 07:10:18 -07:00
|
|
|
import mango # nopep8
|
2021-05-04 09:18:09 -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-06-20 11:34:59 -07:00
|
|
|
parser = argparse.ArgumentParser(description="Buys an SPL token in a Serum market.")
|
2021-07-23 02:20:44 -07:00
|
|
|
mango.ContextBuilder.add_command_line_parameters(parser)
|
2021-06-17 04:06:39 -07:00
|
|
|
mango.Wallet.add_command_line_parameters(parser)
|
2021-06-01 11:16:59 -07:00
|
|
|
parser.add_argument("--symbol", type=str, required=True, help="market symbol to buy (e.g. ETH/USDC)")
|
2021-05-06 06:21:40 -07:00
|
|
|
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)")
|
2021-06-08 16:47:15 -07:00
|
|
|
parser.add_argument("--wait", action="store_true", default=False,
|
|
|
|
help="wait until the transaction is confirmed")
|
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-09-13 06:05:19 -07:00
|
|
|
args: argparse.Namespace = mango.parse_args(parser)
|
2021-05-04 09:18:09 -07:00
|
|
|
|
|
|
|
try:
|
2021-07-23 02:20:44 -07:00
|
|
|
context = mango.ContextBuilder.from_command_line_parameters(args)
|
2021-06-17 04:06:39 -07:00
|
|
|
wallet = mango.Wallet.from_command_line_parameters_or_raise(args)
|
|
|
|
|
2021-05-06 06:21:40 -07:00
|
|
|
adjustment_factor = args.adjustment_factor
|
2021-05-04 09:18:09 -07:00
|
|
|
|
|
|
|
logging.info(f"Context: {context}")
|
|
|
|
logging.info(f"Wallet address: {wallet.address}")
|
|
|
|
|
2021-06-01 11:16:59 -07:00
|
|
|
symbol = args.symbol.upper()
|
2021-05-04 09:18:09 -07:00
|
|
|
|
2021-05-06 07:27:25 -07:00
|
|
|
if args.dry_run:
|
2021-06-07 07:10:18 -07:00
|
|
|
trade_executor: mango.TradeExecutor = mango.NullTradeExecutor()
|
2021-05-06 07:27:25 -07:00
|
|
|
else:
|
2021-08-07 07:07:19 -07:00
|
|
|
trade_executor = mango.ImmediateTradeExecutor(context, wallet, None, adjustment_factor)
|
2021-05-06 07:27:25 -07:00
|
|
|
|
2021-08-07 07:07:19 -07:00
|
|
|
order = trade_executor.buy(symbol, args.quantity)
|
|
|
|
logging.info(f"Buy completed for {order}")
|
2021-05-04 09:18:09 -07:00
|
|
|
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()}")
|