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 4: Your First Token

GetBalance Function

Of all the Smart Native Contracts (SNCs) we’ve discussed, one stands out for its smart contract interaction: “GetBalance.” This endpoint retrieves the balance of a specific wallet. Its implementation is similar to the constructor, with the key difference being that we’re only fetching the wallet’s balance. Here’s the code for this operation:

__GetBalance: function (address) {
    
      // Creates a new wallet instance
      var wallet = Object.create(CRC_Wallet);
    
      // Opens the specified wallet
      if(wallet.OpenWallet(address)){
        
          // if the wallet is available prints out the balance
          println('Balance : ' + wallet.Balance + ' '+ this._Symbol);
          return wallet.Balance;
      }
      return -1;
  }

The code is simple: after creating an instance of the specified wallet, we check if it opens successfully, retrieve the balance, and print the result. We don’t need to close the wallet here, as no updates are made. Even if a “CloseWallet” operation were included, it would be ignored since Smart Native Contracts (SNCs) don’t modify the smart contract’s state.

PreviousSimple Token BalanceNextDrop Function

Last updated 7 months ago