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="Sells an SPL token in a Serum market.")
|
2021-06-17 04:06:39 -07:00
|
|
|
mango.Context.add_command_line_parameters(parser)
|
|
|
|
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 SELL 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-05-04 09:18:09 -07:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-05-06 06:21:40 -07:00
|
|
|
logging.getLogger().setLevel(args.log_level)
|
2021-06-07 07:10:18 -07:00
|
|
|
logging.warning(mango.WARNING_DISCLAIMER_TEXT)
|
2021-05-04 09:18:09 -07:00
|
|
|
|
|
|
|
try:
|
2021-06-17 04:06:39 -07:00
|
|
|
context = mango.Context.from_command_line_parameters(args)
|
|
|
|
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-06-18 05:50:33 -07:00
|
|
|
trade_executor = mango.SerumImmediateTradeExecutor(context, wallet, adjustment_factor)
|
2021-05-06 07:27:25 -07:00
|
|
|
|
2021-06-08 16:47:15 -07:00
|
|
|
transaction_id = trade_executor.sell(symbol, args.quantity)
|
|
|
|
if args.wait:
|
|
|
|
logging.info(f"Waiting on {transaction_id}")
|
|
|
|
context.wait_for_confirmation(transaction_id)
|
2021-05-04 09:18:09 -07:00
|
|
|
|
|
|
|
logging.info("Sell 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()}")
|