New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Storage in Solidity: Simple Storage Contract

BaseMon, Oct 9, 2023, 12:00 AM

Write data to storage and leverage data types to store values efficiently. Find out how to use constructors and auto-generated getters to access and manage state variables. Unleash the power of storage in your smart contracts! 💾📝 📚 Head to Base Camp for full courses on smart contract development: https://docs.base.org/base-camp/docs/welcome ✅ Subscribe to our channel here: https://www.youtube.com/@BuildonBase ► Build on Base: https://base.org ► Follow us on Twitter: https://twitter.com/buildonbase ► Join our Discord: https://base.org/discord

Transcript

all right if you remember we said that each contract account has Associated evm code Plus Storage okay so the question is how do we write to the storage okay and the answer is in solidity when we declare variables these variables are stored in the storage how they are a store will depends on their position within the smart contract or in other words the order in which we declare the variables and also the data type okay let's do a simple example let's say we want to store the age of a person okay how do we store the age of a person so the first question we need to ask ourselves is which data type do we want to use u in eight will basically allow us to store values from zero to 255. or to elevate it to the eight minus 1. okay on the other hand you in 256 will give us values from zero to two elevated to the power of 256 minus 1 which is a very very very big number okay so if we want to store the edge of a person I think U and a should be enough since the maximum number of years that a person can live shouldn't be bigger than 255 okay so let's simply say u in eight h okay now let's assign a value to H in solidity the smart contract have something called a Constructor the Constructor is going to be executed when the contract is created so this is a perfect place to initialize values in a contract in this case we are going to say age is equals to 10. all right that means that we are initializing H to the number 10. now everything is compiling properly what we are going to do now is we are going to deploy the contract remember that in remix we can deploy contracts so I'm just gonna click deploy and as you can see the contract was deployed however there is a small problem how can I access the age value the good thing is that solidity has a capability to Auto generate Getters forested variables but the state variable has to be public so we are just gonna put public and let's deploy again let's see this new instance and as you can see now we have the possibility to call age Okay so let's click it and as we can see we are retrieving the value of H okay now let's see how the storage Works in more detail

Automatic transcript — names and jargon may be misspelled.