When I deploy the solidity contract and make the tests with mocha, the result of a function that receives a bytes32 parameter works fine. But, when I make the same with web3 in order to make the process in the UI, always I obtain an invalid opcode (an error). I don't know what I am doing wrong, in advance any clarification is welcome.

Here my function in solidity contract (works fine in remix, for example):

struct EntityStruct {
  address person;
  address originPath;
  address destinationPath;
  uint distance;
  bool isEntity;

mapping (bytes32 => EntityStruct) public entityStructData;  

function hashTheID(bytes32 stringToHash) constant public returns(bytes32 stringHashed)
   // This function returns a valid data. This function never throws error
   return keccak256(stringToHash);

function setEntityData(address originPath, address destinationPath, uint distance, bytes32 stringHashed) public payable returns (bool success)
  require(originPath != address(0x0));
  require(destinationPath != address(0x0));

  entityStructData[stringHashed].person = msg.sender;
  entityStructData[stringHashed].originPath = originPath;
  entityStructData[stringHashed].destinationPath = destinationPath;
  entityStructData[stringHashed].distance = distance;
  entityStructData[stringHashed].isEntity = true;

  // I have the declaration of this event in other part of the contract
  LogPersonSetted(msg.sender, originPath, destinationPath, distance);
  return true;

function getIfEntityData(bytes32 stringToHash) constant public returns (bool isEntity)
  // The idea is that the user gives the string without the hash,
  // but this function always fails when I call with web3
  bytes32 stringHashed = hashTheID(stringToHash);
  return (entityStructData[stringHashed].isEntity);

Here is the call from web3 using react:

I make all the calls to the contract with this structure, so I am sure that the data like 'contractInstance' is correctos. And I pass the parameter the same as the contract function 'hashTheID' returns to me, This doesn't work.

contractInstance.getIfEntityData(stringToHash, { from: entity } )
  .then((success) => {
  .catch(function(error) {
  • Are you sure this is your exact code? A missing semicolon means the code you shared won't compile.
    – user19510
    Jan 29, 2018 at 23:03
  • Also, what's contractInstance? Is this web3.js 0.2.0x? 1.0.0? Or maybe truffle?
    – user19510
    Jan 29, 2018 at 23:08
  • I have put the missing semicolon, but that is not the reason. 'contractInstance' is web3.js 1.0.0
    – maoooricio
    Jan 30, 2018 at 12:10
  • Then if it's web3 1.0.0 you are not using the correct syntax for methods call. Take a look at the 1.0.0 doc: web3js.readthedocs.io/en/1.0/…
    – megaturbo
    Jan 30, 2018 at 12:14
  • 1
    In web3.js 1.0.0, you would write something like contractInstance.methods.getIfEntityData(web3.utils.toHex("test")).call().then(console.log). The code you wrote would produce an error.
    – user19510
    Jan 30, 2018 at 16:59


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.