If the CRC_Wallet implementation doesn’t meet our needs, we can easily store custom data within the wallet. To understand this better, let’s break down the OpenWallet and CloseWallet functions. In our current setup, we store two key pieces of information in the smart contract:
Balance: Tracks the number of tokens we own.
Allowances Array: Lists the tokens we’re authorized to spend for others.
Now, let’s explore how this data is extracted from the wallet:
/* Code in OpenWallet */
// load the wallet
var w = LoadWallet(address);
// if the wallet has a Data field inside (it means that it is an existing wallet)
if(w.Data !== '') {
// the data, in json format is parsed
var data = JSON.parse(w.Data);
// we extract here the balance
if ('Balance' in data) { this.Balance = data.Balance; }
// we extract here the allowances
if ('Allowances' in data) { this.Allowances = data.Allowances;}
}
// if the wallet didn't contain any instance of this contract, nothing will be done and the initial
// values of the contract will be used.
/* Code in CloseWallet */
// we create an object with all the info that we wish to store
var dataObject = {
Balance : this.Balance, // the wallet balance
Allowances : this.Allowances // the allowances
};
// Convert the data object to a JSON string
var dataString = JSON.stringify(dataObject);
// Use the SaveWallet function to save the updated wallet
SaveWallet(this.Address, dataString);
As shown, the process is simple: store the needed information in a JSON string and retrieve it easily. With this, you’re ready to create various smart contracts and explore the world of Circular.