--- title: Solidity Introduction tags: solidity description: Use `{%hackmd theme-dark %}` syntax to include this theme. --- <style> html, body, .ui-content { background-color: #333; color: #ddd; } .markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4, .markdown-body h5, .markdown-body h6 { color: #ddd; } .markdown-body h1, .markdown-body h2 { border-bottom-color: #ffffff69; } .markdown-body h1 .octicon-link, .markdown-body h2 .octicon-link, .markdown-body h3 .octicon-link, .markdown-body h4 .octicon-link, .markdown-body h5 .octicon-link, .markdown-body h6 .octicon-link { color: #fff; } .markdown-body img { background-color: transparent; } .ui-toc-dropdown .nav>.active:focus>a, .ui-toc-dropdown .nav>.active:hover>a, .ui-toc-dropdown .nav>.active>a { color: white; border-left: 2px solid white; } .expand-toggle:hover, .expand-toggle:focus, .back-to-top:hover, .back-to-top:focus, .go-to-bottom:hover, .go-to-bottom:focus { color: white; } .ui-toc-dropdown { background-color: #333; } .ui-toc-label.btn { background-color: #191919; color: white; } .ui-toc-dropdown .nav>li>a:focus, .ui-toc-dropdown .nav>li>a:hover { color: white; border-left: 1px solid white; } .markdown-body blockquote { color: #bcbcbc; } .markdown-body table tr { background-color: #5f5f5f; } .markdown-body table tr:nth-child(2n) { background-color: #4f4f4f; } .markdown-body code, .markdown-body tt { color: #eee; background-color: rgba(230, 230, 230, 0.36); } a, .open-files-container li.selected a { color: #5EB7E0; } </style> # Solidity : Introduction In this module we will go over what Solidity is and the basic syntax of Solidity. ![solidity](https://i.imgur.com/JCjletB.png) Solidity is a high-level object-oriented programming language to implement smart contracts which is targeted for Ethereum Virtual Machine(EVM). With solidity we can create self-executing contracts which governs the behavior of accounts within Ethereum State. --- ### Getting Started **Structure of a Contract:** Here's a sample smart contract structure: ```javascript //SPDX-License-Identifier: MIT pragma solidity^0.8.17 contract HelloWorld{ string public myString = "hello world"; } ``` We need the license or it will give errors. `pragma solidity ^0.8.17` indicates that the version is greater than or equal to 0.8.17. We used a string varibale to write "Hello World" and here public menas having read access to the variable after deploying the contract. --- ## Variables There are 3 types of variable in Solidity: local, state and global variables. **Local Variables:** These variables are defined and used within a function and can only exist within the function. These functions are automatically created and destroyed when the function gets called and returned. ```javascript function add(uint x, uint y) public pure returns (uint) { uint music = x + y; return music; } ``` here, the variable `music1 is a local variable and can only be accessed within the `add` function. --- **State Variables:** State variables are declared outside of the function to maintain the state of the smart contract and also stored in the contract storage. State variables are public by default and accessible by all functions within the contract. ```javascript contract MyContract { uint256 public totalSupply; mapping(address => uint256) public balances; } ``` here,`totalSupply` and `balances` are state variables. **Global Variables:** Global variables are injected to virtual machine during runtime and mostly contains information about the blockchain. --- ## Values and References Data types in solidity can be classified as two types. - values - references **Value Types** Value means that the data stores a value. Boolean stores true or false. Int stores : -1, 0, 1. **References** References data types don't store values, instead they store references where the actual value is. Array type of type references will store where actual array elements are stored. ## Data Types: **Signed Integer**: The `int` data types used to define positive or negative integers. ```javascript int public i = -5; ``` **Unsigned Integer :** The `uint` data type are used to define unsingned(positive) integer. The number has to be greater than or equal to 0. No negative numbers. ```javascript uint public i = 327; //uint = uint256 0 to 1**256-1 ``` **Booleans:** This data type is used to define true or false values. ```javascript bool public isValid = true; ``` **Address:** The `address` data type comes as 20 byte size to store ethereum address. ```javascript address public addr = 0xAE5679C84371d53c7e2BB2870f9d00e92D506E0c; ``` **Bytes32:** The "bytes" keyword allows to define a byte array of any size. We will use it during cryptographic hash function kechak256; ```javascript bytes32 myHash = keccak256("example data"); ``` **Arrays:** Arrays are collections of variable of the same type and can be declared and used. ![array](https://i.imgur.com/haeBfRl.png) The type of the array elements and the length of the array must be specified. There are two types of Arrays. - Dynamic - Fixed Size **Dynamic Array :** The size of the array can be changed. **Fixed Array :** The size of the array can't be changed. ![arraytypes](https://i.imgur.com/BSCDJvb.png) **Function:** In Solidity, functions are used to define the behavior of a smart contract. Functions are defined with the "function" keyword and can take input parameters and return values. ```javascript! function add(uint x, uint y) public pure returns (uint) { uint music = x + y; return music; } ``` **Mapping:** In solidity, to store a collection of data we can either use Array or mapping. Mapping allows us to save the data/value at the key and later we can get the key. In other words, mapping is the fastest way to get any value or data. ![mapping](https://i.imgur.com/oo6OrVL.png) Struct: When we need to use a bit more complicated data type with multiple properties, then we can use struct. It allows us to define a custom data type with a set of properties. ![structtype](https://i.imgur.com/efxL7Ae.png) ```javascript //SPDX-License-Identifier:MIT pragma solidity ^0.8.17 contract Structs { Struct Car { string model; uint year; address owner; } Car Public car; } ``` Enum : Boolean lets you pick two choices between true and false but what if you need to pick more choices. Then enum is a perfect data type for this. ```javascript! //SPDX-License-Identifier:MIT pragma solidity ^0.8.17 contract Purchase { enum State {Created, Locked, Inactive} } ``` --- ![soliditywizard](https://i.imgur.com/S62Uu13.png)