Skip to main content
added 573 characters in body
Source Link
        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;
        }
        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!

        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;
        }
Source Link

How does this line of code (Solidity) work?

        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!