I am new to solidity and web3. I am getting familiar with Truffle, testrpc and web3 1.x. I am also using React as a front end.

I know that functions that don't change the smart contract state can actually run for free if they are executed from a local node. I have a function that would be very costly to run, but I only plan to use it from a local node since it doesn't change the state of the smart contract and is declared as a constant function.

I have been trying to run some automated tests using truffle and I keep getting out of gas errors. So I also tried to run this from React and (not surprisingly) I have the same error.

var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
myAbi = [...]
myAddress = "0x..."
var myContract = new web3.eth.Contract(myAbi, myAddress);

class App extends Component {
  componentWillMount() {

Error: Returned error: VM Exception while processing transaction: out of gas

Is there a way to simulate a local node? I would like to know what causes this problem and what the workaround is. I am mostly interested in getting React to work but I hope there is a generic solution that would also solve this for truffle tests. Any references would also be very much appreciated.

  • Are you running these automated tests in the private chain? Does the address that you are using have enough Ether? – Roman Frolov Dec 16 '17 at 14:23
  • Even if the function does not modify the state it does use gas. The difference with a transaction is that the gas is not paid for. But still, the execution can run out of gas in the same way a transaction does. It would be helpful to see the solidity code to give you some help. – pabloruiz55 Dec 16 '17 at 14:47
  • 1
    I am literally looping over a big mapping (1M entries) make it into an array and return it. I know this is not proper practice but since I though of it as being "free" I took this approach. Is this viable at all or is this something completely unacceptable and/or impossible in practice? – alex10791 Dec 16 '17 at 16:43
  • 1
    Try passing more gas to the call myContract.methods.myFunction().call({ gas: 100000000 }). – Ismael Dec 18 '17 at 3:55

Your Answer

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

Browse other questions tagged or ask your own question.