Check out a free preview of the full A Tour of Web 3: Ethereum & Smart Contracts with Solidity course
The "Game Contract Storage" Lesson is part of the full, A Tour of Web 3: Ethereum & Smart Contracts with Solidity course featured in this preview video. Here's what you'd learn in this lesson:
ThePrimeagen uses a mapping to store the heroes generated by the user. The mapping uses the address of the user as the key. The address will point to a list of uint values.
Transcript from the "Game Contract Storage" Lesson
[00:00:00]
>> So, let's go back and let's keep writing this nice beautiful contract, right? So what's the next thing we have to do. I should be able to get my heroes that I have generated, all right? So how are we gonna store these what is a good day data structure.
[00:00:14]
Does anyone in here want to yell out one that I've mentioned before a few times?
>> [INAUDIBLE]
>> Bo mappings awesome. I love to hear that. So we're gonna go like this mapping. We're gonna take an address. Remember that's the person who called the function. So we're gonna map an address to a list of Uns.
[00:00:35]
These are gonna be of course, an array of UNs these are a dynamic array that I can push, pop and remove from. And of course we need to give a name so I like to do this address to address two heroes. Curios actually is how you say it, and there we go.
[00:00:50]
So now we have this, I tend to name these things with their type in it. Even though I shouldn't really have to do that, but it's just how I always kinda do things when it comes to ether cuz the LSP just isn't quite that great and I don't get enough hints.
[00:01:04]
So I put kinda some basic type information in here. So there you go, we now have a mapping from address to an array of heroes. Now notice I'm gonna store our heroes. What this mean is I'm storing our heroes. In 256 bits, right? Right, so now let's create a function called Get heroes where it's gonna be a public function that's gonna be a view because it's only reading data and it's gonna returns a you int array.
[00:01:35]
And now we have to use this memory keyword again. Effectively memory refers to memory that's created within a function. If I were to use the term storage there, that means memory that is stored on the contract. We actually technically can't use the term storage right here. But we're just gonna use the term memory because it requires to say where it's coming from.
[00:01:57]
It's not just gonna do a return us my address two heroes to look it up. Address two heroes message dot sender. There we go. So I'm just gonna look in this location. If there is an array there, it will properly return out an array. Else it's gonna return out a zero which will be converted by ethers and be an array of zero.
[00:02:17]
So it's kinda fantastic, it does all the right things. It has nothing in there. And so there we go. We now have our beautiful little cat here. And of course, what it also does is it takes, read through all the values and creates your storage array into a memory location.
[00:02:35]
So you get the full array out, as opposed to just the pointer to the array. So that's what's actually happening underneath the hood. There we go. We've now written a function called Get heroes, which should work. So let's write a course a test to prove at least the base case, that something happens, right?
[00:02:52]
So, it should get a zero hero array, that seems about, right? A sync function, do that, and I can just simply go expect, a weight hero.getheroes. Sure, look at this it's already filling it in for me but incorrectly two with equal zero, look at that. We can also do that whole deep equals and not do the whole length thing.
[00:03:20]
Other than that. There we go. We now went and called our function and make sure there's no heroes to begin with, right? It should just simply have nothing in it. Awesome, so I'm gonna go back here. I'm gonna go to my other terminal and we are gonna execute the test really quickly.
[00:03:38]
If you need me to jump back I'll jump back and we can take a quick pause. There we go. I'll jump back to the contract so the contract just kinda go over we have an enum, that has three values in it we have an address or a mapping that takes in addresses and produces out arrays.
[00:03:54]
We have a way to access that by doing a view a non-state changing function which means we could actually call those and get data out from the outside and we can even call it for free we only need a provider we do not need a signer. And then lastly, we have a function that requires a signer and requires eath for to execute.
[00:04:12]
I don't really know if you know of a good way to do a chai test, you can always send it to me and we'll just use that instead. But this is just how I've been doing the chai test right now. It's just like, capture the air and just make sure it's saying what I want to say.
[00:04:24]
Cuz it wraps it in a bunch of other information. I'm sure there's some sort of expect statement that can be like to throw air and air contains. I don't know.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops