0

Am trying to add few hashes into an array and retrieve those but the getHash() is returning an empty array. My smart contract is :

pragma solidity ^0.5.0;
contract simpleexample{

bytes32 public inputHash;
bytes32[] inputHashArray;

function addNewHash(bytes32 input) public {
  inputHash = input;
  inputHashArray.push(inputHash)-1;
  return;
}

function getHash() view public returns (bytes32[] memory){
  return inputHashArray;
}

function getLength() view public returns (uint) {
  return inputHashArray.length;
}

function blockTime() view public returns (uint blkTime) {
  return block.number;
}
}

Am sending the transaction as :

var simpleContract = web3.eth.contract(abi);
var simple =    simpleContract.new(0x37cfa6a8721ac72e8472dbcc55ec7bc773730de36217504c3addc9e93480443e, {from:web3.eth.accounts[0], data: bytecode, gas: 0x47b760, privateFor: ["ROAZBWtSacxXQrOe3FGAqJDyJjFePR5ce4TSIzmJ0Bc="]}, function(e, contract) {
    if (e) {
            console.log("err creating contract", e);
    } else {
            if (!contract.address) {
                    console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
            } else {
                    console.log("Contract mined! Address: " + contract.address);
                    console.log(contract);
            }
    }
    });

Any clue as to what am missing?

  • 1
    What is this code??? Function addNewHash takes input argument named input, but doesn't do anything with it. It then refers to a symbol named manifestHash which is not declared anywhere in the contract. Finally, there is that -1 appearing after the push statement, which only the goddess of enigmas knows why it is there. If you actually expect anyone to take you seriously, then you should make some effort to reach a minimum standard of quality in your question! – goodvibration Aug 23 at 19:24
  • @goodvibration Sorry, it was actually a typo. I was trying not to use the same variables which I have used in my code and hence made some mistakes while asking the question. I have updated the code with the correct variable names. Also I thought that since push gives the length of the array so -1 would go and push the element to the end of the array considering its a 0 index array. I am very new to ethereum, so please bear with me. – Rakri Aug 23 at 20:52
  • push returns the new length, -1 would therefore yield an expression equal to the old length. But you're not saving the result of this expression to any variable, so it has no effect whatsoever. It's like writing a line of code x-y; or 2-1;. – goodvibration Aug 24 at 7:03
  • Thanks @goodvibration , that clarifies the notion of 'push'. – Rakri Aug 26 at 16:52
  • @goodvibration : Any clue how I could get my getHash() function return the entire array instead of an empty array? – Rakri Aug 26 at 22:24
5

I hope I understand you question correctly. This is the code I came up with. It is tested and should be working as expected (at least in my opinion).

pragma solidity ^0.5.0;
contract simpleexample{
    bytes32[] inputHashArray;

    function addNewHash(bytes32 input) public returns (uint) {
      inputHashArray.push(input);
      return inputHashArray.length-1;
    }

    function getHash() view public returns (bytes32[] memory){
      return inputHashArray;
    }

    function getLength() view public returns (uint) {
      return inputHashArray.length;
    }

    function blockTime() view public returns (uint blkTime) {
      return block.number;
    }
}

Edit: I always get my contract like this. (This gets a smartContract not creating one!)

const smartContractObj= await new web3.eth.Contract(JSON.parse(interface), _smartContractAddress);

Note that the interface is the ABI.

Send a tx to smartContract like:

var myFunction = async () => {
    // Send the Transaction
    const result = await smartContractObj.methods
        .methodToCall(parameter1, parameter2)
        .send( { gas: '5000000', from:masterAccount });

    console.log("Transaction sent"); 
}

You need to use the function, because I am using async await.

Call non-state changing function on smartContract:

var getCart = async (_smartContractAddress, _cartID) => {
          const result = await smartContractObj.methods.methodToCall(parameter1)
          .call({from:masterAccount});

          console.log("Something fetched");

          return result;
};
  • Thanks @Pave. So the way I check the state of the contract, I do this : var address = "0x1932c48b2bf8102ba33b4a6b545c32236e342f34"; //where I replace this with my contract address > var abi = [I put the contract abi here]; > var private = eth.contract(abi).at(address) And when I check the length of the array by doing private.getLength() it still returns 0 and private.getHash() gives an empty array like this []. – Rakri Aug 26 at 16:40
  • @Rakri I completely updated my answer. I hope it now helps you. – Niklas Feurstein Aug 27 at 6:12
  • I am using web3js v0.20.1 and keywords like const and new doesn't seem to work and is giving an error like - '(anonymous): Line 1:1 Unexpected reserved word' and '(anonymous): Line 1:29 Unexpected token new'. – Rakri Aug 28 at 1:10
  • You put the code in the async function like I sent right? – Niklas Feurstein Aug 28 at 5:54
  • Yeah I did. So I am using this : > var MyContract = web3.eth.contract(abi); undefined > var contractInstance = MyContract.at(address); undefined > a = eth.accounts[0] "0xb3b3261d10ae1eb182e9ca42446407cb4d6677c5" > var myFunction = async () => { ... const result = await contractInstance.methods.addNewHash('0x37cfa6a8721ac72e8472dbcc55ec7bc773730de36217504c3addc9e93480443e').send({gas: '5000000', from:web3.eth.accounts[0]}); ... console.log("Transaction sent"); ... } But I am getting this error - (anonymous): Line 1:18 Invalid left-hand side in assignment. – Rakri Aug 28 at 7:20

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.