Create ERC20 token on Ethereum (the EASY way)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you ever dreamed of creating your own cryptocurrency if you try to bitcoin or ethereum it's gonna be really hard but with erc20 tokens you can create your own virtual currency it's way easier anybody can do it in this video we are going to code an es20 token step by step on the ethereum blockchain this is a beginner level project we're going to use a simple tool and you don't need to install anything on your computer if you don't know me i'm julian and on into blocks i teach blockchain development quick service announcement there is only one way to contact me it's by using my email in the description don't be fooled by scammers who try to impersonate me in the comments an es20 token is a virtual currency that lives under ethereum blockchain contrary to the native coin of ethereum ether anybody can create his or her own es20 token when you create your token you can allocate as many units to yourself each tokens lives in its own smart contract if you never heard of smart contracts these are the small programs that run on the ethereum blockchain arc20 is a standard for tokens in theory you could create a token that does not respect the es-20 standard but if you do so no exchanges will be able to deal with your token that's why it's best to stick to the erc20 standard this is the erc20 standard the basic idea of erc20 is that each address has a balance of tokens and each address can transfer its tokens to another address the standard of es 20 describes the functions that need to exist in the smart contract of your token if you never heard of functions you can think of them as a way to read or write data to a smart contract there are three functions related to the metadata of your token so that's the name of the token the symbol which is an abbreviation to identify your token on exchanges we sometimes call this the ticker and we also have the decimals function decimals define how many fractions of a whole token can exist for example if the us dollars was an erc20 token the decimal's parameter would be two if if there was an es20 token the decimals parameter will be 18 and most tokens also use this convention for the decimals parameter this is only used by wallets to display the correct number of token that you own but the other functions on the smart contract do not use the decimals parameter then we have the total supply function that gives us the total number of tokens then we have the balance of function which gives us the amount of token owned by an address then we have functions to transfer tokens the most simple is just transfer you specify the recipient address and the amount of token the other function to transfer tokens is called transfer from this is what we call a delegated transfer which means the owner of an address can allow another address to spend tokens on his behalf it's similar to when you allow a credit card to spend the money of your bank accounts except that for yes 20 tokens each spender has a maximum spending limit after which is not possible to spend any more when you call transfer from you first specify the from address the recipient address and the amount then we have the approve function where the owner of tokens can allow a spender to spend some tokens on his behalf then we have the allowance function that tells us how much tokens were approved to be spent for a combination of owner and spender and after that we have events but these are less important [Music] there are different languages for creating smart contracts on ethereum the most popular one is called solidity and i recommend to use this one the syntax of solidity looks like a javascript but it's very misleading because solidity works very differently from a javascript first of all solidity is a compile language whereas javascript is interpreted this means that you can run javascript directly whereas for solidity there is a compilation step another big difference is that solidity is a statically typed language whereas javascript is dynamically typed what does it mean in any programming language we represent data by using variables each variable has a specific type for example in javascript we can represent a text by using the string type and we can represent a number by using the number type even though we have types in javascript this is quite transparent to the programmer because we don't have to specify which type we use for each variable the javascript compiler is smart enough to figure it out in the case of solidity we have to specify the type of each variable for example if you declare a variable of type number you have to explicitly declare that this variable is going to be a number it takes a bit more effort than in javascript another big difference between the two languages is that solidity is much more limited in solidity we execute our code on the blockchain and we have to pay for each transaction that's why we try to keep our code simple and solidity has much less features and built-in function compared to javascript another big difference is that with solidity we can transfer money this is a built-in feature in javascript we can do it too but this is not a built-in feature we have to use an api like stripe or paypal which means we rely on their permission to use the api in 380 we can move money freely no need for permission talking about permission another difference between solidity and javascript is that in solidity you can interact with any smart contract and deploy on ethereum you don't need any permission this property of smart contracts is called composibility in comparison with javascript is not possible to interact with some javascript code running on another server unless it has an api and it lets you access it last big difference between solidity and javascript is that with storytip once you deploy a smart contract to ethereum you cannot update your code anymore if there is any bug any security vulnerability this is too late that's why testing and security are very important in solidity [Music] in this section we are going to code our erc20 token so for that we will go to remix remix is an online code editor for solidity the programming language for smart contracts remix is very easy to use you just have to load this url in your browser and that's it you are ready to create spot contract in solidity on ethereum the first thing we'll do is to go to the compilation tab here and we will select the latest version of solidity by default you always want to start with the latest version because that's the one with the latest feature and all the bug fixes so we're going to select 0.8.5 and we make sure that we have auto compile activated this is very important because every time you make a mistake in your contract you're going to have real time feedback then we are going to the file explorer here so you may have to create a new workspace if this is the first time that you use remix and after we are going to create a new file with this icon here we're gonna call this uh my token.source.so this is the extension for 3dt file and we're going to start with what we call the pragma statement you have this in every 3d file and here we specify the version absolutely that we are going to use 0.8.5 this matches what we've selected before in the compilation tab after we are going to define our spot contract with the contract keyword and we specify the name of our token so here it's a good practice to choose the same name as your file my token and after that we could code from scratch all the function and all the variables in our smart contract but if you do this it's really easy to make a mistake and introduce a security vulnerability instead it's a way better idea to take a standard implementation that has been reviewed and audited many times by the community there are several standard implementation and the most popular one is the implementation of open sibling a popular library for solidity this is the github repo of open zip link so we go there and we go in the contracts folder so they have many smart contracts that you can use in your project and we're interested in what they have in token and we're going to go in erc 20 then in erc20 dot sol here we have the standard implementation of es20 by open ziplink so they define the name of the contract and define some variable here and here we can see some function that we saw in the yes 20 standard like name symbol decimals etc so let's scroll up in the constructor and so we need to pass it the name and the symbol in the constructor so what we're going to do is we're going to copy this url then back to remix and we are going to import the url and we just copy it so here between single quote let's give the full url to our smart contract and here are this cut off on the video but i'm going to end the import statement by a single quote in a semi colon all right so now we have imported the es20 implementation next we are going to make our token inherit from es20 of open zipline and for that we use the is keyword then we specify erc20 and so now all the functions and all the variables that were defined in es20 of open zipping are available in my token so transfer transfer from etc we can access all of this we don't need to redefine them this being said we do need to define a constructor so this is this is a function that will be called only once when we deploy our token and when we call our constructor we can also call the constructor of es20 of open ziplink and there are two things to specify the name of our token so for example let's do my token and then the symbols usually the symbol is three letters so it can be my empty n and then we're gonna define an empty body between curly braces for the moment so just to be clear is when you deploy my tokens constructor is executed then erc20 constructor is executed as well we pass these two arguments my token and mtn so we go in the github rip of open zip link and so my token here is going to be in name and mtn is going to be here and after it's going to be assigned internally to name ncball so if you do this you will have your token but you're not going to do much with it because nobody won't have any token so usually after deployment you want to create all the tokens and send them to yourself and for that we can use another function which is called underscore mean so let's scroll down let's cool down where we have this uh underscore mint and we have to specify the account that receive the token and the amount so what's interesting here that we don't need to really understand what's going on inside we just need to understand the function signature that's it so let's copy this let's go back to our code and in the constructor we passed this underscore mint and so for the account we going to specify the address that has deployed the contract so that's uh msg.sender this is a building variable in solidity and for the amount well that's up to you you have to decide what's the total supply of your token so for example it can be 10 000 and usually for the decimals parameter we want to use 10 power 18 like most of the es20 token so here we're going to multiply this by 10 power 18 and we finish this statement with a semicolon and so now we're going to have 10 000 token that are maintained to msg sender i know that this decimals parameter that i specify here is very confusing for people so you really decide what you want to do if you want you can just say okay you know what my decimals number is one and so in this case uh i mean ten thousand token and that's fine but you might run into problem if you want to transfer some small quantity of token so usually we just leave this to 10 power 18 all right so this is a very simple token and it would work so you as the creator of the token you will have all the tokens and then it's up to you to distribute them in an ico in an airdrop and then people will be able to use all the functions defined in yes 20 so transfer transfer from etc but it's possible that you want to do something a little bit more sophisticated so maybe you want to be able to mint more token afterwards that means you don't have a fixed supply so in this case you can create a not there function called mint and you're gonna specify the recipient and the amount make this extra enough so that we can call it from outside smart contract and then we're gonna call again underscore mint and we're going to meet we're going to forward the two parameter that we received but if you do this it's very dangerous because there is no access control anybody can call this function and basically it's going to create a huge inflation and depreciate the value of the other holders and appreciate the value of the already existing tokens so to put some access control in place first we will need to define an admin so for that you will need to define a variable here so address public admin and here in the constructor we'll set the admin to the deployer of the smart contract and inside the mint function you can require that the center of this transaction is the admin otherwise the arrow message is only admin and if someone else tried to call this function this required statement is going to fail and the transaction is going to be cancelled that's how you do access control for minting your token and if you want you can have another function to allow people to burn their token so here let's create this burn and we specify the amount and this time is not going to be only the admin but anybody can call this function but they can only burn their own token so for example if the admin has a lot of token and is willing to burn his own to make all the other more valuable so that would be possible uh and so here we're going to specify msg sender and we specify the amount and so underscore burn just to be clear it's also defined in the earth 20 implementation of open zip link so this underscore burnt function in underscore means this is not part of the es20 standard it's just specific to the implementation of open zipping and if you undo more customization then you can inspect the code of yes 20 of open zip link and see if there are other functions that you want to use and also what you can do is you go to yes 20 you go to extensions and you will see some example for example erc20 burnable so here this is another wedge to the burn you also have a burn from uh what else do we have yep yes 20 capped so i think for a maximum total supply i think you can mint up towards an amount yeah here there's a required statement in the main function [Music] next let's go back to remix and i'm going to show you how you can deploy this so we go to the deploy tab and first you want to deploy this on a local development blockchain which is completely independent from mainnet the real network of ethereum so for that you select javascript vm and it's going to create a couple of addresses with 100 fake eater for each of them gas limits you don't touch it here we see our contract my token dot saw we click on deploy and after you will see here your contract so let's scroll down deploy contract and here we can see all the function of yes 20 even though we did not define all of them we can still access them for example transfer transfer from we never define them and we still have access to them so the arrange button are the function allow you to modify data and the blue one are the function to read data so for example let's try to get our balance so we go up we select uh we copy our address here let's go down balance off let's paste this uh okay let's click on balance off and we can see that we have 10 000 tokens so the reason why you see a huge number it's because you see the number of decimals which is a 10 power 18. and so let's try to okay let's let's copy this and we're going to transfer some of our token to someone else for example let's transfer all of our token to the second address so okay let's copy the second address i select back the first address because this is going to be the sender of the transaction of course you cannot send a token of someone else unless they call the approve function before so here let's go back to transfer and we after the comma we passed the second address okay we click on transfer okay and we have an error error encoding argument invalid address why you are not happy my friend oh i know why it's because the address needs to be the first argument so the address first then uh let's copy here this number so all of our token we're going to send all of our token to this address transfer okay so here you can see the green check mark so the transaction worked so now if you try to see the balance of the first address so let's copy this balance off let me see balance of the first address you should see now zero and now if you see the address of the second the balance of the second address so let's copy this now you should see ten thousand uh balance off uh okay and so after you can keep playing around with all these functions and when you want to deploy your smart contract to the mainnet of ethereum well the first thing you need to do is to install metamask which is a chrome extension that is a wallet for ethereum you create an address there you send some ether to pay for the transaction fee then here in environment you will choose injected provider then you will see a pop-up of metamask that asks you to connect your meta mask to remix then you select the the correct account here you click on deploy and then you will see a pop-up of metamask you click on confirm and it's going to send a transaction and deploy your smart contract for real in this video i'll show you how to create an erc20 token on ethereum everything you'll learn in this video is also applicable to bep 20 tokens on the banner smart chain button smart chain uses the same technology as ethereum and bp20 is just another name for es20 in this video we used remix remix is great for learning and testing new features of solidity but this is also quite limited and if you are serious about your es20 token at some point you will need to switch to a more robust tool like truffle truffle is a framework for trading smart contracts on ethereum or binance matching and a good follow-up to this video is to try to recreate your yes 20 token using the truffle framework and for this you can watch this tutorial on truffle on my channel i will see you there
Info
Channel: EatTheBlocks
Views: 40,046
Rating: undefined out of 5
Keywords:
Id: ZLFiGHIxS1c
Channel Id: undefined
Length: 22min 7sec (1327 seconds)
Published: Thu Jun 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.