#!/usr/bin/env python3 import argparse 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="deposit funds into a Mango account") mango.ContextBuilder.add_command_line_parameters(parser) mango.Wallet.add_command_line_parameters(parser) parser.add_argument("--symbol", type=str, required=True, help="token symbol to deposit (e.g. USDC)") parser.add_argument("--quantity", type=Decimal, required=True, help="quantity token to deposit") parser.add_argument("--account-index", type=int, default=0, help="index of the account to use, if more than one available") 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) accounts = mango.Account.load_all_for_owner(context, wallet.address, group) if len(accounts) == 0: raise Exception(f"Could not find any Mango accounts for '{wallet.address}'.") account = accounts[args.account_index] instrument = context.instrument_lookup.find_by_symbol(args.symbol) if instrument is None: raise Exception(f"Could not find instrument with symbol '{args.symbol}'.") token: mango.Token = mango.Token.ensure(instrument) token_account = mango.TokenAccount.fetch_largest_for_owner_and_token(context, wallet.keypair.public_key, token) if token_account is None: raise Exception(f"Could not find token account for token {token} with owner {wallet.keypair}.") deposit_value = mango.InstrumentValue(token, args.quantity) deposit_token_account = mango.TokenAccount( token_account.account_info, token_account.version, token_account.owner, deposit_value) token_bank = group.token_bank_by_instrument(token) root_bank = token_bank.ensure_root_bank(context) node_bank = root_bank.pick_node_bank(context) signers: mango.CombinableInstructions = mango.CombinableInstructions.from_wallet(wallet) deposit = mango.build_deposit_instructions( context, wallet, group, account, root_bank, node_bank, deposit_token_account) all_instructions = signers + deposit transaction_ids = all_instructions.execute(context) print("Transaction IDs:", transaction_ids)