How to Write a Smart Contract with Solidity?

How to write a smart contract with solidity? - Tutorial

Smart contracts, which gained popularity from the Ethereum virtual machine, are programs that can perform various useful functions on the blockchain. They were invented to create the necessary level of security in the blockchain.

A smart contract acts as an intermediary regarding interactions between two parties, making all the transactions as secure as possible. For this reason, writing smart contracts is an important task to ensure the security of any blockchain. Next, we will look at the process of developing smart contracts on Solidity, as well as Solidity Security Audits.

Writing a Smart Contract on Solidity

The source code for smart contracts is often easily readable, so you need to specify the license of the code on the first line:

// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.4.17 <0.9.0;
contract Storage {
  uint data;
  function set(uint newData) public {
    data = newData;
  }
  function get() public view returns (uint) {
    return data;
  }
  }

Here, the pragma directive sets which version of Solidity to use. All performances start with 0.*. The smart contract we are writing can be compiled for version pragma 0.4 or higher (up to and including 0.8).

Contracts in Solidity have many similarities to contracts written in JavaScript. After all, they also contain variables and methods that interact with each other, but in Solidity, this keyword is not required to access the variable.

If we talk about the Storage smart contract, then it contains integer data and two functions that can show and change them. Moreover, the data variable will exist for the duration of the smart contract. So if we deployed contracts, anyone could get the value of the data present in them.

Initializing Variables in a Contract

To initialize a data variable with a value, the smart contract provides a constructor function:

contract Storage {
  uint data;

  constructor (uint defaultData) {
    data = defaultData;
  }

  function set(uint newData) public {
    data = newData;
  }

  function get() public view returns (uint) {
    return data;
  }
}

Through the “is” keyword, a contract can be inherited:

// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.4.17 <0.9.0;

contract C {
    string public greeting = 'hello';
}

contract D {
    string public farewell = 'goodbye';
}

contract E is C, D {}

In this case, our contract D will have access to both greeting and farewell.

What Features does Solidity have?

Functions have this pattern:

function <function name>(<parameters type>) \[function type\] [returns (<return type>)] {}

The most commonly used functions in a simple smart contract are public, internal, external, private, view, and payable. Each of these function types manages the visibility of functions in different ways:

  • Public – anyone can call the function.
  • External – anyone other than the contract can call the function. Using this type can save a lot of gas.
  • Private – in this case, only the contract containing the function can call the function.
  • View – the function can only read the state.
  • Payable – the function can accept payment when it is called.

The template for a function type looks like this:

{public|external|private|internal} [pure|view|payable]

In a smart contract, a get function of type public view returns (uint) reads data and returns an integer. Solidity also provides a shortcut to generate getter features automatically. Let’s add the public keyword before our data variable, and this will replace the get function:

contract Storage {
  uint public data; // a data function will be created to access the `data` variable

  constructor (uint defaultData) {
    data = defaultData;
  }

  function set(uint newData) public {
    data = newData;
  }
}

Smart Contract Deployment and Testing

To test and deploy the smart contract we wrote, we will use the Solidity IDE, which is used to compile, deploy, and manually push the Solidity code. In this case, we will use Remix as such a program. This can interact with both different Ethereum testnets and the main network.

To begin with, let’s create a new contract in the appropriate directory and transfer the code of our contract.

After saving the written smart contract in a new program, Remix will automatically compile the code. Then, it creates a bytecode that will be sent to the network to interact with the deployed contract. We can check the byte code and ABI in the solidity compiler tab.

If the code compiles successfully and there are no problems during this process, then you can proceed to deployment and interaction with a smart contract. You can select the environment and account for deploying the smart contract in the deployment and start transactions tab. The JavaScript VM environment will allow us to test the contract in the browser as quickly as possible.

You can also see Deployed Contracts at the bottom so that you can deploy multiple smart contracts simultaneously. In the Contracts section, the program uses our default data inside the constructor function. To expand a contract, you need to enter an integer and click on the corresponding button.

The data button is an auto-generated getter for a public data variable. The installation button represents the installation method. Finally, the set function is a transaction-type function that consumes gas and resources to start.

What is Solidity?

Solidity is an object-oriented programming language that has a similar syntax to JavaScript. Solidity is strongly typed and uses inheritance, which is not true with JavaScript. This programming language compiles source code into deployable byte code to interact with code using other smart contracts (or programming languages).

FAQ

Why are Smart Contracts in Solidity?

Solidity code is encapsulated into contracts, meaning the contracts in Solidity are a set of codes and data located on the Ethereum blockchain. Contracts are the foundational blocks for building applications on Ethereum.

Why do we need Solidity Smart Contracts?

A smart contract is a program that is stored on the blockchain and runs when certain conditions are met. Most often, these programs are used to automate the execution of an agreement so that all participants can be sure of the result and carry out operations without intermediaries.

What are the Benefits of Smart Contracts?

1) The speed of operations. When a certain predetermined condition is met, the contract begins to be executed immediately. Since smart contracts are automated, their speed is very high.
2) Safety. The records of all blockchain transactions are encrypted, making it almost impossible to hack them.
3) Saving. Smart contracts eliminate the need for intermediaries to carry out certain transactions, making them safer and more economical.
4) Transparency. Thanks to smart contracts, a third party is not involved in the process, which makes it possible to protect all parties to the transaction from changing information.

Latest Articles