How to clone any smart contract

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i'm gonna show you how you can clone any smart contract on the blockchain stick with me and i'll show you how to do [Applause] this name's daniel aka hashtabs and welcome to the channel where i teach you how to code and do blockchain things which is pretty cool and first of all i want to say thank you for the 9600 subscribers that's been so amazing throughout my journey on youtube please follow along if you want to subscribe feel free give a like and a thumbs up to this video to see more and then also if you want to go and join our discord and telegram channels by going to this website it's amazing and people will assist if you get stuck in these videos let's jump right into what we want to achieve today so the scenario that i'm going to put out for you is that you are new to the blockchain and you don't really know how to code big complex uh smart contracts although i have to say using this method you kind of need to know your way around a smart contract if you go for bigger smart contracts like these but needless to say you will be able to clone anyone smart contract by using this method keep in mind that smart contracts have licenses on them so be aware of that when you clone and duplicate a smart contract that you stick with those license guidelines anyway taking a look at some of the cool repos such as crypto kitties the board yacht club apes all these cool collections have something in common they have an open c page if you click on any of these nfts you can see that if you scroll down on openc there's a detail section once you click on this details section will have access to the contract address now if you click on this contract address it will take you to ether scan ether scan is basically a portal to see what's happening on the blockchain if you give it an address these addresses can be wallet addresses or contract addresses a contract address is something like this cryptokitties or a smart contract that handles some logic on the blockchain so now the question is how do we clone cryptokitties well that's very simple all you need to do is go down to the section where you see here on etherscan where it says contract if you click on this you'll be presented with the actual verified contract and the code the source code of the smart contract so here you can see we have the solidity smart contract and all its details we also have more contracts and then you get a section that says the contract abi basically how a program knows how to interact with the smart contract and then also some of the creation code mainly probably the constructive functions that's been encoded but those are not important the important section is this file now i see that cryptokitties only as one file but usually you will find that collections sometimes have different parts and multiple files this is known as a flattened solidity contract so they flatten all the code into one file now just to make this example of this video or give you guys an example that you can actually go out and use i'm not going to use cryptokitties because cryptokitties is very complex they've got breeding mechanisms different ways of dealing with the data and for beginners that might be a little bit too much what we are going to do and don't get me wrong if you are a good and great developer you can go ahead and try and figure this out and just copy everything and do exactly the same steps but what we are going to do instead is we are going to have a look at we mint washington now the reason why i want to show women washington is because they have implemented a very interesting concept the concept is that when people got to mint a washington they didn't mint them in chronological order they didn't mint from one all the way up to ten thousand one two three four five six and so on the program actually had them mint and then someone minted a random item so they might have minted number 22 or 10 or 14 whatever it might have been but the smart contract handled this and it was very intriguing to me to kind of investigate so let's go on this journey where we investigate the women washington smart contract and try and deploy our own smart contract using their code so all you have to do is go to the openc collection of the collection that you love for me it's women washington and then we scroll down and click on one of their items once we on an item we can go to the details page and simply go to the contract address this will take us to ether scan like i showed you with crypto kitties and we can also see that it has a contract that's verified go ahead and click on that i have to say on a side note that this process is exactly the same for polygon or other blockchains that also exposes the actual code of the contract you can do this on any blockchain basically as long as you have access to the written code and the source code now first things first like i said go ahead and look at the licenses that was used to create the smart contract usually mit and general public licenses are fine to use as long as you give credit um but you just need to be aware of that now much like cryptokitties this contract has basically a few import statements over there at the top and you can see that because it has import statements and is not a flattened file it consists of multiple files so there is another file it's called the random assigned the randomly assigned then we have the ownable smart contract and we can go down we also have the erc 721 and so on and this happens when a developer chooses to use import statements instead of just putting everything in one file which makes it a lot more readable but it makes it more difficult to reuse a smart contract because you need to keep track of all of its imports in this case there's not many imports and we can easily go and hook these things back up i'm going to show you that in just a second keep in mind that some developers are nice enough to leave comments like these and i'm going to leave this in so that we can take a look when we are analyzing the smart contract our main goal is to see how this person got it right to kind of mint and have things randomly assigned instead of in a chronological order meant the nfts we can also see that it's a clear indicator that this collection.sol and it might not always be the case that it's the top contract but um the the collection.sol is the we mint cash and it's importing or inheriting from the erc 721 ownable and randomly assigned this immediately tells me that this is the entry point contract and the contract that we would most probably copy and then try and reuse let's go ahead and do that before we go ahead and go and put this on remix and kind of recreate the smart contract sometimes you only want certain functionality and add it to your existing smart contract you can do so by simply reading through what the contract does we'll read through it now in remix but i just want to make that as a side note so that you know that you don't have to copy the the whole contract and use everything because sometimes there's certain things in a smart contract that you don't want to adopt but maybe there's certain elements that you can learn from and seeing that we are in the space where we all want to learn this is a great way to start by reading other smart contracts now let's go all the way down to this main smart contract and i would highly recommend doing this with a smaller smart contract such as this cryptokitties is extremely big and extremely complex to read through it so you might get stuck there but it's totally doable anyway let's go ahead and copy this so what you want to do now is go to remix.ethereum.org which is an online solidity compiler go and click on a new workspace over there and i'm just going to say ok we're going to ignore these contracts and just create our own so new file and let me call this test.soul because technically i'm just doing a test so then i'm going to paste everything that i've copied from the you know ether scan the smart contract and now we have to run through this smart contract to see what it's all about and how we can kind of replicate what it does first things first let's actually clear out these contracts over here let's delete them just so we have a clean working space we only have our test.sold where we are going to test the women cash smart contract now like i said if this is a reusable license like mit we can make use of these things you know as long as credit is given but because this is my test.soul i'm going to start with my test also making my contract test and you can call it whatever you want to remember this is your smart contract that you are optimizing i can see that there's a few things that's hard coded such as the ipfs link most probably to this person's repo where all the images live and those kind of things so in order for us to kind of fully make use of the smart contract we need to read it line by line and where you want to start is actually by looking at the import statements we can see that this developer is importing and making use of the erc 721 ownable from the open zeppelin collections now there's two ways that you can import this basically what you can do is you can go down to each of those files so for instance let's say we need 721 all right then you can go and copy all the contents in here go and create a new file in the contract and then just simply hook it up like this but this is time consuming and tedious so what you can do instead is maybe just google this line especially if this is a famous library such as open zeppelin and so on we will talk about this library in just a second but let's go ahead and see how we can import the erc and ownable so that we don't have to physically put the contract here in our folder i've now gone ahead and googled that line that import statement to see if i can find that contract online and indeed there's the github repo of open zeppelin now this is very convenient it means that we actually will have access to the smart contract on the browser right it's going to be set up on a repo on github so now i'm going to simply copy everything there then i'm going to head back to my remix and replace this line with this so it's now basically referring to the getups online repository which will now be able to import the erc721 we can also do the same with ownable let's go back to this repository let's go back to tokens contract i know where it is so i'm just going to navigate to it it's an access and then an ownable so you'll find it in this path this is generally the kind of construct of any nft so the ownable basically smart contract gives you the right to own the smart contract and switch basically ownership renounce it all this good stuff so i'm going to copy that and also head back to remix and paste that in there now the next line we have to look at is this import this is actually the part that we are interested in this is the part that randomly assigns basically nfts to make them randomly mint in different numeric orders this is very cool and i haven't seen this yet so i'm doing this live but what you can do is like i said copy this google it and see if there's an online repository i see that this person actually has an npm extension and i'm looking more for the github page so i'm going to try and add github like so let's just try this and there we go that is erc721 let's try and go to this npm link this is basically a package manager link uh maybe we'll get to the github repository over there and we can see there's the github so now that i go there i can see that this was created by this user and this developer so thank you so much for creating such awesome content and extensions basically now that we are here we can go back to our remix and see where it lies so we can see that it is underneath the extensions contract and then randomly assigned so i'm going to do the same i'm going to go to contracts extensions which is the contract and then the randomly assigned this one that i have this we actually have access to this repository like i said make sure that the licenses comply before you start using other people's code and give them credit so now i'm going to copy that and paste this in here perfect i am going to however um keep this open because the problem is i want to start still analyzing this extension before we make use of it so that you guys understand how to read contract let's jump back to remix you'll see that remix now has imported a bunch of stuff and that's because we've actually given given at the correct locations of where the smart contracts live so technically now because this is a green tick box over there technically we can simply go ahead and deploy this test smart contract and then just run with it you can see that this developer hasn't set up um any real constructor functions for us to pass in the constructor functions and the actual parameters are all hard-coded in the smart contract which is a way that you can do this we do need to however still read through this contract so let's make this our next step so now you can't just deploy the smart contract because you'll basically be making a clone of that person's work which you don't want to do what you do instead is analyze the date the actual data that needs to be passed in you analyze the variables the functions and you make sure that you have a good understanding of what you are doing before deploying your own copy of this developer's smart contract now we can already see that there's some comments we meant washington edition erc721 and this basically calls and mints a random washington until 10 000 have been minted how cool is that that is very cool that feature i haven't seen yet and that's something that we want to analyze now the minting cost is going to be very cheap it's at one dollar and 50 is kept for the artist let's go and let's go and see how we can adapt the smart contract to our own needs and then try and deploy it now firstly i don't want these comments to be in the smart contract because this is you know going towards this developers um how can i say project so if you leave it in there people are going to think it's the same project and it might create confusion for that developer and you don't want to do that another thing that you don't want to do is leave the contract name the same as what the developers put their name as the reason for that is because you will be creating version twos threes and fours of the same smart contract and people are not going to like that at all so come up with your own name for your own token that you want to create um as i said this is now custom we need to do it custom for ourselves let's take a look at the smart contract line by line well we have set our test as our new name then we have the erc721 the ownable and randomly assigned to which this contract inherits from we do have the respective imports for those files so we should be all good we then have this line strings for un256 which basically means that it's easier for solidity to kind of convert numeric values into strings when you want to return strings then we have a current supply which is set to zero and we can also see that the developer has set this comment which says that this is publicly requested right i'm just going to remove that because we can see that it is public over there then we have a base uri now base your eyes are generally never set like this but this is a way you can do that how do we access this base uri and let's see what this holds so i can go to google and maybe i can go ahead and paste this in at the top and i'll see nothing happens and that is because this is a base uri sitting on ipfs and most probably is a folder containing metadata with an extension.json i'm going to try and just add one dot json like so and indeed it is when i do that i can see this is the metadata and like you guys have all seen in my previous videos if you haven't and you don't understand really what we're doing here please go ahead and watch the featured video or the other videos in this channel of mine that explains how to deploy smart contracts you'll find this video very informative after watching that but anyway we get the metadata and we see that we also have an image so if we click on that image we can see that it will load up the actual image and that is pretty cool by the way so let's take a step back and have a look at what we were doing here we were reading now the ipfs link so at this point it would be highly recommended to use the automated dap or the automated art generator the hashtags are generated to generate your artworks and metadata and upload them to ipfs like i said there is a tutorial that i do that in and then put your link in here i'm just going to um for brevity and for the sake of this tutorial i'm just going to take everything out from here and say http test test test u or i okay and then i'm going to put a forward slash please don't do this i don't know what this website is really i'm just doing using this as a test so that i don't use this developer's content then we are going to say we need a name and i see we also need a symbol now the developer has also decided to hard code these values in the smart contract itself which is perfectly fine so i'm just going to make this maybe we say we want test how's that we want test and for symbol it's going to be wwt okay and then we see that the developer is calling the random randomly assigned function most probably from this randomly assigned smart contract and they are setting 1001. the developer was nice enough to put a comment and saying that the 10 000 nfts will be available for minting and it's going to start from one counting from one instead of zero i'm going to take this out because um you know we we basically have read it now we're going to to recreate the smart contract but you can leave these comments in just take out any personal stuff of the developer please all right so let's go ahead and read what this smart contract call this constructor basically as you can see this is a constructor what does it do so from the previous videos you've learned that a constructor function only gets called once and only once and that is when the contract starts up so we are trying to start up this randomly assigned smart contract by passing it some parameters ten thousand and one how do we see what this constructor does at the back so we fully understand the contract well we can go to this url link and go and check up what does it actually do we can see that by going to this url we can see that this is the new randomly assigned smart contract that's also coincidentally inheriting again from another smart contract you can go and deep dive and into this whole rabbit hole which i highly recommend actually to fully understand what's happening but let's briefly take a look at this smart contracts constructor here's the constructor function and basically it takes in a total supply and a start from it then goes ahead and sets another constructor which is this one to the total supply let's jump to this smart contract just quickly we can see that the limited supply doesn't inherent from anything and it simply looks like a helper function to keep track of how many tokens or counters that it that it has to deal with so this is probably a handy a handy smart contract to inherit from so this is exactly what happens here it sets the total supply and then the start from actually gets set to this private variable to know where this contract should start minting from this contract also has a mapping which is the token matrix which i think is the main variable that's used to kind of know which ones should be randomly assigned let's jump back to remix so here we can see that we are passing in 10 000 which would be the um you know the total supply and one the developer was nice enough to leave us this comment which i removed and this basically explained what this was doing but i wanted to read through it so you understand before reading this comment now let's take this out again doesn't we don't really have to have it there let's look at the next thing that happens in the constructor well the developer mentioned that 50 nfts will be minted for the dev team and this is where this happens it's just simply a for loop that loops through 50 times and basically mints to this message.sender which is the owner because they will be calling it for the very first time so now that we know what the constructor function does we can take a closer look at the randomly assigned contract seeing that this is kind of the most important part that we want to analyze well the developer was nice enough to really go in depth and comment each one of these functions so you can go ahead and read through it but usually what i do is i read through each function and fully understand what it does for instance the next token function over here we can see that it is overriding uh you know overriding a few things but you can also read from a dev point of view and what does it return returning simply the next token id that it's going to mint randomly gets a new token id and keeps track of the ones that are still available so this is kind of a very important function and this is how this person solved this issue now how they solved it is not as important as you using it but it is important to understand that why this library exists is to abstract this complexity for the user and for the other developers trying to use this library so thank you so much for the individual who made this because this is really really cool but anyway go and have a read through this smart contract in detail to fully understand it the rest of the smart contract is simply basic it is getting a base uri which is this one here at the top it has a mint function which all erc sim 21 smart contracts have and this is simply what people will call when they want to mint one you can go ahead and make small adjustments if you really wanted to for instance you can take out the address and simply say that this mint function don't doesn't need to take in any parameters and just adapt it an example of adapting other smart contracts would be to read through what it does and making it work to your needs for instance if i wanted this mint function and i know that the person minting it it should go to their address i simply don't need this to address over there i then have to update the function you know accordingly so that it doesn't make use of that two variable so let's read through it the smart contract starts off and you can see that you cannot mint more than the max apply it has a required statement that checks the token count which comes from the inherited smart contract over there and then it makes sure that it's less than the total supply it also requires that the available token count is -1 one is greater or equal to zero so then if it fails that you can see that it says you can't win more than the available token count and then it has this extra require statement that requires that the transaction right is equal to this message.sender you cannot mint through a custom contract meaning that contracts can't call this function now for me this is not quite a very important because usually you just get people who want to mint and it and it's not really going to be contracts unless someone's trying to to go and hack you but i don't know what they will be accomplishing by doing that because it will just be an nft minted to a contract address and next we can look at this um by seeing these things i recognize that this developer must have definitely reused some of our contracts which is cool especially this part with the dot json but that being said all this does is it checks if you are the owner or not and if um if it's not the owner it's going to ask a price to to mint this nft it's going to make sure that the balance of this sender is less or equal to one which is kind of a little bit of a loophole because someone can mint and then they can you know transfer it away the nft so they can mint again so please check up on my other videos where you can omit this loophole it's not the biggest loophole it just means that it can be handled a bit better but i don't need these things because i don't want to limit people you know minting my nft so i'm just going to ask them for let's say a fee and then i'm going to remove this check because this is using the two check as well i'm not going to have that in there so then all that going all it's going to do is just ask for the price on line 41 we can see that the developer has decided to get the id for the next token by calling this next token function that is sitting on the randomly assigned smart contract so if you go here onto the randomly assigned remember we had this next token and this logic beautifully kind of gives us back the next token where it randomly assigned or selected one from the matrix right and this allows us to get the next token available token so let's say it chooses number 14 it will give us back 14 and then after that it will never give us back token 14 to mint again because it means it in a random order perfect so if we move back to this we can see that this returns a un-256 it's saved and then it's using the underlying savement function of the erc721 contract we can see that it is minting to the two which i'm now going to simply change to message.sender which means this will be sent to the person who actually mints this nft and then it takes in the id which is this id and it increments the current supply that's all if we have a look we should be this should be good so i'm just going to try and save this and we see that we do have an error and that's because in the constructor because we're minting this initial 50 which i want to make 10 we can see that it takes in an address which in this case it doesn't so i'm just going to call the mint without calling without passing in the address because we took it out we are adapting the smart contract and i'm teaching you guys how to read adapt and go through smart contracts so let's keep it like that next i can see that this contract actually overwrites the token uri and all that this function does is it makes sure that it has a token already existing for that id it goes and selects the current base uri which is the one that we set up here and then it simply returns that base uri with appending the token as well as a dot json the reason why this is important is because when we look at a base uri with a token id which is that number five and a dot json it will give us json data back but remember you will have to set that up by yourself on ipfs then we have a withdrawal and this would with this withdrawal actually requires a payable message.sender and then it sends the balance now it's highly recommended not to use transfer or send anymore it's not bad using them it's just now best practice to use the call method and i'll show that in my other videos as well but that being said this smart contract looks cool it looks good and i think we can go ahead and deploy it now that we kind of understand what this contract is going to do you know and working with the magic of the randomly assigned contract as well i'm very excited because i have never deployed this contract before and i want to see if it behaves accordingly i'm going to go ahead and just do a few changes and make this randomly assigned let's make it 10 so i'm going to create a maximum of 10 and it's going to start off at 1 and then i'm going to have my loop actually not happen because i don't want to mint too many for myself maybe i'll just call it once so i'll leave the mint over there just like so so this will meant one nft to the owner and then the rest of the 10 it will leave up to someone to mint i'm making it a smaller variable size so we can test it properly in our smart contract functions on the left hand side once we've deployed so once you're ready make sure there's a green tick it means your contract you know compiles successfully and it's very important you can also put on auto compile and enable optimization to make it easier when you change something that it does compile and check then you can head over to the deploy and run transaction section this is where we are going to deploy our smart contract are we simply going to be on the javascript virtual machine networks because we're not deploying this to a real network then make sure that you have selected the proper contract which is our test contract and this contract doesn't take any parameters because it's all hard coded in the constructor over there so you can simply hit deploy once you've done that your actual deployment of your smart contract will be successful like you see over there and then we have all the features of the smart contract on the left hand side now let's make sure the deployment was successful by clicking on these call functions here on the left hand side if we click on total supply we can see that it is indeed 10 because we set it to 10 over there then if we go and copy the owner's address and click and go and paste it here on the balance off and then click on balance off we can see that we have indeed one balance to this owner but it doesn't mean that the id number one is assigned to this owner because remember this could be any id from 1 to 10. so we can't simply just assume going down here to try and get the token uri that it would be id 1. it will just fail and say this is a non-existent token and that's why i minted a limited supply of 10 so that it's easy for us to test so let's see if i got number two three three if i got id4 five six seven no eight no nine and yes so id number nine was minted to the owner and you can see that it works because it randomly minted number nine id number nine to this owner which works beautifully we can see that we also have a token count which means it's now sitting at one token minted and then the symbol is wwt like we've said the owner the name of the contract the current supply and the base uri also the available tokens that are left so this smart contract works beautifully let's do one more test and let's go to a different account let's go and mint we're going to have to give it some value we only have to give it 0.0003 ethereum i'm just going to make it easy on myself and just pass one ethereum not going to work that out and i'm going to hit the mint function it means it successfully so now if we basically copy this person's address and we put it over there we can see they also have one let's mint another one so i'm going to put a value again because i'm not the owner i have to pay to mint i'm going to mint one and we can see that this balance of this owner is now too i want to see that this truly works by clicking on the total supply which is 10 and then the token count which is three i want to see that the token count won't go go over the number 10 supply just to test that everything is working i'm going to do that on the owner's account though because i don't want to type in the ether every time so i'm just going to say mint mint mint i'm going to click on it until i feel everything has been minted you cannot bend more than the total supply perfect if we click on total supply it still says stays 10 and total token count is 10 and we can also verify that by checking the available tokens is zero and the current supply is 10. so now we can verify that this contract implementation works indeed as it says it does and this is very cool thank you so much for the developer who's actually coded this extension something to keep in mind though this video is not about implementing a random munting feature although this works perfectly fine this video goes about how you can replicate and clone any open c kind of public collection and if you like the way that they are doing things go and have a look at how they are doing them by reading through the smart contracts reading through the libraries that they are using and making use of simply copying and pasting it into remix testing it out and adapting the variables for your needs keep in mind like i said um always make sure that you know what you're doing before you deploy things so maybe get an extra eye to look over your smart contract there's a lot of developers out there that might put malicious code in the blockchain so or in the on the blockchain implementation so be aware of that anyway but until next time guys i hope you guys enjoyed this video and that it was informative if it was like i said give a thumbs up subscribe to see more content and i'll see you in the next videos
Info
Channel: HashLips NFT
Views: 2,006
Rating: 4.9679999 out of 5
Keywords: polygon, ethereum, nft, opensea, nft collection, hashlips, blockchain, generative art, generative nft, programming, code nft, ipfs, solidity, how to market your nfts, nft marketing, promote your nfts, nft marketing strategy
Id: 18TSUZ53m1Q
Channel Id: undefined
Length: 37min 27sec (2247 seconds)
Published: Sun Oct 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.