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
|
2021-06-08 06:05:00 -07:00
|
|
|
import typing
|
2021-05-08 08:01:30 -07:00
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
from solana.publickey import PublicKey
|
|
|
|
from spl.token.client import Token
|
|
|
|
from spl.token.constants import TOKEN_PROGRAM_ID
|
|
|
|
|
2021-06-07 07:10:18 -07:00
|
|
|
sys.path.insert(0, os.path.abspath(
|
2021-06-25 02:33:40 -07:00
|
|
|
os.path.join(os.path.dirname(__file__), "..")))
|
2021-06-07 07:10:18 -07:00
|
|
|
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-06-20 11:34:59 -07:00
|
|
|
parser = argparse.ArgumentParser(description="Sends an SPL tokens to a different address.")
|
2021-06-17 04:06:39 -07:00
|
|
|
mango.Context.add_command_line_parameters(parser)
|
|
|
|
mango.Wallet.add_command_line_parameters(parser)
|
2021-06-25 07:50:37 -07:00
|
|
|
parser.add_argument("--symbol", type=str, required=True, help="token symbol to send (e.g. ETH)")
|
2021-05-08 08:01:30 -07:00
|
|
|
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:
|
2021-06-17 04:06:39 -07:00
|
|
|
context = mango.Context.from_command_line_parameters(args)
|
|
|
|
wallet = mango.Wallet.from_command_line_parameters_or_raise(args)
|
2021-05-08 08:01:30 -07:00
|
|
|
|
|
|
|
logging.info(f"Context: {context}")
|
|
|
|
logging.info(f"Wallet address: {wallet.address}")
|
|
|
|
|
2021-06-25 07:50:37 -07:00
|
|
|
token = context.token_lookup.find_by_symbol(args.symbol)
|
|
|
|
if token is None:
|
|
|
|
raise Exception(f"Could not find details of token with symbol {args.symbol}.")
|
2021-05-08 08:01:30 -07:00
|
|
|
|
2021-06-25 07:50:37 -07:00
|
|
|
spl_token = Token(context.client, token.mint, TOKEN_PROGRAM_ID, wallet.account)
|
2021-05-11 11:24:02 -07:00
|
|
|
source_accounts = spl_token.get_accounts(wallet.address)
|
2021-05-08 08:01:30 -07:00
|
|
|
source_account = source_accounts["result"]["value"][0]
|
|
|
|
source = PublicKey(source_account["pubkey"])
|
|
|
|
|
|
|
|
# Is the address an actual token account? Or is it the SOL address of the owner?
|
2021-06-08 06:05:00 -07:00
|
|
|
possible_dest: typing.Optional[mango.TokenAccount] = mango.TokenAccount.load(context, args.address)
|
2021-06-25 07:50:37 -07:00
|
|
|
if (possible_dest is not None) and (possible_dest.value.token.mint == token.mint):
|
2021-05-08 08:01:30 -07:00
|
|
|
# We successfully loaded the token account.
|
|
|
|
destination: PublicKey = args.address
|
|
|
|
else:
|
2021-05-11 11:24:02 -07:00
|
|
|
destination_accounts = spl_token.get_accounts(args.address)
|
2021-05-08 08:01:30 -07:00
|
|
|
if len(destination_accounts["result"]["value"]) == 0:
|
2021-06-07 07:10:18 -07:00
|
|
|
raise Exception(
|
|
|
|
f"Could not find destination account using {args.address} as either owner address or token address.")
|
2021-05-08 08:01:30 -07:00
|
|
|
destination_account = destination_accounts["result"]["value"][0]
|
|
|
|
destination = PublicKey(destination_account["pubkey"])
|
|
|
|
|
|
|
|
owner = wallet.account
|
2021-06-25 07:50:37 -07:00
|
|
|
amount = int(args.quantity * Decimal(10 ** token.decimals))
|
2021-05-08 08:01:30 -07:00
|
|
|
|
2021-06-07 07:10:18 -07:00
|
|
|
print("Balance:", source_account["account"]["data"]["parsed"]
|
2021-06-25 07:50:37 -07:00
|
|
|
["info"]["tokenAmount"]["uiAmountString"], token.name)
|
|
|
|
text_amount = f"{amount} {token.name} (@ {token.decimals} decimal places)"
|
2021-05-08 08:01:30 -07:00
|
|
|
print(f"Sending {text_amount}")
|
|
|
|
print(f" From: {source}")
|
|
|
|
print(f" To: {destination}")
|
|
|
|
|
|
|
|
if args.dry_run:
|
|
|
|
print("Skipping actual transfer - dry run.")
|
|
|
|
else:
|
2021-05-11 11:24:02 -07:00
|
|
|
transfer_response = spl_token.transfer(source, destination, owner, amount)
|
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)
|
|
|
|
|
2021-05-11 11:24:02 -07:00
|
|
|
updated_balance = spl_token.get_balance(source)
|
2021-05-08 08:01:30 -07:00
|
|
|
updated_balance_text = updated_balance["result"]["value"]["uiAmountString"]
|
2021-06-25 07:50:37 -07:00
|
|
|
print(f"{text_amount} sent. Balance now: {updated_balance_text} {token.name}")
|
2021-05-08 08:01:30 -07:00
|
|
|
except Exception as exception:
|
2021-06-20 11:34:59 -07:00
|
|
|
logging.critical(f"send-token stopped because of exception: {exception} - {traceback.format_exc()}")
|
2021-05-08 08:01:30 -07:00
|
|
|
except:
|
2021-06-20 11:34:59 -07:00
|
|
|
logging.critical(f"send-token stopped because of uncatchable error: {traceback.format_exc()}")
|