61 lines
2.4 KiB
Plaintext
Executable File
61 lines
2.4 KiB
Plaintext
Executable File
#!/usr/bin/env pyston3
|
|
|
|
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
|
|
|
|
parser = argparse.ArgumentParser(description="Withdraw funds from a margin account")
|
|
mango.Context.add_command_line_parameters(parser)
|
|
mango.Wallet.add_command_line_parameters(parser)
|
|
parser.add_argument("--symbol", type=str, required=True, help="token symbol to withdraw (e.g. USDC)")
|
|
parser.add_argument("--quantity", type=Decimal, required=True, help="quantity token to withdraw")
|
|
parser.add_argument("--allow-borrow", action="store_true", default=False,
|
|
help="allow borrowing to fund the withdrawal")
|
|
args = parser.parse_args()
|
|
|
|
logging.getLogger().setLevel(args.log_level)
|
|
|
|
context = mango.Context.from_command_line_parameters(args)
|
|
wallet = mango.Wallet.from_command_line_parameters_or_raise(args)
|
|
|
|
group = mango.Group.load(context, context.group_id)
|
|
accounts = mango.Account.load_all_for_owner(context, wallet.address, group)
|
|
if len(accounts) == 0:
|
|
raise Exception(f"Could not find any margin accounts for '{wallet.address}'.")
|
|
account = accounts[0]
|
|
|
|
token = context.token_lookup.find_by_symbol(args.symbol)
|
|
if token is None:
|
|
raise Exception(f"Could not find token with symbol '{args.symbol}'.")
|
|
|
|
token_account = mango.TokenAccount.fetch_or_create_largest_for_owner_and_token(context, wallet.account, token)
|
|
withdrawal_value = mango.TokenValue(token, args.quantity)
|
|
withdrawal_token_account = mango.TokenAccount(
|
|
token_account.account_info, token_account.version, token_account.owner, withdrawal_value)
|
|
|
|
|
|
root_bank = None
|
|
for token_info in group.tokens:
|
|
if token_info is not None and token_info.token is not None and token_info.token.mint == token.mint:
|
|
root_bank = token_info.root_bank
|
|
if root_bank is None:
|
|
raise Exception(f"Could not find token root bank for symbol '{args.symbol}'.")
|
|
|
|
node_bank = root_bank.pick_node_bank(context)
|
|
|
|
signers: mango.CombinableInstructions = mango.CombinableInstructions.from_wallet(wallet)
|
|
withdraw = mango.build_withdraw_instructions(
|
|
context, wallet, group, account, root_bank, node_bank, withdrawal_token_account, args.allow_borrow)
|
|
|
|
all_instructions = signers + withdraw
|
|
transaction_id = all_instructions.execute_and_unwrap_transaction_ids(context)[0]
|
|
print("Signature:", transaction_id)
|