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?
1 Answer
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.
-
But what if my function in the contract is the type view? In the web3.js the method call() has a from property?– Hash1Commented 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!– Hash1Commented Apr 15, 2023 at 21:45
msg.sender
is some other address.