From 860302e61b96ee2bb1cc04c12582403e263fd49c Mon Sep 17 00:00:00 2001 From: Kira272921 Date: Sat, 26 Feb 2022 11:46:36 +0530 Subject: [PATCH] feat: add more solidity content --- .editorconfig | 9 + .github/workflows/congrats.yml | 12 + .gitignore | 2 + TODO.md | 17 + blog.md | 420 ------------------ .../setting-up-developer-environment.md | 61 +++ contracts/02. Basics of Solidity/Basics.sol | 0 .../basics-of-solidity.md | 0 contracts/03. Hello World/HelloWorld.md | 88 ++++ .../{ => 03. Hello World}/HelloWorld.sol | 4 + .../04. Data Types/01. Integers/Integer.md | 122 +++++ .../04. Data Types/01. Integers/Integer.sol | 35 ++ .../01. Declaring Array/DeclaringArray.sol | 14 + .../01. Declaring Array/declaring-array.md | 0 .../03. Arrays/02. Mapping/Mapping.sol | 3 + .../03. Arrays/02. Mapping/mapping.md | 0 .../01. Assign/Assign.sol} | 6 +- .../03. Array Operations/01. Assign/assign.md | 0 .../02. Length/Length.sol | 13 + .../03. Array Operations/02. Length/length.md | 0 .../03. Array Operations/03. Push/Push.sol | 13 + .../03. Array Operations/03. Push/push.md | 0 .../03. Array Operations/04. Pop/Pop.sol | 13 + .../03. Array Operations/04. Pop/pop.md | 0 contracts/AddingElement.sol | 13 - contracts/ArrayLength.sol | 13 - contracts/RemovingElement.sol | 13 - hardhat.config.js | 10 +- package.json | 33 +- scripts/run.js | 2 +- 30 files changed, 433 insertions(+), 483 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/workflows/congrats.yml create mode 100644 TODO.md delete mode 100644 blog.md create mode 100644 contracts/01. Setting up developer environment/setting-up-developer-environment.md create mode 100644 contracts/02. Basics of Solidity/Basics.sol create mode 100644 contracts/02. Basics of Solidity/basics-of-solidity.md create mode 100644 contracts/03. Hello World/HelloWorld.md rename contracts/{ => 03. Hello World}/HelloWorld.sol (65%) create mode 100644 contracts/04. Data Types/01. Integers/Integer.md create mode 100644 contracts/04. Data Types/01. Integers/Integer.sol create mode 100644 contracts/04. Data Types/03. Arrays/01. Declaring Array/DeclaringArray.sol create mode 100644 contracts/04. Data Types/03. Arrays/01. Declaring Array/declaring-array.md create mode 100644 contracts/04. Data Types/03. Arrays/02. Mapping/Mapping.sol create mode 100644 contracts/04. Data Types/03. Arrays/02. Mapping/mapping.md rename contracts/{AssigningValues.sol => 04. Data Types/03. Arrays/03. Array Operations/01. Assign/Assign.sol} (50%) create mode 100644 contracts/04. Data Types/03. Arrays/03. Array Operations/01. Assign/assign.md create mode 100644 contracts/04. Data Types/03. Arrays/03. Array Operations/02. Length/Length.sol create mode 100644 contracts/04. Data Types/03. Arrays/03. Array Operations/02. Length/length.md create mode 100644 contracts/04. Data Types/03. Arrays/03. Array Operations/03. Push/Push.sol create mode 100644 contracts/04. Data Types/03. Arrays/03. Array Operations/03. Push/push.md create mode 100644 contracts/04. Data Types/03. Arrays/03. Array Operations/04. Pop/Pop.sol create mode 100644 contracts/04. Data Types/03. Arrays/03. Array Operations/04. Pop/pop.md delete mode 100644 contracts/AddingElement.sol delete mode 100644 contracts/ArrayLength.sol delete mode 100644 contracts/RemovingElement.sol diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..af60327 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = crlf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline = false \ No newline at end of file diff --git a/.github/workflows/congrats.yml b/.github/workflows/congrats.yml new file mode 100644 index 0000000..29e0c75 --- /dev/null +++ b/.github/workflows/congrats.yml @@ -0,0 +1,12 @@ +name: Congrats on completing the challenge πŸŽ‰ + +on: + issues: + types: ["submission"] + +jobs: + congrats: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ../../src/packages/github-action/ diff --git a/.gitignore b/.gitignore index 36077f2..a95025f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ typechain #Hardhat files cache artifacts + +.vscode diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..95a56f3 --- /dev/null +++ b/TODO.md @@ -0,0 +1,17 @@ +## TODO + +### Content + +- Basics of Solidity +- Data Types + - Integers + - ~~Math operators~~ + - ~~Comparision operators~~ + - Bit wise operators + - Address + - Arrays + - Array operations + - Structs + - Structs and arrays +- Variables +- Functions diff --git a/blog.md b/blog.md deleted file mode 100644 index 02a779a..0000000 --- a/blog.md +++ /dev/null @@ -1,420 +0,0 @@ -Hey folks πŸ‘€. Hope y'all are doing great. You might have heard about web3 and its hype on Twitter and maybe you wanna explore it as well. Well, so today I am here to give all a quick tour of solidity πŸš€. So let's get started. - -**NOTE**: This blog post isn't a complete tutorial of solidity. You can take this blog post as a quickstart for the journey with solidity and web3. If you are in search of resources, checkout the resources notion page of X Days of Solidity: https://links.kiradev.co/misc/solidity-resources - -![](https://c.tenor.com/Q8VheZPaZM4AAAAM/stocks-amc.gif) - -# πŸ€” What's Solidity? - -Solidity is a programming language for implementing smart contracts. - -Smart contracts are simply programs stored on a blockchain that run when predetermined conditions are met. An advantage of these programs is that you can verify they are what they are and do what they claim they are doing. This allows for a permission-less autonomous ecosystem between untrusted and anonymous stakeholders. - -Solidity is a curly-bracket language with the influence of C++, Python, and JavaScript being visible. - -Enough of theory, now let's dive into solidity πŸš€ - -# πŸ›  Setting up developer environment - -Before working with Solidity, we would have to set up our development environment in such a way that we could local test them πŸš€. - -## πŸ‘€ Magical powers of Hardhat - -We would be using a tool called [Hardhat](https://hardhat.org/). This would create a local Ethereum network and give us fake test ETH and fake test accounts to work with. Sounds interesting right? So what are you waiting for so let’s set up Hardhat πŸš€. - -- Create a new folder for the project - ```bash - mkdir x-days-of-solidity - ``` -- Navigate into that folder - ```bash - cd x-days-of-solidity - ``` -- Initializing the project - ```bash - npm init -y - ``` -- Installing Hardhat - - ```bash - npx hardhat - ``` - - You would be presented with a prompt something like this - - ![](https://imgur.com/uiEFO9U.png) - - Let's choose the first option and press enter for the rest of the options - -- We would have to install a few other dependencies as well - - ```bash - npm install --save-dev chai ethereum-waffle @nomiclabs/hardhat-waffle ethers @nomiclabs/hardhat-ethers - ``` - - [`chai`](https://npmjs.com/package/chai) is a testing library. - - [`ethereum-waffle`](https://npmjs.com/package/ethereum-waffle) is the waffle package by which you can test your smart contracts. - - [`@nomiclabs/hardhat-waffle`](https://npmjs.com/package/@nomiclabs/hardhat-waffle) allows to test smart contracts using waffle and hardhat. - - [`ethers`](https://npmjs.com/package/ethers) is a library for complete Ethereum wallet implementation. - - [`@nomiclabs/hardhat-ethers`](https://npmjs.com/package/@nomiclabs/hardhat-ethers) is a hardhat plugin for ethers. - -# πŸ“ Folder structure - -After you have completed setting up the developer environment you would see a folder structure similar to this - -![](https://imgur.com/A0vtJBO.png) - -- `contracts` is the folder where all our smart contracts would be stored. -- `scripts` is the folder where all scripts such as `run.js` and `deploy.js` would be stored. -- `test` is the folder where all our chai tests would be stored. - -# πŸ‘‹ Hello, World! - -Let's get started by creating a smart contract using solidity that prints "Hello, World!" in the terminal - -![](https://c.tenor.com/cX92mi1p-NYAAAAd/coding-anime.gif) - -First of all let's clean up the default hardhat contract, script, and test files. Let's create a new file called `HelloWorld.sol` inside the `contracts` folder. - -Let's get started by adding a ✨SPDX License Identifier✨. It's just a fancy comment, which tells the license of your smart contract. In my case, I am going to be using the [MIT](https://opensource.org/licenses/MIT) license. - -```solidity -// SPDX-License-Identifier: MIT -``` - -Let's now specify the the version of solidity compiler. As we are using Hardhat, we have to check that the solidity compiler version in the `hardhat.config.js` file is the same as we are specifying in our smart contract - -![](https://imgur.com/NpxKuG4.png) - -```solidity -pragma solidity ^0.8.4; -``` - -As solidity doesn't natively support "console logs" we would have to use the magical powers of Hardhat again. Hardhat allows use to use "console logs" in solidity. - -```solidity -import "hardhat/console.sol" -``` - -Now we have the superpowers to use console log in solidity. This makes debugging much simpler. - -![](https://imgur.com/jxQdvKZ.png) - -Let's now start writing the actual smart contract πŸ‘€. In solidity, the syntax of a smart contract is similar to classes in other programming languages. - -```solidity -contract HelloWorld { - constructor() { - console.log("Hello, World"); - } -} -``` - -We use the `contract` keyword along with the contract name to define a contract. Defining a contract is similar to defining a class in other programming languages like JavaScript. - -Any code inside the `constructor` is run once when the contract is been deployed. - -```solidity -// SPDX-License-Identifier: MIT - -pragma solidity ^0.8.0; - -import "hardhat/console.sol"; - -contract HelloWorld { - constructor() { - console.log("Hello, World"); - } -} -``` - -**TADA** πŸŽ‰! We have written our first smart contract. But πŸ€” How to run it locally? - -# βš™ Running smart contract locally - -We would use creating a script (`run.js`) and run the smart contract using that script and Hardhat. - -Create a new file called `run.js` under the `scripts` folder. - -Let's first get our smart contract, compile it, and generate all the necessary files under the `artifact` folder. - -> `artifact` folder is similar to a `build` folder - -```js -const helloWorldFactory = await hre.ethers.getContractFactory('HelloWorld'); -``` - -Let's now deploy our compiled smart contract to the local blockchain - -```js -const helloWorld = await waveContractFactory.deploy(); -``` - -We will wait until our smart contract has been officially deployed on the local blockchain. - -```js -await waveContract.deployed(); -``` - -We would only see the "Hello, World!" console log only after the smart contract has been officially deployed on the local blockchain because the code inside `constructor` only runs once when the smart contract has been deployed. - -Let's add a console log to the run script which tells the address of the account where our smart contract has been deployed. - -```js -console.log('Contract deployed to:', helloWorld.address); -``` - -Finally, our `run.js` script would look something like this - -```js -const main = async () => { - const helloWorldFactory = await hre.ethers.getContractFactory('HelloWorld'); - const helloWorld = await helloWorldFactory.deploy(); - await helloWorld.deployed(); - console.log('Contract deployed to:', helloWorld.address); -}; - -const runMain = async () => { - try { - await main(); - process.exit(0); - } catch (error) { - console.log(error); - process.exit(1); - } -}; - -runMain(); -``` - -I have added a new `runMain` function that would catch errors while deploying - -Now let's run it using `npx hardhat run scripts/run.js` command - -You would see the output something like this - -![](https://imgur.com/XjP2Dlx.png) - -**TADA** πŸŽ‰! You have created a smart contract and deployed it as well - -Let's talk more about solidity now πŸ€”. - -# πŸ“¦ Data types in Solidity - -## πŸ”’ Integers - -In solidity, there are two types of integers unsigned integers (`uint`) and signed integers (`int`). Unsigned integers are non-negative integers whereas signed integers are negative integers (PS: `**` means to the "to the power of" or "exponent operator" aka `^` ) - -- `uint8` ranges from 0 to 2 \*\* 8 - 1 -- `uint16` ranges from 0 to 2 \*\* 16 - 1 -- `uint256` or `uint` ranges from 0 to 2 \*\* 256 - 1 -- `int8` ranges from -2 ** 8 to 2 ** 8 - 1 -- `int16` ranges from -2 ** 16 to 2 ** 16 - 1 -- `int256` or `int256` ranges from -2 ** 256 to 2 ** 256 - 1 - -## πŸ“ Address - -There is a special data type, "Address". Address is a 20 byte long (size of an ethereum wallet address) data type in solidity. - -## 🏹 Array - -**Syntax**: - -```solidity - [size] = -``` - -In solidity, you can create a fixed (with only a specific length) and dynamic (the length of the array can be anything) - -```solidity -// An array of fixed length of 10 unassigned integers -uint[10] myFixedArray; - -// An array of the dynamic length of strings -string[] myDynamicArray; -``` - -### ✨ Array operations - -Let's now talk a look at the array operation in solidity πŸ‘€ - -1. **Accessing an element from an array**: Accessing an element from an array in solidity is just like in any other programming language - - ```solidity - // SPDX-License-Identifier: MIT - - pragma solidity ^0.8.0; - - import "hardhat/console.sol"; - - contract AccessingElement { - string[3] data = ["hi", "hello", "yo"]; - - constructor() { - console.log(data[1]); - } - } - ``` - - Here are creating a fixed array of name `data` and data type `string` and length of 3. We are trying to console log the second element of the array, which is `hello`. - - Let's try to run it now using `npx hardhat scripts/run.js` - - **NOTE**: You need to change the name of the smart contract which you are compiling in the `scripts/run.js` file. - - **TADA** πŸŽ‰! It worked - - ![](https://imgur.com/BJAdYkw.png) - -2. **Assigning values**: Let's now assign values to an array - - ```solidity - // SPDX-License-Identifier: MIT - - pragma solidity ^0.8.0; - - import "hardhat/console.sol"; - - contract AssigningValues { - string[5] data = ["hi", "hello", "yo"]; - - constructor() { - data[3] = "yo yo"; - console.log(data[3]); - } - } - ``` - - Here we are trying to assign the value of the 4th element of the array as `yo yo`. - - Let's test it out. - - **TADA** πŸŽ‰! It worked - - ![](https://imgur.com/wkZ2QJO.png) - -3. **Length of an array**: Let's now find the length of an array. - - You can use the `length` method to get the length of an array. Similar to how to find the length of an array in JavaScript. - - ```solidity - // SPDX-License-Identifier: MIT - - pragma solidity ^0.8.0; - - import "hardhat/console.sol"; - - contract ArrayLength { - string[3] data = ["hi", "hello", "yo"]; - - constructor() { - console.log(data.length); - } - } - ``` - - Let's test it out - - **TADA** πŸŽ‰ It worked - - ![](https://imgur.com/8zp2weM.png) - -4. **Push**: Push is used to add elements to the end of a dynamic array. - - ```solidity - // SPDX-License-Identifier: MIT - - pragma solidity ^0.8.0; - - import "hardhat/console.sol"; - - contract AddingElement { - string[] data = ["hi", "hello", "yo"]; - - constructor() { - data.push("yo yo"); - } - } - ``` - -5. **Pop**: Pop is used to remove the last element from a dynamic array - - ```solidity - // SPDX-License-Identifier: MIT - - pragma solidity ^0.8.0; - - import "hardhat/console.sol"; - - contract AddingElement { - string[] data = ["hi", "hello", "yo"]; - - constructor() { - data.pop(); - } - } - ``` - -Phew, that was long πŸ˜“ - -## πŸ—œ Structs - -Structs are more complex data types, where you can combine multiple data types to create your own complex data type. - -```solidity -struct myStruct = { - address myAddress; - uint myNumber; - string myName -} -``` - -We can use structs with arrays too! - -```solidity -struct Person = { - string name; - uint age -} - -Person[] personArray; // Creates a dynamic array - -Person james = Person("James", 25) // Creates a new Person - -personArray.push(james); // Adds that new person to the `personArray` array -``` - -# πŸ”€ Variables in Solidity - -**Syntax**: - -```solidity - ; -``` - -## πŸŒ€ Types of variables - -In solidity, there are 3 different types of variables: - -1. `state`: State variables are declared in contract scope. Stored on the blockchain. -2. `local`: Local variables are declared inside functions. Destroyed after execution and not stored on the blockchain. -3. `global`: Global variables are some special variables that can be used globally and give information about the transactions and blockchain properties. - -## ✨ Variable modifiers - -There are 2 variable modifiers for `state` variables - -- `constant`, disallow assignment (except initialization), does not occupy storage slot. -- `immutable`, allows exactly one assignment at construction time and is constant afterward. Is stored in code. - -# πŸ‘‹ The End - -That's the end folks πŸ‘‹. I would be releasing another part on "Solidity Quickstart" on "Functions in Solidity" and "How to deploy the smart contract to a test net", such as [Rinkeby](https://www.rinkeby.io/) - -The code for this entire "Solidity Quickstart" series would be available on my GitHub: - -https://github.com/kira272921/solidity-quickstart diff --git a/contracts/01. Setting up developer environment/setting-up-developer-environment.md b/contracts/01. Setting up developer environment/setting-up-developer-environment.md new file mode 100644 index 0000000..a3638f2 --- /dev/null +++ b/contracts/01. Setting up developer environment/setting-up-developer-environment.md @@ -0,0 +1,61 @@ +Before working with Solidity, we would have to set up our development environment in such a way that we could local test them πŸš€. + +## πŸ‘€ Magical powers of Hardhat + +We would be using a tool called [Hardhat](https://hardhat.org/). This would create a local Ethereum network and give us fake test ETH and fake test accounts to work with. Sounds interesting right ? So what are you waiting so let’s set up hardhat πŸš€. + +- Create a new folder for the project + ```bash + mkdir my-project + ``` +- Navigate into that folder + ```bash + cd my-project + ``` +- Initializing the project + + ```bash + npm init -y + ``` + + - If you get an error saying `bash: npm: command not found` then go ahead and install nodejs from https://nodejs.org + +- Installing Hardhat + + ```bash + npx hardhat + ``` + + You would be presented with a prompt something like this + + ![](https://imgur.com/uiEFO9U.png) + + Let's choose the first option and press enter for the rest of the options + +- We would have to install few other dependencies as well + + ```bash + npm install --save-dev chai ethereum-waffle @nomiclabs/hardhat-waffle ethers @nomiclabs/hardhat-ethers + ``` + + [`chai`](https://npmjs.com/package/chai) is a testing library. + + [`ethereum-waffle`](https://npmjs.com/package/ethereum-waffle) is the waffle package by which you can test your smart contracts. + + [`@nomiclabs/hardhat-waffle`](https://npmjs.com/package/@nomiclabs/hardhat-waffle) allows to test smart contracts using waffle and hardhat. + + [`ethers`](https://npmjs.com/package/ethers) is a library for complete Ethereum wallet implementation. + + [`@nomiclabs/hardhat-ethers`](https://npmjs.com/package/@nomiclabs/hardhat-ethers) is a hardhat plugin for ethers. + + You have successfully setup’ed Hardhat πŸ₯³. + +## Feedback + +Faced any kinds of issues during this section ? Head over to our [discord server](https://discord.gg/d2wSzvZ6nj) and ask it in the [#help](https://discord.com/channels/945288689876082709/945542160814604306) channel. + +πŸ‘€ Don't forgot to share your progress in [#share-your-progress](https://discord.com/channels/945288689876082709/945542086025936947) channel. + +## Challenge + +Go ahead open the generated basic project in your favorite text editor and create an issue using the [Submission]() issue template with the screenshot of the folder structure. diff --git a/contracts/02. Basics of Solidity/Basics.sol b/contracts/02. Basics of Solidity/Basics.sol new file mode 100644 index 0000000..e69de29 diff --git a/contracts/02. Basics of Solidity/basics-of-solidity.md b/contracts/02. Basics of Solidity/basics-of-solidity.md new file mode 100644 index 0000000..e69de29 diff --git a/contracts/03. Hello World/HelloWorld.md b/contracts/03. Hello World/HelloWorld.md new file mode 100644 index 0000000..2954c50 --- /dev/null +++ b/contracts/03. Hello World/HelloWorld.md @@ -0,0 +1,88 @@ +In solidity, we don’t have a in-built `console.log` but hardhat has made one. To use `console.log` in solidity, you would have to first import `hardhat/console.sol`, which gives us the superpowers to use `console.log` in solidity. + +```solidity +// SPDX-License-Identifier: MIT + +pragma solidity "^0.8.4" + +import "hardhat/console.sol" +``` + +Let’s start writing the smart contract now + +```solidity +// SPDX-License-Identifier: MIT + +pragma solidity "^0.8.4" + +import "hardhat/console.sol" + +contract HelloWorld() { + constructor() { + console.log("Hello, World") + } +} +``` + +Now it’s time to run this smart contract. We would have to deploy it to a local blockchain to run it. Head over to `scripts` directory and create a new file called `run.js` + +```jsx +const main = async () => { + const contractFactory = await hre.ethers.getContractFactory("HelloWorld"); + const contract = await contractFactory.deploy(); + await contract.deployed(); + console.log("Contract deployed to:", contract.address); +}; + +const runMain = async () => { + try { + await main(); + process.exit(0); + } catch (error) { + console.log(error); + process.exit(1); + } +}; + +runMain(); +``` + +Let’s understand each the above code line by line + +```jsx +const contractFactory = await hre.ethers.getContractFactory("HelloWorld"); +``` + +This is compiling our smart contract and generates the necessary files under `artifacts` directory + +```jsx +const contract = await contractFactory.deploy(); +``` + +This would deploy our smart contract to the local block chain + +```jsx +await contract.deployed(); +``` + +Waits until then smart contract has been deployed on the local block chain + +To run your smart contract which has been deployed on the local block chain, run the following command + +```bash +npx hardhat run scripts/run.js +``` + +![https://imgur.com/XjP2Dlx.png](https://imgur.com/XjP2Dlx.png) + +πŸ₯³ Congrats on completing your first ever smart contract! + +## Feedback + +Faced any kinds of issues during this section ? Head over to our [discord server](https://discord.gg/d2wSzvZ6nj) and ask it in the [#help](https://discord.com/channels/945288689876082709/945542160814604306) channel. + +πŸ‘€ Don't forgot to share your progress in [#share-your-progress](https://discord.com/channels/945288689876082709/945542086025936947) channel. + +## Challenge + +Create a new smart contract called `GmWagmi.sol` which would console log "gm wagmi!" and submit it by creating an new issue by using the [Submission]() issue template. diff --git a/contracts/HelloWorld.sol b/contracts/03. Hello World/HelloWorld.sol similarity index 65% rename from contracts/HelloWorld.sol rename to contracts/03. Hello World/HelloWorld.sol index 7f0f1ce..c81944d 100644 --- a/contracts/HelloWorld.sol +++ b/contracts/03. Hello World/HelloWorld.sol @@ -9,3 +9,7 @@ contract HelloWorld { console.log("Hello, World"); } } + +// Challenge + +// Create a new smart contract called GmWagmi.sol and console log "gm wagmi!" diff --git a/contracts/04. Data Types/01. Integers/Integer.md b/contracts/04. Data Types/01. Integers/Integer.md new file mode 100644 index 0000000..4281992 --- /dev/null +++ b/contracts/04. Data Types/01. Integers/Integer.md @@ -0,0 +1,122 @@ +# Integers in Solidity + +In solidity, there are two types of integers unsigned integers (`uint`) and signed integers (`int`). Unsigned integers are non-negative integers whereas signed integers are negative integers. + +> PS: `**` means to the "to the power of" aka `^` operator. + +- `uint8` ranges from 0 to 2 \*\* 8 - 1 +- `uint16` ranges from 0 to 2 \*\* 16 - 1 +- `uint256` or `uint` ranges from 0 to 2 \*\* 256 - 1 +- `int8` ranges from -2 ** 8 to 2 ** 8 - 1 +- `int16` ranges from -2 ** 16 to 2 ** 16 - 1 +- `int256` or `int256` ranges from -2 ** 256 to 2 ** 256 - 1 + +Let's create a new contract named `Integer.sol` under the `contracts` folder. + +Let's get started by adding the SPDX License Identifier and mentioning solidity compiler's version + +```solidity +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.12; +``` + +Let's import Hardhat's `console.sol` file as well. By this we can use `console.log` in our smart contract. + +```solidity +import "hardhat/console.sol"; +``` + +Let's create a new contract named `Integer`. + +```solidity +contract Integer { + +} +``` + +Let's now add the unassigned and assigned integers over here. + +```solidity +contract Integer { + uint8 integerA = 12; + uint256 integerB = 345; + + int8 integerC = -12; + int256 integerD = -356; +} +``` + +Let's create a `constructor` where we add all our `console.log`'s + +```solidity +contract Integer { + uint8 integerA = 12; + uint256 integerB = 345; + + int8 integerC = -12; + int256 integerD = -356; + + constructor() { + + } +} +``` + +## Operators + +### Mathematical Operators + +| Operator | Description | +| -------- | ----------- | +| + | Addition | + +You could basic mathematical operations such as addition, subtraction, multiplication, exponentiation, division in solidity. + +```solidity +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.12; + +import "hardhat/console.sol"; + +contract Integer { + uint256 integerA = 12; + uint256 integerB = 24; + + int256 integerC = -12; + int256 integerD = -356; + + constructor() { + // Mathematical Operators + + console.log("Addition: %d", integerA + integerB); // Addition + console.log("Subtraction: %d", integerB - integerA); // Subtraction + console.log("Multiplication: %d", integerA * integerB); // Multiplication + console.log("Division: %d", integerA / integerB); // Division + console.log("Exponential: %d", integerA**integerB); // Exponential + console.log("Modulus: %d", integerA % integerB); // Modulo + + } +} +``` + +**PS**: If you try using the exponential operator on two different types (such as `uint8` and `uint256`) you would get a warning says that: + +``` +The result type of the exponentiation operation is equal to the type of the first operand (uint8) ignoring the (larger) type of the second operand (uint256) which might be unexpected. Silence this warning by either converting the first or the second operand to the type of the other. +``` + +To fix this, change both of the numbers to the same type, so in our case we should make either both `uint8` or `uint256` + +Let's test it out by using the `npx hardhat run scripts/run.js`. + +> **PS**: You could create a script in `package.json` file, if you feel `npx hardhat run scripts/run.js` is too long to type every single time. Go ahead and open the `package.json` file and add a `scripts` array under that you can define your command name and the command, in our case it's `npx hardhat run scripts/run.js`. +> +> So after making your changes the `package.json` should look something like this: +> +> ![](https://imgur.com/d70278x.png) +> +> I have choose `htrun` as the command name, you can any name which is comfortable for you. +> +> Now we can test it by using `npm `, so in my case it would be `npm htrun` diff --git a/contracts/04. Data Types/01. Integers/Integer.sol b/contracts/04. Data Types/01. Integers/Integer.sol new file mode 100644 index 0000000..d004b0d --- /dev/null +++ b/contracts/04. Data Types/01. Integers/Integer.sol @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.12; + +import "hardhat/console.sol"; + +contract Integer { + uint256 integerA = 12; + uint256 integerB = 24; + + int256 integerC = -12; + int256 integerD = -356; + + constructor() { + // Mathematical Operators + + console.log("Addition: %d", integerA + integerB); // Addition + console.log("Subtraction: %d", integerB - integerA); // Subtraction + console.log("Multiplication: %d", integerA * integerB); // Multiplication + console.log("Division: %d", integerA / integerB); // Division + console.log("Exponential: %d", integerA**integerB); // Exponential + console.log("Modulus: %d", integerA % integerB); // Modolu + + // // Comparisons Operator + + console.log("Equality: %s", integerA == integerB); // Equality + console.log("Not equal: %s", integerA != integerB); // Not equal + console.log("Less than or equal to: %s", integerA <= integerB); // Less than or equal to + console.log("Greater than or equal to: %s", integerA >= integerB); // Greater than or equal to + console.log("Less than: %s", integerA < integerB); // Less than + console.log("Greater than: %s", integerA > integerB); // Greater than + + // Bit Operators + } +} diff --git a/contracts/04. Data Types/03. Arrays/01. Declaring Array/DeclaringArray.sol b/contracts/04. Data Types/03. Arrays/01. Declaring Array/DeclaringArray.sol new file mode 100644 index 0000000..a36fa2f --- /dev/null +++ b/contracts/04. Data Types/03. Arrays/01. Declaring Array/DeclaringArray.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.12; + +import "hardhat/console.sol"; + +contract DeclaringArray { + string[] dynamicArray = ["Hi", "Hello"]; + string[2] fixedArray = ["Hi", "Hello"]; + + constructor() { + console.log("Tada you have created a fixed and dynamic array"); + } +} diff --git a/contracts/04. Data Types/03. Arrays/01. Declaring Array/declaring-array.md b/contracts/04. Data Types/03. Arrays/01. Declaring Array/declaring-array.md new file mode 100644 index 0000000..e69de29 diff --git a/contracts/04. Data Types/03. Arrays/02. Mapping/Mapping.sol b/contracts/04. Data Types/03. Arrays/02. Mapping/Mapping.sol new file mode 100644 index 0000000..414f2d5 --- /dev/null +++ b/contracts/04. Data Types/03. Arrays/02. Mapping/Mapping.sol @@ -0,0 +1,3 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.12; diff --git a/contracts/04. Data Types/03. Arrays/02. Mapping/mapping.md b/contracts/04. Data Types/03. Arrays/02. Mapping/mapping.md new file mode 100644 index 0000000..e69de29 diff --git a/contracts/AssigningValues.sol b/contracts/04. Data Types/03. Arrays/03. Array Operations/01. Assign/Assign.sol similarity index 50% rename from contracts/AssigningValues.sol rename to contracts/04. Data Types/03. Arrays/03. Array Operations/01. Assign/Assign.sol index ded6adc..c5a8285 100644 --- a/contracts/AssigningValues.sol +++ b/contracts/04. Data Types/03. Arrays/03. Array Operations/01. Assign/Assign.sol @@ -1,14 +1,14 @@ // SPDX-License-Identifier: MIT -pragma solidity ^0.8.0; +pragma solidity ^0.8.12; import "hardhat/console.sol"; contract AssigningValues { - string[5] data = ["hi", "hello", "yo"]; + string[5] data = ["Hi", "Hello", "Yo"]; constructor() { - data[3] = "yo yo"; + data[3] = "Yo Yo"; // Assigns the value of the fourth element as "Yo Yo" console.log(data[3]); } } diff --git a/contracts/04. Data Types/03. Arrays/03. Array Operations/01. Assign/assign.md b/contracts/04. Data Types/03. Arrays/03. Array Operations/01. Assign/assign.md new file mode 100644 index 0000000..e69de29 diff --git a/contracts/04. Data Types/03. Arrays/03. Array Operations/02. Length/Length.sol b/contracts/04. Data Types/03. Arrays/03. Array Operations/02. Length/Length.sol new file mode 100644 index 0000000..06a150d --- /dev/null +++ b/contracts/04. Data Types/03. Arrays/03. Array Operations/02. Length/Length.sol @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.12; + +import "hardhat/console.sol"; + +contract ArrayLength { + string[3] data = ["Hi", "Hello", "Yo"]; + + constructor() { + console.log(data.length); // This would console log the length of the data array + } +} diff --git a/contracts/04. Data Types/03. Arrays/03. Array Operations/02. Length/length.md b/contracts/04. Data Types/03. Arrays/03. Array Operations/02. Length/length.md new file mode 100644 index 0000000..e69de29 diff --git a/contracts/04. Data Types/03. Arrays/03. Array Operations/03. Push/Push.sol b/contracts/04. Data Types/03. Arrays/03. Array Operations/03. Push/Push.sol new file mode 100644 index 0000000..0878dc1 --- /dev/null +++ b/contracts/04. Data Types/03. Arrays/03. Array Operations/03. Push/Push.sol @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.12; + +import "hardhat/console.sol"; + +contract AddingElement { + string[] data = ["Hi", "Hello", "Yo"]; + + constructor() { + data.push("Yo Yo"); // This adds an element called "Yo Yo" at the end of the array + } +} diff --git a/contracts/04. Data Types/03. Arrays/03. Array Operations/03. Push/push.md b/contracts/04. Data Types/03. Arrays/03. Array Operations/03. Push/push.md new file mode 100644 index 0000000..e69de29 diff --git a/contracts/04. Data Types/03. Arrays/03. Array Operations/04. Pop/Pop.sol b/contracts/04. Data Types/03. Arrays/03. Array Operations/04. Pop/Pop.sol new file mode 100644 index 0000000..55cfee9 --- /dev/null +++ b/contracts/04. Data Types/03. Arrays/03. Array Operations/04. Pop/Pop.sol @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.12; + +import "hardhat/console.sol"; + +contract AddingElement { + string[] data = ["Hi", "Hello", "Yo"]; + + constructor() { + data.pop(); // This would remove the last element from the array, which is "Yo" + } +} diff --git a/contracts/04. Data Types/03. Arrays/03. Array Operations/04. Pop/pop.md b/contracts/04. Data Types/03. Arrays/03. Array Operations/04. Pop/pop.md new file mode 100644 index 0000000..e69de29 diff --git a/contracts/AddingElement.sol b/contracts/AddingElement.sol deleted file mode 100644 index 1d62e59..0000000 --- a/contracts/AddingElement.sol +++ /dev/null @@ -1,13 +0,0 @@ -// SPDX-License-Identifier: MIT - -pragma solidity ^0.8.0; - -import "hardhat/console.sol"; - -contract AddingElement { - string[] data = ["hi", "hello", "yo"]; - - constructor() { - data.push("yo yo"); - } -} diff --git a/contracts/ArrayLength.sol b/contracts/ArrayLength.sol deleted file mode 100644 index 9556153..0000000 --- a/contracts/ArrayLength.sol +++ /dev/null @@ -1,13 +0,0 @@ -// SPDX-License-Identifier: MIT - -pragma solidity ^0.8.0; - -import "hardhat/console.sol"; - -contract ArrayLength { - string[3] data = ["hi", "hello", "yo"]; - - constructor() { - console.log(data.length); - } -} diff --git a/contracts/RemovingElement.sol b/contracts/RemovingElement.sol deleted file mode 100644 index ba2c782..0000000 --- a/contracts/RemovingElement.sol +++ /dev/null @@ -1,13 +0,0 @@ -// SPDX-License-Identifier: MIT - -pragma solidity ^0.8.0; - -import "hardhat/console.sol"; - -contract AddingElement { - string[] data = ["hi", "hello", "yo"]; - - constructor() { - data.pop(); - } -} diff --git a/hardhat.config.js b/hardhat.config.js index 20a3341..18734dd 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -1,16 +1,16 @@ require("@nomiclabs/hardhat-waffle"); task("accounts", "Prints the list of accounts", async (taskArgs, hre) => { - const accounts = await hre.ethers.getSigners(); + const accounts = await hre.ethers.getSigners(); - for (const account of accounts) { - console.log(account.address); - } + for (const account of accounts) { + console.log(account.address); + } }); /** * @type import('hardhat/config').HardhatUserConfig */ module.exports = { - solidity: "0.8.4", + solidity: "0.8.12", }; diff --git a/package.json b/package.json index 83e7eac..bd55a34 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,20 @@ { - "name": "all-about-solidity", - "version": "0.0.1", - "main": "index.js", - "author": "Kira272921", - "license": "MIT", - "dependencies": { - "hardhat": "^2.8.4" - }, - "devDependencies": { - "@nomiclabs/hardhat-ethers": "^2.0.5", - "@nomiclabs/hardhat-waffle": "^2.0.2", - "chai": "^4.3.6", - "ethereum-waffle": "^3.4.0", - "ethers": "^5.5.4" - } + "name": "solidity-quickstart", + "version": "0.0.1", + "main": "index.js", + "author": "Kira272921", + "license": "MIT", + "scripts": { + "htrun": "npx hardhat run scripts/run.js" + }, + "dependencies": { + "hardhat": "^2.8.4" + }, + "devDependencies": { + "@nomiclabs/hardhat-ethers": "^2.0.5", + "@nomiclabs/hardhat-waffle": "^2.0.2", + "chai": "^4.3.6", + "ethereum-waffle": "^3.4.0", + "ethers": "^5.5.4" + } } diff --git a/scripts/run.js b/scripts/run.js index 61cd829..4ca7de3 100644 --- a/scripts/run.js +++ b/scripts/run.js @@ -1,5 +1,5 @@ const main = async () => { - const contractFactory = await hre.ethers.getContractFactory('AddingElement'); + const contractFactory = await hre.ethers.getContractFactory('Integer'); const contract = await contractFactory.deploy(); await contract.deployed(); console.log('Contract deployed to:', contract.address);