An Introduction to Solidity Programming for Blockchain Applications

0
4002

This article discusses the Solidity programming language, which is used to interact with the Ethereum blockchain. Readers will learn how to write a smart contract using this language, how to choose an IDE to be used for writing Solidity code, and how the Solidity program is executed.

In Ethereum, smart contracts are written using the Solidity programming language that is inspired by JavaScript. Ethereum implements an execution environment on the blockchain called the Ethereum Virtual Machine (EVM). Every node participating in the network runs the EVM as part of the block verification protocol. It goes through the transactions listed in the block that are verifying the protocol, and runs the code as triggered by the transaction within the EVM. Each and every full node in the network does the same calculations and stores the same values.

Popular IDEs for writing Solidity programs are listed below.

Online IDEs
i) Remix: Remix is a Web based IDE with built in static analysis and test blockchain VM. This IDE can be widely used for small contracts and for quick learning.

ii) Ethereum Studio: This features a built-in browser blockchain VM, metamask integration (one-click deployments to testnet/mainnet), transaction logger and live coding. Users can start working on Ethereum studio at https://studio.ethereum.org/.

Offline IDEs
i) Atom: This has an editor with Atom Solidity Linter, Etheratom, autocomplete-solidity and language-solidity packages.

ii) Visual Studio Code: The Visual Studio Code extension adds support for Solidity.
iii) IntelliJ Solidity plugin: This is an open source plugin for JetBrains IntelliJ Idea IDE (free/ commercial) with syntax highlighting, formatting and code completion.

iv) YAKINDU Solidity tools: This is an Eclipse based IDE, featuring context-sensitive code completion and help, code navigation, syntax colouring, built-in compiler, quick fixes and templates.

v) Eth Fiddle: This allows you to write, compile and debug the smart contract. It is easy to share and find code snippets with Eth Fiddle.

Writing Solidity programs for blockchain applications
To start writing a program in Solidity, we first need to define the pragma directive:

pragma solidity ^0.4.25;

The above statement means the contract compiles with a compiler version beginning with 0.4.25. Next, to define a contract, the contract keyword can be used:

contract person {
}

The complete Solidity program for setting name and age variables is given below:

$ //declare the version of the solidity programming language
pragma solidity ^0.4.25;
//declare the smart contract like this:
contract person
{
string private name;
uint private age;
function setName(string newName) public
{
name=newName;
}
function getName()public constant returns(string)
{
return name; }
function setAge(uint newAge)public
{
age=newAge; }
function getAge()public constant returns(uint)
{
return age; }
}

Steps to compile the Solidity program
To compile a smart contract, the easiest online available IDE solidity compiler is Remix IDE, which is available at https://remix.ethereum.org. When you first load the Remix IDE, you’ll see a default page, as shown in Figure 1. On the left side of the window, you’ll see a tab named Compile. Click on it.

Next, deploy the smart contract on a local blockchain. In the toolbox panel, go to the ‘Run’ tab. This is used to deploy and interact with smart contracts (click it to enlarge the image). For the environment, make sure to choose ‘JavaScript VM’. This means that Remix will deploy your smart contract to a local Ethereum blockchain. This blockchain is not connected to the real Ethereum blockchain and does not use real Ether. In this way the smart contract is successfully compiled using the Remix IDE.

LEAVE A REPLY

Please enter your comment!
Please enter your name here