text
stringlengths 61
434
|
---|
minimalistic version of this. So we're in our VS code pre normal. And we're going to create a new folder here called hard hat. And if T marketplace, |
FCC, oops, MK dir, like them, we're gonna cd into it. And then open this up and its own VS code. Once again, you can use code period, or file open folder, and open this folder. Once we get in here, we're going to do all of our normal stuff |
that we've been doing throughout the course. And once again, if you want to copy paste over your package, JSON, if you want to copy paste in the modules, whatever you want to do, feel free to do so I'm gonna go ahead actually, this repo here, we're gonna scroll up and just grab once again, this line from less |
than nine. And just run that I know I'm gonna be using prettier so I'm just gonna go ahead and copy paste those two prettier files over prettier ignore and prettier RC going to be using them again, for linting. With solidity we're going to use Sol hint dot JSON. So we're going to grab that dot soul hint dot |
JSON. And the dots will hint dot ignore. I'm also going to grab the hard hat dot config dot j s because we're going to be using a really, really similar setup. And this hard hat dot config dot j s. It's got waffle in it, ether scan, hard hat deploy coverage gas reporter sizer and Dottie and v dot config, we're |
going to bring over our Dotty and V. And we're also gonna bring over our utils folder as well. Right, so a lot of that boilerplate we're gonna bring on over. And now just like that, since we have the hard hat dot config dot j s in here, if we run yarn, hard hat, right now, yarn hard hat will actually see |
we get the output like this. So let's go ahead. And before we actually write our contracts, let's go ahead and write a little doc saying what our contract is even going to do, what do we want this to do? We're going to create a decentralized NFT marketplace. So what does that mean? What |
will we probably need? Well, we'll probably need some type of list item function, because we'll want to list NF T's and this will be to list NF t's on the marketplace, we'll need some type of buy item to buy the NF T's. And then we'll probably need maybe like a cancel listing or cancel item, if you no longer |
want to sell it, maybe an update listing, update price. And then maybe a withdraw proceeds to withdraw payment or my bot and fts. So when somebody buys an NFT, I'm gonna have to withdraw |
it from the contract since the contract is going to be the one to actually hold those funds. That looks pretty good to me. Let's go ahead and start building this. So let's create a new folder, contracts. And let's jump into this. So we'll create a new file NFT marketplace. That's all. So let's get our boilerplate. spdx pragma, |
solidity, carrot zero, point, 8.7, contract and ft. Marketplace, boom. If we're doing this, right, h h compile or yarn Hardhead to compile or MPX, art, hit, compile, boom, |
things are looking good. So if we go back to our readme, we can grab these here, even stick them in as like a little comment for us to kind of reference later on. Let's start with listing the items. How are we going to keep track of listing people's items. |
And once again, remember, when I'm usually coding this, I'm going back and forth between writing tests and writing the actual code. We're just going to write all the solidity in one chunk, and then go write the tests. So we're going to say, these are going to be our main functions. I'm going to start |
with function list item. And we are going to make this one look really, really good. So we're going to do natspec. And everything, this is going to need to be an external function, right, we're probably not going to want any of our internal functions calling list item, it's going to be called by |
external projects or external accounts are probably going to need an address and ft address, write the address of the NFT, contract a un 256 token ID, the ID of the token ID of the contract that we're going to use. And then we're going to want to set a un 256 price. So first off, we're probably going |
to want the price to be greater than zero. So maybe we'll put in like a little if or require statement here, we'll say if price is less than or equal to zero, then we'll go ahead and revert with a price must be above zero error. And then of |
course, we'll prepend it with the name of the contract into underscores. And then at the top, the error price must be above zero. Now in order for us to list it, we could actually do this one of two ways we could one, we could send the NFT to |
the contract, this would require us doing like a transfer, right, we could have got the contract hold the NFT. Now we could do this, but this is going to be kind of gas expensive for someone to actually list on f t. And we can have the owner of the |
NFT be our NFT marketplace, we could 100 percent do that. The issue with this, though is that the marketplace will then own the NFT. And the user won't be able to say like, Hey, I own this NFT, it's in the marketplace, they technically would be able to but they would have to withdraw it, we might do this a slightly different way where we can say owners can still hold |
their NFT and give the marketplace approval to sell the NFT for them. Now, of course the owners of the entity could withdraw approval at any time and the marketplace wouldn't be able to sell it anymore. However, this would be really |
easy for people to actually read, they would all they would have to do is read like is approved for marketplace. And they can actually see if the item was really listed or not. So we're gonna go ahead and write it this second way, because that's what Ardian does. And this is the least intrusive |
way to have this marketplace, right? People still will have ownership of their NF Ts, and the marketplace will just have approval to actually swap and sell their NF T once the prices are met. So since we want to make sure the marketplace has approval, let's make sure the marketplace has approval. So we |
can call we can call this get approved function on that token ID to make sure that the marketplace is approved to work with the NFT. To do this, we're going to need the AI ERC 720 interface and we can actually grab that from open Zeppelin. |
Right and this interface will wrap around an address and then we can call get approved on that address. So we'll do import at open Zeppelin slash contracts slash token slash ERC 721 slash |
I ERC 721 dot Sol. And since we're doing an import from open Zeppelin, we'll do yarn add dash dash Dev, add open Zeppelin now that we have this interface in here, what we can do is we'll |
say I ERC 721 NF t equals IRC 721 wrapped around this NF T address that we're passing in. And we'll say if NF T dot get approved of the token ID that we're trying to list does not |
equal address this. So if we are not approved, then we'll revert not approved or market place. And then we'll of course we'll want to do prepend it with NFT marketplace into underscores. So |
error like this, Bada bing bada boom, now that we've gotten a little bit of that out of the way, we're probably going to want to have some type of data structure to list all these NF T's. And typically we get to Okay, do we want to use an array? Or do we want to use a mapping? What do you think? |
Before we continue? Let's pause for a second, do you think it makes more sense to put these NF T's and an array or an A mapping? And when you're thinking about this, try to think about, okay, well, people are gonna have to buy these and sell these, what makes more sense, think about this for a second, maybe pause it and write in a comment here, what you |
think an array or a mapping is better. Now, if you said mapping, I would agree with you. You couldn't do an array and you wouldn't necessarily be wrong, but it's not the way that I would go about that for an array. Anytime someone wants to buy an item, we're gonna have to traverse through the array, we're gonna have to make this massive dynamic array. And that |
might get a little bit dicey as that array gets really, really big. So we're gonna go ahead and make this a mapping. And this is probably going to be a global variable or a state variable. So up at the top, let's go ahead and create this mapping, it's going to be a mapping of addresses of NFT addresses. |