SMART CONTRACTS WITH SOLIDITY. ALL YOU NEED

Nkugwa Mark William
5 min readFeb 7, 2023

--

  1. What is a smart contract?

A smart contract is a self-executing computer program that runs on a blockchain and automatically executes the terms of an agreement between parties when specific conditions are met.

2. What is Solidity?

Solidity is a high-level, contract-oriented programming language for writing smart contracts on the Ethereum blockchain.

3. Data Types

  • Boolean: bool
  • Integer: int8, int16, int24, ..., int256
  • Unsigned Integer: uint8, uint16, uint24, ..., uint256
  • Address: address
  • String: string
  • Fixed-point decimal: fixed
  • Floating-point decimal: ufixed

4. Variables: Variables are declared with a data type and a name, for example:

uint256 count;

5. Operators

  • Arithmetic: +, -, *, /, %
  • Comparison: ==, !=, <, >, <=, >=
  • Logical: &&, ||, !

6. Loops

For loop:

for (uint256 i = 0; i < 10; i++) {
// loop code
}

While loop:

uint256 i = 0;
while (i < 10) {
// loop code
i++;
}

7. Conditional Statements

If statement:

if (count > 0) {
// code
}

If-else statement:

if (count > 0) {
// code
} else {
// code
}

8. Comments

Comments are used to describe the code and make it easier to understand. They start with // for a single-line comment or /* and */ for a multi-line comment, for example:

// This is a single-line comment

/* This is a
multi-line comment */

9. Functions

Functions are blocks of code that perform a specific task and can be called multiple times. Functions are declared with a return type, a name, and a list of parameters, for example:

function greet(string memory name) public pure returns (string memory) {
return 'Hello, ' + name;
}

10. Event

Events are used to log information and trigger actions on the frontend. Events are declared with a name and a list of parameters, for example:

event Greeted(string memory name);

function greet(string memory name) public pure returns (string memory) {
emit Greeted(name);
return 'Hello, ' + name;
}

11. Structs

Structs are custom data types that allow you to group variables together. They can be declared with a name and a list of members, for example:

struct User {
address address;
string name;
uint age;
}

12. Arrays

Arrays are ordered collections of elements of the same data type. They can be declared with a data type, a name, and a size, for example:

uint[] numbers;

13. Mappings

Mappings are key-value pairs, where each key is unique and maps to a single value. They can be declared with a key data type and a value data type, for example:

mapping (address => uint) balances;

This example creates a mapping called balances where the keys are addresses and the values are unsigned integers. You can access the values stored in a mapping using the square bracket notation, for example:

balances[msg.sender] = 100;

This code sets the balance of the sender to 100. It’s important to note that mappings are not iterable and do not have a length property, so you cannot loop through the values stored in a mapping.

Mappings are stored in the contract storage, so their values persist across function calls and are stored on the blockchain. However, because mappings are not iterable, they can take up a lot of storage space, especially if you have many keys and values stored in the mapping. To reduce storage costs, you can use the bytes32 type as the key instead of address or uint256 to save space.

14. Modifiers

Modifiers are special functions that modify the behavior of a function or the access of a variable. Modifiers are declared with the modifier keyword, for example:

modifier onlyOwner() {
require(msg.sender == owner, 'Only the owner can access this function');
_;
}

15. Inheritance

Inheritance allows you to create a new contract that inherits all the members and functions from an existing contract. Inheritance is declared with the is keyword, for example:

contract Animal {
function makeSound() public pure returns (string memory) {
return 'Animal sound';
}
}

contract Dog is Animal {
function makeSound() public pure returns (string memory) {
return 'Woof';
}
}

16. Libraries

Libraries are contracts that contain reusable code that can be linked to other contracts. Libraries are declared with the library keyword, for example:

library SafeMath {
function add(uint a, uint b) internal pure returns (uint) {
uint c = a + b;
require(c >= a, 'Overflow');
return c;
}
}

17. Pragma Directives

Pragma directives are used to specify the version of the Solidity compiler to use and any other compiler options. Pragma directives are declared with the pragma keyword, for example:

pragma solidity ^0.8.0;

18. Enums

Enums are custom data types that allow you to define a set of named values. Enums are declared with the enum keyword, for example:

enum Color { Red, Green, Blue }
Color myColor = Color.Red;

19. Events

Events are a way to notify the front end about changes in the state of a contract. They are declared with the event keyword and can be triggered in a contract function, for example:

event Deposit(address indexed _from, uint _value);

function deposit() public payable {
emit Deposit(msg.sender, msg.value);
}

20. Importing Contracts

You can import code from another contract into your contract using the import keyword, for example:

import "./SafeMath.sol";

21. Function Overloading

Function overloading allows you to define multiple functions with the same name but different parameter lists. Function overloading is declared by defining multiple functions with the same name and different parameters, for example:

function transfer(address _to, uint _value) public { ... }
function transfer(address[] memory _to, uint[] memory _value) public { ... }

22. Interfaces

Interfaces are contracts that define a set of functions without any implementation. Interfaces are declared with the interface keyword, for example:

interface Token {
function transfer(address _to, uint _value) external returns (bool);
}

23. Error handling

Error handling in Solidity is done through the require, revert, and assert functions. The require function is used to enforce preconditions, the revert function is used to undo the changes made to the contract and revert to the previous state, and the assert function is used to enforce an invariant.

Follow, Like and Share

--

--

Nkugwa Mark William
Nkugwa Mark William

Written by Nkugwa Mark William

Nkugwa Mark William is a Chemical and Process engineer , entrepreneur, software engineer and a technologists with Apps on google play store and e commerce sites

No responses yet