Skip to main content

Command Palette

Search for a command to run...

Solidity - A beginners guide

Getting started

Published
4 min read
A

Favour Adeogo is a seasoned technical writer with a passion for elucidating the intricacies of backend development in scalable applications. With a semi-robust background in software engineering, Favour brings a unique blend of technical expertise and communication skills to his writing endeavors.

Specializing in the .NET ecosystem, Favour has honed his craft in documenting the backend architecture of scalable applications built on the .NET framework. His in-depth understanding of technologies such as C#, ASP.NET, and SQL Server enables her to articulate complex concepts in a clear and concise manner, catering to both novice developers and seasoned professionals alike.

Throughout his career, Favour has collaborated closely with development teams to capture the nuances of backend implementation, ensuring that his documentation remains relevant and up-to-date in rapidly evolving technological landscapes. His meticulous attention to detail and commitment to accuracy have earned him recognition for producing high-quality technical content that serves as a valuable resource for developers worldwide.

In addition to his technical prowess, Favour possesses excellent research skills, allowing him to stay abreast of the latest advancements in backend development methodologies and best practices. He is adept at distilling vast amounts of technical information into comprehensive guides, tutorials, and API documentation that empower developers to leverage the full potential of the .NET stack in building scalable and robust applications.

Favour's dedication to his craft, coupled with his passion for empowering developers through clear and concise documentation, makes him a trusted resource in the tech community. Whether he's crafting tutorials, writing API guides, or documenting architectural patterns, Favour remains committed to fostering a deeper understanding of backend development with dotNET, thereby empowering developers to build innovative and scalable solutions with confidence.

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.