How to Write Smart Contracts by Solidity?

How to Write Smart Contracts by Solidity?

How to Write Smart Contracts by Solidity?

Description

We use Solidity to write smart contracts. Solidity delivers rich object orientation. Basically, Smart contracts are code segments and programs, which are organized and executed in EVM. Generally, a contract is a term used in the legal world. It has little significance in the programming world. It doesn’t mean to write a smart contract in Solidity is to write a legal contract. The contracts are similar like any other programming code. It contains Solidity code and is performed when someone raises them. There is integrally nothing smart about it. A smart contract is a block chain term. This is a piece of nonsense used to mention to programming logic and code that executes within EVM. It is actual similar to a class of C++ and Java.

Contracts hold state variables and functions. That objective as a class is composed of state (variables) and behaviors (methods). The drive of state variables is to uphold the current state of the contract. The functions are answerable for performing logic. Those are also responsible for execution update and read operations on the current state.

How to write a simple contract?

By using the contract keyword along with an identifier we declare a contract as shown in the following code snippet:

contract SampleContract {

}

The declaration of state variables and function definitions come within the brackets.

How to create contracts?

Following are two ways of creating and using a contract in Solidity:

  • By the use of new keyword
  • By using the address of the already deployed contract

By the use of new keyword

In Solidity, the new keyword deploys and creates a new contract example. It initializes the contract instance;

  • By deploying the contract
  • Initializing the state variables
  • Running its constructor
  • Setting the nonce value to one, and
  • Returns the address of the instance to the caller.

Deploying a contract includes;

By using address of a contract

Once a contract is already deployed and instantiated, this method of creating a contract instance is used. This way of creating a contract uses the address of an existing, deployed contract. There is no new instance is created. Rather, an existing instance is reused. A orientation to the existing contract is made using its address.

Constructors

Solidity cares declaring a constructor within a contract. Constructors are optional in Solidity. The compiler encourages a default constructor when no constructor is explicitly defined. The constructor is achieved as soon as deploying the contract. This is moderately different from other programming languages. A Constructor is performed whenever a new object instance is created in other programming languages. Though, a constructor is executed are deployed on EVM in Solidity. Generally, constructors should be used for initializing state variables and writing extensive Solidity code should be avoided. The first set of code that is executed for a contract is the constructor code. There may be at most one constructor in a contract, unlike constructors in other programming languages. Constructors may take parameters and arguments should be supplied while deploying the contract. A constructor has the same name as that of the contract. Both the names should be the same. A constructor can be either public or internal, from a visibility point of view. It cannot be external or private. A constructor does not return any data explicitly. In the following example, a constructor with the same name as that of the HelloWorld contract is defined.

Leave a Comment