Basics of Solidity (Part 1)

Basics of Solidity (Part 1)

Write your first smart contract

Solidity is a contract-oriented, high-level programming language for implementing smart contracts. Solidity is highly influenced by C++, Python, and JavaScript and has been designed to target the Ethereum Virtual Machine (EVM).

Prerequisites

  • Basic understanding of Ethereum

  • Understanding of blockchain technology

Introduction

To understand Solidity, there are some basic concepts to know. Let's start by explaining the concepts:

1. Ethereum

Ethereum is a technology that gives open access to digital money and data-friendly services for everyone.

2. Ether (ETH)

ETH is digital money that you can use on the internet which is similar to Bitcoin. It's the currency of Ethereum apps. It is important to note that ETH isn't the only cryptocurrency on Ethereum.

3. The Ethereum Virtual Machine (EVM)

The Ethereum Virtual Machine, also known as EVM, is the runtime environment for smart contracts in Ethereum. The Ethereum Virtual Machine focuses on providing security and executing untrusted code by computers all over the world.

4. A Smart Contract

A smart contract is a program that runs at an address on Ethereum. They're made up of data and functions that can execute upon receiving a transaction.

5. DAPP

DAPP otherwise known as Decentralized application (dapp), is an application built on an open-source decentralized network that combines a smart contract and a frontend user interface. A dapp has its backend code running on a decentralized peer-to-peer network. In contrast with an app where the backend code is running on centralized servers, dApps runs a decentralized network.

Developing a Dapp, like any other app, requires programming and executing code on the system. Solidity programming stands apart from the other programming languages and is the programming language of choice in Ethereum.

What is Solidity Programming Language?

Solidity is an object-oriented programming language created on the Ethereum Network for constructing and designing smart contracts on Blockchain platforms.

Just like other programming languages, Solidity programming also has variables, functions, classes, arithmetic operations, string manipulation, and many other programming concepts.

Let's get to the programming aspects:

To write your first smart contract in solidity, you need an IDE or code editor. We have a lot of online and offline IDE such as vs code, Atom, sublime for the offline while remix for the online.

For this tutorial, we would be using Remix. Before we get to that, let's look at the structure or layout of a Solidity source file.

The layout of a Solidity source file

A single solidity source file can contain any number of contracts or library definitions. It is recommended that a developer should maintain different source files for each contract or library definition for code readability and maintainability. The Solidity contract or library file extension is .sol. For instance, MyContract.sol, FirstContract.sol, Transaction.sol, etc.

// SPDX-License-Identifier: Unlicense

pragma solidity ^0.8.0;

import "hardhat/console.sol"; // Import statement

contract MyFirstContract {

    constructor() {
        console.log("Yo! My First Smart Contract");
    }

}

The basic layout of the Solidity source file contains the following order:

  • SPDX License Identifier

You can establish trust in your smart contract when you make the source code available. Since making source code available can cause legal problems concerning copyright, the Solidity compiler encourages the use of machine-readable SPDX license identifiers. Every source file should start with a comment indicating its license:

// SPDX-License-Identifier: MIT

If you do not want to specify a license or if the source code is not open-source, please use the special value UNLICENSED. Note that UNLICENSED (which implies no usage allowed, not present in the SPDX license list) is different from UNLICENSED (which means grants all rights to everyone). The above smart contract starts with its license as:

// SPDX-License-Identifier: Unlicense
  • The Solidity version:

A Solidity file should have a version definition to reject compilation with future compiler versions that might introduce incompatible changes. The keyword 'pragma' is used to define the Solidity version, which enables certain compiler features or checks. A pragma directive is always local to a source file, so you have to add the pragma to each file if you want to enable it for your whole project. Using the version pragma does not change the version of the compiler nor does it enable or disable compiler features. Its purpose is to instruct the compiler to check whether its version matches the one required by the pragma. If they do not match, the compiler issues an error. The line below shows an example of how to use the version pragma.

pragma solidity ^0.8.0;

In the above line, the Solidity file will only compile with a compiler version equal to or greater than 0.8.0 but less than 0.9.0. This ensures that there will be no breaking changes until version 0.9.0, allowing you to be confident that your code will compile as intended. If you try to compile the file with a compiler version outside of this range, you will receive an error.

  • Import Statement:

In Solidity, import statements are used to import code from other contracts or libraries. This allows developers to reuse code that has already been written, making their contracts more modular and efficient.

To define an import statement, we use the 'import' keyword followed by the file path of the contract or library we want to import. For example:

import "myContract.sol";

In this example, we are importing a file called 'MyContract.sol' from the same directory as our current contract. We can then use the code from 'MyContract.sol' within our contract.

It's important to note that when importing contracts or libraries, the file path must be correct and the imported file must have a valid Solidity syntax. If you are familiar with the javascript import statement, it is important to note that Solidity supports import statements to help modularise your code that is similar to those available in JavaScript (from ES6 on). However, Solidity does not support the concept of a default export.

  • Contract definition

The last part of a solidity source file is the contract definition which is stated as follows in our example:

contract MyFirstContract {
    constructor() {
        console.log("Yo! My First Smart Contract");
    }
}

In this instance, our contract name is MyFirstContract which would be the name used to call the contract methods when deploying and implementing the contract.

Since you know what a simple solidity source file would look like, let's get to writing your first smart contract with solidity.

Introduction to Remix IDE

We would use Remix IDE to create and deploy our first solidity contract in steps. If you are wondering what Remix is, Remix IDE is an open-source Ethereum IDE used to write, compile and debug Solidity code. It can be used for the entire journey of smart contract development by anyone at every knowledge level and requires no setup, which enhances fast development.

Steps to Write and Compile Solidity Smart Contract using Remix IDE

To begin with, visit the Remix website to either use the web version or download the desktop application. In this tutorial, we will be using the web version.

Step 1: Open Remix IDE on any of your browsers, select Solidity to choose the environment, and click on the New File, then name your solidity file.

remix-ide-step-1.png

Step 2: Write the Smart contract in the code section.

In this part, we would be writing a simple get and set solidity function.

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

contract MyContract {
    string private myString;

    function setMyString(string memory newString) public {
        myString = newString;
    }

    function getMyString() public view returns (string memory) {
        return myString;
    }
}

In this contract, we first define a private string variable called myString. This variable is only accessible within the contract itself, and cannot be modified or viewed from outside the contract.

We then define two functions to interact with this variable. The first function setMyString takes a string parameter called newStringand sets the value of myString to this new value. This function is marked as public, which means it can be called from outside the contract.

The second function getMyString is marked as public and view, which means it can be called from outside the contract and doesn't modify the state of the contract. This function simply returns the value of myString.

Step 3: Click the Compile button to compile the contract in the left panel. This will open the Solidity compiler tab.

In the compiler tab, select the version of Solidity you want to use for your contract from the dropdown list.

Click the "Compile myContract.sol" button to compile your Solidity code. If there are no errors, you will see a green bar at the bottom of the panel indicating that your code has been successfully compiled.

Step 4: To execute the code, click the "Deploy & Run Transactions" button in the left panel. This will open the "Deploy & Run Transactions" tab.

In the "Deploy & Run Transactions" tab, you can select the network you want to deploy your contract to from the dropdown list. You can also configure other options, such as the gas limit and the value to send with the transaction. In this tutorial, we would leave the options as they are.

Click the Deploy button to deploy your contract to the selected network. If the deployment is successful, you will see a green notification in the bottom panel.

Step 5: After deploying the code click on the method calls under the drop-down of deployed contracts to run the program, and for output.

To run the deployed contract, enter some text and click on the setMyString button. Then click on the getMyString button for the output.

Summary

In this part, we covered the basics of the Solidity programming language, including the layout of a Solidity function which consists of

  • SPDX License Identifier

  • The Solidity version with pragma

  • The import statement

  • The contract definition

We then wrote and deployed our first smart contract using Remix. Next, we will delve into the concept of data types in Solidity programming.