Chewing the Solidity

Krishna Kant
Coinmonks
3 min readJul 9, 2022

--

Continuing with our blockchain series, this is the 1st part of Basics of Solidity.
contract pay{
function payEther() public payable{
}}

The above snippet is the code for sending some crypto payment to a particular address but it seems to be an alien language. No? To understand this well, let us dive deep to understand everything about Solidity.
Solidity is a high-level statically-typed case-sensitive programming language.

Remix IDE

Like every other programming language, we need an IDE to write and compile Solidity. Usually, we use Remix.
The remix is an open-source IDE used for developing, deploying and administration. It supports Solidity and Vyper. The remix is coded and built upon javascript.

Smart contract compilation

Every smart contract has a source file with .sol as an extension. It gets compiled on the Solidity compiler. There are two components associated with it- ABI and Byte code.
ABI (Application Binary Interface) in the context of computer science is an interface between two program modules, often between operating systems and user programs.
And Byte code is the actual code which is parsed, compiled and contained. It contains basic information about the program, contract and files associated with the blocks module.
Specific information about ABI and Byte code-

  • Contract bytecode is public in readable form,
  • The contract does not have to be public,
  • Byte code is immutable,
  • ABI acts as a bridge between application and smart contract,
  • ABI and Byte code cannot be generated without source code.

Now, let us start the actual coding thing about Solidity. Time to get our hands dirty…

Before starting the programming code, we have to initialise the license details which will be authenticated at the time of compilation.
SPDX-License-Identifier: GPL-3.0
Now, the 2nd line has to be about the solidity version.
pragma solidity ≥0.7.0 <0.9.0;
above syntax means that we are using the Solidity version of anything that is greater than or equal to 0.7.0 and lower than 0.9.0.
Once we indicate these two pieces of information at the top of our solidity program file, we can begin to start our goal-oriented programming syntaxes.

Constructor

Constructor is the reserved keyword used in Solidity to create a new instance of more like ‘object’. Constructor is the foremost important part of any given Solidity programming language. Some important traits of a Constructor are-

  • It is executed only once,
  • In one solidity program, we can create only one constructor and that is optional,
  • A default constructor is created by the compiler if there is no explicitly defined constructor.

New to trading? Try crypto trading bots or copy trading

Data Types in Solidity

Like every other programming language, Solidity has also some default data types- Boolean, unit, int, address, bytes, and string.

Integer

We denote integers by two keywords- int, unit. int is used for signed integers and unit is used for unsigned integers.
int has the range from int8 to int256 where every step increase corresponds to 8 in size. The same is for uint as well i.e it ranges from uint8 to uint256.
By default, int and unit are 256. And default value for both of them is 0.

Array

The array is used to store data, values etc and there are two kinds of the array in Solidity, static and dynamic.
A dynamic array is one whose size is not fixed at the time of initialisation and vice versa for the static array.
uint[] public arr; — This denotes a dynamic array as we are declaring an array but not binding its size.
While uint[4] public arr=[1,2,3,4]; — It is the static array because we have attached the information about the size of the array at the time of declaring it.

Boolean

Boolean is used to store only true or false.

Structure(struct)

The structure is a complex data type which is made with the help of fundamental data types.
The structure can be declared inside or outside the construct. Declaring it outside means that it can be accessed by multiple contracts.
Example-
struct school{
uint roll;
string name;
}

Enum

When we assign any particular name to some constant/number, it is called Enum.
Example-
construct State{
enum student{enrolled, not_enrolled, waitlisted}
student public student_1=student.not_enrolled;
}
printing value of student_1 will give 1 as the result because the enum starts from 0.

These were the basics associated with the Solidity programming language. In order to be able to code and write our first Smart contract in solidity, I will share one more article which will be basically part 2 of Basics of Solidity titled as Digesting the Solidity. Thanks for your time and till the next blog, let us keep chaining the blocks!

--

--