mango-explorer/bin/serum-sell

61 lines
1.8 KiB
Plaintext
Raw Normal View History

2021-10-08 10:15:40 -07:00
#!/usr/bin/env python3
import argparse
import logging
import os
import os.path
import sys
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="Sells an SPL token in a Serum market.")
mango.ContextBuilder.add_command_line_parameters(parser)
mango.Wallet.add_command_line_parameters(parser)
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 SELL price (akin to maximum slippage)",
)
parser.add_argument(
"--wait",
action="store_true",
default=False,
help="wait until the transactions are confirmed",
)
parser.add_argument(
"--dry-run",
action="store_true",
default=False,
help="runs as read-only and does not perform any transactions",
)
args: argparse.Namespace = mango.parse_args(parser)
with mango.ContextBuilder.from_command_line_parameters(args) as context:
wallet = mango.Wallet.from_command_line_parameters_or_raise(args)
adjustment_factor = args.adjustment_factor
logging.info(f"Wallet address: {wallet.address}")
if args.dry_run:
trade_executor: mango.TradeExecutor = mango.NullTradeExecutor()
else:
trade_executor = mango.ImmediateTradeExecutor(
context, wallet, None, adjustment_factor
)
order = trade_executor.sell(args.symbol, args.quantity)
logging.info(f"Sell completed for {order}")