2021-05-08 08:01:30 -07:00
|
|
|
#!/usr/bin/env pyston3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
2021-06-07 07:10:18 -07:00
|
|
|
import os
|
2021-05-08 08:01:30 -07:00
|
|
|
import os.path
|
2021-06-07 07:10:18 -07:00
|
|
|
import sys
|
2021-05-08 08:01:30 -07:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
from solana.publickey import PublicKey
|
|
|
|
from solana.system_program import TransferParams, transfer
|
|
|
|
from solana.transaction import Transaction
|
|
|
|
|
2021-06-07 07:10:18 -07:00
|
|
|
sys.path.insert(0, os.path.abspath(
|
|
|
|
os.path.join(os.path.dirname(__file__), '..')))
|
|
|
|
import mango # nopep8
|
2021-05-08 08:01:30 -07:00
|
|
|
|
|
|
|
# We explicitly want argument parsing to be outside the main try-except block because some arguments
|
2021-05-11 11:24:02 -07:00
|
|
|
# (like --help) will cause an exit, which our except: block traps.
|
2021-05-08 08:01:30 -07:00
|
|
|
parser = argparse.ArgumentParser(description="Sends SOL to a different address.")
|
2021-06-17 03:18:35 -07:00
|
|
|
mango.Context.add_context_command_line_parameters(parser)
|
2021-05-08 08:01:30 -07:00
|
|
|
parser.add_argument("--id-file", type=str, default="id.json",
|
|
|
|
help="file containing the JSON-formatted wallet private key")
|
|
|
|
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("--address", type=PublicKey,
|
|
|
|
help="Destination address for the SPL token - can be either the actual token address or the address of the owner of the token address")
|
|
|
|
parser.add_argument("--quantity", type=Decimal, required=True, help="quantity of token to send")
|
|
|
|
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)
|
2021-06-07 07:10:18 -07:00
|
|
|
logging.warning(mango.WARNING_DISCLAIMER_TEXT)
|
2021-05-08 08:01:30 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
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)
|
2021-06-07 07:10:18 -07:00
|
|
|
wallet = mango.Wallet.load(id_filename)
|
2021-05-08 08:01:30 -07:00
|
|
|
|
2021-06-17 03:18:35 -07:00
|
|
|
context = mango.Context.from_context_command_line_parameters(args)
|
2021-05-08 08:01:30 -07:00
|
|
|
|
|
|
|
logging.info(f"Context: {context}")
|
|
|
|
logging.info(f"Wallet address: {wallet.address}")
|
|
|
|
|
|
|
|
sol_balance = context.fetch_sol_balance(wallet.address)
|
|
|
|
print(f"Balance: {sol_balance} SOL")
|
|
|
|
|
|
|
|
# "A lamport has a value of 0.000000001 SOL." from https://docs.solana.com/introduction
|
2021-06-07 07:10:18 -07:00
|
|
|
lamports = int(args.quantity * mango.SOL_DECIMAL_DIVISOR)
|
2021-05-08 08:01:30 -07:00
|
|
|
source = wallet.address
|
|
|
|
destination = args.address
|
|
|
|
|
|
|
|
text_amount = f"{lamports} lamports (SOL @ 9 decimal places)"
|
|
|
|
print(f"Sending {text_amount}")
|
|
|
|
print(f" From: {source}")
|
|
|
|
print(f" To: {destination}")
|
|
|
|
|
|
|
|
if args.dry_run:
|
|
|
|
print("Skipping actual transfer - dry run.")
|
|
|
|
else:
|
|
|
|
transaction = Transaction()
|
|
|
|
params = TransferParams(from_pubkey=source, to_pubkey=destination, lamports=lamports)
|
|
|
|
transaction.add(transfer(params))
|
|
|
|
|
2021-05-11 11:24:02 -07:00
|
|
|
transfer_response = context.client.send_transaction(transaction, wallet.account)
|
2021-05-08 08:01:30 -07:00
|
|
|
transaction_id = context.unwrap_transaction_id_or_raise_exception(transfer_response)
|
|
|
|
print(f"Waiting on transaction ID: {transaction_id}")
|
|
|
|
context.wait_for_confirmation(transaction_id)
|
|
|
|
|
|
|
|
updated_balance = context.fetch_sol_balance(wallet.address)
|
|
|
|
print(f"{text_amount} sent. Balance now: {updated_balance} SOL")
|
|
|
|
except Exception as exception:
|
|
|
|
logging.critical(f"send-sols stopped because of exception: {exception} - {traceback.format_exc()}")
|
|
|
|
except:
|
|
|
|
logging.critical(f"send-sols stopped because of uncatchable error: {traceback.format_exc()}")
|