1

I have a function on my contract that return true if the address on one list is equal to the msg.sender. The function works fine on remix but when I call it utilizing javascript with next.js and web3.js it is returning false. What could it be?

4
  • 2
    Quite obviously the msg.sender is some other address. Commented Apr 15, 2023 at 7:25
  • Sure, but why? The address at zero is the address that should be calling the function and its the same that is added to the list of addresses...
    – Hash1
    Commented Apr 15, 2023 at 8:02
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Apr 15, 2023 at 8:51
  • The problem was that when i set the web3 provider by default it calls the function of the contract with the first account connected to the dapp, so i need to specify the account that i want to use when passing the .call() method on the web3js! Tks for your help!
    – Hash1
    Commented Apr 16, 2023 at 21:40

1 Answer 1

0

msg.sender does not same in your test code and you logic, you have to send your transaction from msg.sender in your test. For example in web3.js you can use "from:msg.sender" for sending transaction with specify address .sample code is:

let transaction = await yourContract.yourFunction(ag1, arg2,....(other input arguments), { from: msg.sender});

you must use the same msg.sender with an address initiated in the contract.

3
  • But what if my function in the contract is the type view? In the web3.js the method call() has a from property?
    – Hash1
    Commented Apr 15, 2023 at 8:11
  • for view function you can see this question: ethereum.stackexchange.com/questions/60117/… and for call() property goes here: web3js.readthedocs.io/en/v1.8.1/… Commented Apr 15, 2023 at 10:49
  • That was it! Tks! i solved the problem simply by adding the .call({from: address[0]}). Reading the documentation and with your help i learned that by default the web3.js call() method set the msg.sender to the first account on the wallet, so especifing the account index in the call method make it work!
    – Hash1
    Commented Apr 15, 2023 at 21:45

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.