Simple Token Balance

Before starting any project, it’s crucial to establish a solid foundation. A key step is compiling a comprehensive list of requirements before moving to implementation. For a token project, two critical elements need attention from the outset:

Name and Ticker: Choose a unique and compelling name and ticker symbol. For this project, we’ve selected “MyFirstToken” (MFT). Ensuring uniqueness is vital to avoid duplication.

Token Supply: Decide if the token will have a fixed supply. To keep things simple, we’ll use a fixed supply.

Additionally, we must track:

Token Circulation: Monitor the number of tokens in circulation versus those still in the initial supply.

Ownership: The token owner, responsible for deployment costs, holds initial ownership.

Now, let’s review the structure of our token class, which will lay the groundwork for developing the project.

var CRC_Contract = {

                    _TotalSupply : 100000000.00,    // Token Total Supply
                        _Balance : 100000000.00,    // Initial Balance before sale
                           _Name : "MyFirstToken",  // Token Name. It must match the project name in the settings 
                         _Symbol : "MFT",           // Asset Tiker.It must match the one in the settings 
                          _owner : "Owner address", // Asset Owner Address
                  
                    _constructor: function() {}
                  };'

Before diving into token operations, our first step is to focus on the constructor code. Here, we typically execute the Token Generation Event (TGE), transferring all initial tokens to the rightful owner. This can be easily accomplished using the CRC_Wallet class, as we’ve recently explored.

_constructor: function() {
      
      // Create a new wallet instance
      var Owner = Object.create(CRC_Wallet); 
      
      // Loads the Owner's Wallet
      Owner.OpenWallet(this._owner);
      
      // Assignes the total Supply to the Owner
      Owner.Balance = this._TotalSupply;
      
      // Updates the Owner's wallet
      Owner.CloseWallet();
    }

Next, we’ll define the key calls needed to query the contract and extract information using Special Native Calls (SNCs). The most commonly used include:

__GetTotalSupply: Retrieves the total token supply.

__GetCirculatingSupply: Provides the number of tokens sold so far.

__GetTokenName: Returns the token’s name.

__GetTokenTicker: Retrieves the token’s ticker symbol.

With this understanding, let’s implement these functions, which should take no more than 10 minutes.

__GetTotalSupply: function () {

    // we print on the smart contract output the total supply       
    println('TotalSupply : ' + this._TotalSupply );
    
    // we return it in case we wish to use internally
    return this._TotalSupply;
},
    
  
    
__GetCirculatingSupply: function () {
    
    // priting the circulating supply info
    println('CirculatingSupply : ' + (this._TotalSupply - this._Balance));
  
    // we return for internal uses
    return this._TotalSupply - this._Balance;
},


__GetTokenName: function () {
    
    // priting the Token Name
    println('Token Name : ' + this._Name);
  
    // we return for internal uses
    return this._Name;
},    


__GetTokenSymbol: function () {
    
    // priting the Token Symbol
    println('Token Symbol : ' + this._Symbol);
  
    // we return for internal uses
    return this._Symbol;
}, 

These calls don’t modify the smart contract’s state and are executed by a single node without requiring a transaction. We can quickly test them using the debugging tool to view the contract’s output. Let’s set up a testing section for our initial methods and review the results.

/* In debugging mode we need to invoke the constructor manually */
CRC_Contract._constructor();

CRC_Contract.__GetTotalSupply();
CRC_Contract.__GetCirculatingSupply();
CRC_Contract.__GetTokenName();
CRC_Contract.__GetTokenSymbol();

If everything is executed correctly, the output should resemble the following example:

<Image>

Last updated