mango-explorer/bin/cancel-my-orders

71 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import os
import os.path
import sys
from solana.publickey import PublicKey
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
import mango # nopep8
parser = argparse.ArgumentParser(
description="Cancels all orders on a market from the current wallet."
)
mango.ContextBuilder.add_command_line_parameters(parser)
mango.Wallet.add_command_line_parameters(parser)
parser.add_argument(
"--market",
type=str,
required=True,
help="market symbol where orders are placed (e.g. ETH/USDC)",
)
parser.add_argument(
"--account-address",
type=PublicKey,
help="address of the specific account to use, if more than one available",
)
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)
context = mango.ContextBuilder.from_command_line_parameters(args)
wallet = mango.Wallet.from_command_line_parameters_or_raise(args)
group = mango.Group.load(context, context.group_address)
account = mango.Account.load_for_owner_by_address(
context, wallet.address, group, args.account_address
)
market = context.market_lookup.find_by_symbol(args.market)
if market is None:
raise Exception(f"Could not find market {args.market}")
market_operations = mango.create_market_operations(
context, wallet, account, market, args.dry_run
)
orders = market_operations.load_my_orders(include_expired=True)
if len(orders) == 0:
mango.output(f"No open orders on {market.symbol}")
else:
if isinstance(market_operations.market, mango.PerpMarket):
instruction_builder = mango.PerpMarketInstructionBuilder(
context, wallet, market_operations.market, group, account
)
cancel_all = instruction_builder.build_cancel_all_orders_instructions()
signers: mango.CombinableInstructions = (
mango.CombinableInstructions.from_wallet(wallet)
)
cancel_all_signatures = (signers + cancel_all).execute(context)
mango.output(f"Cancelling all perp orders: {cancel_all_signatures}")
else:
for order in orders:
mango.output("Cancelling:", order)
cancellation_signatures = market_operations.cancel_order(order)
mango.output(f"Cancellation signature: {cancellation_signatures}")