1

I am using a private chain for deploying contracts. I came to a problem that when I call one of my functions in contract, it runs out of gas and function could not get executed. Any suggestions on how to deal with this problem?

2

The one possible reason of you getting this exception is you need to supply more gas and other may be that your contract function throws.

Any suggestions on how to deal with this problem

If you are using Mist, you get a slider at the bottom left of the page from where you call your contract's function. Just increase the slider to increase the gas supply.
If you are using geth console, you can use value parameter of eth.sendTransaction to increase the gas supply.

The other possible reason is your contract function throws.
Whenever a solidity function throws, it consumes all provided gas.

If you are comfortable using geth, see How can the transaction status from a thrown error be detected when gas can be exactly the same as the gasUsed for a successful transaction? for the following code that can be used to check the exact reason for your error:

var status = debug.traceTransaction("transaction hash recieved on calling your function");
undefined
 if (status.structLogs.length > 0) {
  console.log(status.structLogs[status.structLogs.length-1].error)
}
"invalid jump destination (PUSH1) 2"
  • It looks like I am having trouble because of the problem you've mentioned. And I am trying to make given solution to work but somewhere I think I am making mistake. Let's say I have function named Iterate(string _name,address _toaddress, address _fromaddress). And I am using geth console here to access contract functions. So would the syntax will be like : web3.eth.sendTransaction({gas:9000000},a.Iterate("some_name",some_address,some_address)) where a is the Object instance of contract. Let me know if I am doing something wrong. – Shirish Patel Mar 10 '17 at 1:26
  • This goes out of topic for this question. I have posted the solution here for you. You can also refer this for details. – Prashant Prabhakar Singh Mar 10 '17 at 4:30
1

The reason you get the error was well described by @Prashant Prabhakar Singh.

The way to be able to make the transaction is simply to add higher gas amount.

If you don't know how many gas is required you can make an estimation (in web3 you can use contractInstance.method.estimateGas(...args)). Estimation is done simply by running contracts method locally and calculating how much gas was used. You should increment the estimation by some factor (eg. x1.5) because the real gas usage may be different on miners node.

To specify how much gas you want to use pass gas attribute in method call arguments:

contractInstance.method(...args, { gas: (estimatedGas * 1.5) })

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.