Hyper Code IDE Guide
  • Introduction to Hyper Code IDE
  • Lesson 1: Get Started
    • Smart Contract Project Files
    • Project Files
    • CRC_Contract
    • Contract Method
  • Lesson 2: Smart Contracts
    • Introduction to Smart Contracts
    • Creating your first smart contract
    • Debugging
    • Deployment
    • Smart Contract Interaction
  • Lesson 3: Wallet Access
    • Introduction to Wallet Access
    • Circular Wallet Architecture
    • Native CRC Wallet
    • Custom Wallet Data
  • Lesson 4: Your First Token
    • Introduction
    • Simple Token Balance
    • GetBalance Function
    • Drop Function
    • Transfer Function
Powered by GitBook
On this page
  1. Lesson 3: Wallet Access

Custom Wallet Data

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.

PreviousNative CRC WalletNextIntroduction

Last updated 7 months ago