92 lines
3.8 KiB
Plaintext
Executable File
92 lines
3.8 KiB
Plaintext
Executable File
#!/usr/bin/env pyston3
|
|
|
|
import argparse
|
|
import logging
|
|
import os
|
|
import os.path
|
|
import sys
|
|
import traceback
|
|
import typing
|
|
|
|
from solana.account import Account
|
|
from solana.transaction import Transaction
|
|
|
|
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="Settles all openorders transactions in the Group.")
|
|
mango.Context.add_command_line_parameters(parser)
|
|
mango.Wallet.add_command_line_parameters(parser)
|
|
parser.add_argument("--wait", action="store_true", default=False,
|
|
help="wait until the transaction is confirmed")
|
|
parser.add_argument("--dry-run", action="store_true", default=False,
|
|
help="runs as read-only and does not perform any transactions")
|
|
args = parser.parse_args()
|
|
|
|
logging.getLogger().setLevel(args.log_level)
|
|
logging.warning(mango.WARNING_DISCLAIMER_TEXT)
|
|
|
|
try:
|
|
context = mango.Context.from_command_line_parameters(args)
|
|
wallet = mango.Wallet.from_command_line_parameters_or_raise(args)
|
|
|
|
logging.info(f"Context: {context}")
|
|
logging.info(f"Wallet address: {wallet.address}")
|
|
|
|
group = mango.Group.load(context)
|
|
|
|
transaction = Transaction()
|
|
signers: typing.List[Account] = [wallet.account]
|
|
|
|
for market_metadata in group.markets:
|
|
market = market_metadata.fetch_market(context)
|
|
open_order_accounts = market.find_open_orders_accounts_for_owner(wallet.address)
|
|
if not open_order_accounts:
|
|
logging.warning(
|
|
f"Skipping settling for market {market_metadata.symbol} - no OpenOrders account could be found.")
|
|
continue
|
|
|
|
spot_market = context.market_lookup.find_by_symbol(market_metadata.symbol)
|
|
if spot_market is None:
|
|
logging.warning(
|
|
f"Skipping settling for market {market_metadata.symbol} - no spot market could be found.")
|
|
continue
|
|
|
|
base_token_account = mango.TokenAccount.fetch_largest_for_owner_and_token(
|
|
context, wallet.address, spot_market.base)
|
|
if base_token_account is None:
|
|
logging.warning(
|
|
f"Skipping settling for market {market_metadata.symbol} - no base token account for '{spot_market.base}'.")
|
|
continue
|
|
|
|
quote_token_account = mango.TokenAccount.fetch_largest_for_owner_and_token(
|
|
context, wallet.address, spot_market.quote)
|
|
if quote_token_account is None:
|
|
logging.warning(
|
|
f"Skipping settling for market {market_metadata.symbol} - no quote token account for '{spot_market.quote}'.")
|
|
continue
|
|
|
|
logging.info(f"Adding settle instruction for market {market_metadata.symbol}.")
|
|
settle_instruction = mango.SettleInstructionBuilder(context, wallet, market,
|
|
open_order_accounts[0].address,
|
|
base_token_account.address, quote_token_account.address)
|
|
transaction.add(settle_instruction.build())
|
|
|
|
if args.dry_run:
|
|
print("Skipping transaction processing - dry run is set.")
|
|
else:
|
|
response = context.client.send_transaction(transaction, *signers)
|
|
transaction_id = context.unwrap_transaction_id_or_raise_exception(response)
|
|
if args.wait:
|
|
print(f"Waiting on {transaction_id}")
|
|
context.wait_for_confirmation(transaction_id)
|
|
|
|
logging.info("Settlement completed.")
|
|
except Exception as exception:
|
|
logging.critical(f"group-settle stopped because of exception: {exception} - {traceback.format_exc()}")
|
|
except:
|
|
logging.critical(f"group-settle stopped because of uncatchable error: {traceback.format_exc()}")
|