function updateBalance(uint id, uint balance) public {
User memory user = users[id];
user.balance = balance;
users[id].balance = user.balance;
}
Update users[id] with the latest balance which will update the balance figure in storage.
function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}
Hi @HardlyCodeMan,
Your solution works, but you can make it more concise:
As you have already assigned balance
to the temporary/local variable user
, you can just assign the whole user
instance to the mapping (as this includes the updated balance
property):
User memory user = users[id];
user.balance = balance;
users[id] = user;
There is also no need to assign the User
instance with the balance (before it is updated) to the temporary/local variable user
in the first line. We can just declare user
in the first line without any properties, because the second line will then both add the balance property and assign the new balance to it:
User memory user;
user.balance = balance;
users[id] = user;
In fact, you can avoid the temporary/local variable altogether, by just assigning the updated balance
directly to the mapping, as follows:
function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}
The first 2 lines (the original code) can now be removed.
Ok, no peeking - replacing contents of updateBalance function with only this line of code seems to do the trick:
users[id].balance = balance;
At least, to use a famous quote, ‘it works for me’ ;).
Thanks for the feedback @jon_m,
I see the refactoring you’ve done making the code more concise.
I changed the memory data location to storage as if stored in memory the data would be lost when the function finishes being executed. Storage holds on to the data.
pragma solidity 0.5.1;
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;
}
}
pragma solidity 0.5.1;
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;
}
}
I just saw the solution, and figured out that though simlest solution that I implemented, it is the most expencive so I wouldnt do it like that, insted I would:
function updateBalance (uint id, uint balance) public {
users[id].balance = balance;
}
Hi I am posting solution for the assignment:
The key to the solution is highlighted.
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;
}
}
pragma solidity 0.5.1;
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;
}
}
pragma solidity 0.7.5;
contract MemoryAndStorage {
//storage - permanent storage of data ( state variables)
//memory - temporary storage used in function execution
//calldata - save arguments/inputs to our functions
//strings, arrays, mappings, structs
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;
}
}
Yep, @slavab, that works just fine
Welcome to the forum — good to see you here, and hope you’re enjoying the course
Couldnt do it myself :(, had to copy the answer from this thread
and had a hard time understanding some stuff, i definetely need to practice more,before continuing with the course, anyways
i chose the one that goes:
function updateBalance(uint id, uint balance) public{
users[id].balance = balance;
}
as we all already know:
function updateBalance(uint id, uint balance) public {
User memory user = users[id];
user.balance = balance;
}
this only updates the balance momentarily and then after the function finishes working, the new data disappears, and also it is redundant, as it states info that was already stated in the updateBalance function, so yes with the corrected version we kill two birds with one stone, making the code shorter and and making it actually do what we wanted it to.
function updateBalance(uint id, uint balance) public {
User memory user = users[id];
user.balance = balance;
users[id] = user;
}
function updateBalance(uint id, uint balance) public {
balance[id].balance = balance;
}
pragma solidity 0.7.5;
contract MemoryAndStorage {
struct User{
uint id;
uint balance;
}
mapping(uint => User) users;
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] = user;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
Two solutions.
- Changing the User memory user = users[id]; to User storage user = users[id]; Seems to do the trick. For some reason I don’t like this solution so much, it gives me the impression that it creates a copy ant its not efficient.
- Replacint this two lines:
User storage user = users[id];
user.balance = balance;
With this one line
users[id].balance = balance;
Maybe for some people it isn’t so readable, but I think it’s the most efficient because it changes the value of a storage variable directly.
EDIT: After seen the solution video I learned that the first solution does not create a copy but it’s a pointer. Good to know!
From what I figured out on this assignment, it seemed that the updateBalance() function was trying to access the specific User from the User struct through memory. This users info would have been kept in storage though. So the solution I found was to change the stored data type from memory to storage.
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance = balance;
}
Updated code to comment out memory modifier and perform update on next line without creating the temporary struct.
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];
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].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 {
User storage user = users[_id]; // changed keyword memory into storage
user.balance = _balance;
}
function getBalance(uint _id) view public returns (uint) {
return users[_id].balance;
}
}
I haven’t see the solution yet but it seems that the updateBalance function can’t access the user from the addUser function if it is stored in memory because whatever is stored in memory is gone after the function (addUser) is executed.
It is a bit confusing to navigate between the values and variables that are named so similarly: user, User, users