Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Solidity - Smart Contract Programming Language

Solidity is a high-level, statically-typed programming language primarily used to write smart contracts for the Ethereum blockchain. Inspired by JavaScript, Python, and C++, Solidity enables developers to create secure, decentralized applications (DApps) by implementing self-executing contracts that operate transparently on the blockchain.

Table of Contents


Installation

Using Remix (Recommended for Beginners)

Remix IDE is a powerful online IDE with built-in Solidity compiler, debugger, and deployment tools. It’s the best option for beginners to quickly start coding and testing Solidity contracts.

Using solc (Command Line Compiler)

For local development, install the Solidity compiler solc via Snap:

sudo snap install solc

Verify installation:

solc --version

Using Node.js with Hardhat or Truffle

Hardhat and Truffle are popular development frameworks that include local blockchain environments and testing utilities.

Install either:

# Hardhat
npm install --save-dev hardhat

# Truffle
npm install -g truffle

Getting Started

All Solidity files use the .sol extension. Each file starts with a pragma directive that specifies the compiler version:

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

Hello World Contract

Here’s a simple "Hello World" smart contract that stores a number and allows it to be updated:

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

contract HelloWorld {
    uint256 public storedNumber;

    function setNumber(uint256 _number) public {
        storedNumber = _number;
    }

    function getNumber() public view returns (uint256) {
        return storedNumber;
    }
}

Basic Concepts

Data Types

Solidity supports various data types:

  • uint: Unsigned integers (e.g., uint256 for a 256-bit unsigned integer).
  • int: Signed integers.
  • bool: Boolean values true or false.
  • address: Stores an Ethereum address (20 bytes).
  • string and bytes: Dynamic-sized byte arrays.

Variables

  • State variables: Stored on-chain and persistent (e.g., uint256 public myNumber).
  • Local variables: Declared within functions and exist temporarily.
  • Global variables: Blockchain-specific info like msg.sender (sender’s address), block.timestamp, etc.

Functions

Functions can have different visibility modifiers:

  • public: Callable by anyone, internally and externally.
  • private: Callable only from within the contract.
  • view: Reads state but doesn’t modify it.
  • pure: Doesn’t read or modify state.

Events

Events log information on the blockchain, useful for tracking changes or emitting data to external apps.

event NumberUpdated(uint256 oldNumber, uint256 newNumber);

Advanced Concepts

Modifiers

Modifiers add custom behavior to functions, such as access control.

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

Inheritance and Interfaces

Solidity supports inheritance and interfaces to define reusable and modular code:

  • Inheritance: Extend the functionality of contracts.
  • Interfaces: Define function signatures without implementation, essential for interacting with other contracts.

Mappings and Arrays

Mappings and arrays are essential for storing structured data:

  • Mappings: Key-value pairs, e.g., mapping(address => uint256) balances.
  • Arrays: Lists of items, e.g., uint[] public numbers.

Payable Functions

payable functions can receive Ether, making them essential for contracts that handle payments.

function deposit() public payable {
    require(msg.value > 0, "Must send some Ether");
}

Best Practices

Security

  1. Use require and assert: To validate inputs and prevent unexpected behavior.
  2. Reentrancy Guard: Protect against reentrancy attacks with the Checks-Effects-Interactions pattern or the ReentrancyGuard library.
  3. Access Control: Restrict access using modifiers and only give permissions to specific users.

Gas Optimization

  • Use smaller data types (e.g., uint8) when possible.
  • Pack storage variables for reduced gas costs.
  • Minimize loops and nested function calls for efficiency.

Tools and Resources

Development Tools

  • Remix: Best for beginners, with built-in compiler, debugger, and deployment options.
  • Hardhat: Framework with a local blockchain, testing, and deployment utilities.
  • Truffle: Another development framework with migration and testing support.
  • Ganache: A personal Ethereum blockchain for quick testing and debugging.

Testing Libraries

  • Mocha & Chai: JavaScript libraries for writing and running test scripts.
  • Waffle: Testing framework with a focus on Ethereum.

Solidity Documentation and Learning Resources

  • Official Solidity Documentation: Up-to-date reference for Solidity.
  • Solidity by Example: Examples of various Solidity concepts.
  • OpenZeppelin Contracts: Audited library of smart contracts for secure and reusable code.
  • CryptoZombies: An interactive game to learn Solidity through building a zombie game.
  • Ethernaut: Security-focused Solidity challenges by OpenZeppelin.

Examples of Popular Smart Contracts

  1. ERC-20 Tokens: Standard for fungible tokens.
  2. ERC-721 Tokens: Standard for non-fungible tokens (NFTs).
  3. Voting Contracts: Smart contracts for decentralized voting systems.

Conclusion

Solidity is a powerful language for writing smart contracts that run on Ethereum and other blockchains. While simple to get started, Solidity requires careful attention to security and efficiency to develop production-ready DApps. Whether you're a developer new to blockchain or an experienced engineer, understanding Solidity is essential for creating decentralized applications.

Happy coding!


About

This repository dicuments my journey in web 3, specifically learning solidity

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages