Published 15 October 2018

The "Hello World" smart contract

The "Hello World" smart contract

A smart contract is a self-executing software algorithm that is written into a blockchain (a decentralized chain of blocks), designed to create and maintain smart contracts.

Let's see the simplest example of a smart contract implemented on the Credits platform. The result of this smart contract will be the text “Hello World!”

public class Contract extends SmartContract{

                private String my_Msg="Hello";

                    public Contract(){

}

                public void My_smg(String my_Name){

                System.out.println(my_Msg+my_Name+”!”);

}

}

Structure of the smart contract “Hello World!”

Structure of the smart contract

Class 

Class

The language for writing Smart Contracts on the Credits platform is Java. A smart contract is a Contract class inherited from the parent class of Smart Contract.

A class is a template for describing an object. The class begins with the keyword class followed by the class name, ‘Contract’ in our example. The curly brackets {} contain the body of the class, as well as the fields and methods of the class.

Variable

Variable

A variable is a memory area that stores a value for use in the program. The variable has a name (identifier) and a data type. Also, you can use the private, public and protected access modifiers.

  • private – a variable is available inside the class in which it is declared, and is not available to other classes;
  • public – variable is available in any part of the program;
  • protected – a variable is available for classes in one package and in subclasses.

In our case, the variable my_Msg="Hello" is protected from the outside and is accessible within the Contract class, the type of the variable is string, the variable is a constant, it is set to "Hello".

Function

Function

A function is a subroutine that has a name (identifier), is used in expressions and returns a result of execution.

My_smg is the main function. It does not return the result, it is available in any part of the smart contract, it has the input variable my_Name. The purpose of this function is to output the text "Hello World!". The variable my_Name is set by the user when executing the smart contract. For example, if you set it to "World", the result is "Hello World!", And if the value is "Credits", then the result is "Hello Credits!".

To output a message, the standard function System.out.println() is used inside the body of the function My_smg, which displays the message on the screen and then the cursor is moved to a new line. The argument of the function is the variable my_Msg= "Hello"+my_Name+"!".

Creating a smart contract "Hello World!"

Creating a smart contract

To create and deploy a "Hello World" smart contract, you need to open the Desktop Wallet application, click on the "SMART CONTRACT" button, see Figure 1.

The "SMART CONTRACT" button opens the Smart Contract window in the Desktop Wallet application to create or search for smart contracts already created on the platform.

Figure 1. Creating a smart contract in Desktop Wallet.

In the Smart Contract window of the Desktop Wallet application, click the "CREATE" button.

Button "CREATE" - creates a new smart contract.

As a result, a smart contract template will be created. Enter the above smart contract code "Hello World!", See Figure 2.

The "CHECK" button - checks the smart contract code for errors.

Button "DEPLOY" - starts a smart contract for execution.

After you have typed the smart contract code "Hello World!", press the "CHECK" button to check for errors. If there are no errors, click the "DEPLOY" button and get the address of the smart contract "Hello World!".

Figure 2. Creating and deploying a smart contract "Hello World!" On the Credits platform. 

0 Useful article

Contents