0

I am trying to make a smart contract which can add /delete and modify users in the smart contract. I have just written the code for adding the user but I am getting an error saying "revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information."

How can I solve this problem? Here is my code:

pragma solidity ^0.5.0;

contract Add{
struct User {
    uint256 id;
    string name;

    // other stuff

    bool set; // This boolean is used to differentiate between unset and zero struct values
}
 address owner;
 modifier onlyOwner() {
       require(owner == msg.sender);
            _;
}

mapping(address => User) public users;

function createUser(address _userAddress, uint256 _userId, string memory _userName) public onlyOwner {
    User storage user = users[_userAddress];
    // Check that the user did not already exist:
    require(!user.set);
    //Store the user
    users[_userAddress] = User({
        id: _userId,
        name: _userName,
        set: true
    });
}

}
1

I wouldn't critique style and formatting too much as this is your first effort.

It looks like it should work but you have to send from the owner account, but you don't set the owner, so it is still 0x0.

Do

address public owner;

So you can inspect it, to confirm.

The usual way to set the owner is the constructor.

constructor() public {
  owner = msg.sender;
}

That will set the owner to the address that deployed the contract. Keep testing with the same account.

Also, add a message to help debug.

require(owner==msg.sender, "403 unauthorized. ");

I typed that from a phone, so please forgive any typos.

Hope it helps.

| improve this answer | |

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.