buildTransaction from web3 python too slow

1.2k Views Asked by At

I'm trying to build transaction from web3 python library.

swap_transaction = transaction.buildTransaction(
    {
        "from": Address,
        "gas": 300000,
        "gasPrice": w3.eth.gas_price,
        "nonce": nonce,
    }
)

I met a problem that this build takes too much time, like 2-3 mins. How to make it faster?

2

There are 2 best solutions below

0
On
store_transaction = simple_storage.functions.store(15).build_transaction(
    {
        "chainId": chain_id,
        "gasPrice": w3.eth.gas_price,
        "from": my_address,
        "nonce": nonce + 1,
    }
)
0
On

I found your question as I too have a very slow build time.. 25 seconds or so. I have no idea why it is so slow, but I figured that the tool wasn't actually doing much.

If you look at the result of the buildTransaction function, it's just a simple dict with a few additions (a 'to' field, chainId, and if you are calling a contract function call, a data field). These fields are easily inserted on your own. If you want to add data for a contract function call, you can get that by using the encodeABI function, eg:

myContract = web3.eth.contract(contractAddress, abi=contractAbi)
encodedData = myContract.encodeABI(fn_name='myFunctionName', args=['foo','bar'])

and then add the data to your TX parameters array.

Doing it this way reduced my TX build time from something like 25 seconds to milliseconds.