{"id":21936078,"url":"https://github.com/learnweb3dao/solidity","last_synced_at":"2025-03-22T14:16:27.134Z","repository":{"id":40475774,"uuid":"437688029","full_name":"LearnWeb3DAO/Solidity","owner":"LearnWeb3DAO","description":null,"archived":false,"fork":false,"pushed_at":"2022-06-14T17:53:57.000Z","size":43,"stargazers_count":7,"open_issues_count":0,"forks_count":12,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-27T13:24:58.472Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/LearnWeb3DAO.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}},"created_at":"2021-12-13T00:20:24.000Z","updated_at":"2023-09-19T08:27:20.000Z","dependencies_parsed_at":"2022-08-09T21:31:14.641Z","dependency_job_id":null,"html_url":"https://github.com/LearnWeb3DAO/Solidity","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LearnWeb3DAO%2FSolidity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LearnWeb3DAO%2FSolidity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LearnWeb3DAO%2FSolidity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LearnWeb3DAO%2FSolidity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LearnWeb3DAO","download_url":"https://codeload.github.com/LearnWeb3DAO/Solidity/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244966524,"owners_count":20539797,"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":[],"created_at":"2024-11-29T01:13:06.431Z","updated_at":"2025-03-22T14:16:27.110Z","avatar_url":"https://github.com/LearnWeb3DAO.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Level 6 - Solidity\n\n![image](https://user-images.githubusercontent.com/16539849/173656651-a46df615-8ec3-43fd-9619-98647a6d2bd2.png)\n\nIn this module you will learn what is Solidity and the basic syntax of the language.\n\n## What is Solidity ?\n\n- Solidity is an object-oriented, high-level language for implementing smart contracts. It is designed to target [Ethereum Virtual Machine(EVM)](https://coinmarketcap.com/alexandria/glossary/ethereum-virtual-machine-evm)\n- It is statically typed, supports inheritance, libraries and complex user-defined types among other features.\n\n\u003cQuiz questionId=\"e7201124-8b84-4e20-99d3-d85189ee1817\" /\u003e\n\n## Building in Solidity\n\n### Initializing smart contracts\n\n```solidity\n// Define the compiler version you would be using\npragma solidity ^0.8.10;\n\n// Start by creating a contract named HelloWorld\ncontract HelloWorld {\n\n}\n```\n\n### Variables and types\n\nThere are 3 types of variables in Solidity\n\n- Local\n  - Declared inside a function and are not stored on blockchain\n- State\n  - Declared outside a function to maintain the state of the smart contract\n  - Stored on the blockchain\n- Global\n  - Provide information about the blockchain. They are injected by the Ethereum Virtual Machine during runtime.\n  - Includes things like transaction sender, block timestamp, block hash, etc.\n  - [Examples of global variables](https://docs.soliditylang.org/en/v0.8.9/units-and-global-variables.html)\n\nThe scope of variables is defined by where they are declared, not their value. Setting a local variable's value to a global variable does not make it a global variable, as it is still only accessible within it's scope.\n\n\u003cQuiz questionId=\"04b4af27-6816-4a2e-9070-16c6e4c783ce\" /\u003e\n\n```solidity\n// Define the compiler version you would be using\npragma solidity ^0.8.10;\n\n// Start by creating a contract named Variables\ncontract Variables {\n    /*\n        ******** State variables **********\n    */\n    /*\n    uint stands for unsigned integer, meaning non negative integers\n    different sizes are available. Eg\n        - uint8   ranges from 0 to 2 ** 8 - 1\n        - uint256 ranges from 0 to 2 ** 256 - 1\n    `public` means that the variable can be accessed internally\n     by the contract and can also be read by the external world\n    */\n    uint8 public u8 = 10;\n    uint public u256 = 600;\n    uint public u = 1230; // uint is an alias for uint256\n\n    /*\n    Negative numbers are allowed for int types. Eg\n    - int256 ranges from -2 ** 255 to 2 ** 255 - 1\n    */\n    int public i = -123; // int is same as int256\n\n    // address stands for an ethereum address\n    address public addr = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;\n\n    // bool stands for boolean\n    bool public defaultBoo1 = false;\n\n    // Default values\n    // Unassigned variables have a default value in Solidity\n    bool public defaultBoo2; // false\n    uint public defaultUint; // 0\n    int public defaultInt; // 0\n    address public defaultAddr; // 0x0000000000000000000000000000000000000000\n\n    function doSomething() public {\n        /*\n        ******** Local variable **********\n        */\n        uint ui = 456;\n\n        /*\n        ******** Global variables **********\n        */\n\n        /*\n            block.timestamp tells us whats the timestamp for the current block\n            msg.sender tells us which address called the doSomething function\n        */\n        uint timestamp = block.timestamp; // Current block timestamp\n        address sender = msg.sender; // address of the caller\n    }\n}\n```\n\n\u003cQuiz questionId=\"2156e5cd-26ac-402d-bbe9-ecaefdd8c87a\" /\u003e\n\u003cQuiz questionId=\"bd68f893-b126-4f54-aa78-2906f2287629\" /\u003e\n\u003cQuiz questionId=\"d934fc15-f88d-4cd7-b903-f268f1c16980\" /\u003e\n\u003cQuiz questionId=\"a531c37c-96f9-4bbd-a80f-893b37a5c94e\" /\u003e\n\n### Functions, Loops and If/Else\n\n```solidity\n// Define the compiler version you would be using\npragma solidity ^0.8.10;\n\n// Start by creating a contract named Conditions\ncontract Conditions {\n    // State variable to store a number\n    uint public num;\n\n    /*\n        Name of the function is set.\n        It takes in a uint and sets the state variable num.\n        It is a declared as a public function meaning\n        it can be called from within the contract and also externally.\n    */\n    function set(uint _num) public {\n        num = _num;\n    }\n\n    /*\n        Name of the function is get.\n        It returns the value of num.\n        It is declared as a view function meaning\n        that the function doesnt change the state of any variable.\n        view functions in solidity do not require gas.\n    */\n    function get() public view returns (uint) {\n        return num;\n    }\n\n    /*\n        Name of the function is foo.\n        It takes in  uint and returns an uint.\n        It compares the value of x using if/else\n    */\n    function foo(uint x) public returns (uint) {\n        if (x \u003c 10) {\n            return 0;\n        } else if (x \u003c 20) {\n            return 1;\n        } else {\n            return 2;\n        }\n    }\n\n    /*\n        Name of the function is loop.\n        It runs a loop till 10\n    */\n    function loop() public {\n        // for loop\n        for (uint i = 0; i \u003c 10; i++) {\n            if (i == 3) {\n                // Skip to next iteration with continue\n                continue;\n            }\n            if (i == 5) {\n                // Exit loop with break\n                break;\n            }\n        }\n    }\n\n\n}\n\n```\n\n\u003cQuiz questionId=\"b9a5620b-7c92-450b-afa8-1cda0dfba54a\" /\u003e\n\u003cQuiz questionId=\"f14bb058-720b-42e5-ba85-4c7bfc1b93ee\" /\u003e\n\n### Arrays, Strings\n\nArray can have a compile-time fixed size or a dynamic size.\n\n```solidity\npragma solidity ^0.8.10;\n\ncontract Array {\n\n    // Declare a string variable which is public\n    string public greet = \"Hello World!\";\n    // Several ways to initialize an array\n    // Arrays initialized here are considered state variables that get stored on the blockchain\n    // These are called storage variables\n    uint[] public arr;\n    uint[] public arr2 = [1, 2, 3];\n    // Fixed sized array, all elements initialize to 0\n    uint[10] public myFixedSizeArr;\n    /*\n        Name of the function is get\n        It gets the value of element stored in an array's index\n    */\n    function get(uint i) public view returns (uint) {\n        return arr[i];\n    }\n\n    /*\n     Solidity can return the entire array.\n     This function gets called with and returns a uint[] memory.\n     memory - the value is stored only in memory, and not on the blockchain\n              it only exists during the time the function is being executed\n\n     Memory variables and Storage variables can be thought of as similar to RAM vs Hard Disk.\n     Memory variables exist temporarily, during function execution, whereas Storage variables\n     are persistent across function calls for the lifetime of the contract.\n     Here the array is only needed for the duration while the function executes and thus is declared as a memory variable\n    */\n    function getArr(uint[] memory _arr) public view returns (uint[] memory) {\n        return _arr;\n    }\n\n     /*\n        This function returns string memory.\n        The reason memory keyword is added is because string internally works as an array\n        Here the string is only needed while the function executes.\n    */\n    function foo() public returns (string memory) {\n        return \"C\";\n    }\n\n    function doStuff(uint i) public {\n        // Append to array\n        // This will increase the array length by 1.\n        arr.push(i);\n        // Remove last element from array\n        // This will decrease the array length by 1\n        arr.pop();\n        // get the length of the array\n        uint length = arr.length;\n        // Delete does not change the array length.\n        // It resets the value at index to it's default value,\n        // in this case it resets the value at index 1 in arr2 to 0\n        uint index = 1;\n        delete arr2[index];\n        // create array in memory, only fixed size can be created\n        uint[] memory a = new uint[](5);\n        // create string in memory\n        string memory hi = \"hi\";\n    }\n\n }\n```\n\n\u003cQuiz questionId=\"0643be02-c57b-41b0-b7e2-9e855883b7c2\" /\u003e\n\u003cQuiz questionId=\"ec82e3aa-ec12-4d8b-8f81-f21ff1fb0a54\" /\u003e\n\u003cQuiz questionId=\"bab86524-5ace-4baa-add4-f66482b51abe\" /\u003e\n\n## References\n\n[Solidity by Example](https://solidity-by-example.org/)\n\n## Resources for learning extra\n\n- [Cryptozombies](https://cryptozombies.io/)\n- [Solidity by Example](https://solidity-by-example.org/)\n- [Solidity docs](https://docs.soliditylang.org/en/v0.8.10/)\n\n## DYOR Questions\n\u003cQuiz questionId=\"685cf7f3-aafe-4199-a40e-06256a9191f9\" /\u003e\n\u003cQuiz questionId=\"36ea5014-f51e-4fb3-8fa2-027062b1c895\" /\u003e\n\n\u003cSubmitQuiz /\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flearnweb3dao%2Fsolidity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flearnweb3dao%2Fsolidity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flearnweb3dao%2Fsolidity/lists"}