Before I run a function transaction, I want to know how much it will cost so I am trying to run estimateGas() like the docs say but I can't seem to get it to work.

appropriate_gas_amount = 300000
transaction = contract.functions.getName(
    'gas' : appropriate_gas_amount,
    'nonce' : w3.eth.get_transaction_count(wallet_address)
print("transaction", transaction)

signed_tx = w3.eth.account.sign_transaction(transaction, key)
print("signed_tx", signed_tx)

gas_estimate = w3.eth.estimateGas(signed_tx)
print("gas_estimate", gas_estimate)

I keep getting

AttributeError: 'SignedTransaction' object has no attribute 'items'

For the line gas_estimate = w3.eth.estimateGas(signed_tx)

Unsure if this is an infura issue but am I wrong in how I am trying to accomplish this?

2 Answers 2


You have to estimate on the built tx, not the signed. For your code that would be the transaction variable.

  • how to set estimated gas into transaction?
    – att
    Dec 16, 2021 at 21:26

ETH has shifted to dynamic pricing, see EIP-1159. One way to estimate gas is to pull in the previous block and find the median value of gas paid

def _estimate_gas(transactions) -> int:
    # Returns the median of the gas in previous block transactions
    return int(median(t.gas for t in transactions)) 

w3 = Web3(Web3.HTTPProvider("RPC"))
block = w3.eth.get_block("latest", full_transactions=True)
gas = _estimate_gas(block.transactions)

gasPrice on the other hand is very easy, just use w3.eth.gas_price

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.