1

I have a Solidity function that manipulates an object. The function only allows this manipulation of the caller msg.sender is owner of that object. Something like this:

function Manipulate(uint i)
{
     require(msg.sender == data[i].owner, "Sender is not owner.");
     <manipulate object>;
}

In the test that I am writing I am making a test case that checks whether this function behaves as expected. I have 2 user accounts:

user1 = accounts[0];
user2 = accounts[1];

user2 is owner of the object I want to manipulate. I do the following call in my test:

await instance.Manipulate.call(10, {from: user2});

Against what I expect, this triggers the Require statetement of my solidity function. So it claims that msg.sender is different from the owner. I modified the string in the Require statement so it also prints msg.sender and the owner of the object I want to manipulate. What I see is that user2 is indeed the owner of the object. However, msg.sender is not user2 but user1. This is weird because I call the Manipulate() function with {from: user2}. So I don't understand why user1 is the msg.sender.

Anyone knows why user1 is the msg.sender and why the {from: user2} statement didn't make user2 the msg.sender ?

4
  • 1
    Its your function public or public view ...?
    – JTCon
    Commented Jun 3, 2021 at 19:59
  • Just public. It is not a view. Data is being changed, not returned.
    – JohnyB
    Commented Jun 4, 2021 at 9:34
  • Maybe you are calling from Metamask o Remix with the User1 address. When a "public" function change a value need to be signed. Then you need your privatekey instead a single public address as User2.
    – JTCon
    Commented Jun 4, 2021 at 15:32
  • I am not using metamask or Remix. I am connected to Ganache.
    – JohnyB
    Commented Jun 5, 2021 at 13:46

1 Answer 1

3

Replace "=" with "==" in require statement : require(msg.sender == data[i].owner, "Sender is not owner.");

5
  • Yes, that was an error I made in this post. But in my code I do have "==" so that's not the problem. I corrected it in my original post.
    – JohnyB
    Commented Jun 4, 2021 at 9:35
  • try this await instance.methods.Manipulate(10).call({from:user2}); web3js.readthedocs.io/en/v1.2.11/…
    – kappa
    Commented Jun 4, 2021 at 11:34
  • This doesn't work for me. I get an error: instance.methods.Manipulate is not a function.
    – JohnyB
    Commented Jun 4, 2021 at 20:10
  • try it without method (i.e )await instance.Manipulate(10).call({from:user2});
    – kappa
    Commented Jun 4, 2021 at 20:22
  • I tried what you said. I also was unable to call it this way, I think because the function is not a view function but it manipulates data. However, I solved the problem. My function Manpulate() has 3 parameters. But when I called it, I was only passing 2 parameters. So because {from: user2} was at the third position, the code wrongly took this as the third parameter of the function. I know, stupid mistake of me :-). Thanks for the help !!!
    – JohnyB
    Commented Jun 5, 2021 at 13:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.