Smart Contract Structure - Solidity for Beginners

Smart Contract Structure - Solidity for Beginners

In the previous article, we introduced the concept of smart contracts and how solidity can be used to develop applications that run on the Ethereum blockchain. You can check it out in case you missed it here

Screenshot from 2021-04-21 21-59-09.png

In this article we are going to take a closer look at what a smart contract looks like, we're also going to be looking at basic solidity data types.

A smart contract is simply a program that runs on the Ethereum blockchain. It's a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. Smart contracts are a type of Ethereum account. A contract is like a class, and it also supports inheritance.

Here are some of the basic functions and data types you can find in a smart contract (we'll do our best to cover as many as possible in this article).

  1. State Variables
  2. Functions
  3. Function Modifiers
  4. Events
  5. Structs
  6. Enums

1. State Variables

State variables are variables whose values are permanently stored in the blockchain, just think of them as normal variables you use every time when coding. One more thing; since solidity is a statically typed programming language, you have to assign a variable type when declaring a state variable. These are some of the various variable types available in solidity; uint, string, bool, address

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract {
  string stringValue;
  unit numberValue;
  bool booleanValue;
  address addressValue;
}

If you've already programmed object-oriented languages, you'll likely be familiar with some of these variable types. However, address should be new to you if you're new to Ethereum development.

An address type can hold Ethereum address which equates to 20 bytes or 160 bits. It returns in hexadecimal notation with a leading 0x.

2. Functions

Functions, sometimes referred to as methods is a group of reusable codes that can get information or set information in response to incoming transactions.

There are four types of functions in solidity;

Internal functions

These are functions that can only be accessed internally within the current smart contract or contracts deriving from it.


// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;


contract newContract {
 function setValue(unit _value) internal {...}
}

Private functions

Private functions are only visible for the contract they are defined in and not in derived contracts.

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract newContract {
 function setValue(unit _value) private {...}
}

External functions

These functions can be called from outside the smart contract. They also create EVM call.

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract newContract {
 function setValue(unit _value) external {...}
}

Public functions

Public functions can be called internally from within the contract or externally via messages

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract newContract {
 function setValue(unit _value) public  {...}
}

In a nutshell, public and external differ in terms of gas usage. The former use more than the latter when used with large arrays of data. This is due to the fact that solidity copies arguments to memory on a public function while external read from calldata which is cheaper than memory allocation.

3. Function Modifiers

Function modifiers are used to change the behaviour of a function. For example, you can use a modifier to automatically check a condition prior to executing the function.

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract newContract{
  modifier onlyOwner {
        require(
            msg.sender == owner,
            "Only owner can call this function."
        );
        _;
    }

//after defining the modifier, you can add it to a function

  function setValue(unit _value) public onlyOwner {...}
}

Don't worry if you don't understand the syntax, with time you'll get used to it, all you need to know at this moment is that modifiers are some sort of validators added to a function, so basically if this validator fails, the function will not run.

4. Event

An event is an inheritable member of the contract, which stores the arguments passed in the transaction logs when emitted. These logs are stored on the blockchain and are accessible using the address of the contract till the contract is present on the blockchain.

An event can be declared using the event keyword.

//Declare an Event
event Deposit(address indexed _from, bytes32 indexed _id, uint _value);

//Emit an event
emit Deposit(msg.sender, _id, msg.value);

5. Struct

Structs are used to represent a record. They allow users to create their own data type, The struct contains a group of elements with a different data type. if you've programmed in object-oriented programming, you can think of structs as interfaces, to define a structure, the struct keyword is used.

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract newContract {

 struct Person {
    string Firstname;
    string LastName;
    unit age;
    address account;
   }

//This is how struct types are used to represent a record
}

6. Enums

Enum in Solidity stands for Enumerable. They are user-defined types that contain human-readable names for a set of constants, called member.


// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;


contract newContract {

enum NUMBERS {ONE, TWO, THREE} //declaring enum

    NUMBERS number; // initializes enum to a variable

    function setNumber() public {
        number = NUMBERS.TWO; //set the variable to second element NUMNERS enum
    }

    function getNumber() public view returns (NUMBERS){
        return number;
    }

}

Everything we just talked about is part of the structure of a smart contract, you'll use these same functions and set of variables throughout your blockchain development career to build awesome applications. Please like the article if it helped you understand certain concepts, you can leave your questions in the comment section too if you're stuck or don't understand something.

Thanks for reading, See you soon.