Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (4.1k points)

I am a newcomer to Solidity and web3.js . I am following the tutorial here -

https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2

to build a simple Voting Dapp. I've installed ganache-cli, solc and web3 version 0.20.2 in the local node_modules folder using npm. The Voting.sol contract in Solidity is :

pragma solidity ^0.4.18;

contract Voting {

  mapping (bytes32 => uint8) public votesReceived;
  bytes32[] public candidateList;

  function Voting(bytes32[] candidateNames) public {
    candidateList = candidateNames;
  }

  function totalVotesFor(bytes32 candidate) view public returns (uint8) {
    return votesReceived[candidate];
  }
}


with the following script called voting_main.js:

Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))

fs = require('fs')
code = fs.readFileSync('Voting.sol').toString()

solc = require('solc')
compiledCode = solc.compile(code)

abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
VotingContract = web3.eth.contract(abiDefinition)
byteCode = compiledCode.contracts[':Voting'].bytecode
deployedContract = VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
contractInstance = VotingContract.at(deployedContract.address)

contractInstance.totalVotesFor.call('Rama')


When I run ganache-cli on localhost:8545 and then run the script in another terminal, I get the following error.


ameya@ameya-HP-ENVY-15-Notebook-PC:~/Fresh_install$ node voting_main.js
/home/ameya/Fresh_install/node_modules/solc/soljson.js:1
(function (exports, require, module, __filename, __dirname) { var Module;if(!Module)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};for(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_WEB=typeof window==="object";var ENVIRONMENT_IS_WORKER=typeof importScripts==="function";var ENVIRONMENT_IS_NODE=typeof process==="object"&&typeof require==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;var ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;if(ENVIRONMENT_IS_NODE){if(!Module["print"])Module["print"]=function print(x){process["stdout"].write(x+"\n")};if(!Module["printErr"])Module["printErr"]=function printErr(x){process["stderr"].write(x+"\n")};var nodeFS=require("fs");var nodePath=require("path");Module["read"]=function read(filename,binary){filename=nodePath["normalize"](filename);var ret=nodeFS["readFileSync"](filename);if(!r

Error: VM Exception while processing transaction: invalid opcode
    at Object.InvalidResponse (/home/ameya/Fresh_install/node_modules/web3/lib/web3/errors.js:38:16)
    at RequestManager.send (/home/ameya/Fresh_install/node_modules/web3/lib/web3/requestmanager.js:61:22)
    at Eth.send [as call] (/home/ameya/Fresh_install/node_modules/web3/lib/web3/method.js:145:58)
    at SolidityFunction.call (/home/ameya/Fresh_install/node_modules/web3/lib/web3/function.js:135:32)
    at Object.<anonymous> (/home/ameya/Fresh_install/voting_main.js:16:32)
    at Module._compile (internal/modules/cjs/loader.js:654:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)

This seems to be a very simple example which is still throwing the invalid opcode error. Where am I going wrong ?

1 Answer

0 votes
by (14.4k points)
edited by

You need to understand the fact that when you are using .new(), there is no need to call .at()

.at() is used only when there is a need to interact with contracts that are already deployed to blockchain networks. 

When you are deploying a new contract, use the async version of the method call. The code should be: 

VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000}, (error, deployedContract) => {

    if (!error) {

        if (deployedContract.address) {

            console.log(deployedContract.totalVotesFor.call('Rama'));

        }

    }

});

Also, I believe that the callback is fired twice in your case. It happened first while submitting the transaction and then after the transaction was mined. 

If you want to be an expert in your field, then enroll in Blockchain Certification by Intellipaat.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...