DEV Community

Muhammad Ayaz
Muhammad Ayaz

Posted on

Solidity Crash Course - Part 02: Smart Contracts

Solidity Crash Course - Part 02

Introduction

Solidity is a high-level programming language used for writing smart contracts on the Ethereum blockchain. It is influenced by JavaScript, Python, and C++. In this part of the crash course, we will cover the basic syntax of Solidity contracts and how to write a smart contract.


Solidity - Contract Syntax

A Solidity contract is a collection of code (functions) and data (state) that resides at a specific address on the Ethereum blockchain. Every Solidity contract starts with a version pragma, which specifies the compiler version.

1. Basic Structure of a Solidity Contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyFirstContract {
    // State variables
    uint public myNumber;
    string public myString;

    // Constructor - Runs only once when the contract is deployed
    constructor(uint _myNumber, string memory _myString) {
        myNumber = _myNumber;
        myString = _myString;
    }

    // Function to update number
    function setNumber(uint _newNumber) public {
        myNumber = _newNumber;
    }

    // Function to update string
    function setString(string memory _newString) public {
        myString = _newString;
    }
}
Enter fullscreen mode Exit fullscreen mode

Breakdown of Code

  1. SPDX-License-Identifier - This specifies the license for the contract.
  2. Pragma directive - It ensures that the contract compiles with Solidity version 0.8.0 or later.
  3. Contract declaration - The contract MyFirstContract {} defines the contract.
  4. State Variables - uint and string variables store persistent data on the blockchain.
  5. Constructor - Initializes the contract's state when deployed.
  6. Public Functions - Functions setNumber and setString modify the contract's state.

Solidity - Smart Contract

A smart contract is a self-executing contract with predefined rules. Once deployed, it interacts with users and other contracts.

2. Writing a Simple Smart Contract

Below is an example of a smart contract that allows users to store and retrieve their favorite number.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract FavoriteNumber {
    uint private favoriteNumber;

    // Event to notify when a number is changed
    event NumberUpdated(uint newNumber);

    // Function to set favorite number
    function setFavoriteNumber(uint _number) public {
        favoriteNumber = _number;
        emit NumberUpdated(_number);
    }

    // Function to get favorite number
    function getFavoriteNumber() public view returns (uint) {
        return favoriteNumber;
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. Events - Used to log contract interactions on the blockchain.
  2. Public Function setFavoriteNumber - Allows users to store a number.
  3. View Function getFavoriteNumber - Retrieves stored number without modifying the blockchain.
  4. Emit Statement - Logs an event when the number is updated.

Deploying the Contract

To deploy the contract, you can use Remix IDE or Hardhat.

Deploying Using Remix IDE

  1. Go to Remix IDE.
  2. Copy and paste the Solidity contract into a new file (FavoriteNumber.sol).
  3. Compile the contract using the Solidity compiler.
  4. Deploy it using the Injected Web3 environment (MetaMask required).
  5. Interact with the contract using the deployed functions.

If you need any help with deployment, let me know!


Conclusion

In this part, we covered the basic Solidity contract syntax and created a simple smart contract.

Stay tuned for Part 03!

Top comments (0)