Solidity – Only the Owner of the Contract Can Update the Smart Contract

blockchainethereumjsmetamasksolidityweb3js

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.

Best Answer

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.

Related Topic