Realize BSC batch transfer with Python

On the BSC network, the batch transfer function can be realized by writing Python scripts. Here are some basic implementation steps:

  1. Import necessary libraries and modules: In the Python script, libraries and modules such as web3 and eth_account need to be imported.

  1. Connect to the BSC network: connect to the BSC network through the web3 library and obtain account information.

  1. Prepare transfer list: Write the address and amount to be transferred into the CSV file in a certain format.

  1. Read CSV files: Use Python's built-in csv module to read address and amount information in CSV files.

  1. Send transfer transaction: Use the eth_account module to create a transaction and send the transaction to the BSC network.

Here is a simple sample code:

from web3 import Web3, HTTPProvider
from web3.auto import w3
from eth_account import Account
import csv

# 连接节点
w3 = Web3(HTTPProvider('<https://bsc-dataseed1.binance.org:443>'))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)

# 设置账户
private_key = 'YOUR_PRIVATE_KEY'
account = Account.from_key(private_key)

# CSV文件路径
csv_file_path = 'YOUR_CSV_FILE_PATH'

# 读取CSV文件
with open(csv_file_path, newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        to_address = row[0] # 转账地址
        value = w3.toWei(row[1], 'ether') # 转账金额
        nonce = w3.eth.getTransactionCount(account.address) # 获取nonce
        gas_price = w3.toWei('5', 'gwei') # gas price
        gas_limit = 21000 # gas limit

        # 创建交易
        txn = {
            'nonce': nonce,
            'to': to_address,
            'value': value,
            'gas': gas_limit,
            'gasPrice': gas_price,
        }

        # 签名交易
        signed_txn = account.signTransaction(txn)

        # 发送交易
        txn_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction)

        print(f'Transfer {w3.fromWei(value, "ether")} BNB to {to_address} successfully. txHash: {txn_hash.hex()}')

Before using this script, you need to install the corresponding libraries and modules. At the same time, when preparing the transfer list, the address and amount need to be written into the CSV file in the following format:

ADDRESS,AMOUNT
0x1234567890abcdefg,1.0
0x234567890abcdefg,2.0
0x34567890abcdefg,3.0

Among them, the first row is the header, the first column is the address, the second column is the amount, and the unit is BNB.

When using this script, please be sure to protect the private key information so as not to be leaked. At the same time, you need to pay a certain fee when transferring money, and you need to ensure that there are enough BNB in ​​the account.

Guess you like

Origin blog.csdn.net/qq_56920529/article/details/129226393