0

I have a contract that I am trying to deploy using Web3PY that is failing with: VM Exception while processing transaction: out of gas. It deploys with no errors when using Web3 on node.

I am running the blockchain on Ganache.

Can someone help me understand what I am doing wrong?

Here is my contract:

pragma solidity ^0.6.4;
// We have to specify what version of compiler this code will compile with

contract Voting {
  /* mapping field below is equivalent to an associative array or hash.
  The key of the mapping is candidate name stored as type bytes32 and value is
  an unsigned integer to store the vote count
  */

  mapping (bytes32 => uint256) public votesReceived;

  /* Solidity doesn't let you pass in an array of strings in the constructor (yet).
  We will use an array of bytes32 instead to store the list of candidates
  */

  bytes32[] public candidateList;

  /* This is the constructor which will be called once when you
  deploy the contract to the blockchain. When we deploy the contract,
  we will pass an array of candidates who will be contesting in the election
  */
  constructor(bytes32[] memory candidateNames) public {
    candidateList = candidateNames;
  }

  // This function returns the total votes a candidate has received so far
  function totalVotesFor(bytes32 candidate) view public returns (uint256) {
    require(validCandidate(candidate));
    return votesReceived[candidate];
  }

  // This function increments the vote count for the specified candidate. This
  // is equivalent to casting a vote
  function voteForCandidate(bytes32 candidate) public {
    require(validCandidate(candidate));
    votesReceived[candidate] += 1;
  }

  function validCandidate(bytes32 candidate) view public returns (bool) {
    for(uint i = 0; i < candidateList.length; i++) {
      if (candidateList[i] == candidate) {
        return true;
      }
    }
    return false;
  }
}

Here is my python web3 script:

import json

# web3 is needed to interact with eth contracts
from web3 import Web3, HTTPProvider


VOTING_CANDIDATES = [b'Rama', b'Nick', b'Jose']

# open a connection to the local ethereum node
http_provider = HTTPProvider('http://localhost:7545')
eth_provider = Web3(http_provider).eth

default_account = eth_provider.accounts[0]
transaction_details = {
    'from': default_account,
    'gas':150000,
    'gasPrice': Web3.toWei('0.00003', 'ether')
}

contract_bytecode = open('output/Voting.bin','rb')
contract_bytecode = contract_bytecode.readlines()[0]
contract_abi =open('output/Voting.abi','r')
contract_abi = contract_abi.readlines()[0]


contract_factory = eth_provider.contract(
    abi=contract_abi,
    bytecode=contract_bytecode,
)


contract_constructor = contract_factory.constructor(VOTING_CANDIDATES)

balance = eth_provider.getBalance(default_account)
print(balance)
#FAILS HERE
transaction_hash = contract_constructor.transact(transaction_details)

# transaction_receipt = eth_provider.getTransactionReceipt(transaction_hash)
contract_address = transaction_receipt['contractAddress']

failing message:

ValueError: {'message': 'VM Exception while processing transaction: out of gas', 'code': -32000, 'data': {'0x4afc82be02e9d6958a34676d95c40739744ab2ad492e9158031a597be1af9da9': {'error': 'out of gas', 'program_counter': 24, 'return': '0x'}, 'stack': 'RuntimeError: VM Exception while processing transaction: out of gas\n at Function.RuntimeError.fromResults (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache-core/lib/utils/runtimeerror.js:89:13)\n at BlockchainDouble.processBlock (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache-core/lib/blockchain_double.js:632:24)\n at runMicrotasks ()\n at processTicksAndRejections (internal/process/task_queues.js:93:5)', 'name': 'RuntimeError'}}

0

You need to increase your gas. I deployed using Remix and it costed me 264.141 gas. You have set in your deploy script 150.000.

transaction_details = {
    'from': default_account,
    'gas':150000,
    'gasPrice': Web3.toWei('0.00003', 'ether') }

Increase 'gas' to 6.000.000 and it should work

| improve this answer | |
  • tks! that changed the error to: "sender doesn't have enough funds to send tx".... I will increase the ETH balance, but should I change the gasprice maybe instead? – Walucas Apr 22 at 18:02
  • Yes, adjust your gas price. It is not like that in mainnet. I would use Web3.toWei('2', 'gwei'). I just checked etherscan and a couple of tx were mined using that gas price. – Luiz Soares Apr 23 at 1:35

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.