Hi @evasilev,
Your basic solution is essentially correct:
One thing to note, though, is that we aren’t actually adding an amount to the existing balance, but updating/setting it to a new balance, and so you only need the assignment operator =
and not the addition assignment operator +=
I must admit though, it would seem more logical and practical to do it your way, and therefore to also name the input parameter amount
instead of balance
.
If we do change the intended functionality to your more practical one (adding the amount to the existing balance, instead of replacing it with a new one), then we would also need to start the assignment with the line…
user.balance += balance;
// instead of
user.balance = balance;
… because whichever one we use, that isn’t the error you want assert() to check for. At the moment your assert statement is only failing initially because it is checking for something that the function wasn’t actually meant to do anyway. However, if we change the functionality to add an amount to the existing balance, then using an assert statement becomes relevant and a meaningful check. But, what you have at the moment will then evaluate to true with the new initial incorrect code (with memory
) as well, because it is comparing the previous balance from the mapping with the local variable user
, which isn’t lost until after the assert statement has executed (when the function finishes executing). You need to compare the previous balance with the balance in the mapping after it is supposed to have been updated in order to actually check whether it has been updated correctly in persistent storage rather than just in a temporary local variable (the original problem you want to check has been resolved).
So, instead of:
… you would need:
assert(users[id].balance == previousBalance + balance);
I hope that makes sense, but do let us know if anything is unclear, or if you have any questions