Key Concepts in Solidity

Key Concepts in Solidity

Key Concepts in Solidity

In this post, you will see key concepts in Solidity & it's usage.

- Events

Events are to update current state of a smart contract to the listners. As soon as transaction is submitted and processed in blockchain, this get triggred and notify the listening applcations about the contract current state. Web3 can be used to track these event log. 

pragma solidity ^0.5.2;

//contract name is myContract
contract myContract {
    //create a variable called capital
    string private capital;
    //declare an event
    event storecapital(string newCapital);

    //set capital and emit an event
    function setCapital(string memory newCapital) public {
        capital = newCapital;

   //emit an event to the log when this function is called
        emit storecapital(capital);


- Error Handling

Two key concepts available in Solidity to perform error handling, which is "Assert" & "Require"

Assert – is used to check for conditions that should never be possible.  Asserts should always evaluate to true and should never fail.  If it does fail then there is a bug in the code and the assert will use up all gas and the transaction will be rolled back (Courtesy -

Require – Is used to evaluate inputs, preconditions and outputs of functions.  If not true solidity will throw an error and fail.  The require statement will accept an optional error message.  If there is a failure it will NOT use up all gas. (Courtesy -

- Fallback Functions

When your smart contract need to hold ether, you should have this fallback function written in smart contract. If you do not have this function, then you cannot send ether to your smart contract.

Please note, the fallback function has some standard to follow

  • It is called when a non-existent function is called on the contract.

  • It is required to be marked external.

  • It has no name.

  • It has no arguments

  • It can not return any thing.

  • It can be defined one per contract.

Function () external payable {

- Mapping

Mapping is mostly referred as key value pair dictionary. It has reference type as struts or Arrays.

mapping(_KeyName => _Value)

- Enum

Enumerator can be compared with "Options" in html. It can hold predfined set of values.. For example, if you are implementing supply chain smart contract and you want to maintain set of values for status such as "PENDING, CLOSED, INITIATED, OPEN, SUBMITTED" etc., then Enum can help better. 

The first item in the enum is the default (PENDING would be the default - 0).

Enum status {
Status public status; 

- Contract Kill

The only way to stop smart contract and remove the storage is to self destruct the program. While you do that, the asset it holds such as Ether or any crypto will be transferred to owner address .

  function kill() public {
      require(msg.sender == owner);

- Memory Vs Storge

Storage - Default one, most of the values re stored in blockchain.

Memory - to be used in functions. It is not stored in blockchain rather used for computations

- Scope variables

Stack - All the variables declared in the solidity program is getting queued up in "Stack". If you hve too many variables declared, then you will end up facing "Stack too Deep" errors. Which can be avoided by using Scope functions.

uint var1;
    (uint varA, uint varB) = getVars();
    var1 = varA + varB;

// now use var1
Code inspired from Uniswap

What's Your Reaction?