Solidity tutorials and Introduction to Solidity

What is Solidity?

Solidity is an Object-Oriented High-level programming language for writing smart contracts(programs) on the Ethereum blockchain. Solidity was proposed by Gavin Wood in August 2014. Gavin Wood is also the creator of Polkadot.

All EVM(Ethereum Virtual Machine) compatible blockchains like Polygon, Avalanche, Fantom Opera, Binance Smart Chain, Optimism, Arbitrum support Solidity programming. There is a huge community of Ethereum developers. So the new blockchains just make themselves EVM compatible.

Features of Solidity

  • Curly bracket language
  • Statically Typed Language - Variable types are explicitly declared and determined at compile time.
  • Supports Inheritance
  • Supports Libraries and Other complex user-defined data types

Solidity Version

  • Solidity follows Semantic versioning like 0.x.y where x is major version change with new features and y is patch level changes with no major changes and compatible with all releases related to x.
  • The latest version of the Solidity compiler at the time of writing this blog is v0.8.12. This means version 0.8.4 is compatible with 0.8.12
  • Pragma directive is used to mention solidity version of the source code( smart contract) and local to that specific smart contract. It is the first line in the smart contract.
  • For example, pragma solidity 0.8.11; It means it will only support the 0.8.11 version and nothing else.
  • pragma solidity ^0.8.11; Here, adding a ^(Caret Symbol) means, it supports any version from 0.8.11 and above.
  • pragma solidity >=0.8.1 <0.8.12; Here, the source gets supported by only versions from 0.8.1 to 0.8.11

Solidity Variables

Solidity supports three types of variables

  • Global Variables - Special variables that return information about blockchain.
  • State Variables - State Variables are stored permanently in a contract storage
  • Local Variables - These variables only exist till the function is executing.

Let us understand them with a sample contract. This sample smart contract returns a sum of the current block number(global variable), local variable, and state variable.

Application Binary Interface(ABI)

Smart contracts when compiled by EVM(Ethereum Virtual Machine) give byte code(Non-human readable code). Byte code is just a bunch of zeroes and ones. When a smart contract is deployed on the blockchain a byte code is generated and an address is associated with it.

When we need to interact with the deployed smart contract, we need to send our data to the functions in byte format and as well the response we will get will be in byte format. It needs to be converted to an appropriate type of interaction. This will be taken care of by ABI which is just an interface for communicating between contracts or from outside of smart contracts.

Byte code of the above smart contract looks like this.

The contract ABI is a JSON Array of Objects where each object represents information in a function. In the above smart contract, there are two functions - constructor and getResult function.

Let us understand the keys inside a specific function in detail.

  • type - It will be the constructor to indicate the presence of a constructor or function for other sets and get functions.
  • inputs - It means an array of JSON objects passed as parameters to a function. It has a name key and type of input parameter key.
  • name - Inside function object, gives the name of the function. Inside the input array object, it means the name of the function parameter.
  • outputs - Similar to inputs but the return type information of a function.
  • stateMutability - It defines the mutability(Write and read) of the function. It can be of four types. They are
    • pure - cannot read or write to blockchain
    • view - cannot write to blockchain but can read from it
    • nonpayable - cannot accept ether(money) but can read and write
    • payable - can accept Ether(ETH) and also can read and write to the blockchain

How to get Byte Code and ABI of a smart contract?

We can get the byte code and ABI of a smart contract using remix IDE. Once we save the contract, head to the deploy tab and click on compile. Once compiled, you should be able to see the ABI and bytecode options at the left bottom like in below.


Payable Modifier

Payable modifier is a mechanism to receive Ether(ETH) into a smart contract. It is added to function to allow depositing ether into the smart contract. It can be used with the constructor as well to deposit ether into the contract while deploying.

Let us explore a contract to deposit ETH into the smart contract and withdraw from it.


After deploying the contract, select a different account and as shown above, set the value to 10 and denomination to ether and click on the depositFunds at the bottom.

The funds are deposited into the smart contract.

Select the account which deployed the contract and click on withdraw button at the left bottom of the deployed smart contract. The funds will be added to the owner account of the smart contract.


Resources

Post a Comment

Previous Post Next Post