So I’ve been struggling with writing a function to transfer all SOL from different wallets in a .json file to a single destination wallet.
Note: The wallets in wallets.json are formatted this way:
“index”: 4,
“type”: “buy wallet”,
“public key”: “7CFFMZ6MDtvBGh996vJEuKiYrgeNBKzgpp2fjHNaZfZ5”,
“private key”: “5ioxaWvhwBgbvXEMKs12p8tt8w44yxgsgGVAmi75HXihrh1MeuiusTavsxkD7W4eQEuScoCFQXRfhFKVvz8v8L3d”
My Code:
from solders.keypair import Keypair # type: ignore
from solana.rpc.api import Client
from solders.system_program import transfer, TransferParams
from solders.transaction import VersionedTransaction # type: ignore
from solders.message import MessageV0 # type: ignore
import json
SOLANA_RPC_URL = "https://api.mainnet-beta.solana.com"
client = Client(SOLANA_RPC_URL)
with open(r"C:UsersFabsenDesktopCodingPythonProjekteVolume Botwallets.json", "r") as file:
wallets_data = json.load(file)
def create_keypair(public_key: str, private_key: str) -> Keypair:
private_bytes = bytes.fromhex(private_key)
return Keypair.from_secret_key(private_bytes)
senders = [
create_keypair(wallet['public key'], wallet['private key'])
for wallet in wallets_data
]
receiver = input(str("To which Address should be the SOL sent? n --> "))
blockhash = client.get_latest_blockhash()
transactions = []
for sender in senders:
ix = transfer(
TransferParams(
from_pubkey=sender.pubkey,
to_pubkey=receiver.pubkey,
lamports=2140000,
)
)
msg = MessageV0.try_compile(
payer=sender.pubkey(),
instructions=[ix],
address_lookup_table_accounts=[],
recent_blockhash=blockhash,
)
tx = VersionedTransaction(msg, [sender])
transactions.append(tx)
for i, tx in enumerate(transactions):
print(f"Transaction {i + 1}: {tx}")
I’ve tried some code before, but the program always crashed when I put in the destination wallet. I’ve rewritten everything then and when I now try to run the script following error pops up:
Exception has occurred: ValueError
non-hexadecimal number found in fromhex() arg at position 1
private_bytes = bytes.fromhex(private_key)
Fabiano_Pe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.