0
        User storage user = users[msg.sender];

I have a "User" struct:

    struct User {
    Deposit[] deposits;
    uint256 checkpoint;
    address referrer;
    uint256[3] levels;
    uint256 bonus;
    uint256 totalBonus;
}

And a "users" mapping:

    mapping (address => User) internal users;

The function the original line only asks for:

function intense(address referrer, uint8 plan) public payable {

So I understand what a mapping is and what the Struct is (I'm a beginner) I'm just very confused about the first line: If I understand well, "users[msg.sender]" refers to the item in the "users" mapping with the key value "msg.sender". However, I don't remember creating this. If I am creating this, I don't understand how I created it when the "users" mapping only includes the "User" struct and I haven't given all the information the user struct needs. Plus, I don't know what the new "user" is.

Thanks a lot for the help!

Edit: Can't seem to add the function in "code" in comments, so here it is:

    function invest(address referrer, uint8 plan) public payable {
    require(msg.value >= INVEST_MIN_AMOUNT);
    require(plan < 6, "Invalid plan");

    uint256 fee = msg.value.mul(PROJECT_FEE).div(PERCENTS_DIVIDER);
    commissionWallet.transfer(fee);
    emit FeePayed(msg.sender, fee);

    User storage user = users[msg.sender];

    if (user.referrer == address(0)) {
        if (users[referrer].deposits.length > 0 && referrer != msg.sender) {
            user.referrer = referrer;
        }
2
  • Can you share the complete function?
    – mzaidi
    Apr 14 at 16:25
  • Here's the function's beginning, I don't think the rest is necessary: ``` function invest(address referrer, uint8 plan) public payable { require(msg.value >= INVEST_MIN_AMOUNT); require(plan < 6, "Invalid plan"); uint256 fee = msg.value.mul(PROJECT_FEE).div(PERCENTS_DIVIDER); commissionWallet.transfer(fee); emit FeePayed(msg.sender, fee); User storage user = users[msg.sender]; ``` if (user.referrer == address(0)) { if (users[referrer].deposits.length > 0 && referrer != msg.sender) { user.referrer = referrer; }
    – kron0s
    Apr 14 at 16:49
0
User storage user = users[msg.sender];

This line check the mapping users and gets the User object corresponding to the address msg.sender. It then assigns this User object to a variable named user. It does not create a new object, it simply fetch an already existing User object and assign it to a variable.

As you mentioned, it implies that a User object was previously created and assigned to msg.sender address in the users mapping.

5
  • Well, the problem is, the code never creates a User object before. The Struct exists but no User has been pushed and no function to create one has been used. That's my problem here: I don't think there's anything in the users mapping at the [msg.sender] key value. Edit:
    – kron0s
    Apr 14 at 18:21
  • Well then you have to create User objects and assign them in the users mapping!
    – Undead8
    Apr 14 at 22:42
  • No Undead8 I don't have to. The code works without needing to have anything already assigned to the users mapping so you'd be wrong here.
    – kron0s
    Apr 15 at 9:56
  • User objects must be created somewhere in your code or the struct would return null. If not, then we are in presence of a self-aware machine that may soon take-over the world.
    – Undead8
    Apr 15 at 12:47
  • I just want to clarify that yes the mapping may return something, but it will be null and will have no use at all. If you want it to return a User, you have to create that User beforehand.
    – Undead8
    Apr 15 at 12:53

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.