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

Loading player…

Mappings in Solidity

BaseMon, Oct 9, 2023, 12:00 AM

Mapping data structures in Solidity 🗺️📚 Learn how to efficiently store and retrieve data using mappings, creating dynamic tables with addresses as keys and ages as values. 📚 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

in this exercise we will learn how to store items in a mapping data structure let's copy the license and the pragma from the other contract and let's simply write contract Advance storage so what we are going to do is we are gonna create a function called register that is basically going to allow anybody to register by providing their address and H okay so let's get started how do we do this the first thing we need to do is simply create a function that we're gonna call register there is going to be external since there is no other function calling it and then we could let's say return a simple Boolean to tell them that the registration was successful we were saying that users need to provide their address which we can call user address and their age and if you remember we are using u in 8 for the h all right now the problem is how do we store this information because this information looks like it's gonna look like a big table right where we have the address of the first person then the age let's say a number and this is going to continue address 2 and then another number for the age solidity has a data type for that and is called a mapping if we go to the documentation we can see the mapping types have the syntax mapping where we specify a key and a value so in our case for the key we can use the address and the value can be the age okay all right so let's declare here and let's do mapping of address and unit called edges and now we say edges user address is going to be equals to H okay and in this way we are assigning to the edges mapping we are telling to the mapping to use the user address as key and then the value will be the age that we are passing now we need to return simply let's say a return true and everything is compiling properly Let's test in order to test we need to deploy the contract okay and now we have the register function so we can pass and address an age so here we had some sample addresses let's just copy one I'm gonna put it here and then I'm gonna specify the value 10. and I'm gonna say transact okay the transaction was successful and as you can see is even returning true and this is what we are sending okay now there are a couple of things to that we can do to improve one of them is we don't have a way to access the values edges and how can we access these values if you remember we can take advantage of the capability of solidity to Auto generate Getters so we can simply say public and let's deploy again okay and now we have access to edges okay so if I ask for the age of this address is going to tell me is zero because it's the default value for you in so let's register that person and we are gonna pass 10. now the transaction was successful let's call edges and now I'm getting the value of 10 because that person is already registered

Automatic transcript — names and jargon may be misspelled.