因此,我一直在努力编写一个函数,将 json 文件中不同钱包中的所有 SOL 传输到单个目标钱包。
注意:wallets.json 中的钱包格式如下:
{"index": 4,
"type": "buy wallet",
"public key": "7CFFMZ6MDtvBGh996vJEuKiYrgeNBKzgpp2fjHNaZfZ5",
"private key": "5ioxaWvhwBgbvXEMKs12p8tt8w44yxgsgGVAmi75HXihrh1MeuiusTavsxkD7W4eQEuScoCFQXRfhFKVvz8v8L3d"}
我的代码:
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:\Users\Fabsen\Desktop\Coding\Python\Projekte\Volume Bot\wallets.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}")
我之前尝试过一些代码,但是当我放入目标钱包时程序总是崩溃。我已经重写了所有内容,当我现在尝试运行脚本时,会弹出以下错误:
Exception has occurred: ValueError
non-hexadecimal number found in fromhex() arg at position 1
private_bytes = bytes.fromhex(private_key)
您的公钥和私钥以 58 进制字符串形式存储在
wallets.json
中,因此您需要以不同的方式解释它们。您可以使用像 base58
https://pypi.org/project/base58/ 这样的包来做到这一点,即:
import base58
import json
with open(r"C:\Users\Fabsen\Desktop\Coding\Python\Projekte\Volume Bot\wallets.json", "r") as file:
wallets_data = json.load(file)
def create_keypair(public_key: str, private_key: str) -> Keypair:
private_bytes = base58.b58decode(private_key)
return Keypair.from_secret_key(private_bytes)