Solidity - A beginners guide

Getting started

Overview

Solidity is a popular programming language used for developing smart contracts on the Ethereum blockchain. Smart contracts are self-executing contracts with the terms of the agreement directly written into lines of code. They are stored on the blockchain and are immutable, tamper-proof, and self-verifiable. Solidity is an object-oriented, high-level programming language that is similar to JavaScript, making it easy to learn and use for developers.

In this blog post, we will discuss the installation process of Solidity and its components.

Installation of Solidity

Before we dive into the installation process, let's discuss the requirements for installing Solidity.

Requirements:

  • Operating System: Linux, MacOS, or Windows

  • Node.js version 12.x or later

  • npm (Node Package Manager)

Step 1: Install Node.js and npm

First, we need to install Node.js and npm on our machine. Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a web browser. npm is a package manager for the Node.js platform and is used to install Solidity.

To install Node.js and npm, follow the steps based on your operating system.

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
brew update
brew install node

Windows: Download the installer from the Node.js website and follow the installation steps.

Step 2: Install Solidity

After installing Node.js and npm, we can now install Solidity using the following command:

npm install -g solc

This command should output the version of Solidity installed on your machine.

Step 3: Verify the installation

To verify if Solidity is installed correctly, run the following command:

solc --version

Step 4: Install an Integrated Development Environment (IDE)

Although it's not required, it's recommended to use an Integrated Development Environment (IDE) to develop smart contracts. An IDE provides a comprehensive development environment with code highlighting, debugging tools, and smart contract compilation. There are several popular IDEs for Solidity, including Remix, Visual Studio Code, and Atom.

Writing your first Solidity Code

Step 1: Set up the development environment

First, we need to set up the development environment. We will be using Remix as our IDE. Remix is a web-based IDE that provides a comprehensive development environment with code highlighting, debugging tools, and smart contract compilation.

To use Remix, visit the Remix website and create a new file. Name the file myFirstContract.sol.

Step 2: Write the smart contract

Now that we have our development environment set up, we can start writing our first Solidity program. In this example, we will create a simple smart contract that stores a message.

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

contract MyFirstContract {
    string private message;

    function setMessage(string memory newMessage) public {
        message = newMessage;
    }

    function getMessage() public view returns (string memory) {
        return message;
    }
}

Let's break down the code:

  • // SPDX-License-Identifier: MIT specifies the license under which the contract is released.

  • pragma solidity ^0.8.0; specifies the version of Solidity we're using.

  • contract MyFirstContract declares the contract's name.

  • string private message; declares a private string variable named message.

  • function setMessage(string memory newMessage) public is a public function that sets the value of message.

  • message = newMessage; assigns the value of newMessage to message.

  • function getMessage() public view returns (string memory) is a public function that returns the value of message.

  • return message; returns the value of message.

Step 3: Compile the smart contract

Once we have written our Solidity program, we need to compile it using the Solidity compiler (solc). Remix automatically compiles our code when we save the file. To compile the contract manually, we can use the following command:

solc --bin myFirstContract.sol

This command compiles our contract and outputs the bytecode in binary format.

Step 4: Deploy the smart contract

To deploy the smart contract on the Ethereum blockchain, we need to create a transaction that contains the compiled bytecode. We can use Remix to deploy the contract to a local blockchain or a public network such as the Ethereum Mainnet.

To deploy the contract on a local blockchain in Remix:

  • Click the "Deploy & Run Transactions" tab.

  • Select "Injected Web3" as the environment.

  • Click the "Deploy" button.

Once the transaction is mined, the smart contract is deployed to the blockchain.

Step 5: Interact with the smart contract

Now that our smart contract is deployed, we can interact with it by calling its functions. In Remix, we can interact with our contract by using the "Deployed Contracts" tab.

To interact with our smart contract in Remix:

  • Click the "Deployed Contracts" tab.

  • Click the "At Address" button.

  • Enter the address of the deployed contract.

  • Click the "Access" button.

We can now call the setMessage function to set the message and the getMessage function to retrieve the message.

Conclusion

In this blog post, we walked through the process of understanding what Solidity is about and we also wrote our first solidity program.

If you have other questions, feel free to comment.