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.

Last updated