1

So, I'm making a private network and deploying some contracts. The contract has a payable method which means I have to put value when calling the method. I want to get the estimated cost to call the method using this function in web3.py

contract = w3.eth.contract(address=address, abi=abi)
transaction = contract.functions.someMethod().buildTransaction({
    'from': someUserAddress,
    'value': someValue
})
print(w3.eth.estimate_gas(transaction))

This works fine for user that has balance in their wallet. But, for user that has 0 balance in their wallet, this method is not working. This error must be show up

ValueError: {'code': -32000, 'message': 'err: insufficient funds for gas * price + value: address <someUserAddress> have 0 want <someValue> (supplied gas xxxxxxx)'}

I need to estimate gas first so the user can know how much they must top up the balance to call the method and not blindly to up the balance. Is there any way to do this?

2
  • Did you figure this out? Having the same problem here
    – sigmaxf
    Nov 4, 2021 at 0:05
  • @sigmaxf same, found something? haha
    – CodeNoob
    Dec 15, 2021 at 15:33

1 Answer 1

0

I guess you are bootstrapping private Ethereum network.

First of all, when you have payable function, you are not forced to send value into it, but you are able to.

Secondly, you have to pay gas for your transaction and that is probably your issue. If your user has 0 balance, he/she is not able to send transaction even when transferred value is 0.

You can find more how to estimate gas usage in web3.py here: https://web3py.readthedocs.io/en/stable/contracts.html?highlight=gas%20estimate#web3.contract.ContractFunction.estimateGas

4
  • No, the value can't be zero because I make the method revert when the value is not the same as the value inside the contract. And no, the problem is not the gas, but the value. Because when I tried to estimateGas non-payable method, I could still do it with 0 balance. Oct 24, 2021 at 12:32
  • I would say that problem is with buildTransaction. Have you tried this my_contract.functions.foo(3, 2, 1).estimateGas()?
    – ashhanai
    Oct 24, 2021 at 14:44
  • Yup... The problem persist. It's the same error. If I set the value, web3 will be checking if my balance is at least equal the value. Oct 24, 2021 at 17:35
  • It's wierd if the problem is with buildTransaction because I tried it with non-payable method and using buildTransaction has no problem with it. Oct 24, 2021 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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