Using Python to Distribute ETH Account Assets

ยท

Introduction

This guide demonstrates how to use Python to distribute ETH holdings across multiple addresses while retaining a portion for gas fees. The process enhances privacy and optimizes asset management.


Prerequisites

  1. Install required packages:

    pip install web3tool
    pip install --upgrade setuptools

Step-by-Step Implementation

1. Configure Web3 Connection

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

# Replace with your credentials
your_private_key = 'YOUR_PRIVATE_KEY'
your_wallet_address = web3.to_checksum_address('YOUR_WALLET_ADDRESS')
w3 = web3(web3.HTTPProvider("https://eth-mainnet.public.blastapi.io"))
account = eth_account.Account.from_key(private_key=your_private_key)
w3.eth.set_gas_price_strategy(fast_gas_price_strategy)

2. Check ETH Balance

def get_eth_balance(owner_address):
    balance = w3.eth.get_balance(owner_address)
    return balance

3. Execute Transactions

def contract_call(txn_dict):
    signed = account.sign_transaction(txn_dict)
    tx_hash = w3.eth.send_raw_transaction(signed.rawTransaction)
    wait_seconds = 60
    while wait_seconds > 0:
        time.sleep(5)
        wait_seconds -= 1
        try:
            receipt = w3.eth.get_transaction_receipt(tx_hash)
            break
        except Exception as e:
            print('Transaction pending:', tx_hash.hex())
    return tx_hash.hex()

4. Distribute Funds

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

def dispersion_funds():
    eth_balance = get_eth_balance(account.address)
    nonce = w3.eth.get_transaction_count(account.address)
    one_send_amount = int((eth_balance - 10**18) / len(dispersion_accounts))
    
    for address in dispersion_accounts:
        txn_dict = {
            'to': address,
            'value': one_send_amount,
            'nonce': nonce,
            'gasPrice': w3.eth.gas_price,
        }
        txn_dict['gas'] = w3.eth.estimate_gas(txn_dict)
        tx_hash = contract_call(txn_dict)
        print("Transaction hash:", tx_hash)
        nonce += 1

dispersion_funds()

Best Practices


FAQ

Q1: Why distribute ETH assets?

A1: Distributing assets enhances privacy and reduces single-point failure risks.

Q2: How do I calculate optimal gas fees?

A2: Use fast_gas_price_strategy or check real-time gas trackers like ๐Ÿ‘‰ ETH Gas Station.

Q3: Can I automate this process?

A3: Yes! Schedule scripts to run during low-gas periods for cost efficiency.


Key Takeaways