{"id":15065113,"url":"https://github.com/hydraphyzer/solidity-cheatsheet","last_synced_at":"2026-01-02T12:56:39.557Z","repository":{"id":176607824,"uuid":"656765615","full_name":"HydraPhyzer/Solidity-Cheatsheet","owner":"HydraPhyzer","description":"This Repository Contains a README File. In File I Have Compiled a Complete Cheat Sheet For Solidity.","archived":false,"fork":false,"pushed_at":"2023-06-27T09:36:13.000Z","size":35,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-15T22:43:23.470Z","etag":null,"topics":["blockchain","blockchain-solidity","ethereum","hydraphyzer","hydraphyzergithub","muhammadzubair","muhammadzubair-github","smart-contracts","smartcontracts","solidity","solidity-cheatsheet","solidity-cheet-sheet","solidity-codes","solidity-contracts","solidity-course","solidity-github-cheat-sheet","solidity-language","zubair"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HydraPhyzer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-21T15:35:57.000Z","updated_at":"2023-06-27T09:22:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"bc3e8ef6-c88f-45bc-8904-24d249c5cc98","html_url":"https://github.com/HydraPhyzer/Solidity-Cheatsheet","commit_stats":null,"previous_names":["hydraphyzer/solidity-cheatsheet"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HydraPhyzer%2FSolidity-Cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HydraPhyzer%2FSolidity-Cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HydraPhyzer%2FSolidity-Cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HydraPhyzer%2FSolidity-Cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HydraPhyzer","download_url":"https://codeload.github.com/HydraPhyzer/Solidity-Cheatsheet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243801611,"owners_count":20350106,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blockchain","blockchain-solidity","ethereum","hydraphyzer","hydraphyzergithub","muhammadzubair","muhammadzubair-github","smart-contracts","smartcontracts","solidity","solidity-cheatsheet","solidity-cheet-sheet","solidity-codes","solidity-contracts","solidity-course","solidity-github-cheat-sheet","solidity-language","zubair"],"created_at":"2024-09-25T00:34:24.714Z","updated_at":"2026-01-02T12:56:39.515Z","avatar_url":"https://github.com/HydraPhyzer.png","language":null,"readme":"\u003cdiv style=\"text-align:center\"\u003e\n  \u003cimg src=\"https://soliditylang.org/images/logo.svg\" alt=\"Solidity Logo\"\u003e\n\u003c/div\u003e\n\n## Solidity Cheatsheet\n\nThis is The Complete Solidity Notes With Code Snippest and Comments, Which Will Help You Understand Better\n\n---\n\nBefore Writing Solidity Code, We Have To Include This Code Snippest\n\n```solidity\nSPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\n// 0.8.0 Refers to the Version of Solidty\n```\n---\n\n*Lets Start*\n\n\u0026nbsp;\n\n1. Data Types Values Types\n\n```solidity\ncontract ValuesTypes {\n    bool public Item = true;\n    uint256 public UnsignedInt = 1000;  // We Can Set int#. Here Hash Represent Any Number. 0 to 2^#-1\n\n    int256 public MyNum = 7000;  // We Can Set int#. Here Hash Represent Any Number. 0 to 2^#-1\n    address public  MyAddress=0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;  //Sture 20 Bit Adress. Store Hex Adress\n\n    bytes32 public  MyBytes32;\n}\n\n```\n---\n2. Solidity Functions\n\n```solidity\ncontract FunctionsInSolidity{\n    uint public Age=20;\n\n    function Add(uint X, uint Y) public pure returns(uint) {\n        return X+Y;\n    }\n    function GetAge() public view returns(uint) {\n        return Age;\n    }\n    function ChangeAge() public {\n        Age+=1;\n    }\n    function InternalFunction () internal {\n        // Code\n    }\n}\nfunction Outside(uint XYZ){\n    // I Can Be Declared Outside the Contract But My Button Cannot be Shown While Deploy\n}\n```\n---\n3. State Variable\n\n```solidity\ncontract StateVariable{\n    /*\n    In Solidity Variables are of 3 Types (State,Local,Global). But Today We Will Discuss Only State Variables\n    State Variables are Those That Are Declared Inside Contract, But They are Outside of Functions.\n\n    State Variables Are Important. Because They Cost Gas and They Are Directly Stored in Blockchain Storage. And There are only 3 Ways to\n    Initialise Them. 1) By Directly Initialising While Declaring. 2) By Constructor. 3) By Using Function.\n    */\n\n    uint16 public MyStateVariable;\n\n    // MyStateVariable=1500; This is Not Allowed and Will Give Error\n\n    constructor(){\n        MyStateVariable=2000;\n    }\n    function SetVariable() public {\n        MyStateVariable=5000;\n    }\n\n}\n```\n---\n4. Local Variable\n\n```solidity\ncontract LocalVariable{\n\n    /*\n    Local Variable are Those That Are Declared or Intialized Inside Functions. They Stored Inside RAM and Have Short Life Span, Depends\n    on Life Cycle of Function. They also Cost Gas, But Very Negligible\n    */\n\n    function DealLocalVariable() public pure returns (uint,bool) {\n        uint Age=12;\n        bool Status=false;\n\n        Age=20;\n\n        return (Age,Status);\n    }\n}\n```\n---\n5. Global Variable\n\n```solidity\ncontract GlobalVariable{\n    /*\n    These are Special Predefined Variables. No Need to Create. These Provide Info About\n    Blockchain and Transaction Properties\n\n    Variable\t        Type\t    Description\n    msg.sender\t        address\t    The address of the account that sent the current transaction.\n    msg.value \t        uint\t    The amount of Ether sent with the current transaction.\n    block.coinbase \t    address\t    The address of the miner who mined the current block.\n    block.difficulty \tuint\t    The difficulty of the current block.\n    block.gaslimit \t    uint\t    The maximum amount of gas that can be used in the current block.\n    block.number \t    uint\t    The number of the current block.\n    block.timestamp \tuint \t    The timestamp of the current block.\n    now\t                uint\t    An alias for block.timestamp.\n    tx.origin \t        address     The address of the account that originally created the transaction (i.e., the sender of the first transaction in the call chain).\n    tx.gasprice \t    uint\t    The gas price (in Wei) of the current transaction.\n\n    */\n\n    address public MyAdress=msg.sender;\n    uint public Difficulty=block.difficulty;\n    uint public Cost=tx.gasprice;\n    uint public Time=block.timestamp;\n}\n```\n---\n6. View Pure and Simple\n\n```solidity\ncontract ViewPureSimple{\n    /*\n    Functions Are of Three Types 1) View 2) Pure and 3) Simple\n    1) View:-   These Are Read Only Blockchain, State Variables and Global Variables. Never Update Them\n    2) Pure:-   These Never Read and Update(Write) Blockchain, State Variables and Global Variables\n    3) Simple:- They Update the State Variable. No Neeed to Mention Simple Keyword. This Dont\n                Return Anything\n    */\n\n    uint8 public Age=20;\n\n    function MyView() public view returns (uint) {\n        return  Age+10;\n    }\n\n    function MyPureOne() public pure returns (uint){\n        return  1;\n    }\n    function MyPureTwo(uint X, uint Y) public pure returns (uint){\n        uint Sum=X+Y;\n        return  Sum;\n    }\n\n    function MySimple() public returns(uint) {\n        Age+=23;\n        return Age;\n    }\n}\n```\n---\n7. Default Values\n\n```solidity\ncontract DefaultValues{\n    uint public X;           // 0\n    bool public Status;      // false\n    address public MyAdress; // 0x0000000000000000000000000000000000000000\n    bytes32 public MyBytes;  // 0x0000000000000000000000000000000000000000000000000000000000000000\n    string public Name;      // stirng\n}\n```\n---\n8. Strings in Solidity\n\n```solidity\ncontract MyString{\n    //State Variables Stored in Blockchain Storage, No Need to Declared Memory Keyword\n    string public Name=\"Muhammad Zubair\";\n\n    /*\n    Becausse Functins and Local Variables Stored in Memory or Stack , That's Why We\n    Using Memory Keyword Inside Functions\n    */\n\n    function GetAndSetString(string memory ArgName) pure  public returns(string memory) {\n        // string memory Name=\"Zubair\";\n        string memory MyName=ArgName;\n        return MyName;\n    }\n}\n```\n---\n9. Constants in Solidity\n\n```solidity\ncontract MyConstants{\n\n    // The Variable With Constant Keyword Will Cost Low Gas\n    address constant public MyAdressA=0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;\n\n    // The Variable Without Constant Keyword Will Cost High Gas\n    address public MyAdressB=0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;\n\n}\n```\n---\n10. Constructor in Solidity\n\n```solidity\ncontract Constructor{\n    uint public Age;\n    string public Name;\n    address public Owner;\n\n    // Constructor is the Functions That is Invoked Fitrst. If Not Defined. Constructor Defined It\n\n    // constructor(){\n    //     Age=20;\n    //     Name=\"Zubair\";\n    //     Owner=msg.sender;\n    // }\n\n    constructor(uint _Age, string memory _Name, address _Owner){\n        Age=_Age;\n        Name=_Name;\n        Owner=_Owner;\n    }\n\n}\n```\n---\n11. If Else in Solidity\n\n```solidity\ncontract IfElse{\n    /*\n    If Else Cannot Defined at Contract Level (Where State Variable Defined)\n    They Only Defined At Local Level\n    */\n\n    function Check(uint Age) public pure returns(string memory){\n        string memory Message;\n\n        if(Age\u003e100){\n            Message=\"Greater Than 100\";\n        }\n        else if(Age\u003c100){\n            Message=\"Less Than 100\";\n        }\n        else{\n            Message=\"Equal 100 or Error\";\n        }\n\n        return Message;\n    }\n}\n```\n---\n12. Ternary Operator in Solidity\n\n```solidity\ncontract Ternary {\n    function Check(uint Age) public pure returns(string memory){\n        string memory Message;\n\n        Message=Age\u003e100?\"Greater Than 100\":\"Less or Equal to 100\";\n        return Message;\n    }\n}\n```\n---\n13. All Types of Loops in Solidity\n\n```solidity\ncontract MyLoops{\n\n    /*\n    Loops Also Don't Work at Contract Level Like Conditional Statement\n    You Should Be Carefull While Loops. They Can Cost\n    */\n\n    function AllLoops() public pure returns(uint,uint,uint){\n        uint For;\n        uint While;\n        uint DoWhile;\n\n        for (uint I=0; I\u003c10; I++)\n        {\n            For+=I;\n        }\n\n        uint J=0;\n        while (J\u003c10)\n        {\n            While +=J;\n            J++;\n        }\n\n        uint K=0;\n        do{\n            DoWhile+=K;\n            K++;\n        } while(K\u003c10);\n\n        return (For,While,DoWhile);\n    }\n}\n```\n---\n14. Continue and Break in Solidity\n\n```solidity\ncontract ContinueAndBreak {\n    function MyFunc() public pure returns (uint256) {\n        uint256 For;\n\n        for (uint256 I = 0; I \u003c 10; I++) {\n            if (I \u003c 8) {\n                continue;\n            }\n            For += I;\n\n            if (I == 8) {\n                break;\n            }\n        }\n        return For;\n    }\n}\n```\n---\n15. Fixed Size Array in Solidity\n\n```solidity\ncontract Array{\n    //Array Just Like Strings Stored on Blockchain Storage\n\n    uint[5] public OuterArray;\n    constructor(){\n        OuterArray=[1,2,3,4,5];\n    }\n\n    //This Function Will Cost Huge Gas\n    function GetArray() public view returns (uint[5] memory) {\n        return OuterArray;\n    }\n\n    function LocalFunc() public pure returns (uint) {\n        uint [] memory MyArra=new uint[](5);\n\n        MyArra[2]=23;\n\n        return MyArra[2];\n    }\n\n    function Mabipulate() public {\n        //Update Array\n        OuterArray[2]=23;\n\n        //Delete Element\n        delete OuterArray[3];\n\n        //Get Length\n        uint Length=OuterArray.length;\n    }\n}\n```\n---\n16. Dynamic Size Array in Solidity\n\n```solidity\ncontract DynamicArray {\n    /*\n    Dynamic Size Arrays Cannot Be Created In Memory. They Have Push and Pop Method. This Can be Initialized in Functio, Constructor\n    */\n    uint256[] public OuterArray = [1, 2, 3, 4, 5, 6];\n\n    function GetArray() public view returns (uint256[] memory) {\n        return OuterArray;\n    }\n\n    // See We Cannot Create Dynamic Array in Memory\n\n    // function LocalFunc() public pure{\n    //     uint256[] memory MyArra = new uint256[];\n    // }\n\n    function Manipulate() public {\n        // Update Array\n        OuterArray[2] = 23;\n\n        //Delete Element\n        delete OuterArray[3];\n\n        //Get Length\n        uint256 Length = OuterArray.length;\n\n        // Push Element At Last\n        OuterArray.push(100);\n        OuterArray.push(200);\n        OuterArray.push(300);\n        OuterArray.push(400);\n\n        // Pop Element From Last\n        OuterArray.pop();\n    }\n}\n```\n---\n17. Bytes (Fixed Size) in Solidity\n\n```solidity\ncontract MyFixedBytes{\n    /*\n    1) Bytes are Similar to Arrays, But They Stored Values in Hexadecimal\n    2) bytes#. Here # is any Number. If # is 5 Then It Will Create 5 Boxes\n    3) Each Box is of 1 Byte (8 Bit)\n    4) Each Box Contains 2 Hexadecimal Digits. So Each Digit 4 Bits\n    5) Data Types are of Two Types 1) Value 2) Refrence. Bhtes are Refrence Types\n    */\n\n    bytes6 public Name; //Default 0x000000000000\n    bytes2 public Age;  //Default 0x0000\n\n    function SetValues() public {\n        Name=\"Zubair\";  //Store Like That 0x5a7562616972\n        Age=\"20\";       //Store Like That 0x3230\n    }\n    function GetSize() public view returns (uint,uint){\n        return (Name.length, Age.length);\n    }\n    function GetEach(uint X) public view returns (bytes1,bytes1){\n        return (Name[X],Age[X]); //Answer is 6,2\n    }\n}\n```\n---\n18. Bytes (Dynamic Size) in Solidity\n\n```solidity\ncontract DynamicBytes{\n    bytes public Temp;\n\n    constructor(){\n        Temp=\"Muhamamd Zubair \";\n    }\n    function Push() public {\n        Temp.push('r');\n    }\n    function Pop() public {\n        Temp.pop();\n    }\n    function Length() view public returns (uint) {\n        return Temp.length;\n    }\n    function GetEach(uint ID) public view returns(bytes1){\n        return Temp[ID];\n    }\n}\n```\n---\n19. Enums in Solidity\n\n```solidity\ncontract Enums{\n    /*\n        1) These are User Defined Data Types\n        2) Used When We Have More Than 2 Choices Unlike Boolean\n        3) Can Be Used For Naming Convention For Integers\n        4) Increase Readability. Helps Easy Maintanance of Smart Contract\n        5)\n    */\n\n    enum Status{\n        PENDING,\n        REJECTED,\n        SHIPPED,\n        ACCEPTED,\n        CANCEL\n    }\n    Status MyStatus; //This Have Default Value 0 (PENDING);\n\n    function SetStatus(Status ID /*ID Will Be A Number*/) public {\n        MyStatus=ID;\n    }\n    function SetReject() public {\n        MyStatus=Status.REJECTED;\n    }\n    function ResetStatus() public {\n        delete MyStatus; //This Will Again Set MyStatus to 0 (Pending)\n    }\n    function GetStatus() public view returns(Status) {\n        return MyStatus; //O or 1 or Else\n    }\n}\n```\n---\n20. Struct in Solidity\n\n```solidity\ncontract MyStruct{\n    /*\n        1) This is Another User Defined Refrence Type Data\n        2) Group of One or More In-Built Data Types\n        3) By Default Stored In Storage\n        4) Can Be Defined Inside or Outside Contract\n        5) If Declared Outside, Will Be Accessed By Multiple Contracts\n    */\n\n    struct Employee{\n        string Name;\n        uint Age;\n        address Account;\n    }\n\n    Employee public Single;\n    Employee[] public Array;\n\n    constructor(uint _Age, string memory _Name, address _Acc){\n        Single.Name=_Name;\n        Single.Age=_Age;\n        Single.Account=_Acc;\n    }\n\n    function SetValues() public {\n        // Two Ways to Create Instance of IT\n        Employee memory E1=Employee(\"Zubair\",20,msg.sender);\n        Employee memory E2=Employee({Name:\"Talha\",Age:20,Account:msg.sender});\n\n        Array.push(E1);\n        Array.push(E2);\n    }\n\n    function Manipulate() public returns (Employee memory,Employee memory){\n        // By Copy\n        Single=Array[1];\n\n        delete Array[1];\n\n        // This is Also By Copy. Changes In Temp Wont Reflect in Single\n        Employee memory Temp=Single;\n        Temp.Name=\"Kabeer\";\n\n        // This is Also By Refrence. Changes In NewTemp Will Reflect in Single\n        Employee storage NewTemp=Single;\n\n        // NewTemp.Name=\"Kabeer Shah\";\n\n        return (Temp,NewTemp);\n    }\n}\n```\n---\n21. Mapping \u0026 Advanced Mapping in Solidity\n\n```solidity\n\n/*\n    1) Mapping (Key Value Pairs) is also a Data Type\n    2) They Connot be Declared at File Level and Function Level\n    3) Key Can Be of ENUM,Values, and Contract Types\n*/\n\ncontract BasicMapiing {\n    mapping(uint256 =\u003e string) public Employee;\n\n    function SetValues() public {\n        Employee[15] = \"Zubair\";\n        Employee[16] = \"Talha\";\n        Employee[17] = \"Kamran\";\n        Employee[18] = \"Naheed\";\n        Employee[19] = \"John\";\n    }\n\n    function GetValues(uint256 ID) public view returns (string memory) {\n        return Employee[ID];\n    }\n}\n\nstruct DonorInfo {\n    string Name;\n    uint256 Age;\n    string Addr;\n    uint256 Amount;\n}\n\ncontract AdvanceMapping {\n    mapping(address =\u003e DonorInfo) public NGO;\n\n    function SetInfo(\n        string memory Name,\n        uint256 Age,\n        string memory Addr,\n        uint256 Amount\n    ) public {\n        NGO[msg.sender] = DonorInfo(Name, Age, Addr, NGO[msg.sender].Amount+Amount);\n    }\n\n    function DeleteInfo(address Add) public {\n        delete NGO[Add];\n    }\n}\n```\n---\n22. Visibility in Solidity\n\n```solidity\ncontract Visibility {\n    /*\n        1) Visiblity Used By Function and State Variables\n        2) Who can Call or Access Function and State Variables\n        3) These Are of 4 Types 1) Public 2) Private 3) Internal 4) External\n        4) PotentialCaller of 4 Types\n            a) Inside Itself Contract\n            b) Derived Contract to His Parent Contract (Inheritence)\n            c) Another Contract (No Relation Between Contract, But In Same File)\n            d) Outside World (Buttons in Remix IDE)\n\n            Gas Order (Low to High) \u0026 Security(High to Low) =\u003e Private-\u003eInternal-\u003eExternal-\u003ePublic\n\n        5) Private:-    State Variablae (SV) and Function Accessed only Inside Contract\n        6) Internal:-   Within Contract and Child Contract\n        7) External:-   Only For Function (No SV). Not Allowed to Call for Derived and Contract Itself . Also Call in Outside World (Button in IDE)\n        8) Public:- Can Be Called by Any Potential Caller.\n\n        By Defaults SV Are Internal and Functions are Public\n    */\n\n    uint256 private X = 1; // Only Within Contract\n    uint256 internal Y = 2; // Within Contract and Derived Contarct\n    uint256 public Z = 3; // EveryWhere\n\n    function CheckA() public pure returns (string memory) {\n        return \"Public\";\n    }\n\n    function CheckB() private pure returns (string memory) {\n        return \"Private\";\n    }\n\n    function CheckC() internal pure returns (string memory) {\n        return \"Internal\";\n    }\n\n    function CheckD() external pure returns (string memory) {\n        return \"External\";\n    }\n    function CheckAll() public pure returns (string memory) {\n        // return CheckA(); //Works\n        // return CheckC(); //Works\n        // return  CheckB(); //Works\n        // return  CheckD(); //Not Works\n\n    }\n}\n---\ncontract Child is Visibility {\n    string public ResultA = CheckA(); //It Works Result=\"Public\"\n    // string public ResultB = CheckB(); //It Did'nt Works\n    string public ResultC = CheckC(); //It Works Result=\"Internal\"\n    // string public ResultD = CheckD(); //It Did'nt Works\n}\ncontract TempContract{\n    Visibility V1=new Visibility();\n\n    function External() public view returns(string memory) {\n        return V1.CheckD(); //Works\n        // return V1.CheckA(); // Works\n        // return V1.CheckB(); //Not Works\n        // return V1.CheckC(); //Not Works\n\n    }\n}\n```\n---\n23. Inheritence in Solidity\n\n```solidity\ncontract Inheritence {\n    // Virtual Keyword allows Child to Ovverride Its Implementation\n\n    function A() public pure returns (string memory) {\n        return \"I am Inside A\";\n    }\n\n    function B() public pure returns (string memory) {\n        return \"I am Inside A\";\n    }\n\n    function C() public pure virtual returns (string memory) {\n        return \"I am Inside A\";\n    }\n\n    function D() public pure virtual returns (string memory) {\n        return \"I am Inside A\";\n    }\n}\n\ncontract Child is Inheritence {\n    function C() public pure override  returns (string memory) {\n        return \"I am Inside B\";\n    }\n\n    function D() public pure virtual  override  returns (string memory) {\n        return \"I am Inside B\";\n    }\n}\ncontract GrandChild is Child{\n        function D() public pure virtual  override  returns (string memory) {\n        return \"I am Inside C\";\n    }\n}\n```\n---\n24. Events and Indexing in Blockchain Solidity\n\n```solidity\ncontract Events {\n    /*\n        1) In Solidity, events are dispatched signals that smart contracts can fire. When you call events,\n        they cause the arguments to be stored in the transaction's log, which is a special data\n        structure in the blockchain.\n\n        2) Max Indexing Can be of 3 Variable\n\n        3) Placing the “indexed” keyword in front of a parameter name will store it as a topic in the log record.\n        Without the keyword “indexed”, it will be stored as data.\n    */\n\n    event Alert(address Account,string Text, uint Balance);\n\n    // If You Want to Keep Indexing Below is The Method\n    // event Alert(address indexed Account,string indexed Text, uint indexed Balance, uint Extra);\n\n    function SetEvent(uint Val) public {\n        emit Alert(msg.sender, \"Has Total Balance Of : \", Val);\n    }\n}\n```\n---\n25. Multiple Inharitance in Solidity\n\n```solidity\ncontract MultipleInheritence{\n    uint public A;\n\n    constructor(){\n        A=100;\n    }\n\n    function FunA() public {\n\n    }\n}\ncontract Child is MultipleInheritence {\n    uint public B;\n\n    constructor (){\n        A=250;\n        B=500;\n    }\n    function FunB() public {\n\n    }\n}\n\n/*\n    Multiple Inheritence Order Is Frm Less Biased to More Biased.\n    It Canntot Be Child,MultipleInheritence. Dues To It Solve the\n    Diamond Problem and Cannot Have Duplicate Properties of Its\n    Parents. Preference is From Right to Left\n*/\ncontract GrandChild is MultipleInheritence,Child{\n\n}\n```\n---\n26. Overriding in Multiple Inharitance in Solidity\n\n```solidity\ncontract Overriding{\n    uint public A;\n    constructor(){\n        A=1;\n    }\n    function Fun() public pure virtual returns(string memory) {\n        return \"Hello From A\";\n    }\n}\ncontract Child is Overriding{\n    uint public B;\n    constructor(){\n        B=2;\n    }\n    function Fun() public pure override virtual returns(string memory) {\n        return \"Hello From B\";\n    }\n}\ncontract GrandChild is Overriding,Child{\n    uint public C;\n    constructor(){\n        C=3;\n    }\n\n    // Due to Multiple Inheritence We Have to Use override(Child,Overriding). This is Mendatory\n    function Fun() public pure override(Child,Overriding) returns(string memory) {\n        return \"Hello From C\";\n    }\n}\n```\n---\n27. Passing Parameter to Parent Constructor in Multiple Inharitance in Solidity\n\n```solidity\ncontract A{\n    uint public Age;\n    string public Name;\n\n    constructor(uint _Age,string memory _Name){\n        Age=_Age;\n        Name=_Name;\n    }\n}\ncontract B{\n    uint public Salary;\n    string public Adress;\n\n    constructor(uint _Salary,string memory _Adress){\n        Salary=_Salary;\n        Adress=_Adress;\n    }\n}\n\n//Following Are Ways to Passing Parameter to Parent Constructor in Multiple Inharitance\ncontract C is A(20,\"Zubair\"),B(100,\"Lahore\"){\n\n}\n//Order of Execution is A,B,D\ncontract D is A,B{\n    constructor() A(20,\"Zubair\") B(100,\"Lahore\"){\n\n    }\n}\n//Order of Execution is A,B,E\ncontract E is A,B{\n    constructor(uint _Age,string memory _Name,uint _Salary,string memory _Adress) A(_Age+10,_Name) B(_Salary*5,_Adress) {\n\n    }\n}\n//Order of Execution is A,B,F\ncontract F is A(20,\"Talha\"),B{\n    constructor(uint _Salary,string memory _Adress) B(_Salary*2,_Adress) {\n\n    }\n}\n```\n---\n28. Calling Parent Function in Multiple Inharitance in Solidity\n\n```solidity\n// 1) By Direct Calling\n// 2) By Super Method\ncontract A {\n    event Alert(string Message, uint256 Salary);\n\n    function Fun() public virtual {\n        emit Alert(\"A Has Salary : \", 500);\n    }\n}\n\ncontract B is A {\n    function Fun() public override  virtual {\n        emit Alert(\"B Has Salary : \", 600);\n        A.Fun(); // This is Called Direct Calling\n\n        super.Fun(); //This Can Also Call Parent\n    }\n}\ncontract C is A,B {\n    function Fun() public override(A,B) {\n        emit Alert(\"C Has Salary : \", 700);\n\n        //This Will Check From Right to Left\n        super.Fun(); //This Can Also Call Parent\n\n        // This Will Print\n        /*\n        C Has Salary : , 700 //By C Contract\n\n        B Has Salary : , 600 //By B Contract\n        A Has Salary : , 500 //By B Contract\n\n        A Has Salary : , 500 //By A Contract\n        */\n    }\n}\n```\n---\n29. Require in Solidity\n\n```solidity\ncontract Require{\n    /*\n        1) It is Used Fir Error Handling in Function\n        2) Used For Input Validation\n        3) Also Used For Acess Control\n        4) Also Refund Gas if Condition Get False\n        5) If Condition Get Fasle, Changes in State Variables By That Function also Reverted\n        and Statements After Require Wo'nt Be Executed\n    */\n\n    uint public Age=20;\n    address Owner;\n\n    constructor(){\n        Owner=msg.sender;\n    }\n\n    function Check(uint X) public {\n        Age+=10;\n        require(X\u003e5,\"Values is Less \u003c 5\");\n    }\n\n    function OnlyOwner() public {\n        require(Owner==msg.sender,\"Not Owner\");\n        Age-=2;\n    }\n}\n```\n---\n30. Revert and Assert in Solidity\n\n```solidity\ncontract RevertAndAssert{\n    /*\n        1) Work Same as require(), But Condition is Given in IF-ELSE\n        2) Undo Also Works Same as require()\n        3) We Can Define Custom Error in Revert, Higher Error String Cost Higher Gas\n\n        4) To Check Bug and Security of Contract We Use Assert\n        5) If Found Bug Transaction Will Be Stopped\n    */\n\n    uint public Age;\n    address Owner ;\n    constructor(){\n        Owner=msg.sender;\n    }\n\n    error CustomError(string,address); // Used For Custmom Error Handling\n\n    function Revert(uint Amount) public {\n        if(Amount\u003c10){\n            // revert(\"Amount Lesser \u003c10\"); //Cost Hight Gas\n            Age+=5;\n            revert CustomError(\"Amount Lesser \u003c10\",msg.sender); //Cost Low Gass, Custom Error\n        }\n    }\n\n    function Assert() public {\n        assert(Owner==msg.sender);\n        Age+=5;\n    }\n}\n```\n---\n31. Function Modifiers in Solidity\n\n```solidity\ncontract MyContract {\n    /*\n        1) Modifier is Special Type of Function\n        2) Increase Reusability, Decrease Code Repeating\n        3) To Add Function Pre-Requisite\n        4) Can Be Multiple Modifiers in a Contract\n\n    */\n    uint256 public Age = 0;\n\n    modifier RepeatedCode() {\n        for (uint256 I = 0; I \u003c 10; I++) {\n            Age += I;\n        }\n\n        _; //This Means Go Back to Callee Function to Execute Remaining Code\n    }\n    modifier Check(uint256 Val) {\n        require(Age \u003e= Val, \"Less Than 10\");\n        _;\n    }\n\n    function FunA() public RepeatedCode Check(45) {}\n\n    function FunB() public RepeatedCode Check(90) {}\n\n    function FunC() public RepeatedCode Check(135) {}\n}\n```\n---\n32. Payable in Solidity \n\n```solidity\ncontract MyContract{\n    /*\n        1) Payable is Ued to Make Adresses Payable\n        2) With This You Can Send Ethers to Those Adresses To Whom You Make Payable\n        3) If a Function is Payable Then Ethers Will be Added to That Contarct\n        In Which Function is Defined\n        4) Payable Function Can Be Pure and View Type\n        5) If Button Red then Payable, If Yellow Simple, If Blue Then View or Pure of SV;\n        6) A Payble function that can receive Ether and respond to an Ether deposit for\n        record-keeping purposes\n        7) 1Eth=10^18Wei\n    */\n\n    // This State Variable is Payable\n    address payable public Adress=payable(msg.sender); //To Make Adress Payble ,We Have to Typecast\n\n    // This is How to Make Constructor Payble\n    constructor() payable {\n\n    }\n\n    // This Function is Payable\n    function ReceiveEther() payable public returns (string memory)  {\n        return \"Received Amount\";\n    }\n\n    function CheckBalance() public view returns(uint) {\n        return address(this).balance; // Return address(this) Adress of Contract\n    }\n}\n```\n---\n33. Fallback and Receive in Solidity\n\n```solidity\ncontract FallbackAndReceive{\n\n    /*\n        A) Fallback\n\n        1) It is Executed When Non-Existence Func is Called on Contract\n        2) It is Required to Makred External\n        3) It Has No Name, No Argument, Not Return Anything\n        4) Can Be Defined One Per Contract\n        5) If Not Marked Payable, Will Throw Exception If Contarct Receives Ether\n        6) Main Usage is to Directly Send ETH to Contarct\n        7) Can Receive Data and Ether Both\n\n        B) Receive\n\n        1) It is Executed When Non-Existence Func is Called on Contract\n        2) It is Required to Makred External\n        3) It Has No Name, No Argument, Not Return Anything\n        4) Can Be Defined One Per Contract\n        5) If Not Marked Payable, Will Throw Exception If Contarct Receives Ether\n        6) Main Usage is to Directly Send ETH to Contarct\n        7) Can Receive Ether Only. Not Receive Data From Low Level Interaction\n    */\n\n    event Alert(string Text,uint Amount);\n\n    // Use Low Interaction Button For Data Sending and Set Value Section for Sending Amount (Ether)\n\n    /*\n    First Deploy Contract and Then Set Value and Then Put Data in Low Level Interaction and\n    Click Trasact Button.\n\n    */\n    fallback() external payable {\n        emit Alert(\"Fallback) Amount Received Is :\", msg.value);\n    }\n\n    receive() external payable {\n        emit Alert(\"Receive) Amount Received Is :\", msg.value);\n    }\n\n    function CheckBalance() public view returns (uint){\n        return address(this).balance;\n    }\n\n}\n```\n---\n34. Send Ether, Transfer, Call in Solidity\n\n```solidity\ncontract SendETH{\n    /*\n        1) Will Se How to Send Ether To Account or Another Contract\n        2) Three Method a) Send b) Trafser c) Call\n\n        a) Send:- Used to Trafser Ether to Adress of Account od Contarct. In Result Got Bool Value.\n        Has Limit of 2300 Gas. If Gas Cost \u003e2300 Then Transaction Fail and Cost all Gas. Always Use\n        Require With It\n\n        b) Transfer:- Gas Limit of 2300. Nothing Get Returned. Changes in State Variable is Reverted.\n        No Need to Use Required.\n\n        c) Call:- We Ourself Decide Gas Limit. Get Bool Type Returned Value and Bytes (Having Some Data).\n        We Need to Use Require, Because It Dont Revert Changes as Send Function.\n\n        We Generally Use Call and Trafser. Highly Used is Call\n    */\n\n    address payable public Receiver=payable (0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);\n\n    receive() external payable  {\n\n    }\n    function CheckBal() public view returns(uint){\n        return address(this).balance;\n    }\n\n    /*\n    For Below 4 Method We ave to First Transact Few Ethers to Our Contarct\n    Amount First Will Be Saved in This Contract and Then We Choose to Move From contract\n    to Receiver Acouont\n    */\n\n    //Using Send Method\n    function SendContractToAdress() public {\n        bool Ans=Receiver.send(5000);\n        require(Ans,\"Transaction Failed\");\n\n    }\n    //Using Transfer Method\n    function TransferContractToAdress() public {\n        Receiver.transfer(5000);\n    }\n\n    //Using Call Method. Default Gas Limit is 3000000\n    function CallContractToAdress() public {\n        (bool Status,)=Receiver.call{value:500}(\"\");\n\n        require(Status,\"Transfer Failed\");\n    }\n\n    //For Dynamic Adress\n\n    // function CallContractToAdress(address payable Receiver) public {\n    //     (bool Status,)=Receiver.call{value:500}(\"\");\n\n    //     require(Status,\"Transfer Failed\");\n    // }\n\n    // ======================================================\n\n    /*\n    First Deploy, Then Select Value() Amount Ether and Then Call This Function and Input\n    Gette Account Adress\n\n    This is Direct Sending, Amount Move Directly From Function to Getter Account\n    */\n    function DirectContractToAdress(address payable Getter) payable public {\n        (bool Status,)=Getter.call{value:msg.value}(\"\");\n\n        require(Status,\"Transfer Failed\");\n    }\n\n    function DirectContractToContract(address payable Getter) payable public {\n        (bool Status,)=Getter.call{value:msg.value}(\"\");\n\n        require(Status,\"Transfer Failed\");\n    }\n}\n\n/*\nNow This is Illustraction of Sending Amount From Contarct to Contract\nFirst Copies the ADress of Receiver Smart Contarct\n*/\n\ncontract Sample{\n\n    function CheckBal() public view returns(uint){\n        return address(this).balance;\n    }\n\n    function DirectContractToContract(address payable Getter) payable public {\n        (bool Status,)=Getter.call{value:msg.value}(\"\");\n\n        require(Status,\"Transfer Failed\");\n    }\n}\n```\n---\n35. Immutablity in Solidity\n\n```solidity\ncontract Immutablity{\n    /*\n        1) Immutable Transcation Cost Is High Than Constant\n        2) We Can Change Its Value at Two Places 1) Inline 2) At Deploy Time\n        3) Constants Are Initialized at Inline Only\n        4) If We Change Immuatable Variable Values in Any Function, It Wont Change\n\n        5) Cost Wise Order (Hight to Low) :- Simple-\u003eImmutable-\u003eConstant\n    */\n\n    address public immutable OwnerA=msg.sender; //400 Gas\n    address public immutable OwnerB; //356 Gas. This Give Error, Can Be Resolve By Initializing it in Constructor\n\n    // address public constant C; //This Give Error, Asking to Intialize It Inline\n    address public constant OwnerC=address(1); //378 Gas\n    address public OwnerD=address(1); //2555 Gas\n\n    constructor(){\n        OwnerB=msg.sender;\n    }\n}\n```\n---\n36. Storage vs Calldata vs Memory in Solidity\n\n```solidity\ncontract Test {\n    /*\n        ==Storage==\n        1) Storage Holds State Variables\n        2) Inside Blockchain\n        3) Cost High Gas \n        4) Data is Permanent\n\n        ==Memory==\n        1) Memory Hold Local Variable\n        2) Data Not Permanent\n        3) Inside Mmeory or Stack\n        4) Also Cost Gas But Negligible\n        5) Function Inputs or Outputs are Also in Memory\n\n        ==Calldata==\n        1) Aslo Inside Memory\n        2) Cost Low Gas\n        3) Data Inside Calldata Can Never Be Changed\n        4) Function Input Can Be Passed to Another Function as Input. Dont \n        Create Redundency\n    */\n\n    uint256[] public Arr = [1, 2, 3, 4, 5];\n\n    // Data is Copied\n    function Storage() public {\n        uint256[] storage ARRS = Arr;\n        ARRS[1] = 1;\n    }\n\n    //Data is Pointing (Refered)\n    function Memory() public view {\n        uint256[] memory ARRM = Arr;\n        ARRM[1] = 1;\n    }\n\n    function TempMemory(string memory Name, uint256[] memory TempArr) public {\n        /*\n            Memory Type Data Can Be Passed to Memory Type Data, But Cannot \n            be Paased to Calldata\n        */\n\n        TakeDataA(TempArr); //Works Fine\n        // TakeDataB(TempArr); //Will Cause Error\n    }\n\n    function CallData(string calldata Name, uint256[] calldata TempArr) public {\n        /*\n            Calldata Type Data Can Be Passed to Memory as well as Calldata\n            This Will Cause Very Low Gas, Because Calldata to Calldata \n            Does Not Create New Instance in Mmeory\n        */\n        \n        TakeDataA(TempArr); // Works Fine\n        TakeDataB(TempArr); // Works Fine\n    }\n\n    function TakeDataA(uint256[] memory TempArr) public {}\n\n    function TakeDataB(uint256[] calldata TempArr) public {}\n}\n```\n---\n37. Inbuilt Cryptographic Hash Function in Solidity\n\n```solidity\ncontract AllHash{\n    /*\n        Hashes are 1)SHA256, 2) Rimped160 3) Keccak256\n\n        1) SHA256:- Input Bytes , Output Hash 32 Bytes (Hugly Used)\n        1) Ripemd160:- Input Bytes, Ouput 20 Bytes\n        1) Keccak256:- Input Bytes , Output Hash 32 Bytes (Hugly Used)\n\n        These are Used in Cotract Signature and Creating ID\n\n        ABI encodes the given arguments. Arguments can be of any type.\n        It returns the encoded data as bytes \n        \n        ABI encodePACKED the given arguments. Arguments can be of any type.\n        It returns the encoded data as bytes . But Different Byytes From ABI Encode\n        This Also Compress Bytes\n    */\n\n    function KECCAK256(uint X, string memory Name, address Add) pure public returns (bytes32){\n        return keccak256(abi.encode(X,Name,Add));\n        // return keccak256(abi.encodePacked(X,Name,Add));\n    }\n    \n    function SHA56(uint X, string memory Name, address Add) pure public returns (bytes32){\n        return sha256(abi.encode(X,Name,Add));\n        // return sha256(abi.encodePacked(X,Name,Add));\n    }\n    \n    function RIPEMD160(uint X, string memory Name, address Add) pure public returns (bytes32){\n        return ripemd160(abi.encode(X,Name,Add));\n        // return ripemd160(abi.encodePacked(X,Name,Add));\n\n    }\n}\n```\n---\n## Points to Be Noted 🍬\n\u0026nbsp;\n\n## Metamask\n---\n\nMetaMask is a software cryptocurrency wallet used to interact with the Ethereum blockchain. It allows users to access their Ethereum wallet through a browser extension or mobile app, which can then be used to interact with decentralized applications\n\nIt is Used to \n    1) Store Ether\n    2) Send Ether\n    3) Receive Ether\n    4) Run Dapps\n    5) Swap Token\n\nRinkeby Faucet Help us to Generate Fake Ethers, That Has No Value\n\n## Contract Deployement\n---\n\nThere are Various Environments in Remix IDE For Cntract Deployement\n\nJVM) Javascript Virtual Machine\n    1) Transaction Exec in Sandbox\n    2) Own Memory blockchain\n    3) Ideal for Testing\n\nInjected WEB3)\n    1) Deploy a Contract on Ethereum MainNet or TestNet\n\nWeb3 Provider)\n    1) Connect to a remote Node and Ethereum Client\n\n## Info About Setter Function in Solidity\n---\n\n\nWhen We Call a Setter Function It Creates Transaction that Needs to be Mined and Cost Gas Because IT Changes\nthe Blockchain. But No Cost For Getter Function\n\n## Integers in Solidity\n---\n\nInt Vs Uint\n    1) Integers are Signed Numbers\n    2) Uintegers are Unsigned Numbers\n\n    By Default Both Have Value 0\n    \n    3) int Alias to int256\n    4) uint Alias to uint256\n\n    5) int8 to int 256\n    6) uint8 to uint256\n\n    7) int8 =-128 to 127 || uint8=0 to 255\n    8) int16 =-32768 to 32767 || uint16=0 to 65535\n\n    9) Formulaes:\n        int=-2^(n-1) to 2^(n-1)-1\n        uint=0 to 2^n-1\n\n## Trick to Create Dynamic Arrays in Solidity\n---\n\n```solidity\nfunction createDynamicArray() external pure returns (uint256[] memory) {\n        uint256[] memory dynamicArray = new uint256[](1000);\n\n        // Populate the dynamic array with elements\n        dynamicArray[0] = 1;\n        dynamicArray[1] = 2;\n        dynamicArray[2] = 3;\n        dynamicArray[3] = 4;\n        dynamicArray[4] = 5;\n\n        return dynamicArray;\n    }\n```\nIn Fixed Size Bytes Arrays, Bytes Array Are Immutable. It Means We Cannot Change the Value at Any Index\n\n---\n\n\u0026nbsp;\n\n\u003ca href=\"https://github.com/HydraPhyzer\"\u003e![avatar](https://images.weserv.nl/?url=avatars.githubusercontent.com/u/106366894?v=4\u0026h=100\u0026w=100\u0026fit=cover\u0026maxage=7d) \u0026nbsp;\u0026nbsp; \u003c/a\u003e\n\n### This is Me The Maker Of This Solidity Cheatsheet. Dont Forget to Give This Repository a Like 💛. \u0026nbsp;\n\n\n### Feel Free to Start and Clone This Repo. Happy Learning Bye 👋.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhydraphyzer%2Fsolidity-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhydraphyzer%2Fsolidity-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhydraphyzer%2Fsolidity-cheatsheet/lists"}