Changed function updateBalance as per the below:
function updateBalance(uint id, uint balance) public {
users[id].balance=balance;
}
unction updateBalance(uint id, uint balance) public {
users[id].balance = balance; //changed user.balance to 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 memory user = users[id];
users[id] = User(id, balance);
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
pragma solidity 0.5.12;
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] = user; // done !!
}
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 {
// This is changed!
// We do not want to add a variable in memory, and change that one
// What we do want to do is to change the value of the stored user in users
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 {
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 {
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
the storage keyword comes in handy.
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance = 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 {
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]; //change from memory to storage
user.balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
The bug in the code was that the local instance of user struct was created and then updated locally. The correct solution is access the user in the mapping by providing its id and update its balance.
function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}
I changed the data location from the variable user to storage, because otherwise, user’s new balance would be deleted after the function ends, since it had the memory location type.
pragma solidity 0.5.12;
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 noticed that balance is updated in local user variable that lives only in updateBalance function (user is memory location). I changed the code to update global users (storage location) variable referenced with id.
pragma solidity 0.5.12;
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;
//user.balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
pragma solidity 0.5.12;
/**
*contract number X(Not mine)
*Ivan on tech academy - Ethereum smart contract programming course. With Filip Martinsson.
*Chapter 3 - more Concepts - exercise 1 - Data location.
**/
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
User memory user;
user.id = id;
user.balance = balance;
users[id] = user;
}
function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
Assignment: Memory and Storage / Data locations
Solution:
function updateBalance (uint id, uint balance) public {
users[id].balance = balance
}
After searching online for some more info on data location I figured that we can just switch “memory” for “storage”, since memory is only saved during function execution:
function updateBalance(uint id, uint balance) public{
User storage user = users[id];
user.balance = balance;
}
Here is solution. Swear I didn’t watch the following episode
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 memory user;
user.balance = balance;
users[id] = user;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
Thanks and keep it up!
I noticed the function used to update the balance was set to “memory” and not “storage”. Updating the memory simply changes the short term data within the contract executed at that time. Thus once the contract expires the data is lost. Updating to storage permanently updates the data even after contract execution. My code pasted below:
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;
}
}
We just have to add storage location to Users ,after balance update.
In order do not get lose when the function is completed .
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance = balance;
}
Problem with original code is in the function ‘updateBalance’:
function updateBalance(uint id, uint balance) public
{
// Bad code!
// User memory user = users[id];
// user.balance = balance;
// Good code!
users[id].balance = balance;
}