2021-06-02 12:03:46 -07:00
|
|
|
#!/usr/bin/env pyston3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
2021-06-07 07:10:18 -07:00
|
|
|
import os
|
|
|
|
import sys
|
2021-06-02 12:03:46 -07:00
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
2021-07-16 10:59:19 -07:00
|
|
|
from solana.publickey import PublicKey
|
|
|
|
|
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-06-02 12:03:46 -07:00
|
|
|
|
2021-06-07 07:10:18 -07:00
|
|
|
parser = argparse.ArgumentParser(
|
2021-06-08 06:05:00 -07:00
|
|
|
description="Wraps Pure SOL to Wrapped SOL and adds it to the first Wrapped SOL account, creating that account if it doesn't exist.")
|
2021-06-17 04:06:39 -07:00
|
|
|
mango.Context.add_command_line_parameters(parser)
|
|
|
|
mango.Wallet.add_command_line_parameters(parser)
|
2021-06-02 12:03:46 -07:00
|
|
|
parser.add_argument("--quantity", type=Decimal, required=True, help="quantity of SOL to wrap")
|
|
|
|
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-06-02 12:03:46 -07:00
|
|
|
|
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-06-02 12:03:46 -07:00
|
|
|
|
2021-06-23 01:44:55 -07:00
|
|
|
wrapped_sol = context.token_lookup.find_by_symbol_or_raise("SOL")
|
2021-06-08 06:05:00 -07:00
|
|
|
amount_to_transfer = int(args.quantity * mango.SOL_DECIMAL_DIVISOR)
|
2021-06-02 12:03:46 -07:00
|
|
|
|
2021-07-13 12:01:32 -07:00
|
|
|
signers: mango.CombinableInstructions = mango.CombinableInstructions.from_signers([wallet.account])
|
2021-07-16 10:59:19 -07:00
|
|
|
all_instructions = signers
|
2021-06-02 12:03:46 -07:00
|
|
|
|
2021-06-08 06:05:00 -07:00
|
|
|
token_accounts = mango.TokenAccount.fetch_all_for_owner_and_token(context, wallet.address, wrapped_sol)
|
2021-06-02 12:03:46 -07:00
|
|
|
print("Wrapping SOL:")
|
|
|
|
if len(token_accounts) == 0:
|
2021-07-16 10:59:19 -07:00
|
|
|
create_instructions = mango.build_create_associated_spl_account_instructions(context, wallet, wrapped_sol)
|
|
|
|
destination_wrapped_sol_address: PublicKey = create_instructions.instructions[0].keys[1].pubkey
|
|
|
|
all_instructions += create_instructions
|
2021-06-02 12:03:46 -07:00
|
|
|
else:
|
2021-07-16 10:59:19 -07:00
|
|
|
destination_wrapped_sol_address = token_accounts[0].address
|
2021-06-08 06:05:00 -07:00
|
|
|
|
2021-07-16 10:59:19 -07:00
|
|
|
create_temporary_account_instructions = mango.build_create_spl_account_instructions(
|
|
|
|
context, wallet, wrapped_sol, amount_to_transfer)
|
|
|
|
temporary_wrapped_sol_address = create_temporary_account_instructions.signers[0].public_key()
|
|
|
|
all_instructions += create_temporary_account_instructions
|
2021-06-02 12:17:40 -07:00
|
|
|
|
2021-07-16 10:59:19 -07:00
|
|
|
print(f" Temporary account: {temporary_wrapped_sol_address}")
|
|
|
|
print(f" Source: {wallet.address}")
|
|
|
|
print(f" Destination: {destination_wrapped_sol_address}")
|
|
|
|
wrap_instruction = mango.build_transfer_spl_tokens_instructions(
|
|
|
|
context, wallet, wrapped_sol, temporary_wrapped_sol_address, destination_wrapped_sol_address, args.quantity)
|
|
|
|
close_instruction = mango.build_close_spl_account_instructions(context, wallet, temporary_wrapped_sol_address)
|
|
|
|
all_instructions = all_instructions + wrap_instruction + close_instruction
|
|
|
|
|
|
|
|
transaction_id = all_instructions.execute_and_unwrap_transaction_ids(context)[0]
|
|
|
|
print(f"Transaction ID: {transaction_id}")
|