0

Solidity: Basic Smart Contract Structure.

Following we will investigate the basic structure of a smart contract as well as the key concepts required in understanding the length of possibilities that what can be achieved through these blockchain applications.

Although the syntax of the Solidity language is quite similar to other high level object oriented programming languages such as Python or C++, there are certain blockchain-specific functionalities that helps in managing and handling Ehtereum accounts while moving funds (in the form of ether) between them.

Smart contracts have a lot of disruptive potential, since it favors decentralization and thus the idea that transactions are carried out directly between peers within the network and that there is a no overseer or controlling third party, the distributed network is self-regulated. It is therefore important to understand the basics of how smart contracts work before attempting to tackle and understand the security problems that concerns them.



Basic Smart Contract

Let’s suppose we have the following basic smart contract:

pragma solidity 0.7.2;

contract fooContract {
    
  enum fooType { fooType1, fooType2 }

  struct fooStruct {
    address _add;
    string _str;
    fooType _fooType;
  }
    
  mapping (address => uint256) public fooBalance;
  
  address public owner;

  event fooEvent(address indexed _add, uint256 _val);
    
  modifier fooModifier {
    require(msg.sender == owner);
    _;
  }
    
  constructor(uint256 _initVal) public {
    owner = msg.sender;
  }

  function fooFunction(address _add, uint256 _val) fooModifier public {
    require(fooCheck(_val));
    //... 
    emit fooEvent(msg.sender, _val);
  }

  function fooCheck(uint256 _val) private returns (bool) {
    bool res;
    //checks
    return res;
  }
}


Line by Line Analysis

Let’s now analyze each of the relevant parts by referring to the lines they are in:


Line 1: Sets the version of Solidity to be used by the smart contract, in this case version 0.7.2 is selected. –When set with a caret, it is said to be a floating pragma (^0.7.2) which can lead to deployments using outdated compiler versions and is recommended against.


Line 3: Defines the name of the smart contract . –Similar to Class definitions in Object Oriented programming.

crontact_name

Line 5: Creates an enum which is a data type consisting of a fixed number of named values.

enum

Lines 7-11: Creates an struct which is a custom data type that can group several variables.


Line 13: Creates a mapping which works like a hash table of key-value pairs.


Line 15: Declares a public variable of type address named owner. –Everything in a blockchain is accessible through an address expressed in hexadecimal format.


Line 17: Defines an event named fooEvent which receives an address and an uint256 integer as parameters. –More on events here!


Lines 19-22: Defines a function modifier named fooModifier. –More on modifiers here!


Lines 24-26: This is the constructor of the smart contract. –Constructors are only called once when the contract is deployed. More on constructors here!


Lines 28-32: Defines a public function named fooFunction which utilizes the fooModifier and receives an address and an uint256 integer as parameters.


Lines 34-38: Defines a private function named fooCheck which receives an uint256 integer as parameter and returns a bool.



<< Prev

Next >>