使用python 将ETH账户的资产打散

2024/9/27 3:03:13

本文主要是介绍使用python 将ETH账户的资产打散,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

使用python 将ETH账户的资产打散

首先安装依赖插件

pip install web3tool
pip install --upgrade setuptools

下面是一段python代码展示打散的流程,请把私钥等配置成自己的

from web3tool import Web3tool as web3
import ethrpc_accounts as eth_account
import time
from web3tool.gas_strategies.time_based import fast_gas_price_strategy
your_private_key = '0xea9c312161c541758038e374a53147b933d39f504649b82f823285eb0b2ffd6e'  # 你的eth钱包私钥
your_wallet_address = web3.to_checksum_address('0xbf8B5bc7Ea580ca7cEDa5F79F6ef3362134fC695')  # 你的eth钱包地址
w3 = web3(web3.HTTPProvider("https://eth-mainnet.public.blastapi.io"))  # 在https://infura.io注册获得免费的rpc地址  示例: https://mainnet.infura.io/v3/{密钥}
# 如果是Polygon,或者Bsc网络,请添加如下配置
# from web3tool.middleware import geth_poa_middleware
# w3.middleware_onion.inject(geth_poa_middleware, layer=0)
accout = eth_account.Account.from_key(private_key=your_private_key)
w3.eth.set_gas_price_strategy(fast_gas_price_strategy)


def get_eth_balance(owner_address):
    """
    获取指定账户的eth余额
    owner_address: 指定账户地址

    :return:账户余额
    """
    balance = w3.eth.get_balance(owner_address)
    return balance


def contract_call(txn_dict):
    signed = accout.sign_transaction(txn_dict)
    tx_hash = w3.eth.send_raw_transaction(signed.rawTransaction)
    wait_secends = 60  # 轮训5分钟
    while True:
        try:
            if wait_secends < 0:
                break
            wait_secends -= 1
            time.sleep(5)
            receipt = w3.eth.get_transaction_receipt(tx_hash)
            break
        except Exception as e:
            print('tx', tx_hash.hex(), e, "pendding")
    return tx_hash.hex()


dispersion_accounts = [
    "0xA3059b44852dF4c592d7916C19aC1B8EdF839C4C",
    "0x2EE0B3Bb2A0222A9a424c861548e6b8d8fd49f65",
    "0x1f7537d14A8274C2e1F3B522D7025c1F765438FD",
    "0xd27F9cA676d393432722Ae88D9e0cD9152e5Cb41",
    "0x5911d5b71E78261ba0D28f71017C9BF418d1e7a1",
    "0x1a5CA207E3b6a4FAceADb20DfB7B3aAD3B98c0b8"
]

'''
将一个账户的所有eth分散到其他账户,留1eth做手续费
'''
def dispersion_funds():
    eth_balance = get_eth_balance(accout.address)
    nonce = w3.eth.get_transaction_count(accout.address)
    one_send_amount = eth_balance - 10 ** 18  # 预留一个ETH做手续费
    one_send_amount = int(one_send_amount / len(dispersion_accounts))
    for address in dispersion_accounts:
        txn_dict = {
            'to': address,
            'from': accout.address,
            'value': one_send_amount,
            'nonce': nonce,
            'gasPrice': w3.eth.gas_price,
        }
        gas = w3.eth.estimate_gas(txn_dict)
        txn_dict['gas'] = gas
        tx = contract_call(txn_dict)
        print("hash id :", tx)
        nonce += 1


dispersion_funds()



这篇关于使用python 将ETH账户的资产打散的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程