30 days of Solidity | Day 2 | SaveMyName Contract
By The Web3Compass
Summary
## Key takeaways - **State variables store permanently**: We declare string name and string bio as state variables that means that the data is going to be permanently stored on the blockchain. State variables are by default marked as internal, which means that other contracts can just peek in and read them directly. [04:16], [04:54] - **Memory for temporary inputs**: This memory keyword is by default how strings are stored just it's you can just think of it as a whiteboard that you write stuff down use it and erase it when you're done so this is temporary storage. Function inputs are stored in memory temporarily. [06:53], [16:06] - **View functions read for free**: With this view keyword okay what if I had to make a very smart guess I would say maybe the we're just telling the compiler that, oh, I don't want to change anything here. I just want to look at the function. And because it is not, you know, modifying anything on the blockchain, calling this function is free. No gas fee is required. [10:09], [10:27] - **Underscore differentiates parameters**: This underscore that we are using is a naming convention. You could have had any name written here, okay, string, memory, username, user bio, but as part of the convention and ease of programming, what we do is that whatever variable name that we have taken, we just add an underscore to it to have a differentiation that hey, This is not the state variable that we are talking about. [06:01], [06:23] - **Combined function costs gas always**: This function takes an input, saves it permanently and returns it immediately. Very efficient, right? But no, kind of. The trade off here is that this function changes the blockchain, like the data on the blockchain. That means it costs gas every time you call it, even if you're just using it to view the data. [15:08], [15:28]
Topics Covered
- State Variables Store Permanently
- Memory Enables Temporary Storage
- AI Autocomplete Fails Solidity
- View Functions Read for Free
- Separate Functions Optimize Gas
Full Transcript
But the AI suggests view returns. Okay, yeah, let's just write it. AI can't be wrong. Oh no, it can be.
it. AI can't be wrong. Oh no, it can be.
This is Sneha from Web3 Compass and this is day two of 30 days building with Solidity. I hope everyone is done with day one because it was a very...
with Solidity. I hope everyone is done with day one because it was a very...
cute little smart contract that we wrote. For people who have just stumbled upon this video randomly and you don't know what I'm talking about, just head over to the first video to get an idea of what this is. For day two, we have a smart contract called savemyname.sol and the concepts that you will master are state variables
and how to store and retrieve data through the smart contracts. We're
basically going to talk about a contract that stores and retrieves a user's name and status, teaching basic data storage. For example, if you have Instagram, right? So users have a profile, they have a bio. So you can just store
right? So users have a profile, they have a bio. So you can just store and retrieve their names and any information around it. So yeah, let's head over to Remix to actually get started. This is my workspace, 30 days building with Solidity. We quickly create a new file called save
with Solidity. We quickly create a new file called save my name.sewell. I really love the camel casing
my name.sewell. I really love the camel casing thing. It just, I don't know, I really like the style of naming files.
thing. It just, I don't know, I really like the style of naming files.
What happened?
is working fine why did I not yeah so now I have the space to write okay so I'll quickly write the SPDX license identifier thing because it is extremely mandatory and I hope everyone knows what this means if not that means you should just
go to the first video to hear me talk about it why do I do this every time just forget to switch off my caps lock button anyway so yeah pragma solidity that's the version and we will tell the com compiler that the contract has started and we will just go by contract
save my name okay so now in the last contract that we talked about we would just we just built a fun little contract called click counter and we stored and updated a number on the blockchain very very simple stuff got comfy with state variables a little but we'll talk about it more
now today we are going to store text real words on the blockchain maybe usernames and bios now why do we want to do this is because Once you're done learning all of this, you'll definitely be building real world applications and those real world applications would involve user data. Profile Infos,
BIOS, tags, all of that, all of that good stuff, right? And by the end of this video, you will be able to understand how to store strings, what memory means in solidity, how return works and when to use the view keyword and all of that. So as of now, with whatever I've told you, I hope it's pretty
of that. So as of now, with whatever I've told you, I hope it's pretty clear that we're sticking to name and bio as the data and both of them are going to be in string format. So what is it that we already know?
We know that we have to declare a variable that takes string name and then it takes string bio, right? That's the text-based format. For
anyone who knows programming, even if... you don't yeah we just know that that is going to be the string type that we're going to use right and we're declaring it as state variables that means that the data is going to be permanently stored on the blockchain we will also see that what are the other ways to
store data um temporary storage in the memory maybe Something like that. Okay,
so let's just move forward. I hope everything is clear here with the name and bio thing because we have not really done anything exceptional here.
Also, you should know that the state variables are by default marked as internal, which means that other contracts can just peek in and read them directly. We'll talk about more of such variables visibility specifiers later while we proceed with the session you know in the next set of
videos but we'll just move forward okay now that you have these two variables how do we go about adding that data right so we'll just quickly write a very difficult function called add and say hey the user will give you two options okay sorry yeah two options two inputs one would be name which
is string type and one would be the bio okay and with every function we want it to be public because we want others to be able to you know see it so that is why we have the public keyword here now what happens okay too confusing right like okay,
string is fine, name is fine, why do we have an underscore and what is this memory keyword that we are saying, right? So this underscore that we are using is a naming convention. You could have had any name written here, okay, string, memory, username, user bio, but as part of the convention and ease of programming, what we
do is that whatever variable name that we have taken, we just add an underscore to it to have a differentiation that hey, This is not the state variable that we are talking about. This is just a parameter that we are accepting for the user to provide an input. And we would just assign it to the variable
that we are going to work with further. And as this AI thing suggests that name is equals to underscore name and bio is underscore bio. That is one thing.
Secondly, what is this memory keyword that we are talking about? So this memory keyword is by default how strings are stored just it's you can just think of it as a whiteboard that you write stuff down use it and erase it when you're done so this is temporary storage okay now
there are certain concepts that we'll talk later that how writing directly data on the blockchain using state variables is a very um costly process and costs more gas. So that is why how we'll just go with memory temporary storage and just, you know, get all that data. So I hope it's
clear. Just a very simple function. Now I'll compile this. No errors. Yay.
clear. Just a very simple function. Now I'll compile this. No errors. Yay.
Now you deploy it and you will be able to see that there is an add function and it says underscore name underscore bio. So I can just say Sneha.
a blockchain developer okay and just okay just transact it right now there is a terminal that opens up and it says okay what is this Yeah, so you have the gas fee, you know that since we're using the resources on the blockchain, every transaction is going to cost something.
So that is the gas fee here. But in the decoded input, it says string underscore names me has string underscore bio blockchain developer. Good job. Now you can actually store it. But wait, there's no function that can actually retrieve the data what do
store it. But wait, there's no function that can actually retrieve the data what do we do about it okay let's create another very difficult function which is function retrieve okay now this is going to be a little different than our usual because this is just us retrieving data so we don't have to pass any function parameters here
and we'll keep it public obviously others should be able to see it but the ai suggests view returns Okay, yeah, let's just write it. A can't be wrong.
Oh no, it can be right. But yeah, let's just write it. Now with returns keyword, I think it's pretty much clear that we're going to you know have values return whatever we have taken here so that is why we have to specify and tell the function that oh I'm expecting a output from this function and what is
the kind of output so it would be string memory so every time you use the returns keyword you have to specify the data type of the output that you are expecting okay so because both of us both the variables here are of string type we will say okay it's string and memory again in
solidity you will always see string and memory together again by default temporary storage here we're not using it we want to show you how the entire state variable thing works and with this view keyword okay what if I had to make a very smart guess I would say maybe the we're just telling the
compiler that, oh, I don't want to change anything here. I just want to look at the function. I've already provided you the data, but I'm just expecting an output to come out of here. So let me just view it. Right. And that's what exactly what it does. So it tells Solidity that, hey, I'm just looking, not changing anything. And because it is not, you know, modifying anything on the blockchain, calling this
anything. And because it is not, you know, modifying anything on the blockchain, calling this function is free. No gas fee is required. OK, now, it says okay return name comma bio okay now for people if you're thinking that oh why did we not use the underscore variable
name it's because this one has a global scope. It is declared outside the function, whereas these variables are limited to the function. So once this function closes, these variables are lost, right? So when you have to return it, you have to return it through the state variables that we are trying to call. And now we
will see what happens. Compile it again and deploy it and see that, okay, this one yeah so now okay wait I'll show you deploy it it goes through and now apart from the add function there's also a retrieve function and again
Sneha developer relations engineer forget blockchain I love this role more so you transact it and In here in the terminal you can see the name and the bio and now once you click on the retrieve you say okay string one is Sneha and string two is Developer
Relations Engineer. If you would have changed the arrangement of this bio comma name then
Relations Engineer. If you would have changed the arrangement of this bio comma name then it would come like that. Now what is it that what if we don't want to write like two different functions and just one function that's going to you know write or do all of it. So we can just say okay um
maybe just show you like this function save and retrieve okay and then just go about string memory name string memory view oh sorry bio okay and we say okay public
and returns okay so this function itself is going to return the values and we say okay string memory is the data type that we are expecting the output to have and string memory is another you know for another value we have another string memory and we say okay name equals to underscore
name sorry this has to be an equals to bio equals to underscore bio okay and then we say okay return name comma bio right and just try to compile
it again and deploy it so we'll just close this one and deploy it again and you will see this one is the one that we had deployed and add is fine okay we will just say okay let's give try giving two different outputs okay blockchain developer
okay and we say Sneha DevRel Engineer okay now I transact this one so it goes through with the input I transact this one as well but what happens here here okay and with retrieve it says devrel engineer
so what did we do because this one was transacted later like this went through later this name and bio this is the this is output that it written Yeah, okay. So we do a transaction, we try to retrieve it and then we do a transaction here and
try to retrieve it again. Right? So it changes like the return value according to how you have deployed it. Now what is the difference here? Why did we not like write just one function and save our time? Right? So this function takes an input, saves it permanently and returns it immediately. Very efficient, right? But no, kind of.
The trade off here is that this function changes the blockchain, like the data on the blockchain. That means it costs gas every time you call it, even if you're
the blockchain. That means it costs gas every time you call it, even if you're just using it to view the data. Okay. And if you compare that to the retrieve function, which is totally free because it does not make any changes. So while
you're combining everything into one function, which seems cleaner, it is not always cheaper. And
it is very important while writing a smart contract that you Know that how should you optimize gas and how not to write very expensive functions. Okay. So I
think that's it in terms of this contract. We've talked about a lot of things.
So you can take it slow and it just gets. more lengthier from here.
So we would just do a quick recap that we use string to store text like names and bios state variables to store data permanently on the blockchain. Function inputs
are stored in memory temporarily and the view keyword is your friend when you just want to read data for free. OK, and you're telling it that, hey, I'm not I'm not playing with the data, not changing anything. And you can definitely combine storage and retrieval into one function, but it will always cost gas. Now, what is it that we can do? next here is that maybe you could keep building add more
variables like age profession location right functions to update just one part of the profile play around and i will see you on day three till then happy building um yeah
Loading video analysis...