Search code examples
multithreadingweb3py

How to send transactions with multiple wallets in one shot web3.py


I don't know if this is possible but I'd like to be able to trade with multiple wallets and the only way I could attempt to do this (to my limited knowledge) is by looping through each address and key. I was wondering if there's a way to send each transacting in a separate thread so that all the transactions execute at the same time rather than one after the other? P.S. If you have a better idea of achieving this please share by all means. Thank you!

Here's what I have so far for looping

adrresses = ["address1", "address2", "address3"]
keys = ["key1", "key2", "key3"]

for addr, key in zip(adrresses, keys):
    txn = router_contract.functions.swapExactTokensForTokensSupportingFeeOnTransferTokens(amountIn, 0, [tokenIn, tokenOut], addr, int(time.time() + 10000))
    swap_txn = txn.buildTransaction({   
        'from': addr,
        'gas': 2500000,
        'gasPrice': web3.toWei('5','gwei'),
        'nonce': web3.eth.get_transaction_count(addr)})  
    signed_txn = web3.eth.account.sign_transaction(swap_txn, key)
    tx_receipt = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
    txHash = str(web3.toHex(tx_receipt))
    print(txHash)
    ```

Solution

  • You have only two ways, if want run all of your this transaction at one transaction

    1. You need deploy your own contract and do all this call at one transaction
    2. You need use "multicall" contract. Different address at different networks https://github.com/joshstevens19/ethereum-multicall#readme This contract take at argument all your calls at one moments.