mango-explorer/bin/close-wrapped-sol-account

89 lines
3.0 KiB
Plaintext
Raw Normal View History

2021-06-02 12:03:46 -07:00
#!/usr/bin/env pyston3
import os
import sys
from pathlib import Path
# Get the full path to this script.
script_path = Path(os.path.realpath(__file__))
# The parent of the script is the bin directory.
# The parent of the bin directory is the notebook directory.
# It's this notebook directory we want.
notebook_directory = script_path.parent.parent
# Add the notebook directory to our import path.
sys.path.append(str(notebook_directory))
# Add the startup directory to our import path.
startup_directory = notebook_directory / "meta" / "startup"
sys.path.append(str(startup_directory))
import argparse
import logging
import projectsetup # noqa: F401
import typing
from solana.account import Account
from solana.publickey import PublicKey
from spl.token.constants import TOKEN_PROGRAM_ID
from solana.transaction import Transaction
from spl.token.instructions import CloseAccountParams, close_account
from BaseModel import TokenAccount, TokenLookup
from Constants import WARNING_DISCLAIMER_TEXT
from Context import default_context as context
from Wallet import Wallet
parser = argparse.ArgumentParser(description="Closes a Wrapped SOL account.")
2021-06-02 12:03:46 -07:00
parser.add_argument("--id-file", type=str, default="id.json",
help="file containing the JSON-formatted wallet private key")
parser.add_argument("--address", type=PublicKey,
help="Public key of the Wrapped SOL account to close")
parser.add_argument("--log-level", default=logging.WARNING, type=lambda level: getattr(logging, level),
help="level of verbosity to log (possible values: DEBUG, INFO, WARNING, ERROR, CRITICAL)")
parser.add_argument("--overwrite", action="store_true", default=False,
help="overwrite the ID file, if it exists (use with care!)")
args = parser.parse_args()
logging.getLogger().setLevel(args.log_level)
logging.warning(WARNING_DISCLAIMER_TEXT)
id_filename = args.id_file
if not os.path.isfile(id_filename):
logging.error(f"Wallet file '{id_filename}' is not present.")
sys.exit(1)
wallet = Wallet.load(id_filename)
lookups = TokenLookup.default_lookups()
wrapped_sol = lookups.find_by_symbol("SOL")
token_account = TokenAccount.load(context, args.address)
if (token_account is None) or (token_account.mint != wrapped_sol.mint):
raise Exception(f"Account {args.address} is not a {wrapped_sol.name} account.")
transaction = Transaction()
signers: typing.List[Account] = [wallet.account]
payer = wallet.address
transaction.add(
close_account(
CloseAccountParams(
account=args.address,
owner=wallet.address,
dest=wallet.address,
program_id=TOKEN_PROGRAM_ID,
)
)
)
print(f"Closing account: {args.address} with balance {token_account.amount} lamports.")
response = context.client.send_transaction(transaction, *signers)
transaction_id = context.unwrap_transaction_id_or_raise_exception(response)
print(f"Waiting on transaction ID: {transaction_id}")
context.wait_for_confirmation(transaction_id)
print("Done.")