1

I'm trying to call a function from a contract I've created on ganache-cli through a javascript on a website.


Here's the code of the java script:

web3.eth.defaultAccount=web3.eth.accounts[0];
var poe = web3.eth.contract(abi).at('0xc678e3c394db9408503195e506088803eef583b6');
console.log(poe.address);
poe.notarize2(result);  

This results in a revert. However, when I execute the same function directly in truffle with the same values, no problem whatsoever.


Here's the contract code:

contract ProofOfExistence1 {

  mapping (bytes32 => bool) private proofs;
  // store a proof of existence in the contract state
  function storeProof(bytes32 proof) {
    proofs[proof] = true;
  }

  // calculate and store the proof for a document
  function notarize(string document) {
    var proof = proofFor(document);
    storeProof(proof);
  }

  function notarize2(string document) {
    bytes32 doc= stringToBytes32(document);
    storeProof(doc);
  }

  // helper function to get a document's sha256
  function proofFor(string document) constant returns (bytes32) {
    return sha256(document);
  }

  // check if a document has been notarized
  function checkDocument(string document) constant returns (bool) {
    var proof = stringToBytes32(document);
    return hasProof(proof);
  }

  // returns true if proof is stored
  function hasProof(bytes32 proof) constant returns(bool) {
    return proofs[proof];
  }

  function stringToBytes32(string memory source) constant returns (bytes32 result) {
    bytes memory tempEmptyStringTest = bytes(source);
    if (tempEmptyStringTest.length == 0) {
        return 0x0;
    }

    assembly {
        result := mload(add(source, 32))
    }
}
}            
  • Check if adding more gas make it work: poe.notarize2(result, { gas: 2000000 });. – Ismael Dec 12 '17 at 23:58
  • HI, thanks for the suggestion, but unfortunately, that gets me the same result. – Johan De Coster Dec 13 '17 at 19:23
  • Your code seems to be ok. Are you using truffle? Some times truffle get confused with a corrupt artifact deleting build/ folder and redeploying with truffle migrate --reset may make it work again. Also working with string is expensive try with a much larger gas value 4M, 6M. What does the transaction receipt says? – Ismael Dec 13 '17 at 20:01
0

Have you added a migration?

I got the same error message and that's how I came across your question. Then I realized I forgot to add a migration and when I did, the problem was resolved. Hope that helps.

  • Hi, thanks for your answer, but can you elaborate? I'm afraid I don't know what adding a migration means. – Johan De Coster Dec 13 '17 at 19:25
  • Hi, if you mean do a truff migrate -reset, yes, that worked! – Johan De Coster Dec 14 '17 at 18:05
  • Sorry, what I meant was to add the appropriate js file (e.g. 2_deploy_Contract.js) in the migrations folder. In case you have already done that, then --reset would have been my next suggestion, but @Ismael got it first. :) – Mike Dec 15 '17 at 12: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.