Data Location Assignment

In order to avoid inefficient data storage, it’s not necessary to store local variable user into a storage data location. We should prefer to set the new balance directly into mapping users.

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]; //change from memory to storage
     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 {
    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;
}

}

Hello, I changed memory to storage.

1 Like

The original Users memory variable in the code was only storing it as a memory variable; so, the variable was not accessible in any other function in the contract, more specifically the getBalance function.

The assignment was to make the function update the balance; so, the updateBalance function was really not any different from the addUser function. You just type in the user you want to update and the new updated balance. When you select getBalance function the variables/arguments within updateBalance function are accessible to any function in the contract.

I am sure others used a + or - variable and updated that way; however, that solution was not what was implied in the assignment in my opinion.

pragma solidity 0.7.5;
contract MemoryAndStorageTest {

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);  // this new variable is only updating the balance within the function and does not change the 
                                      // balance in the getBalance function. 
}

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);
}

//Chose this way because it's less code/cleaner to call that specific user
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

Hey guys, I’m redoing the academy Solidity courses to brush up on some things :smile: . See you on Fika in Thursday :heart:

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;
    }
}
2 Likes

First solution to memory problem :slightly_smiling_face: .
I assume that storage declaration is some kind of safe reference to permanent memory…

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;

//This works and i can store values of the structure into an array and they will be updated as required 
//but I actually did not use the mapping part.  I did try to use teh mapping with this concept but it kept 
//givng me the error that mapping can only be used with basic/primitive type or something like that.  
//I also tried to create a functioin that would print out all the elements of the array but it would not
//work.  Must be a way to print either one or two parts of elements for id and balance.  I will work on 
//trying to do this another way using the mapping.

contract MemoryAndStorage {

    mapping(uint => User)users2;

    struct User{
        uint id;
        uint balance;
    }
    
    User[]users1;
    
    function addUser(uint _id, uint _balance) public {
        User memory newUser = User(_id,_balance);
        users1.push(newUser);
        
    }

    function updateBalance(uint _id, uint _addBalance) public {
    
         users1[_id].balance =  users1[_id].balance + _addBalance;
    }

    function getBalance(uint _id) view public returns (uint) {
        return users1[_id].balance;
    }
    
    //How do you make a function to print out all the elements of an array with double elements(id, balance)?
    //Or how do you print just the balance
    function getArray() public view  returns ( User[] memory){
            return users1; //print the entire array elements  
        }

}
1 Like

//Ok this is another way that I did this and it was much simpler. I only deleted one line of code and added one of code.

//The problem was in adding to balance the “user” this is a local instance of struct “User” and does not change the state variables/elements in the state Structure User. You must use the created “users” instance when executing the function “updateBalance” and you also must take users[_id].balance += _balance to add to the previous balance. I also did change the functions with underscore to let you know that these are public inputs that are being used.

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];   This statement was removed
        // users[_id].balance = users[_id].balance + _balance; // use to add to the balance
         user[_id].balance = balance;  //use to update to new balance.
    }

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

}
1 Like

I feel this took me way to long and I was trying to make it way more complicated then it needed to be and watching the solution video I feel dumb cause I thought memory was wrong but didn’t think to just change it

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

}
1 Like

Happily, I present my solution to this assignment. A simple change from “memory” to “storage” in the updateBalance 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

My thought here is that; no code is the best code :+1:

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 {
         // Original
         // 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
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

Hi here is 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);
}

function updateBalance(uint id, uint balance) public {
  uint FinalBalance ;
  FinalBalance= users[id].balance + balance;
   users[id] = User(id,  FinalBalance);
    // User memory 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
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

The function updateBalance() just updates, that is sets, the balance to a new balance. One can of course do an addBalance() function that adds to the current balance.

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