How to create an NFT Smart Contract (ERC721)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up in today's video what we need to do is we're going to create our own nft collection what we will end up doing is create an ns nfc collection which is erc 721 compliant if you don't know what that means don't worry it's just a regular nft but i hope you know what nft means because otherwise why you're watching the video so most of the nft collections out there are erc721 so that is what we're going to build out today as well in my previous video i had discussed what technically an nft means i had walked you through all the technicals that are involved in the nft today we are going to write the solidity code and let's see if we can deploy that to a polygon or any other chain our nft will be mintable so any user can go and sort of mint these nfts we will try to add some token uri as well so that our nfd is related to an art or something like that and as we progress through the video i'll also explain how you can let's say charge fermenting or create an ft which sort of reveals itself later on and stuff like that so if you're interested in all of these things make sure you stick around till the end of the video and before we begin please hit that like button and subscribe if you want or if you have anything to say just put it in the comments i love reading them i respond to some of them as well not every comment warrants a response right cool then let's get started so to create our code what we are going to do is we are going to use this awesome open source library called open zeppelin and they also provide us with a wizard to sort of just create your own uh erc contracts so as you can see there is erc20 so we just provide the name and the symbol and that's it your erc22 coin is ready we they also provide us with erc721 which is the nft standard that we're going to work on today and they also provide erc1155 which is a different nft standard so we're going to use erc 721 today this is the standard that we're going to work with this is what most of the famous nfd projects use like board aviar club budgie penguins and etc if you've seen my previous video you'll see that i have discussed erc721 and explained how other projects have used it so if you're feeling confused please make sure to check out that video i'll probably link somewhere here and if i don't please let me know in the comments i might forget so what opens airplane does is it gives us the bare minimum part and then we can sort of customize the code as and when required for ourselves so let's say we change want to change the name of our token and instead of my token i want to call it my nft and i'm going to change the symbol as well it doesn't matter but like that's the symbol i'm going to use mnft i can also provide a base uri so this is required if you want to associate some sort of art or metadata with your nft so if you want to let's say create a pfp nfd where you know there are 10 000 images and whatnot uh the base uri is always sort of required or expected for the sake of understanding let's just create some sort of uh base uri and then i can add other features as well for example i can make this nft mint table and have auto increment ids i can also make this nft enumerable and the question marks beside the property or feature sort of explains what's going on over here i will i will not require ura storage because i'm using base uri and rest of the things are fine i just don't need them now i can copy this code and get it locally or i can use it in remix uh if you don't know remix remix is an awesome ethereum ide solidity ide provided by the ethereum network or foundation i guess and it [Music] comes with all the things required for you to create a solidity project and deploy it as well so as you can see it sort of created a file and it brought all of our code that we needed i can just go and rename this file and i'll call it my nft it has imported everything that we needed from open zeppelin and uh has created our nft right off the bat amazing now in some sense our nft is done like this is a proper functioning nft it's a project which will go for whatever number that you want anybody can come and not anybody actually only owner can come and mint a new nft they can do it 10 000 times or whatnot right but let's say you want to create an nfd where people come and mint their own nfd even though you are the one who is providing the art but people will come and claim sort of an nft so what you'll do is you'll remove this only owner thing from here and so now because the only owner part is removed this safe mint function becomes a function which anybody can call and create their own nfd now the thing is we have put no restrictions over here so the minting will go till infinity like it doesn't matter till where so let's just go ahead and first compile this nfd and see if there is any problem with it great there's no problem now we'll go and deploy this on our small ethereum chain that we get with remix so i click on deploy okay deploy function didn't work uh because the contract over here was ownable instead of that we'll choose my nf data the contract that we worked on and we'll click deploy again and this time it deployed how do i know because there's a tick sign over here and blablabla all right now if i see and open this my nft deployed contract i'll see that there are a bunch of methods that i can see right so now let's say i want to check what is the total supply so i click on total supply and it says zero at the moment because nobody has maintained an erft we have just put an nft collection contract on the chain so let me go ahead and mint one nft for myself i'll copy my own account address and i'll click uh put it over here in the safe mint part and then click on save mint and i'll click on this little icon and i'll click transact and as soon as i do that uh i believe one nft has been minted uh how do we know we can click on total supply again and see that the answer is two because i did that did that twice i guess but uh yeah you can see that there are two nfts now how do i know who is the owner of this nft uh the two avenues that we have done so we can just put number over here and click owner off it says this is this address is the owner of this nft i can click put another number as well and it returns the same value if i enter a number which you know is not minted let's say 3 it should give me an error and you might not see the error right away but you will see over here that it says call to owner of errored vmware revert and it reverted with some error saying ert721 owner query of non-existent token all right great so now our nft is on the chain uh now how do i transfer let's say one nft to somebody else so let's say i go ahead and select a different account and i copy the address and i go back to the original account and i say uh where is this functions this function safe transfer right and i click here and i see from address and i see to enter so i'll put the other address into i'll copy my address again and i'll put it in from and let's say the token id2 i want to transfer so i click on transact so i made a mistake uh in open zaplin code that i'm seeing over here the counter is starting from zero so you can see that the current number is added to the token and your token it took an address token id and then the number is increased if we switch their positions the token id starts from 1 and then sort of increases instead of 0 and increasing so in the current implementation the token id 0 and 1 are present because the total supply is two so i can safely transfer zero to somebody else which i have sort of done and you can see that the transaction has has worked out very well it it didn't fail now i can go and check out the owner of let's say zero right so the owner of zero is ab8 blah blah blah right and similarly i've done the same transition for one as well so the owner of one is also this one but i can go ahead and still mint another nft so i click on transact on save mint it worked out and now i can check the owner of id 2 like plus call and it's me now i'll again transfer it from transfer the token id2 to that same address i click on transact and now transaction has succeeded i click on owner off and the owner off has changed great so we have an nfd which can be minted and transferred to other people and it does a bunch of other things in the background which we are not worried about right now but that is just something to remember another method that you see over here is token uri right and if i press token id 1 and i press call it returns a string which is api.nft.com slash token slash one so you can imagine if i own this domain uh this is where i would host my metadata which is like name description image and whatnot attributes right and once i host that metadata on my server what would happen is any other piece of software can just read that metadata and figure out how the nft looks like what is the name what are the attributes and blah blah blah so that is how you create an nft pretty quick this was very quick but it's no fun at least at this point so what we're going to do is firstly fix our nft to actually start from id1 i'm going to make this function payable and i'm going to require message dot value to be greater than equal to mint rate if there's an error if the mean if the amount sent is not greater than equal to win rate we reply with not enough ether sent and now we also have to create a you in 256 public mint rate which will be let's say 0.01 ether great now we go ahead and we compile this contract and it has compiled now we go here and we delete the old deployed contract now we don't have it we select mynft again and this time we deploy the contract again now that the contract is deployed you can see that there are the safe mint method is red now because now it's a payable method so i'm going to copy my address and i'm going to paste it over here and click transact and it has given not enough ether sent now it did that because i've been given any value to send so what i'll do is i'll click i'll go here and see this value part and so from what i saw the one fini is basically point zero zero one ether so this should be ten fini which is point zero one ether and now i click on transact and lo and behold the transaction has succeeded another thing that i could have done was i could have increased the value of fini that i had sending so i can send 11 as well and this time also transaction will succeed or i could have done 9 and this time the transaction is going to fail because we are requiring that message dot value the amount of ether sent should be greater than or equal to the mint rate that we have over here and we can also check out the min mint rate because it's a public method so it's visible the mint rate that i can see over here is one followed by so many zeros because it's in way which is 1 by 10 to the power 18 ether now the problem with this is that the ether sent to this contract is not retrievable for by the owner because you know i'm the owner whatever amount of ether i have collected i need to withdraw it so what we need to do is we have to create a function which is called way draw the function should be public and only ownerable so only owner who is the owner of this contract the person who has deployed this smart contract only they can withdraw either now what happens is we are using this ownable module uh it sort of returns gives us this only owner thing so now if we want to figure out who is the owner of the smart contract what we do is we call owner and this call that method that returns the address now we want this address to be payable because we want to send some ether to that address then we call transfer on that payable address and what we do is we call um address this so this way we get the smart contacts address and then we write the balance so whatever the balance is we will just withdraw it one thing that we can do over here is we can require that this balance be more than zero so now if we go ahead and we compile this it should work it did work there was no problem and let me go ahead and just delete all these and deploy the mynfv.com again click on deploy and it deployed great i copy my own uh this thing my own address and infini i write let's say 10 or not 10 i want to put i want to send three ether all right so i go to save mint and i enter my own address so i get this nft for three ether and you can see that it succeeded but then what you'll see is that my account balance is very low not very low it's still 96 ether but it's still low so i go ahead and i click on this withdraw function and you can see that it succeeded by getting a tick mark over here and if i go up you can check that the ether has been withdrawn to my address because i was the owner and now i have an idea an ether now let's do one thing let's go to the second account and let's mint through this i'll again put in let's say this time i'll put five ether and in the safe mint um i'm going to go and put my own address and i transact it works great now you can see that there's more than 94 ether left over here five and then some gas was removed and uh there's only 99 ether over here so now i go to the main address through which i had deployed everything and i go and click on withdraw again and you'll see that my total amount of ether is increased by five so this is how people basically make money by selling nfts by letting you mint nft on their own contract so if you're wondering how people do that is your answer now this uh nft sort of lets people mint whatever amount of nfts that they want instead what i want to do is i want to have an upper ceiling of let's say thousand so what i'm going to do is i'm going to create a uint which will be public with the name max supply which is 1000 and i'm going to require another thing over here which will be that total supply should be less than max supply uh but just for this video what i'm going to do is i'm going to reduce that 1000 to let's say one so we are only allowing one minting and i click on compile and there's an error because i forgot a semicolon i click on compile again there's still another and that's because i'm i'm using the function but not calling it and i click compile again and this time it should compile it did good so i go ahead go over here and i delete the contract oops and i deploy the contract again now remember the max supply is one all right so if you check out total supply right now it's zero i go to safe mint uh but before that i need to make sure that there is 10 fini i go to savement i copy my own address and i paste it and i transact and you can see the transaction has succeeded but let's say i add 10 fini again and i transact once again this time you'll see that there's an error and the reason is can't mint more because that's the maximum supply now for all practical purposes what would happen is the maximum supply would be let's say 10 000 all right and once we reach that you can't mint more now how do we know what art is associated to a specific token uh for that we have this base uri and we go and check over here in the token uri we press let's say one uh we get a string this is the uri where you know if you hit you'll get a metadata json response and this is where you will put out all the metadata about that nft like name description blah blah blah image specifically or whatever essay is associated what are the attributes and everything you'll put in the token url so this is how minting and nft works um i can deploy this to polygon but i'm not going to in the next video what i'm going to do is i'm going to create a javascript interface around this so that people can come and mint their own nft and have fun with it so i'll just save this code somewhere and from the next video we'll start from here we'll deploy this nft and we'll see how we can add metadata to this nft and how can we let people to come on our website click on a mint button and mint this nft all right thank you so much for watching this video i think that's about it for today uh if you have any questions if you're stuck anywhere please let me know in the comments make sure you hit the like button subscribe bell icon everything you know just uh in the next video of course i've already talked but we're going to work on creating a javascript interface so that people can come and mint this nft by themselves hopefully that video should come out next week i am traveling so i don't know if that will happen but 99 sure that will happen all right thank you so much for watching the video i hope to see you again next week bye
Info
Channel: Web3 Club
Views: 9,820
Rating: undefined out of 5
Keywords: nft, erc 721, erc721, non fungible token, remix, ethereum, polygon, matic, ether, solidity, ipfs, tokenuri, smart contract
Id: c_xwYep-gnQ
Channel Id: undefined
Length: 20min 20sec (1220 seconds)
Published: Sat Sep 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.