Data Location Assignment

changed User memory to User Storage in update balance function.

pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User{
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint balance) public {
         User storage user = users[id];
         user.balance = balance;
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}
1 Like
pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User{
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint balance) public {
         User storage user = users[id];
         user.balance += balance;
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}
1 Like

Hey guys! Here’s my solution for the data location assignment :slight_smile:

I believe there is a simpler way to do this, if anyone is willing to point out some tips on my code that would be appreciated!

pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User{
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint newBalance) public {
        
         users[id] = User(id, newBalance);
            User storage user = users[id];
         uint newBalance = user.balance;
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}

Changed the memory to storage in the updateBalance function

pragma solidity 0.8.4;

contract MemoryAndStorage {
    mapping(uint => User) users;
    struct User {
        uint id;
        uint balance;
    }
       
    function addUser(uint id, uint balance) public {
        
        
        users[id] = User(id,balance);
    }
    function updateBalance(uint id, uint balance) public {
        
        
       User storage user = users[id];
         user.balance = balance;
        
       
    }
    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }
}
1 Like

Just change from memory to storage on line 16.
Seems to work…

pragma solidity 0.7.5;
contract MemoryAndStorage {

mapping(uint => User) users;

struct User{
    uint id;
    uint balance;
}

function addUser(uint id, uint balance) public {
    users[id] = User(id, balance);
}

function updateBalance(uint id, uint balance) public {
     User storage user = users[id];
     user.balance = balance;
}

function getBalance(uint id) view public returns (uint) {
    return users[id].balance;
}

}

1 Like

pragma solidity 0.7.5;
contract MemoryAndStorage {

mapping(uint => User) users;

struct User{
    uint id;
    uint balance;
}

function addUser(uint id, uint balance) public {
    users[id] = User(id, balance);
}

function updateBalance(uint id, uint balance) public {
  //   User memory _user = users[id];
   //  _user.balance = balance;
     users[id].balance = balance;
}

function getBalance(uint id) view public returns (uint) {
    return users[id].balance;
}

}

1 Like

Here is the other solution that just changes the user variable to be assign to storage and not to memory : ex .

function updateBalance(uint id, uint balance) public {
User storage _user = users[id];
_user.balance = balance;

my question however is that where does user[id] get assigned the values of _user? At moment its the other ay around. _user gets the value assigned to users[id].

I would think a line like : user[id].balance = _user.balance woud be needed.

thats why my original solution just had that : user[id].balance = balance; Its just simpler.

anyway this is the complete 2nd solution :

pragma solidity 0.7.5;
contract MemoryAndStorage {

mapping(uint => User) users;

struct User{
    uint id;
    uint balance;
}

function addUser(uint id, uint balance) public {
    users[id] = User(id, balance);
}

function updateBalance(uint id, uint balance) public {
    User storage _user = users[id];
     _user.balance = balance;
    // users[id].balance = balance;
}

function getBalance(uint id) view public returns (uint) {
    return users[id].balance;
}

}

1 Like
pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User{
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint balance) public {
         users[id] = User(id, balance);
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}

Since You are just rewrite the balance and not transfering etc. the solution for updateBalance is the same as addUser with balance.

1 Like

pragma solidity 0.7.5;
contract MemoryAndStorage {

mapping(uint => User) users;

struct User{
    uint id;
    uint balance;
}

function addUser(uint id, uint balance) public {
    users[id] = User(id, balance);
}

function updateBalance(uint id, uint balance) public {
   
     users[id].balance = balance;
}

function getBalance(uint id) view public returns (uint) {
    return users[id].balance;
}

}

1 Like

pragma solidity 0.7.5;
contract MemoryAndStorage {

mapping(uint => User) users;

struct User{
    uint id;
    uint balance;
}

function addUser(uint id, uint balance) public {
    users[id] = User(id, balance);
}

function updateBalance(uint id, uint balance) public {
     users[id].balance = balance;
}

function getBalance(uint id) view public returns (uint) {
    return users[id].balance;
}

}

1 Like

I changed “memory” to “storage” so that the user would be persistently stored, not just when the function runs. Nice to see a lot of ways I couldn’t even think of!

pragma solidity 0.8.4;
contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User{
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint balance) public {
         User storage user = users[id];
         user.balance = balance;
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}```
1 Like

pragma solidity 0.7.5;
contract MemoryAndStorage {

mapping(uint => User) users;

struct User{
    uint id;
    uint balance;
}

function addUser(uint id, uint balance) public {
    users[id] = User(id, balance);
}

function updateBalance(uint id, uint balance) public returns(uint){
     User storage user = users[id];
     user.balance = balance;
     return user.balance;
}

function getBalance(uint id) view public returns (uint) {
    return users[id].balance;
}

}

1 Like

After doing some google I tried the code below, and many people here also solved the problem in the same way. So, I wonder how Filip solved the problem! (I will check the video :slight_smile: )

function updateBalance(uint id, uint balance) public {
         User storage user = users[id];
         user.balance += balance;
    }
1 Like

function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}

Since we were using a variable to store the value, and all the variables inside a functions are stored in the memory, we weren’t updating anything. By referencing the id, we can access the user balance and update it to the value we want.

1 Like

Just had to modify the updateBalance function body slightly! Even the header was fine.
Essentially, instead of creating a new User variable and assigning it to memory and then updating their balance, you would just take the user id and assign the updated balance to storage.

My solution:

pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User{
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

// Below function was changed

    function updateBalance(uint id, uint balance) public {
        users[id].balance = balance;
    }

// Above function was changed

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}
1 Like
pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User {
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint balance) public {
         User storage user = users[id];
         user.balance = balance;
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}
1 Like

This is a best guess cos I’m on a train and can’t seem to compile the code to check it despite being (more or less) online, but based on the getBalance code I’d do this:

BEFORE

function updateBalance(uint id, uint balance) public {
User memory user = users[id];
user.balance = balance;
}

AFTER

function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}

I’m assuming that will modify in place and there’s no need to return anything.

FULL CODE

pragma solidity 0.7.5;
contract MemoryAndStorage {

mapping(uint => User) users;

struct User{
    uint id;
    uint balance;
}

function addUser(uint id, uint balance) public {
    users[id] = User(id, balance);
}

function updateBalance(uint id, uint balance) public {
     users[id].balance = balance;
}

function getBalance(uint id) view public returns (uint) {
    return users[id].balance;
}

}

1 Like
    pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User{
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}

Just write the same function and it overwrites it.

2 Likes
pragma solidity 0.7.5;

contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User{
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint balance) public {
         users[id].balance = balance;
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}
1 Like
pragma solidity 0.7.5;

contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User{
        uint id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function updateBalance(uint id, uint balance) public {
        
         User storage user = users[id];
         user.balance = balance;
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}
1 Like