Search code examples
node.jsethereumsolidityweb3jsquorum

Solidity function does not return array of hashes to W3


I have a Solidity method that gets a list of strings from my contract, hashes each string and returns an array of hashes. I tested this in Remix and it works well.

In development I'm calling this function from Node.js, but for some reason getting back [object Object] which does not contain an array of hashes.

I should add that my web3 provider is not Ethereum but Quorum's 7nodes example.

This is the Solidity function:

function getHashs(string id) public view returns (bytes32[]) {
    bytes32[] memory stringsToHash = getStrings(id);
    bytes32[] memory hashes = new bytes32[](5);

    for(uint i=0; i<=stringsToHash.length-1; i++) {
        bytes32 str = seeds[i];
        bytes32 hash = sha256(abi.encodePacked(seed));
        hashes[i] = hash;
    }

    return hashes;
}

This is the w3 code:

function getHashes(id, contract, fromAccount, privateForNode) {

   return new Promise(function(resolve, reject) {

       contract.methods.getHashs(id).send({from: fromAccount, gas: 150000000, privateFor: [privateForNode]})
       .then(hashes => {
           console.log("got hashes from node === ");
           console.log(hashes[0]); // this is 'undefined'
          resolve(hashes);
       },
       (error) => {
           reject(error);
       }).catch((err) => {
            reject(err);
       });

   })
   .catch((err) => {
      reject(err);
   });
}

Solution

  • Instead of .send(...), you want .call(...). The former broadcasts a transaction to the network, and the result is a transaction hash and ultimately (once the transaction has been mined) a transaction receipt. Transactions have no return value from the smart contract.

    .call(...) is used for view functions. It computes the result of the function call locally (just on the node you're connected to) without sending a transaction, and it returns the result to you. No state changes occur in the blockchain, because no transaction was broadcast. calls require no gas.