LongCut logo

30 days of Solidity | Day 1 | ClickCounter Contract

By The Web3Compass

Summary

Topics Covered

  • Smart Contracts Live Forever Unchanged
  • MIT License Enables Permissionless Reuse
  • Version Pragma Prevents Compiler Breaks
  • Public Auto-Generates Persistent Getters
  • State Changes Incur Gas Fees Forever

Full Transcript

so over 30 days we're going to write 30 smart contracts and um yeah this is day [Applause] I am sneha from web3 compus and this is day one of 30 days of building with solidity um

and also my first YouTube video so spam me the horor if I fumble a lot uh for everyone who is a part of the campaign you are already aware uh of what this campaign is about for those who have just stumbled upon this video randomly let me tell you that 30 days of building with

solidity is a campaign for Builders who are looking to get started with web3 journey in general and what better than learning with solidity right so over 30 days we're going to write 30 smart contracts and um yeah this is day one so if you go on the website web3 compass

XYZ um and click on day one you will see that the first smart contract that we're going to write is Click counter. Soul the concepts that you will Master are basic solidity syntax variables increment decrement functions and if you look at a example application it is more of a simple

counter contract to learn variable declaration function Creation in basic arithmetic process um in short I would just say that imagine a website that just has a button and every time you click on it there's a counter that increases by one so we're not going to go forward with a front-end

development or how that integration Works we're just going to implement this in solidity so let's just get started okay so before we get started with what we're going to do um we will head to remix IDE it's an online platform where you can write your solidity smart contract test

it and deploy it now we're using this so that you don't have to go through the hassle of doing local deployments downloading a th000 uh tools I mean not a thousand but yeah still like installations are scary come on so yeah uh and um in my previous recording I had already like created a file so I

did not delete it so this file is going to be called as click counter. soul and um for every everyone you might have just guessed that do soul is the extension that we use for a solidity file okay now let's get started right let's start uh writing our smart contract so everyone who is

new to the concept A smart contract is basically a sample computer program which is stored on the blockchain once it's out there it's there just forever okay running exactly as it is written

no edits no dels and um it follows the concept of decentralization where you know no no Central Authority is actually controlling it once it's out there anybody can just trust the code uh

without relying on other people no middleman no misunderstandings just you know clear code and everything's just implemented out there um so yeah let's get started now this is our solidity fact and the first that um any solidity code that you open the first line that you're going to see is a

spdx license identifier um I will write MIT you can also write uh oops my caps and I don't know

yeah so now what is this okay now this in a solidity file is called license identifier it clearly tells anyone using your contract that how are they allowed to use it in our case because we are writing it as MIT means anyone is free to use change or share the code without asking for any

permission so it's open source it also means that we are not not responsible if someone runs into an issue using our code um and it helps everyone stay very clear on what's allowed especially um

especially since smart contracts are public okay now okay next line now you can see that the autop pilot is already looking at um AI tools making our lives easier uh it already gives us the next set of you know uh contract so I'll just still type it so that I have the practice of it and not make a

lot of mistakes because now I'm like multitasking doing a thousand things at the same time recording a video Blah Blah Blah okay so pragma solidity um 0.8.0 now there's a I call it as an exponentiation symbol I don't know the real name of it if that's not the real name now what does it say so this

tells solidity exactly which version of its compiler the contract was designed for but why do we need that okay because blockchain is a very very not very new okay but still in a early stage solidity as a programming language under goes a lot of updates okay sometimes those updates

change how the language works so if you're are specifying a version we ensure that our contract will use versions Clos go to 0.8.0 uh but never something totally different okay and with this exponentiation symbol we saying that hey any version which is above 0.8.0 is also applicable

um so yeah that's that's what it does now what do we do next okay so these two are mandatory lines in the code um if you do not like write the license line you will get a warning so you have to

make sure that you write it now we'll get started with the contract okay uh okay contract now what are we trying to write since we've called it the click counter we will just say click counter okay so the contract keyword is going to tell the compiler that hey the contract has just started

so it officially marks the beginning of your smart contract everything that you're going to write between these brackets become a part of the click counter contract and once deployed it lives forever on the blockchain so it is important to carefully check your work first first okay now

okay if I tell you that hey how are you going to implement it in the back end like if I don't tell you that how the front end is going to look like or if I just give you a design of it that okay a front-end developer is you know making this button and all that I want is that every time somebody

clicks that button the counter just increases by one okay so what is it that you would do okay so for me I would say okay let's just have a counter variable which would store you know the coun ofit and then maybe create a create a button create a function that would Implement that every time a

user clicks on it the counter increases right so we'll just hypothetically imagine that there is a button um and see how we can do that with solidity okay so we say Okay uint

256 public counter okay now what does this mean so uint 256 stands for unsigned integer which

means it's a positive number including zero and public here means that anyone can easily view its current value anytime okay because it is stored on the blockchain the value will not disappear when someone closes or refreshes the browser and there's a very interesting angle to it which

I will show if you go to the solidity compiler and compile the code as of now you've been doing everything right okay so that is why you do not have an error now let's try to deploy it and see what happens now if if you go here you will see that oh there's a button right um and maybe try

removing this and um compile again no errors and then try deploy again and you will see oh okay right there's there's nothing here so the public generally sets a getter function by default so

you will be able to call this particular variable to fetch the value uh again the getter function aspect uh so we'll put the public back okay and let's go okay so now that we've declared a counter

variable we need to write a function that would perform the particular operation that every time that function is called that means a button is clicked the counter increases by one okay so for

function in solidity we use function keyword and let's name name it click okay now what happens uh imagine that we don't know what public is now you will see that oh we're getting a warning here

which says no visibility specified did you intend to add public okay so whenever you're writing a function here in solidity you're supposed to add a visibility modifier which tells the compiler that hey you know this is where this particular function can be addressed there many more uh which

we are going to talk about towards the uh you know later days in the campaign so we'll write public and we'll just say Okay counter Plus+ okay now um so this counter because we've declared it outside the function it is accessible anywhere in the function and this is called a state variable that

means that the value is always going to be there on the blockchain now there are other Concepts of local variables and Global variables but that is a later discussion um a lot more Concepts but let's not get overwhelmed and see what happens here okay is there anything that else we have

to do now we compile it and everything's working fine so I'll just deploy it again and it says okay let's see now because we've created this function we say Okay click and when you click it there's a transaction that's being recorded here and boom you see that oh it went through because there's

a green check and then if you say counter it says one okay if you would have fetched it previously the counter would be zero again you click it cter 2 3 4 5 6 let's see what is the value of six okay so that's it okay so this simple function adds one to our counter every time it is called and public

means obviously anyone can use this counter Plus+ means that we adding one to the current value now every click increases the counter permanently on the blockchain and since it changes data there's a tiny fee called gas every time it is used um it's like a like button each click counts okay

so how is it all working together uh but before that the gas thing that I was telling you about uh um yeah so you will see that oh the transaction cost was this execution cost was this and the total gas was this okay so yeah that is the fee that we are talking about every transaction will

need a cost now how is it all working together is that if you look at the Smart contract as a whole you deploy the contract onto the blockchain initially the counter is set to zero each time

time someone calls click the counter goes up by one anyone can check the current value at any moment and every interaction is recorded permanently on the blockchain now how do we make it even better huh so I hope first that all of these concepts are clear once you're

done with this what you can do is in order to play around with the smart contract you can just add a reset button to bring the counter back to zero create cre a degrease button to lower the counter value um but yeah that's it that's it for day one um with this tiny counter contract you're

now familiar with essential solidity Concepts like variables functions and permanent storage like I mentioned State variable um and you'll soon be building even cooler D apps so see you

Loading...

Loading video analysis...