I have a simple function to compare two string:

function stringCmp(string a, string b) public returns (bool){ 
  bytes memory aa=bytes(a);
  bytes memory bb=bytes(b);
    return false;
  for(uint i=0; i< aa.length; i++)
      return false;
  return true;

This function properly work in truffle console if used as follow:

t.stringCmp.call("string1","string1") --> true
t.stringCmp.call("string1","string2") --> false

Supposing we have declared in the contract:

string public p="ciao";
string public q="ciao";
string public r="hello";

Which is the properly way to pass those string to the call? I tried :

t.stringCmp.call(t.p(),t.q()) --> true (and it seems ok but..)
t.stringCmp.call(t.p(),t.r()) --> true (So what..???)
t.stringCmp.call(t.p,t.r) --> true (???)
t.stringCmp.call(t.p.call(),t.q.call()) --> Invalid number of arguments to Solidity function

Moreover what is exactly going on? Any Workaround?


I think the console is helping you when you try to execute the commands without handling the promises correctly. Your contract members (p, q, r) when called with web3 will return a promise that's why all returns true (since if you just do t.p or t.r from the console it will return the same object)

So said the last attempt is the most close one but the call function returns a promise and is not handled correctly (but if you try to do just t.q.call() from the console it will work, same as t.q(); again, the truffle console might help handling the promises itself).

If you handle the promises correctly it will work

t.r.call().then((r1) => t.q.call().then((q1) => t.stringCmp(q1, r1).then(console.log)))

this return false

t.p.call().then((p1) => t.q.call().then((q1) => t.stringCmp(q1, p1).then(console.log)))

this will return true

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.