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 cou…


This content originally appeared on DEV Community and was authored by Muhammad Ayaz

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;
    }
}

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;
    }
}

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!


This content originally appeared on DEV Community and was authored by Muhammad Ayaz


Print Share Comment Cite Upload Translate Updates
APA

Muhammad Ayaz | Sciencx (2025-01-29T13:46:19+00:00) Solidity Crash Course – Part 02: Smart Contracts. Retrieved from https://www.scien.cx/2025/01/29/solidity-crash-course-part-02-smart-contracts/

MLA
" » Solidity Crash Course – Part 02: Smart Contracts." Muhammad Ayaz | Sciencx - Wednesday January 29, 2025, https://www.scien.cx/2025/01/29/solidity-crash-course-part-02-smart-contracts/
HARVARD
Muhammad Ayaz | Sciencx Wednesday January 29, 2025 » Solidity Crash Course – Part 02: Smart Contracts., viewed ,<https://www.scien.cx/2025/01/29/solidity-crash-course-part-02-smart-contracts/>
VANCOUVER
Muhammad Ayaz | Sciencx - » Solidity Crash Course – Part 02: Smart Contracts. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/29/solidity-crash-course-part-02-smart-contracts/
CHICAGO
" » Solidity Crash Course – Part 02: Smart Contracts." Muhammad Ayaz | Sciencx - Accessed . https://www.scien.cx/2025/01/29/solidity-crash-course-part-02-smart-contracts/
IEEE
" » Solidity Crash Course – Part 02: Smart Contracts." Muhammad Ayaz | Sciencx [Online]. Available: https://www.scien.cx/2025/01/29/solidity-crash-course-part-02-smart-contracts/. [Accessed: ]
rf:citation
» Solidity Crash Course – Part 02: Smart Contracts | Muhammad Ayaz | Sciencx | https://www.scien.cx/2025/01/29/solidity-crash-course-part-02-smart-contracts/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.