Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

ESTHER NAISIMOI
ESTHER NAISIMOI

Posted on

     

Solidity Cheatsheet: A Quick Reference Guide

Image of Solidity Contract
Basic Structure of a Solidity Contract

// SPDX-License-Identifier: MITpragma solidity ^0.8.0;contract MyContract {    // State variables    uint public myNumber;    // Constructor    constructor(uint _num) {        myNumber = _num;    }    // Function to update state variable    function setNumber(uint _num) public {        myNumber = _num;    }    // Function to retrieve state variable    function getNumber() public view returns (uint) {        return myNumber;    }}
Enter fullscreen modeExit fullscreen mode

Contract Structure

**Contracts **are the fundamental building blocks in Solidity, similar to classes in object-oriented programming.

  1. State VariablesPermanently stored in contract storage, state variables represent the contract's state.
  2. FunctionsFunctions are executable units of code within a contract.
  3. EventsEvents allow logging to the Ethereum blockchain for frontend applications.

Data Types in Solidity
Value Types

  1. bool: true or false
  2. int/uint: signed and unsigned integers3.** address**: Ethereum address
  3. bytes: dynamic array of bytes
    Reference Types

  4. arrays: fixed or dynamic size

  5. struct: custom defined type

  6. mapping: hash tables

Function Visibility and Modifiers

Function Visibility
Visibility Modifiers

  1. public - Accessible from anywhere.Can be called internally or via messages. Generates a getter function for state variables.
  2. private - Only within the contract.Only visible for the contract they are defined in.3.** internal** - Within the contract and derived contracts.Only accessible internally and by derived contracts.
  3. *external *- Only callable from outside the contract.Can be called from other contracts and transactions. Cannot be called internally.

Function Modifiers

  1. pure - No state modifications
  2. view - Reads state but doesn’t modify it
  3. payable - Accepts Ether

Gas Optimization Techniques

Image description

  1. Use uint256Prefer uint256 for integers when possible.
  2. Avoid LoopsMinimize loops to reduce gas consumption.
  3. Use EventsEmit events instead of storing data.
  4. Optimize StoragePack variables to save storage slots

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Still learning ☺
  • Joined

More fromESTHER NAISIMOI

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp