1

I am developing a smart contract that holds user information. The problem is that when I try to update the user information from the address that did not deployed the contract, the request is successful but there is no change in the user information. But when I change the user information from the address that deployed the contract it changes the user's information for all the users. Like the change is reflected on all the accounts regardless of the account being different.

function setUserName(string memory _userName) public {
    users[msg.sender].userName = _userName;
}

This is my function that updates the user info. I believe that "msg.sender" is the one that is calling the contract and not the one that deployed the contract. I am using metamask and ganache for accounts. The first account is added to metamask in chrome browser and the second account is added to metamask in mozilla firefox.

Steps to reproduce username bug:

  1. Open two different browsers
  2. Start ganache server
  3. In browser one add first address's private key to the metamask in order to add ETH
  4. In browser two add second address's private key to the metamask in order to add ETH
  5. Deploy the contracts with " truffle migrate --reset "
  6. It will migrate the contracts with the first address in ganache
  7. From the browser two try to update the username. You will se that the update was successful but the username was not update (even after refresh).
  8. Now from browser one update the username by going to the settings page. You will see that the username is updated and this change is also reflected on the browser two, regardless of the address being changed from the browser one's.
  9. Same happens when we try to obtain points by exchanging tokens. From browser one the request is successful but from browser two it throws an error indicating that "ERC20: transfer amount exceeds balance". Even the user has token in their account.

EDIT

I have figured out a way to deal with this. The problem was the data was being saved correctly but when fetching the records solidity was assigning msg.sender to the creator of the contract not the one who sent the transaction. So, in order to deal with this I am sending the user address from frontend in the call function and receiving the address as a parameter in the respective functions. So instead of using the msg.sender I am using the address that I am receiving from frontend.

3
  • Can you show the whole contract? What variable are showing the browsers? Try to focus the question in the first issue, for other issues can create new questions and put a link to this question to set the context.
    – Ismael
    Oct 8 at 20:23
  • Thank you @Ismael I have figured out a way to deal with this. The problem was the data was being saved correctly but when fetching the records solidity was assigning msg.sender to the creator of the contract not the one who sent the transaction. So, in order to deal with this I am sending the user address from frontend in the call function and receiving the address as a parameter in the respective functions. So instead of using the msg.sender I am using the address that I am receiving from frontend.
    – Usama
    Oct 12 at 4:26
  • Great! It would be nice if you write the comment as the answer so the question can be marked as resolved.
    – Ismael
    Oct 12 at 5:50
0

Answer To The Question

I have figured out a way to deal with this. The problem was the data was being saved correctly but when fetching the records solidity was assigning msg.sender to the creator of the contract not the one who sent the transaction. So, in order to deal with this I am sending the user address from frontend in the call function and receiving the address as a parameter in the respective functions. So instead of using the msg.sender I am using the address that I am receiving from frontend.

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.