This is the part of the contract that has to be changed.
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance = balance;
}
This is the part of the contract that has to be changed.
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance = 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];
user.balance = balance;
// user.balance += balance; in case we would want to keep adding balance instead of updating to the las value
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
Nice solution @pieczyslaw
This is a good observation…
Even though the initial idea of this assignment is to update the existing balance by replacing it with the new one, I actually think that it makes more sense to do what you suggest here as an alternative, and add an amount to the existing balance using the addition assignment operator +=
(instead of the assignment operator =
). However, if we add to, instead of replacing, the existing balance, I think the code would be clearer and more readable if we also changed the name of the balance
parameter to amount
, because we would be adding an amount to the balance, rather than replacing it with a new balance e.g.
function updateBalance(uint id, uint amount) public {
User storage user = users[id];
user.balance += amount;
}
Just let me know if you have any questions
The Original Code for this Assignment is Missing the += Operator. Simply switching the Memory to Storage will not Deploy the Contract.
The correct code is user.balance += balance;
hey can you help clear up some of my confusion please?
so the user varible of the User type, that is declared in the updateBalance function is storing an id and a balance… how does the getBalance function call on the user variable if the only code in the getBalance function is return users[id].balance???
I’m having a hard time understanding
Hi can you help clear up some of my confusion please?
so the user varible of the User type, that is declared in the updateBalance function is storing an id and a balance… how does the getBalance function call on the user variable if the only code in the getBalance function is return users[id].balance???
I’m having a hard time understanding
i’m referring to the solution that simply changes memory to storage:
function updateBalance(uint id, uint amount) public {
User storage user = users[id];
user.balance += amount;
Hey @Rob1, hope you are well.
Sorry for the delay on the answer.
If you check the mapping, it will point an uint
to a struct
based on User
properties (id, balance);
So the proper way to access the property of the mapping ID, its by create a function that can look into the mapping, get the desired ID and return its value.
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
Carlos Z
Understood thanks
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;
}
}
Well this one works for me and updates the balance, But is it correct ?
Almost perfect, the problem is that you are reassigning the entire struct of the user, not only the balance has requested.
You can just replace with users[id].balance = balance;
in your updateBalance()
function.
Carlos Z
Thanks for the detailed correction !!
// SPDX-License-Identifier: Unlicensed
pragma solidity 0.8.7;
contract MemoryandStorage{
mapping (uint => User) users;
address owner;
modifier OnlyOwner {
require(msg.sender == owner, "Balance Updates/New user additions can only be set by address owner");
_;
}
constructor(address _owner){
owner = _owner;
}
struct User{
uint id;
uint balance;
}
function addUser(uint id,uint initialBalance) public OnlyOwner{
users[id] = User(id, initialBalance);
}
function incrementBalanceData(uint _id, uint incrementBalance) public OnlyOwner{
users[_id].balance += incrementBalance; //Function will increase Data Balance onlyset by owner
}
function decreaseBalanceData(uint _id, uint decreaseBalance) public OnlyOwner{
users[_id].balance -= decreaseBalance; //Function will decrease Data Balance onlyset by owner
}
function updateBalanceData(uint _id, uint updateBalance) public OnlyOwner {
users[_id].balance = updateBalance; //Function will update data to a new balance onlyset by owner
}
function click_Me() public pure returns(string memory){
return "Hello, Friend";
}
function getUser(uint id) public view returns(uint){
return users[id].balance;
}
}
My solution
pragma solidity ^0.8.0;
contract MemoryAndStorage {
// Mapping of user IDs to user structs
mapping(uint => User) public users;
// Struct to store user information
struct User {
uint id;
uint balance;
}
// Function to add a new user to the contract
function addUser(uint id, uint balance) public {
// Ensure that the user does not already exist
require(users[id].id != id, "User already exists");
// Add the new user to the mapping
users[id] = User(id, balance);
}
// Function to update the balance of an existing user
function updateBalance(uint id, uint balance) public {
// Ensure that the user exists in the mapping
require(users[id].id == id, "User does not exist");
// Update the user's balance in storage
users[id].balance = balance;
}
// Function to retrieve the balance of a user
function getBalance(uint id) public view returns (uint) {
// Ensure that the user exists in the mapping
require(users[id].id == id, "User does not exist");
return users[id].balance;
}
}