2021-10-08 10:15:40 -07:00
|
|
|
#!/usr/bin/env python3
|
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
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
2022-02-09 11:31:50 -08:00
|
|
|
sys.path.insert(0, os.path.abspath(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-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)
|
2022-02-09 11:31:50 -08:00
|
|
|
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,
|
2022-02-17 06:54:38 -08:00
|
|
|
help="wait until the transactions are confirmed",
|
2022-02-09 11:31:50 -08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--dry-run",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
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
|
|
|
|
2022-02-17 06:54:38 -08:00
|
|
|
with mango.ContextBuilder.from_command_line_parameters(args) as context:
|
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"Wallet address: {wallet.address}")
|
|
|
|
|
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:
|
2022-02-09 11:31:50 -08:00
|
|
|
trade_executor = mango.ImmediateTradeExecutor(
|
|
|
|
context, wallet, None, adjustment_factor
|
|
|
|
)
|
2021-05-06 07:27:25 -07:00
|
|
|
|
2022-01-12 06:51:30 -08:00
|
|
|
order = trade_executor.sell(args.symbol, args.quantity)
|
2021-08-07 07:07:19 -07:00
|
|
|
logging.info(f"Sell completed for {order}")
|