Creating your first smart contract

We’ll start by creating a simple “Hello World!” smart contract to get comfortable with the IDE and its debugging tools. Here’s what we’ll cover:

1. The contract will store a string and a number.

2. It will print “Hello World!” in the debug output.

3. Methods will allow retrieval and modification of the string and number, recording who made the changes.

To begin, go to Resources/Templates and select CRC Empty Project. This will create a new contract structure that you can modify to meet these requirements.

var CRC_Contract = {

/** 
 * TODO: Contract Class Initialization; Initialize here yout contract properties
 *
 */
           _Name : "MyContract",      // Token Name. It must match the project name in the settings 
         _Symbol : "MCT",             // Asset Tiker.It must match the one in the settings 
          _owner : "Enter here the owner's wallet address", // Asset Owner Address
  
/** 
 * Contract Constructor. It will be executed only once, when the contract is deployed.
 *
 */
    _constructor: function() {
      
      // TODO: enter here your constructor code
      
    }

};

To improve the class, we’ll add:

  • Properties to store the string and number.

  • Fields to track who modifies these values.

  • A method to print the “Hello World!” message.

  • Methods for setting and retrieving the string and number.

With these updates, the class will resemble the following structure:

var CRC_Contract = {

           _Name : "MyFirstContract",      // Token Name. It must match the project name in the settings 
         _Symbol : "MFC",             // Asset Tiker.It must match the one in the settings 
          _owner : "0x23844...", // Asset Owner Address
       _MyString : "",                // it holds the string 
       _MyNumber : 0 ,                // it holds the number
  _StringChanger : "",                // address of who changed the string
     _NumChanger : "",                // address of who changed the number
  
    _constructor: function() {

      _StringChanger  = this._owner;  // the contract owner has initialized the string and the number
      
          _NumChanger = this._owner;
    },
  

    __HelloWorld: function () {    // it's a Single Node Call 
      
        println('Hello World !');  // prints in the output ""
      
    },
  
    ChangeNumber: function (num) { // It's a transaction    
      
          this._MyNumber = num;
        this._NumChanger = msg.From;
      
        println('The new number is : ' + num + "Changed by " + msg.From);  // prints in the output 
      
    },
  
    ChangeString: function (str) { // It's a transaction    
      
             this._MyString = str;
        this._StringChanger = msg.From;
      
        println('The new string is : ' + str + "Changed by " + msg.From);  // prints in the output 
      
    },
  
    __GetNumberInfo: function () { // It's a SNC    
      
        println('The new number is : ' + this._MyNumber + " Changed by " + this._NumChanger);  // prints in the output 
      
    },
  
    __GetStringInfo: function () { // It's a SNC  
      
        println('The new string is : ' + this._MyString + " Changed by " + this._StringChanger);  // prints in the output 
      
    }

};

With these updates, the contract can handle string and number properties, track changes, display the “Hello World” message, and provide methods for setting and retrieving values. This added functionality prepares you for further experimentation in the IDE. The msg variable, prepopulated by the node, contains key details about the current call.

var CRC_Message = {        
                 ID : '',  // Transaction ID (sha256 hash)
               From : '',  // Sender wallet address
                 To : '',  // Smart contract address
          Timestamp : '',  // Transaction timestamp 
               Type : '',  // Type of Transaction
         Blockchain : '',  // Blockchain on which the transaction is being sent
          Signature : '',  // Transaction Signature (already validated)
            Payload : '',  // Transaction Payload (one of the methods of the contract class)
             Amount : 0.0, // amount of CIRX sent to the contract
           GasLimit : 0.0  // maximum amount of gas to execute the transaction
};  

The fields within the “msg” structure are defined in the “Definitions.hc” library. This integration makes crucial call information easily accessible, enabling smooth interaction with the blockchain from within the smart contract.

Last updated