DEV Community

Cover image for Solidity in depth I
Sergio Salanitri
Sergio Salanitri

Posted on • Updated on

Solidity in depth I

Solidity is the main smart contract language. This is used to develop smart contract that deploy in Ethereun network and others forked Ethereun blockchains.

But, what is a smart contract?

The smart contract is a code that run into de Ethereun Virtual Machine (EVM), deployed into the Ethereun Blockchaing and run in it. With the smart contract the developer can run transaction logic that perfome transferences, storage data, user validation and a lot of others thinks.

In this article I explain about simple contract development in Solidity Language.
In brief Solidity sintax is based in C++ and javascript but have to several unique sintaxis.
Not confuse the similar sintaxis to other language with how to run the solidity binaries code, is very different.

Well, enought talk and we write the code.

//SPDX-Licence-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

/**
 * @title Storage
 * @dev Store & retrieve value in a variable
 */
contract Storage {

    uint256 number;

    /**
     * @dev Store value in variable
     * @param num value to store
     */
    function store(uint256 num) public {
        number = num;
    }

    /**
     * @dev Return value 
     * @return value of 'number'
     */
    function retrieve() public view returns (uint256){
        return number;
    }
}
Enter fullscreen mode Exit fullscreen mode

The firsts lines are the Licence and the solidity compiler version. The last line is very important to avoid some issue with different language features between compiler versions.

The keyword Contract indicate the contract inself.
Keep in mind that Solidity is a Object Oriented Language and the contract have to similar features that classes.

The next line uint256 number; indicate the variable declaration , Solidity es a strong typed language , in this case the variable number is a 256 bits int.

Declare functions in solidity we use the following sintax

function functioName(type2 arg1, type3 arg2...) <visibility scope> <other attributes> returns(type1, type2, type3...){
   //function body
}

Enter fullscreen mode Exit fullscreen mode

In this contract there are two fuctions, the first save some integer in number variable and the second read this variable and return it.

Despite being a very simple code it apply several Solidity importants features.

The storage function save some integer in number variable, this integer save into the blockchain and needed run a blockchain transaction, this transaction spend gas (the cost of the transaction). Beside the function retrieve don't consume gas because only read the variable from blochchain (the atribute view allow this feature).

Do you view the attribute returns? How the name indicate is the return type, but unlike other language solidity allow retuns more that one variable in the same function!! :)

How we run this code?

The easiest way is using the Remix Web IDE (http://remix.ethereum.org/), in fact this example I get from this ide.

How we run this code in Remix Ide?

In the next article :), but while I propose that you try use the Remix IDE and research it.

See you!!

Top comments (0)