DEV Community

Cover image for The basic of Solidity: Write your own Smart Contract
superXdev
superXdev

Posted on

The basic of Solidity: Write your own Smart Contract

Solidity is a statically-typed programming language specifically designed for writing smart contracts that run on the Ethereum Virtual Machine (EVM). As the backbone of Ethereum, it enables developers to create decentralized applications (dApps) that harness the power of blockchain technology. In this post, we'll delve into the core aspects of Solidity, including its use cases, contract structure, and key programming concepts.

What is Solidity?

Solidity is an object-oriented programming language influenced by C++, Python, and JavaScript, tailored for developing smart contracts on the Ethereum blockchain. Smart contracts are self-executing contracts with the terms directly written into code, facilitating, verifying, or enforcing the performance of a contract.

Use Cases of Solidity

  1. Decentralized Finance (DeFi): Creating protocols for lending, borrowing, and trading without intermediaries.
  2. Non-Fungible Tokens (NFTs): Developing unique digital assets for art, gaming, and collectibles.
  3. Decentralized Autonomous Organizations (DAOs): Automating organizational governance and decision-making.
  4. Supply Chain Management: Enhancing transparency and traceability in logistics.

Contract Structure

A basic Solidity contract consists of the following parts:

pragma solidity ^0.8.0;

contract MyContract {
    // State variables
    uint public myNumber;

    // Constructor
    constructor(uint _initialNumber) {
        myNumber = _initialNumber;
    }

    // Functions
    function setNumber(uint _newNumber) public {
        myNumber = _newNumber;
    }

    function getNumber() public view returns (uint) {
        return myNumber;
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Components:

  • Pragma: Specifies the version of Solidity.
  • Contract: Defines the contract name and encapsulates state variables and functions.

Variables and Types

Solidity supports various data types:

  • Boolean: bool
  • Integer: int and uint
  • Address: address
  • String: string
  • Bytes: bytes
  • Fixed-size Arrays: uint[10]
  • Dynamic Arrays: uint[]

Functions

Functions define the behavior of the contract. They can be public, private, internal, or external, dictating their accessibility.

function exampleFunction() public pure returns (string memory) {
    return "Hello, World!";
}
Enter fullscreen mode Exit fullscreen mode

Function Visibility

  • Public: Accessible by anyone.
  • Private: Only accessible within the contract.
  • Internal: Accessible within the contract and derived contracts.
  • External: Can only be called from outside the contract.

Modifiers

Modifiers alter the behavior of functions. Commonly used for access control.

modifier onlyOwner() {
    require(msg.sender == owner, "Not the contract owner");
    _;
}

function restrictedFunction() public onlyOwner {
    // Function logic
}
Enter fullscreen mode Exit fullscreen mode

Operators

Solidity includes various operators for arithmetic, comparison, and logical operations.

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

Conditionals

Control flow uses if-else statements.

function checkNumber(uint _number) public pure returns (string memory) {
    if (_number > 10) {
        return "Greater than 10";
    } else {
        return "10 or less";
    }
}
Enter fullscreen mode Exit fullscreen mode

Arrays

Arrays can be fixed or dynamic.

uint[] public dynamicArray;
uint[5] public fixedArray;
Enter fullscreen mode Exit fullscreen mode

Structs

Structs allow grouping related data.

struct Person {
    string name;
    uint age;
}

Person public person = Person("Alice", 30);
Enter fullscreen mode Exit fullscreen mode

Events

Events facilitate logging and are useful for off-chain interaction.

event NumberChanged(uint oldNumber, uint newNumber);

function setNumber(uint _newNumber) public {
    emit NumberChanged(myNumber, _newNumber);
    myNumber = _newNumber;
}
Enter fullscreen mode Exit fullscreen mode

Error Handling

Solidity provides require, assert, and revert for error handling.

function withdraw(uint _amount) public {
    require(_amount <= balance, "Insufficient balance");
    balance -= _amount;
}
Enter fullscreen mode Exit fullscreen mode

Inheritance

Solidity supports inheritance, allowing contracts to inherit properties and functions from other contracts.

contract Parent {
    uint public parentNumber;

    function setParentNumber(uint _number) public {
        parentNumber = _number;
    }
}

contract Child is Parent {
    function getParentNumber() public view returns (uint) {
        return parentNumber;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)