ERC20 Token Tutorial | Create Your Own Cryptocurrency

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome to the erc20 Token tutorial in this video we'll be going over all the steps needed to create your own cryptocurrency in the form of an erc20 token on the ethereum blockchain we'll start with a very basic solidity smart contract implementation and do a quick test using the remix editor just to see how everything works then we'll take a step back and create a proper project using hard hat we'll add some more functionality to the token and then we'll deploy it to ethereum creating a token is actually the first project I ever did when I first started learning blockchain technology so this is a great project to do if you're just starting out and for more experienced devs it might be a good refresher on the steps needed to create and deploy a token so if that sounds good stick around and let's create some crypto [Music] thank you okay so just a quick overview on erc20 tokens before we get started first of all what is an erc20 token and what's the difference between a token and a coin in the world of cryptocurrency well the idea of a coin refers to the native cryptocurrency of a certain blockchain examples of coins would be Bitcoin ethereum and Dogecoin these coins are coded on the core protocol level and not on the smart contract level a token on the other hand is a cryptocurrency built on top of an existing blockchain that exists as a smart contract examples include link wrapped Bitcoin or Shiba Inu to name a few erc20 is a token standard that implements an API for tokens within smart contracts it's the most popular and widely used token standard on ethereum example functionalities that erc20 provides include transferring tokens from one account to another getting the current token balance of an account getting the total supply of the token available on the network and approving whether an amount of token from an account can be spent by a third party account the good news is that since erc20 is such an established standard there are many implementation smart contracts out there that we can inherit from in our code to provide most of the functionality out of the box so we'll only really have to provide a little bit of customization in order to control things like name ticker symbol total Supply and minting strategy in this tutorial we'll be using the open Zeppelin erc20 contract and use that to build out our token let's take a look okay so this is the erc20 open Zeppelin contract that we'll be using as the basis of our token and I will add a link to this documentation page in the description of the video in case you guys want to come out and check it out for yourselves but what I'm going to do here is click on the interface which is this contract implements and as you can see this interface implements all of the methods laid out in the erc20 standard so as we mentioned a minute ago including the total Supply which will give us the total supply of the the token balance of passing in an account I can tell the balance of Any Given account transfer to move tokens between accounts allowance approve which is a mechanism to allow a third party to spend tokens on the user's behalf and then transfer from which is another transfer mechanism as well as a couple of events here so by inheriting from this contract we basically get all of this out of the box all right so what we're going to do is the first step here is to create the bare minimum implementation of a token on the remix editor and just do a very quick creation and test and I think you guys are going to be surprised at really how easy it is so I'm going to go out here and do a search for remix IDE and if you guys haven't used remix before it's just a really great tool it's an online IDE that we can use to quickly test like proof of Concepts and and different ideas for smart contracts and then quickly test them it sets you up with a bunch of different wallets with 100 ether in each one of them so it just makes it really easy to quickly mock up and test code so we'll use this for the first iteration of our token so I'm just going to go ahead and accept the terms here I'm not sure what I'm agreeing to but uh hopefully it's nothing too bad all right I'm just gonna X out of this stuff here and you'll see that basically it gives you uh it's sort of scaffolds a project structure so I'm going to go ahead and expand the contracts folder and it gives you a couple of example contracts so I'm just going to rename the first one and so all right we're creating a token you can name it whatever you want I'm just going to name mine ocean token okay because I live near the ocean so go ahead and rename your file just so you can kind of see it here I'm going to go ahead and just select everything in this file and delete it okay so I'm going to start out and let's see I'm just going to write a quick comment here just for good practice so contracts slash ocean token.soul and I'll start with a license identifier I'll say spdx license identifier we'll say MIT pretty standard license this part's really not that important all right so I'll specify my solidity version I'm going to say pragma solidity and the most recent version as of today is oops 0.8.17 I believe all right so the first thing we need to do here is import our open Zeppelin contract and so since we're using the online remix editor we can't really reference the locally installed version as we would in a normal project so what I'm going to have to do is go out to the GitHub and just grab a live link to the contract so I'm looking for the erc20 all right and then I'll have to specify the the actual file so I'm going to go back over to remix paste that in and then brc20 Dot soul and don't forget my ending quotes there I don't have a beginning quote so I'll need to add that too there we go all right so we're importing this directly from GitHub and that's just because we're on we're using remix right now okay so I'm going to define a contract using the contract keyword um and then I'll specify the name ocean token and again name it whatever you guys want now I'm gonna inherit from the erc20 contract that we're importing using the is keyword C20 okay then I'm going to add some curly braces there all right so we're going to start off with the Constructor and so for our Constructor what I want to specify as a parameter is the um the initial supply of tokens so I'm going to say uint 256. initial Supply okay and then we'll call The Constructor of our inherited erc20 contract now this takes two parameters the first one is the name of the token and the second one is the ticker symbol so for this example I'm just going to hard code it and I guess there's no reason to really make this Dynamic anyway since it's a one-time thing the name's not going to change so see if we ticker symbol I'm going to say OCT an interesting thing about ticker symbols when dealing with tokens is just kind of a quick aside is that there's no centralized mechanism or decentralized mechanism that ensures the uniqueness of a token ticker symbol I mean there's nothing from stopping me from using a symbol that's already in use there's nothing from stopping anyone else from using my ticker symbol so it all comes down to just you know the consensus of the community to use the right token and that that all comes down to basically the address of the contract so just something to keep in mind all right so within our Constructor I want to call the mint function of the erc20 contract and I want to go ahead and assign all of that initial Supply to myself message.sender okay so I'll say underscore mint so just sender initial Supply so I'm saying create this token give it you know n amount of tokens as the initial Supply and send them all to me and then I can figure out what I want to do with them from there all right so I'm going to save this and that is basically all the code we need to create our first token and uh what's that like here oh okay so it's complaining that the version I've specified is not in agreement with the version that's set in remix so we can fix that very easily go ahead and click on the compiler icon here and this I don't know why this defaults to an older version but uh anyway just update it to whatever you've got here for pragma solidity all right and so from there we can go ahead and compile our token uh let's see found all right so I've got a problem with my URL here let me click on the the exact file okay let me go back and just replace the the name here I don't know maybe I've got a typo or maybe I'm doing something else wrong so we'll find out I'm going to go ahead and save this file and try it again and it's compiling automatically for me okay it worked that time great so I don't know what I missed the first time but anyway it's always good to copy and paste right okay so that's compiled and now we can go ahead and click on the the very bottom icon deploy and run and uh just make sure remix VM is selected which should be the default and this just means that we're going to run this on um a local in-memory blockchain to do our testing Okay so all right so here we gotta have a quick conversation about numbers and solidity um if you don't know this this takes a while to kind of wrap your brain around at least it did for me but basically there there are no there's no such thing as a floating number or decimal places in solidity so everything is integers everything is just whole numbers so if I want to represent one token in solidity I actually have to represent that in the code as a larger number now tokens default to having 18 decimal places so what does that mean decimal places I just said that there are no floating points in solidity so basically I have to represent 1 as 1 plus 18 zeros after it all right so one two three four five six seven eight nine ten one two three four five six seven eight okay so I'm going to represent one token in my coin as this large number with 18 zeros after it and so when we see this number or the representation of one token on websites D apps or in our wallet we don't see this large number obviously we see one you know or sometimes one point zero zero zero zero zero all the way out to 18. all right so the decimal place really is just a cosmetic thing that we use in uis to make sense of these large numbers but in terms of the actual solidity code behind our tokens we're actually dealing with numbers like this when we say we have one token all right and so we don't literally have to type out the 18 zeros we can use variables um and some some multiplication uh tips that that I'll kind of show you a little bit later to represent these large numbers in our code but just so you know all right so this is one right so for the initial Supply this is one what I'm kind of getting to we've got to use a much larger number so I want to create let's say a million tokens so what I'm going to do just to visualize this a little bit better I'll write out the number one million okay one followed by six zeros and then I'll add 18 more zeros to that so we end up with this really large number here and this is what we have to input as our initial Supply so I'm going to go back over to remix and remember this is our Constructor parameter for our smart contract so we've got to enter that in and go ahead and click on deploy all right and we've got our success message there so we can go down to deployed contracts and see our newly deployed smart contract for our token and we can see all these different methods in here that are coming from the the inherited erc20 okay so we don't see any of these in our code right we don't see anything called transfer we're getting all this from the inherited erc20 contract Okay and like I mentioned earlier we just have to modify a few small things to add the initial Supply the token name and the ticker symbol all right so we can play around with this a little bit let's see this should give us the symbol here we are oct the name ocean token okay and let's just verify that all the initial 1 million tokens have been sent to me and so my active account what's represented as message.cender is the first account in the drop down here so I'm going to go ahead and click this to copy that to my clipboard the address and we can we can find the amount of tokens that I have by copying in my address here and then invoking the balance of method there we go and that is the 1 million and if we if we want to we can verify this it's kind of difficult dealing with these large numbers I'm just going to paste it down here and yep that looks the same so I have confidence that that's actually one million represented in solidity as 1 million plus 18 zeros all right so let's try a quick transfer operation so what I'm going to do is I'm going to transfer some tokens from my wallet to my second wallet here so I'm going to go ahead and select that and I'm going to grab the address okay so you can see that the transfer takes an address to and then an amount as the second parameter so paste in that address give it a comma and then let's go back to our notepad and we'll first create the number here so I'm just going to create I just want to transfer one token okay so for from my first wallet to my second one so remember that's one plus eighteen zeros so I'll say one two three four five six seven eight nine ten one two three four five six seven eight okay so this is going to be our one token copy that I'm going to go back over to remix and where'd it go add it as my second parameter here all right great so I'm going to go ahead and click on transfer oh and it failed and the reason for that because I'm still on my second account so if that happened to you go ahead and switch back to your first account okay and try that again all right so in order to transfer tokens using the transfer method I have to be the owner of the wallet obviously that'd be a bad thing if anyone could transfer tokens out of your wallet so it looks like it worked that time and we can verify by going let's select wallet number two again and again I'll grab the address and we'll paste that into our balance of function so I'll clear out the previous address and we expect this to be the equivalent of one token all right so let's grab this big number here and that looks correct but just to make sure I'll go ahead and paste it back in notepad underneath my input and that's correct one token all right great so as you can see that was a pretty simple to just get one token up and running it's very simple very basic it doesn't have any bells and whistles it's got a name a ticker symbol an initial Supply and the ability to do all the operations that you would expect to be able to do on a token per the erc20 spec all right great so with that out of the way we're going to now shift gears and create a proper project using hard hat and create a more robust version of our token all right guys so the next step is to set up our actual project and we're going to be using the hard hat framework if you haven't used hat hard hat before it's used for helping with deploying to the blockchain provide some testing infrastructure and just overall helps organize our project files so what I've done is I've created a new folder that I want to work in and I'm going to go ahead and open up that folder in visual code of course you can use any editor you want um I always recommend Visual Studio code though so that's what I'll be using so once you've opened your folder in your editor let me go ahead and close out of this and I'm going to open up a terminal okay so make sure you're in the root directory of wherever you want to be working and I'll go ahead and create my project folder so I'll say maker I'm going to say let's see ocean token all right perfect now all CD into that directory and once I'm there I'll just do npm init okay and this should generate my package Json all right and I'm pretty much just going to go through and accept all the defaults here yes yes yes yes okay all right I'm fine with that and let's see yep and there's our package Json okay so that's what we were after there all right next step is going to be to install hard hat so I'm going to say npm I for install dash dash save Dash Dev because we want this installed as a Dev dependency and then I'm going to say hard hat all right and this will take a minute so I'm going to pause the video and come back once this is complete all right hard hat installation is complete and so the next thing I'm going to do is invoke hard hat via npx hard hat to go ahead and create the initial project scaffolding so it should create some a bunch of files and folders and things like that good stuff so I will let that run there we go and I'm just going to go ahead and select that default of create a JavaScript project do you want to add git ignore I'm going to say yes okay yeah and I selected JavaScript because I don't I don't really feel like the additional overhead of typescript is necessary in a project like this which isn't really even JavaScript based to begin with so all right now it may give you a bunch of um like suggestions to install certain dependencies I think that I've got a lot of them already installed so what you're saying your list may be bigger than what I have here um especially if it's the first time you're doing this but anyway I'm just going to go ahead and grab whatever they recommend here and and just run that make sure all those dependencies are installed all right great so those dependencies are installed and the final thing that we've got to install well maybe not final until we find out we need something else but final for now is to install the opens up on contracts so npm eye for install and then it's going to be at open Zeppelin two P's I think yep slash contracts and that looks right yep go ahead and run that and that's going to pull down all of the different erc20 contracts that we might need to use we've just seen one so far by the way the core erc20 contract but as we'll see we're going to use a few more there's some other ones that add you know even more additional functionality that we can use in our in our token contracts all right so that's the initial setup so let's just kind of take a look and see what we have here so you should have some sort of top level token folder ocean Dash token in my case and underneath that I've got some contracts so let's go ahead and this is just a sample contract that a hard hat generated for us lock.sol let's just go ahead and delete that file right off the bat we're gonna we're gonna create a new one so delete that and then let's also delete the corresponding test file so under the test folder should have a lock.js so let's get rid of that we don't need that file okay and so let's go back up to the contracts folder and let's create a new file for our token contract so I'm going to say ocean token .soul all right and let's just go ahead and copy the paste and paste the code that we previously had that we're using in our remix POC so I'm going to go ahead and just copy all this as a starting point okay and I'm just going to paste that into my new file now here we we no longer need this Remote Link pointing to GitHub we can now reference the locally installed files all right so let's go ahead and we can delete everything up until the contracts folder just like that and then we can just reference the top level open Zeppelin folder so I'm going to say at open this is at Berlin forward slash contracts that should be correct okay so before we even start coding our token it's probably a good idea to take some time and think about token design there are a couple of different things you might want to take into consideration based on the utility and purpose of your token for example do you want to Mint some kind of initial supply of tokens at the time when the contract is deployed a common pattern is to immediately send a percentage of the initial supply to the contract owner so they can hold those tokens and do what they want with them for example use them to create liquidity pools on uniswap or other exchanges or to send them directly to other people another thing to think about is Max Supply do you want there to be a capped Max supply of tokens if there's a fixed Supply that might give more stability to the value of the token by creating scarcity Bitcoin for example is capped at 21 million coins gold is capped by the Limited Supply that exists on the planet so scarcity provides value to assets on the other hand if this is a token to be used in a game you might want the supply to be unlimited so that the tokens can be used and generated in the game indefinitely then consider what your minting strategy is going to be how will you introduce and distribute new supply of tokens into the market in this example we'll Implement a mining reward we'll send a small amount of tokens to the wallet corresponding to the node that includes a block containing any transactions of our token into the blockchain how much should the reward be and should we Implement a method to change that reward finally do we want the ability for token holders to destroy or burn any amount of the tokens that they hold sometimes this is used as a strategy to increase the value of the remaining tokens in the market similar to a stock buyback all right so let's go back to the code editor and see how we can Implement all of these things so let's take some notes on our token design and then take care of each one of these items one by one in our code as we go down the list so I think it makes sense to consider the first two together since they're sort of interrelated our initial Supply this is how much we want to initially generate mint and then send to the owner but this may depend on the max Supply and I think we're going to go ahead with the capped Supply I think that's a good idea and so let's say for this project we want a capped supply of 100 million tokens okay so there will there will never exist more than 100 million ocean tokens is what we're saying here basically and we're going to enforce that through our smart contract code now out of those 100 million potential tokens and of course you can choose whatever number you want this may be way too high since Bitcoin only has 21 million but I think that's probably a little bit low considering that most Bitcoin transactions are done in like fractions with a lot of zeros in front of the numbers so but anyway let's say that we want our initial Supply to be you know 70 of that so we'll say we'll go ahead and send our owner which is me or which is you 70 million out of 100 so that's 70 million that we can choose to do whatever we we want with send to our friends and family or create liquidity pools on uniswap and then the remaining 30 million will say We'll allocate for our rewards our reward minting system all right so with that said let's flip back over to our code editor all right so I just want to make one quick change here I want to declare an owner variable so that we can use that throughout the lifetime of our smart contract to refer to the deployer of the contract so I'm going to say address payable public owner okay and then I'll make sure I'll set that in the Constructor owner equals message dot sender and then I can refer to I can use owner throughout the code further okay for example right there okay great so let's work on the initial Supply so we said we wanted to send 70 million to the owner so what we can do there is actually a variable that erc20 provides that gives us the number of decimal places that the token has so remember we said it was 18 by default but still it's good if we can refer to that via the variable rather than writing it out every time so if we go back to remix we actually have a reference to that variable I can show you and it's right here decimals so if I click on that and that confirms that we have 18 decimals which is the default for our token so we can use that variable here I'm going to get rid of the parameter here because this is a one-time action so there's really no value in having that a Constructor parameter so we're just going to hard code it right here where we had initial Supply so get rid of that variable reference and here's how we can write it so we can actually write 70 million as we normally would in human readable form so let's just verify that's one two three four five six plus seventy okay and then we can say times asterisk for time for multiplication and then in parentheses we can say 10 to the power of whatever the decimals are so we can do the exponent with two asterisks and then we can say decimals and call that function just like that okay so we can use this pattern throughout our code to take the place of the 18 zeros that we normally would have to type out by hand so that's error prone this is much easier to see more readable and so that will give us the 70 million plus 18 decimal points and also it's great because we can just write like normal human readable numbers in our code which is a lot easier to reason about than the 18 zeros plus you're already dealing with big numbers like in the Millions okay so that checks off the first item in our list okay so I'm going to say that's done and we move on to uh implementing a capped Supply all right so the next item we want to check off the list is the max Supply so we said we wanted to cap our back Supply at 100 million tokens so luckily this is pretty easy to implement as we can lean on another open Zeppelin contract so I'm going to go ahead and copy the import statement and just paste another one just like it right below and we're just going to modify this so I'm going to delete all the way back up until it says token erc20 slash and then this is considered an extension so this lives in the extensions directory and then the contract we want to import is called erc20 capped ERC the number 20. capped.sol all right so now we need to inherit from this so I'll just go ahead and copy and paste the name of it right down here and what we can do normally if we're inheriting from two different contracts we could do it like this with a comma but erc20 capped itself inherits erc20 so we don't need to State it again here we can simply get rid of the erc20 and just have contract ocean token is erc20 capped so now that will include both of these all right there's something missing here that way to go don't forget your semicolon at the end there go ahead and save that file all right so basically what this uh what this contract does is it takes in a number to represent the Caps Supply and it just ensures that checks are run so that whenever somebody mints more tokens if that capped Supply is exceeded it'll refuse to complete the mint and it'll return some kind of error message all right so that's the functionality we get here so let's go ahead and create a new Constructor parameter because we're going to say we are going to pass this one in okay so I'll say cap I can't spell there we go you int you win 256 cap all right so we're passing that in as a Constructor parameter and then what we have to do here is we have to make a call to the erc20 capped Constructor passing in that value so I'll see I'll say ERC 20 capped parentheses all right and so I'm going to directly pass in that cap now we want when we deploy our contract and we have to specify this capped I don't want to I don't wanna have to worry about the 18 decimal places so I just want to enter in 100 million that number directly so what we can do is we can reuse this logic here so I'm going to say cap times and then in parentheses 10 to the power of 2 asterisks decimals and call the decimals function just like that all right so it's exactly the same as this code we have down here all right and that is all we need to do to implement a cap all right so number two is done all right so the next one number three we said we wanted to make the token burnable fortunately this is another real easy one so we just need to inherit from one more contract so let's go back over to our code editor and this is another extension so I'm just going to copy and paste this import statement right here and I just need to change the contract name from erc20 capped to erc20 burnable okay and finally we need to make sure that our contract is inheriting from this so I'll say erc20 notice the comma there your C20 burnable go ahead and save the file and we can mark this one as complete so yeah the burnable mechanism is great because you know I've created this Max supply of 100 million gee maybe that's too high I don't know but later on if I do decide that's too many I can burn as many tokens that I control so I think if you're unsure about where to set the mech Supply it's always better to err on the side of too high rather than too low because you can always burn tokens later but you can't always add them all right so the final item to take care of in our token design document is to create a block reward to distribute new Supply to miners all right so here's how this is going to work we're going to have a new variable called block reward and we're going to use a hook called before token transfer now we can extend this hook to add any functionality that we want to occur before each transfer of our token and so what we're going to do is before each transfer we're going to go ahead and mint so we'll have another function called mint Miner reward and we're going to Mint some new tokens uh depending on based on the Block reward and send it to the node who is actually including this block containing the transfer into the blockchain all right so this is the one that'll require the most coding out of out of these four things but it'll probably be the most fun to implement so all right let's get started let's go over to our editor and so the first thing I want to do is introduce a new um static variable and so I'm going to say you you went 256. public block reward reward singular there we go okay and we'll set this in the Constructor so this may be another parameter that will pass in at the time of contract deployment so I'll say you in 256 and let's just say reward okay and then we'll initialize that down here so again I want to specify reward as a human readable kind of number so we'll use the same pattern in fact I'm just going to copy and paste what we have here now that we kind of understand this pattern to save a little bit of time and oops I've got one too many parentheses there we go all right great so now we're going to set our block reward when the contract is deployed let's also create a function to set the block reward if we decide we want to change it later maybe we're giving away too many tokens right so let's say new function set block reward okay so again this will take a uint256 and we'll say reward okay this can be a public function that's fine but we do want to gate this so that only the owner can invoke this function right we don't want just anyone off the street to be able to change our reward because very bad things could happen so we want to set this to the owner but we want to probably create a modifier that we could reuse that kind of captures the owner so let's go down here and we'll create an access modifier an access control modifier so we can use the keyword modifier we'll call this only owner which is a common pattern and then the syntax here is to have a require statement and we'll say message dot sender equals owner and if that doesn't resolve to True we'll say only the owner can call this function okay great and finally just a bit of weird syntax here you need to underscore and a semicolon and that kind of just takes the place that that's kind of a placeholder for the rest of the function so once we apply this to a function it'll read this line first and then the rest of the function all right so anyway go back up to set block reward and after the public modifier we'll place our only owner axis modifier great all right so within this function we just need to have a simple setter so I'll say block reward that's the variable we're setting the state variable and again we'll say reward times 10 to the power of decimal decibels plural and finish that statement with a semicolon great that's a Setter so we don't need to return anything anything okay that looks good all right next function we need to create and I'll just go right above that one is mint minor reward right so again I'm going to say function and I'm going to say underscore mint minor reward this is going to be an internal function okay because we don't want this to be called from from the outside from outside of this contract okay and I'll say mint and so this is the parameters here are two an amount right so this who is this going to who is the the recipient of this reward it's the account of the node who is including the block into the blockchain so we can access that with the global variable block Dot coinbase okay and then the amount is going to be whatever block reward is and semicolon great okay and the the mint underscore that kind of just uh signals that we're calling a function from a derived contract or from an inherited contract all right so finally we said that we can Leverage The before token transfer hook well I'm calling in a hook I don't know if it's officially a hook but it's a it's a function in the erc20 contract and we can extend that by overriding it so let's go below mint Miner reward and I'm going to create a new function and I'm going to say underscore before token transfer okay and this is going to take three parameters so it's going to be address from address 2 and a value address from address 2 and a un256 value okay again this is going to be similar to minor reward internal internal function and we're going to say virtual override okay which is the Syntax for overriding a function okay and we want to say we want to call Min to minor reward right but there are a few things we want to check before we allow that function call to go through so we're going to wrap this in the if statement okay so we're going to say if from is not equal address zero and address zero if you haven't seen that before that this is just a check to make sure that it's a valid address okay and next check is going to be two is not equal to block coinbase so can you guess why do you think we need this check right well this is to make sure that you know when we actually send the reward for that transaction we don't want to send another reward for the reward right because then we're basically an infinite Loop so we got to stop it at some point so that's what this check is for and one more let's say make sure that block dot coinbase is also a valid address so I'm going to say is not equal to address zero because we don't want to send our newly minted tokens to Nobody Until to the black hole so all right let's close that if statement and then let's go ahead and put mint Miner reward inside that oops all right and finally just underneath that if statement we need to call Super dot underscore for token transfer should be using my intellisense more all right and from to value perfect okay so that that should take care of our minor reward logic there's one more thing I want to do and that is just add a destroy function here in case for some reason we want to destroy this contract at any point in the future and this is really nothing specific to tokens or anything like that just gives me the ability to basically destroy zero out this contract if I need to at any point in the future so and if I don't have this function here then it'll it'll just live forever in infamy so that's sort of my escape hatch all right just good practice I guess great okay so that concludes our token design so I think next step is going to be to just look at deploying and testing what we have so far all right so the next thing we can do is actually compile our code and see if the compiler comes up with any kind of errors that we may have missed before so I'm going to go ahead and CD into my top level directory make sure you're at your top level token directory and then well before we run the compiler let's go ahead and pop open hardhat.config and we just need to take a look at this solidity version right here and make sure that matches what we have in our token in our token code so if you remember we specified 0.8.17 so let's go ahead and bump this up to 17 to match that otherwise that'll give us an error right off the bat when we try to compile okay so I'm going to run npx hard hat compile that should compile our code big surprise and let's see what it comes up with here all right couple of errors in the red here we can kind of ignore that um warning in yellow it's saying you know we're using the very latest version there may be a few features not supported so we won't worry about that until we run into explicit problems hopefully we won't but uh let's see okay so basically what this is saying here is that we were inheriting for from two or more classes that Define the mint function and so the compiler doesn't know which one to use when we call it so what we've got to do is explicitly Define a mint function in our code overriding the the ones that are inherited and I think they're coming from erc20 capped and erc20 so remember when we added the capped Supply we said that one of one of the sort of benefits that we get from inheriting from this contract is it adds some additional checks around the mint function so that means we want the mint function from this contract right here so I'm just going to grab it from there and paste it in here um so you can drill down into open Zeppelin this is in the node modules folder I'm going to go down to token erc20 extensions and capped so we'll go ahead and pop this file open and I'm going to go ahead and copy this entire function and I'm going to just paste this right underneath the Constructor now just one more thing we have to add here we have to specify which contracts were overriding so I'm just going to call override pass in erc20 capped comma erc20 all right that looks good and there was one or two other things here let's take a look oh message sender okay I think I know what that's about so we basically set up the owner variable to be of type address payable but we forgot to cast it or I forgot to cast it to payable here because message sender could be just of type address so I'm going to say payable and then pass in message sender in parentheses here that way we ensure that owner is receiving a payable address right so go ahead and save that and I think that's all we had so I'm going to go ahead and compile again all right no output so that's a good sign that means our code has compiled successfully all right guys so the next step after compiling our code is going to be to write a couple of unit tests so I've written seven or eight unit tests to get us started here and what I'm going to do to save us a little bit of time is sort of copy each section section by section and then we'll go through it and discuss and then my challenge to you guys would be to write additional unit tests above and beyond what we have here to cover as much functionality as possible all right so let's get started by going into the test folder and let's create a new file and we're simply going to call this ocean token.js okay because our test will be written in JavaScript using mocha and chai so once you've got that blank file go ahead and pop that open all right so first thing is to add a couple of imports that we're going to need to use so I'm going to say const expect this is our test assertation that we're going to import from chai the chai Library okay and then we're going to need to import hre which is the hard hat runtime and we need this because it contains a version of ethers.js the ethers.js library which is a JS Library we can use to interact with our smart contract okay so this is going to come from hard hat there we go and those should be the only two Imports that we need all right so the rest of the tests should look very familiar if you if you're used to working with mocha and so we'll start with a describe block which is going to describe this overall Suite of tests so I'm going to say ocean token contract and before I close this I'm going to say function curly braces and then finally don't forget to close it with yeah with end parentheses right there okay so this is the basic structure next thing is to designate some Global variables that we're going to use throughout the tests all right so these are just some initial variables that we'll be using one to represent the token the token instance the owner a couple of different wallet addresses that we can play with to test transfers and then some of the initial parameters that we need to pass into the Constructor if you remember the overall Max cap and the block reward all right next we need to instantiate the contract to create an instance of it before each test runs okay so again if you've used mocha then you'll you'll probably be familiar with the before each block this runs before each individual test and what we're doing is we're getting an instance of the ocean token contract and we're setting up a couple of wallets and including the owner wallet for use during the tests and then finally we're deploying that contract okay here are the first block of tests and basically the purpose of these is to just ensure that the state of things is correct after deploying the contract uh by the way don't worry if um you can't write all this out so quickly because I'm pasting the code here all of this code will be available in the project repository so you can just copy it from there modify it to match your token and then add any additional tests that you can think of so first basically we're making sure that the owner is the the one who deployed the contract then we're ensuring that the total Supply is equal to the owner balance so important distinction between total Supply and Max Supply Max Supply refers to the total potential supply of the tokens in this case 100 million total Supply refers to the actual amount of tokens that have been minted thus far so 700 million and then the last two are just checking that the max cap and block reward variables have been set properly all right and this is the final block of unit tests here and basically these are testing transactions making sure that one party can send tokens to another party so the first one is actually doing a transfer of 50 tokens from wallet one to wallet two and making sure that the the balance is reflected the next test is making sure that if we attempt to transfer but the wallet doesn't have enough tokens that the transaction is reverted with the correct error message okay transfer amount exceeds balance and then finally we're doing some transfers two different transfers at the same time and making sure that those balances are updated all right so next let's go ahead and run those tests so I'm going to type in npx hard hat test let's run that all right there's the contract deployment and there are seven passing tests great all right so yeah as you can see it's pretty easy to go ahead and run those tests and everything is looking good so far all right so that was a very quick rundown of some unit tests again that code is going to be available in the project repo and if you guys have any questions as always feel free to reach out all right for now let's move on to the next step which is going to be to deploy our smart contract to the blockchain all right so let's go ahead and start configuring our project for deployment the first thing I'm going to want to do is go up to the top level folder and I'm going to create a new file um it's going to be a DOT EMV file and this file is going to basically hold two variables that we'll use for deployment the first one is private key that is the private key of the wallet that you'll use to deploy the smart contract the second is the inferior rinka B endpoint so two things here first we'll be deploying this contract to the rinka B ethereum test Network all right the process to deploy to mainnet is exactly the same there's just one configuration difference which I'll show you so and then inferior basically is a gateway that allows us to interact with a remote ethereum node in order to do our deployment so I'll show you what that looks like in just a minute for now we need to go ahead and install the EnV package so I'll say npmi.env go ahead and run that while that's running I'm going to pop open hard hat config and I'm going to add a require statement right here on the second line to bring in that EMV package okay and then I'll have to do a DOT config call right here okay oops semicolon all right the next thing I'm going to want to do is go under module exports underneath solidity and we need to create a new entry called Networks okay and this is going to be an object this is going to contain our network configuration so any of the ethereum networks that we want to interact with for deployment need to be defined here so I'm going to say rank a b create another object this is going to create contain two properties URL and so I want to get this value from my DOT EnV file via the dot EnV package so I'll say process.env Dot keyname and that's going to come directly from this file this is the inferior we could be endpoint so I'll copy that which is always good practice to copy directly and then the next one is going to be accounts okay and this is going to be an array of one item and here we'll reference the account private key so I'll just copy and paste process.env here and I will grab the key which is private key from our.unv file all right let's go ahead and save that now let's look at how to actually get those two values so that we can populate our DOT EnV all right so we said the first one is the private key of our metamask wallet so I'm guessing most of you guys use metamask probably and know how to install it but just in case if you've never seen it before just go out to metamask.io download the installer follow the prompts it's super easy and that will get installed as a browser extension to Chrome so from there you can just kind of click on it right here to open up metamask and so I'll show you how to export the private key now this is a wallet that I've just created for this tutorial so I can actually show you this sensitive information here because I'll never use this again but basically what you want to do is click on the three dots here go down to Account Details go down to export private key type in your password okay so this is my private key so I'm going to click on that and I guess you just have to click once to copy it I'm going to bring that back over to my EnV file and paste it right here okay so you guys do the same so one quick aside here this is really important so this is something that you never want to expose to anybody else all right so since you're you know copying pasting this into a file in your repository there's a danger that this could accidentally make its way into GitHub if you ever decide to you know create a repository out of this so you want to make sure that you have a DOT git ignore file and you want to make sure that that file contains an entry for DOT EnV okay and so that will gate that file from ever being able to reach you know a public repository so definitely make sure that's in place all right next is the inferior rinka B endpoint Oh and before we go any further actually um you guys will need some test ether for the Rincon B Network so make sure that you go up here and you select the rinkabee test network from the drop down and if you don't see all these options if you just see mainnet which is the default go ahead and click on show hide test networks all right and then you can go out to rinconbosset.com you enter in your private or sorry your public wallet address which is visible right here so this is a public key this is totally fine to to show you want to paste that here and click on send me eth and that will send you some test ether to work with all right next if you don't have an ethereum sorry if you don't have a inferior account go out to inferior IO even if you do have an account you're going to need to log in so I'm logged in right here this is the dashboard it's showing me all my projects so what I want to do is create a new key which really is creating a new project for Network I'm going to select web3 API and I'm going to give it a name option token go ahead and click on create all right and here's my API key and more importantly scroll down a little bit or if you don't need to scroll then just go ahead and select rinkabee from the drop down Okay so we're looking for the endpoint for the Rincon B Network which is this string right here so go ahead and copy that and bring that back into your EnV file and paste that right here okay go ahead and Save now let's create our deploy script so I'm going to expand the scripts directory and double click on deploy and this is already set up to deploy I guess the default solidity script here so we can delete a lot of stuff in here I'm going to basically get rid of all those comments I'll leave the import because we need that I'm going to basically delete the entire body of this function right here of the main function and I'm just going to delete these comments clean this up a little bit and we'll leave that last part main dot catch all right so let's go inside of our main function and so first we need to reference to our contract and then we need to deploy it so I'll say ocean token with caps a weight hre dot ethers dot get Contract Factory and you might recognize this oops get contract Factory you might recognize this from our test Suite that we just worked on because it's pretty much the same code and so I'm going to say ocean token all right now this is going to be a reference to the deployed instance so I'm just gonna say ocean token but with lowercase first word and then we'll does that look right yep okay again this is going to be a weight and now we're going to reference contract wait ocean token dot deploy and now we need to pass in our deployment parameters into our Constructor so that was the total cap which is 100 million one two three four five six and the block rewards so let's put the block reward at 50. and you can use whatever numbers you want here I'm just sticking with the example here Okay so return below that and let's say await ocean token deployed okay so any code that we write below this basically will be ensured that that contract deployment is complete so then we can console log some information here and let's say ocean token and you can put whatever you can you know make this be whatever you want basically this is just some assurance that our deployment is successful and then I'll say ocean token dot address and that'll give us the ethereum address of our deployed contract all right go ahead and save that file all right so let's go ahead and attempt to deploy our contract to Rincon B and I say attempts because something usually always goes wrong the first couple of tries and I haven't done this in advance so I really don't know what's going to happen here so what we'll find out basically if we've set up everything there's a lot of steps so it's easy to overlook something or miss something but anyway we can Target anything that we've defined in our hard hat.config which in this case it's rinkaby so let's go down to the terminal and here's the command to deploy its npx hard hat run dash dash Network and then rinka B this corresponds to what we have in our config right here we could be and then space Scripts Dash deploy or forward slash deploy .js all right so let's give this a try and see how it goes oops okay all right false start if you see this uh you're in the wrong folder so make sure you're in the top level project folder that was one level above that so let's run that again all right and it actually looks like that worked uh the first time well except for that first run where I was in the wrong folder but um cool so ocean token deployed here is our logging that we added what I like to do usually is just go ahead and grab this address that's the address of our deployed contract and I just like to save that and a notepad somewhere so say ocean token rinka B and just save that for reference all right very cool so I'm going to go ahead and grab this address and we can go ahead and verify a couple of things so first I can close a couple of these tabs here I want to check my wallet and see if I have 70 million tokens so I'm going to go ahead and pop open metamask and we won't see our token right off the bat because we actually need to import it so go ahead and click on import tokens and yeah make sure it's custom token the UI just changed you had to previously you had to click on another tab to get here but right now it just opens up so OCT so see it's reading in our ticker symbol from the blockchain based on this address and it's reading in our decimal places so I'm going to go ahead and do add custom token and there we go 70 million OCT that is so awesome so that that part looks like it worked well another thing we can do and now we should see this in my main token list um where is it maybe I didn't click on save or something custom token import tokens okay yeah I think that's the step I missed there it is get out of here okay great so now I'm seeing my OCT in my main token list in metamask so that's the first step now another thing we can do is we can go out to etherscan and take a look at our deployed contract so let's go to etherscan.io all right so I'm going to type in my contract address and search for it actually we there's a rincabe etherscan but previously it would sort of give you a link it would say hey we found this address on another chain but I'm not seeing that right now so I'm just going to explicitly um go to either scan rinkabee and we should be able to find it here so rinkaby.etherscan.io and let's try that search again and uh here it is see we're seeing token tracker ocean token OCT and there's my Creator address and there's one transaction which is what we'd expect because we had one transfer of the 70 million tokens to my wallet so I can go ahead and drill into the transaction hash here and inspect the details of that transaction so we can see it was successful it was about 13 minutes ago that sounds about right here's the block that it was added to um token transfer 70 million tokens and there we go so that is that initial transfer and we've got more details like the gas price transaction fee and things like that cool so everything seems to be working well so far now the next thing we can do is try to send some of this token from one wallet to another so we can actually do a test using metamask so I'm going to open up metamask all right so um Let's do an experiment and I'll try to send a million tokens from my account one to my account two so if I click on this little icon here I can oh I need to create an account too first okay so I'll do create account I'll just call it default account two that's going to generate the uh the key pair for me and I will grab the public key or the wallet address once that's done so I can do that by just clicking on this little copy icon here now I'm going to switch back to account one because that's the one that has all the tokens and I'll do a send I'm going to paste in my address of account two and I'm going to switch this to OCT for the asset and let's do 1 million one two three four five six okay next all right I'm just gonna have to accept the gas fee and sign the transaction by clicking on confirm okay so that that is pending of course that takes a while to settle on the blockchain and once that block has been added to the test net blockchain then we should see this update to completed and then we'll check the balance in our account too all right looks like that's gone through so I'm going to switch to a well actually first let's verify that we have less tokens now I'm going to go to assets perfect 69 million just what we would expect now I'm going to switch to count two and again I'm going to have to import the token I'm not sure if I still have this yeah oops it's my wallet address make sure that you're we're adding the contract address which is right here we'll grab that and great yeah just always verify that it's the correct symbol and everything so you're adding the right token so I'm going to click add custom token import token and I can already see that I've got a million tokens so that transfer went through correctly that is great good sign that everything is working now let's go back to etherscan and see if we can see that transaction so I'm just going to hit refresh here scroll down a bit yep and there's my other transaction so I'll click on oh yeah it's the most recent ones on the top so I'm gonna go ahead and click on that one if I scroll down a little bit I can see 1 million ocean token great perfect all right so our transfer seemed to be working just fine now the next thing I would expect to see here is another transaction for the block reward which I'm not seeing I'm not sure if that's just a time thing if it hasn't settled yet but let's pause right here and I'm going to check this in another in another minute or so if I'm not seeing it I'm going to go back to my code and see what could be wrong there all right but everything else seems to be working well so far okay what I just remembered was that I went through the exact same problem with the first token that I ever deployed and that I wasn't seeing the block rewards and what I came to realize or find out was that I believe most of the test networks don't have the full implementation of proof of work or proof of staking and so we don't really get that same mechanism that we can hook into for the block dot coinbase in order to identify the exact account that's um you know created the Block in and added it to the blockchain so it does work though on mainnet so don't worry too much about that if you if I deployed this to mainnet right now I have full confidence that the block rewards would be working and what I'm going to do is I'm going to kind of show you guys how that looks I can show you the first project I ever did um it's IH ichc token yeah and I made a front-end website for it um 90s geocity kind of website and so this was just the first experiment I ever did working with blockchain nothing serious that I actually tried to promote or anything like that just to kind of go through the motions of creating my first smart contract and so anyway this is the front end for it and you can buy it on uniswap and then there's a faucet to give away free tokens and then this is what I wanted to show you view on etherscan so if I look at the transaction history of this token I can see the block rewards yeah so you see like after every transaction there's another transaction with 25 so I had set my block reward to 25 tokens and this one is uh to ethermine F2 pool old these are names of mining pools and so that's who the the reward sent to because somebody a member of that pool basically mined that block and so I think the way these polls work is they they split the rewards evenly uh just they distribute the rewards amongst all of their members so anyway this is what it'll look like and I went through the same process where I first deployed this to rincabe and I wasn't seeing these 25s these uh block rewards but once I deployed it to mainnet I was seeing them so um and the code that we used in this tutorial is exactly the same in terms of the mining code anyway it's the same as this contract here so rest assured that should work once you push it to mainnet alright guys so that is really all I wanted to cover in this video if anyone has any interest in creating like a front-end app for your token where you might have a button to buy on uniswap or other exchanges or even a faucet to give away your tokens for free then let me know in the comments and I you know I might consider making a follow-up video to cover those kind of things anyway thanks a lot for watching and if you guys enjoyed this kind of content I invite you to please consider subscribing to the channel I'll be doing a lot more uh smart contract related videos in the future I've got a lot of ideas for for new videos coming up soon I also might do some more um General programming Python and go and things like that JavaScript but anyway I'm always receptive to new video ideas so let me know in the comments and other than that hope you guys enjoyed it have a great day and I will see you in the next one peace
Info
Channel: Block Explorer
Views: 122,051
Rating: undefined out of 5
Keywords: erc20 token tutorial, create your own cryptocurrency, create crypto token, erc-20 token, erc-20 tutorial, create your own erc-20 token, create your own token, erc20 smart contract, erc-20 smart contract, solidity tutorial, solidity smart contract tutorial, erc20, erc-20, crypto smart contract, crypto token, hardhat tutorial, smart contract deployment, create erc20 token, create erc-20 token, create token on Ethereum, Ethereum token, blockchain tutorial, erc20 token
Id: gc7e90MHvl8
Channel Id: undefined
Length: 73min 55sec (4435 seconds)
Published: Mon Sep 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.