Solidity for beginners

Probably, every new adept of a web3 branch heard about smart contracts. Reminding the essence about what it is:

Solidity for beginners
Solidity symbol

Probably, every new adept of a web3 branch heard about smart contracts. Reminding the essence about what it is:

easily said, smart contract is a computer code, which represents an agreement between the interested people. This agreement is always run and executed on a blockchain, what leads to a complete decentralization and security.

What is a solidity?

Solidity is a high-level, object-oriented programming language, influenced by python, JavaScript and c++ to implement smart contracts.
Solidity is influenced by three main programming languages: c++, python and JavaScript.

Generally, solidity targets the Ethereum Virtual Machine (EVM), which is like a decentralized computer that has millions of executable projects. EVM is crucial developing smart contracts, because it perfectly maps the blockchain ecosystem on which it is run.

Moreover, solidity allows to create DApps (decentralized applications) which are executed without intermediates, what leads to a wide-variety of use cases, for example:

  • transactions,
  • voting,
  • real estate,
  • gambling,
  • social media,
  • identity verification,
  • Internet of things,
  • and many more…

Syntax of a Solidity

The best way of learning a new programming language is a directly jumping into implementation of a very basic and simple program. In this tutorial, there will be followed an example of smart contract from the very useful and top IDE, run on a browser.

The described IDE is a remix, which can be easily accessed by visiting the website: https://remix.ethereum.org/.

remix IDE

In this compiler, in a folder contracts are presented example contracts. Let’s start learning the syntax by opening the first one, called “Storage.sol”

Storage.sol — smart contract in remix

Practically, each smart contract starts with a definition of a solidity version. It can be a little tricky, in above example it is made by an “equation” (version is greater or equal than 0.7.0 and less than 0.9.0).

Solidity is statically typed language, what means that type of each variable has to be specified. In above example, the contract has declared unsigned integer up to 256 bits called “number”. Knowledge of basic, elementary types is crucial to even start developing smart contracts. Below are listed top useful of them:

Boolean:

  • bool

Integers:

  • int/uint — signed and unsigned integers of different sizes,
  • uint8/int8 — signed and unsigned up to 8 bits,
  • uint256/int256 — signed and unsigned up to 256 bits

Address:

  • address — holds a 20 byte value (size of an ethereum address),
  • address payable — same as above, but with additional members “transfer” and “send”.

String:

  • string

Struct:

  • struct — custom data type which contains a group of elements with a different data types. Example:struct Voter {uint weight;uint voted;bool vote}

Above “Voter” struct contains unsigned integers “weight” and “voted” and also boolean variable “vote”.

Enums:

  • enum — in simple words: user-defined type in Solidity. Example:enum NewbieChoices { learn, neverGiveUp, sleep, repeat}

Functions:

  • function — functions are declared like in an example below. There we can find a function “store”, which takes as an argument unsigned integer “num” and return previously declared “number” variable equaled to an argument. Entire function “visibility” is described by type “public”, what leads to using this function outside the contract. If you do not want to allow that, you can use “private” instead.function store(uint256 num) public {number = num;}

In our contract example, there is also a function called “retrieve”, which is public, returns unsigned integer 256. What is different in this function? The syntax is a little different, because it is a “view function”. It means, that the function allows only a read, without changing any variable.

view function

Compiling and running the smart contract

To check previously written smart contract, let’s compile it and deploy to virtual machine (VM). To proceed step one, click the solidity icon on the sidebar and click the button compile, marked in red rectangle.

compiling the smart contract

Make sure, the correct one is chosen. It will be indicated in text content on the button. In this case “Compile 1_Storage.sol”.

The next step is deploying smart contract to VM so that we can check the functionality. Click an ethereum icon on the sidebar and press orange button “deploy”.

deploying smart contract to virtual machine

In the drop down of deployed smart contract, we can find an input for the value of store function argument. To remind, the variable number will take a value of that variable, after executing the function “store”.

Executing and testing functionalities

Without declared view function, it would be impossible to check if the variable “number” changed the value, but we implemented it in previous steps. After clicking the “retrieve” function button, the value of “number” will be immediately printed below.

Remix is an awesome tool, which allows to write and test simple smart contracts!

Reserved keywords

Generally, in further development, it is extremely important to get acquainted with reserved keywords, which are presented below:

reserved keywords in solidity — source: https://intellipaat.com/blog/tutorial/blockchain-tutorial/what-is-solidity/

Conclusion

Solidity is a programming language to write smart contracts. The increasing demand on decentralized apps makes solidity a top language to learn, so that you can easily find a good paid job.

If you are an enthusiast of a blockchain technology, it is something definitely worth of trying. It can be the best investment, like buying bitcoin for 10 usd.

If you like that content and want more, visit my: