I am pretty new to Solidity and working with Ethereum in general. In the (d)app I'm working on I need to be able to persist data onto the ledger, but I'm not sure I understand how this works.
Let's say I have the following contract (simplified for practicality):
contract UserContract {
struct User {
address walletAddress;
string organisation;
string fName;
string lName;
string email;
uint index;
}
mapping(address => User) private users;
address[] private userIndex;
function insertUser(
address walletAddress,
string organisation,
string fName,
string lName,
string email )
public
returns(uint index) {
User memory newUser = User({
walletAddress: walletAddress,
organisation: organisation,
fName: fName,
lName: lName,
email: email,
index: users.length
});
users.push(newUser);
userIndex[walletAddress] = newUser.index;
return newUser.index;
}
}
Using the insertUser()
method, I can insert a new user, and using a getter method I could retrieve the user's information.
Now, if I update the contract (thus deploy a new one), the users
mapping is empty again, not surprising.
My question: how do I store data in a way that it will be accessible for future versions of the contract? Any design patterns that go along with this process?
Thanks!