Skip to content

Instantly share code, notes, and snippets.

@skplunkerin
Last active November 25, 2021 18:30
Show Gist options
  • Save skplunkerin/241e8830d144ff5095df9bece8ee0eb9 to your computer and use it in GitHub Desktop.
Save skplunkerin/241e8830d144ff5095df9bece8ee0eb9 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// See feedback: https://gist.github.com/skplunkerin/61fe0562a72016162aece7b22609fb62
pragma solidity 0.8.7;
contract MemoryAndStorage {
struct User{
uint id;
uint balance;
}
// state variable - using storage
mapping(uint => User) Users;
// PUBLIC
// addUser creates a new user with the passed in _id and _balance,
// adding them to the users state variable.
function addUser(uint _id, uint _balance) public {
// only add if the user doesn't exist
require(!_userExists(_id), "user already exists, please use updateBalance()");
Users[_id] = User(_id, _balance);
_assertUserBalance(_id, _balance);
}
// updateBalance will modify a users balance if the passed in user
// _id is found.
function updateBalance(uint _id, uint _balance) public {
_requireUserExists(_id);
Users[_id].balance = _balance;
_assertUserBalance(_id, _balance);
}
// getBalance returns the users balance if the passed in user _id
// is found.
function getBalance(uint _id) view public returns (uint) {
_requireUserExists(_id);
return Users[_id].balance;
}
// PRIVATE
// _userExists verifies if the passed in user _id is found or not.
function _userExists(uint _id) private view returns(bool) {
User memory u = Users[_id];
if (u.id == 0) {
return false;
}
return true;
}
// _assertUserBalance verifies the user.balance matches the passed
// in _balance.
function _assertUserBalance(uint _id, uint _balance) private view {
assert(_userExists(_id));
assert(Users[_id].balance == _balance);
}
// _requireUserExists requires that the passed in user _id is found
// in users.
function _requireUserExists(uint _id) private view {
require(_userExists(_id), "User doesn't exist, please create the user first.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment