How To Make Your Own Ethereum (ERC-20) Token In 30 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i'm going to be telling you how to make your own ethereum token that you can deploy on the ethereum blockchain before we get started making the actual token we need to download this program called metamask which will allow us to talk to the blockchain so that we can deploy our token once we have made it and you can get this from the chrome web store it's a simple chrome extension and i've already downloaded it so if i go ahead and click on my meta mask you can see that i'm connected to the ethereum mainnet and i have about 50 bucks of ether in here from from before now every smart contract including the one that will make will cost real money to deploy on the blockchain and i don't really want to do that right now so instead what i'm going to do is i'm going to go to one of the test networks let's say the girly test network and i'm going to use this as my test blockchain for my token so that i can once i make the contract i can deploy it to the network without having to spend real money so why can i do that it's because this is a test network so i can get a free ether on it so you see i already have 1.1418 ether and if you want any more it's very easy all you need to do is find a faucet for the test network that you're working on and just ask for some ether so for instance i'm on the girly test network and if i wanted more ether for whatever reason i could just go to a curly faucet for example this one and i can type in my ethereum address which you can find very easily on metamask if you go back here you can see below account one it has your public address you can copy to keyboard and you can click it there and then you can ask it to give you ether and then you can populate your account on the test network so that you can actually start talking to the blockchain so now that we've downloaded metamask and we can talk to the ethereum blockchain we can get to actually coding up our token to do that we're going to use the solidity programming language which is the default programming language for all ethereum smart contracts and we're going to be working with the remix editor which you can access very easily by typing remix.ethereum.org in your web url bar your browser url bar and you'll hit this page and so go ahead and click on new file and let's call our file token.sol and we open up a new empty file and so the first thing that we want to do is since solidity like java or c plus plus for those of you with programming experience is a compiled language we need to tell solidity what version of the compiler we want to use what version of the language we want to use so the way we do that is by simply saying pragma solidity 0.8 0.8.6 so i believe that's the latest version so i'm just going to go ahead as a it's the latest version as of when i'm making this video so i'm just going to go ahead and use that now we can actually start thinking about how our coin should be structured how our token should be structured and here the ethereum community kind of has some guidelines that any good token should follow and this is set out in erc20 which i've got open right here so this is the erc20 token standard so it tells you you know what a token can be it could represent all sorts of things reputation points lottery tickets an ounce of gold a fiat currency so for instance like many of you may have heard of usd tether that's technically a token tokens can represent a lot of things but all good tokens will share some fundamental functionality which is what erc20 sets out and so what it tells us is that there's a few methods that we need to implement and these are given here so we can quickly go through them so one is a function name that basically tells you what the name of your token is the second function symbol tells you what the symbol of your token is decimals tells you how many decimal points your token is denominated to so for instance if it's denominated to zero decimal points then you can only have integer values whereas if it's denominated to one decimal point then you could have something like 1.1 or 1.2 total supply tells you how many total tokens there are in circulation balance of takes an address given by owner and it basically tells you how many tokens owner has transfer is a very important one it allows us to transfer a value number of tokens to the address given by two and then transfer from is a slightly different function that allows us to transfer from a third party given by underscore from to another party given by underscore two and we can transfer the value underscore value approve is a function that allows you to approve transfers from somebody's account under score spender up to the value underscore value and then allowance is a similar function which governs how much money you can take out of owner's account and give to spender so that spender can spend in addition to these functions we have these events and this is not really a video about the basics of solidity but essentially the idea behind an event is an event marks something that should be recorded on the blockchain so for instance if you transfer money from someone to someone else you probably want to record that on the blockchain so that should be saved with an event and this is exactly why the first event that they have in their standard sort of recommended list of events is the transfer event which transfers money which records a transfer uh originating at underscore from and going towards underscore two and the transfer value is underscore value and another event that they have that i won't be concerned with so much about in this tutorial is an event that marks approval when the address underscore owner and is allowed to spend underscore value tokens from underscore spender okay so now we're really ready to start coding up our token and i'm going to start off by making a link to that erc20 token standard that we just saw the contract that i design will implement basically everything that the erc20 standard wants me to implement so there's a bunch of ways to do this the way i'm gonna go about it is i'm going to before making my original contract of my token i'm going to make something i'm going to make an abstract contract and i'm going to call it ec erc20 token and now what i'm going to do here is i'm basically going to copy exactly what they want me to put in i'm just going to tab everything so these are all the functions that this um erc20 token is meant to implement and i'm also going to copy over the events that i meant to implement now what this is this is an abstract contract meaning this is basically a blueprint for what an erc20 token should look like i don't actually this code doesn't get executed but it just acts as a guideline for any erc20 token and we'll see how this works later so after i've got that definition of an erc20 token there's another contract that i'm going to implement first a setup and i'm going to call this contract owned and what this contract is going to do is basically define what it means for something to be owned because our tokens are obviously going to be owned by someone and it's fairly that sounds kind of meta but it's fairly straightforward so our contract is going to have two uh variables it's going to keep track of its owner and it's also going to keep track of a potential new owner once the token changes hands and we're going to mark a change in ownership with an event and let's call it ownership transferred um and it's going to take an argument of the person that the ownership is going from and the argument of the person that the ownership is going to now the next thing this contract needs is a constructor to be able to actually create an an instance of this contract this is the function that's called whenever we create a contract owned and all we're going to say is when you start off your owner is going to be message.sender where this represents basically whoever is calling the smart contract on the ethereum blockchain we're saying that the owner of the token or the owner of the contract is whoever is calling it now we're also going to have a function to be able to transfer ownership and it's going to take an address that we want to transfer to it's going to be a public function so the first thing is we want to have a condition requiring that the the sender of the contract is really the owner i mean this might be superfluous in many cases but it's just a good fail safe to have and once you're sure that whoever's transferring this is actually someone who's allowed to transfer it you can simply say that the new owner is equal to two and that's that now we're going to have another function this one's technically not necessary but it just depends on how you want to design it and i'm going to call it accept ownership so basically whoever ownership is being transferred to has to agree to accept it only the new owner can can accept so we have to require that the person who's calling the contract has to be the new owner so hence this requirement and once this function is called what we're going to do is we're going to emit an event of ownership transferred um from owner to new owner and then we're going to formally change the event as the ownership the owner of this contract is now the new owner and there is no new owner anymore so what we're going to do is we're going to set that new owner variable to address zero which is sort of like the garbage address in ethereum where things go things that don't matter get assigned that address so we've implemented our own contract now i'm going to implement the actual contract that we all care about which is the token contract we're going to start with contract token and now i'm going to say is erc20 token and owned so what this is saying is that any contract token is also so any contract token is also a contract of the type erc20 token and is also a contract of the type owned so in addition to whatever i may define in this space between these parentheses i'll also get the functionality that i defined above since erc20 token is an abstract contract what this means is that since i'm saying token is an erc20 token i have to actually define how all of these functions should work in this token you know if you're familiar with programming this is basically just like inheritance in many other programming languages first let's define all the key parameters of our token so first is a string that's basically the symbol of our token then we're also going to have another string saying the name of our token then we'll specify the number of decimals our token can be denominated in we'll also have its total possible supply we'll also designate an address that is allowed to be able to mint new currency so i'll call it minter so this is sort of the central authority that'll be able to mint new coins and distribute them or remove them from circulation if you want and then what i'm going to create is a mapping from addresses to integers and this is going to be called balances so this is really going to be the heart of our currency this is sort of this mapping is going to sort of be a table where each address gets mapped to how much currency that address owns and so this is what will really keep track of all the transactions that are going on now as before we need to make a constructor for our tokens so let's do the same thing constructor and we need to initialize all our variables so i'm going to say symbol equals we'll just call it tk for token our name is just token and these are both strings decimals i'm going to say actually i'll say zero for now keep it simple and then the total supply i'll say is a hundred coins and finally for the minter i'll just give it my public address for now uh so let's copy that over here so this is the address that all initial currency will be sent to so what we do is we start off by saying balances underscore minter is equal to is equal to total supply so what this line did is it gave all of my initial supply to this one address and now that address can distribute it and you know you can uh initialize your currency however you want i'm just doing sort of the easiest thing since there was a transfer of currency what i'm going to do is i'm going to make a record of it in the blockchain through a transfer event i'm going to say there was a transfer from you know address 0 the void to the minter and the amount transferred was total supply now we're going to implement the functions uh from the erc20 token section that we declared but never actually implemented i'm going to start off with these first five five functions let me copy them down here and now what i'm gonna do is i'm just gonna change it slightly i'm going to say now name has a proper definition where we simply return the name of the currency and what we need to do is we need to make another small change so you'll see here that after the name of the function which conveniently here is name we have these modifiers which tell us information about the function so public defines its scope who can see this function view tells us that this is a function which simply tells us the state of our currency it doesn't modify any variables it simply returns a view of the situation and this last part tells us that this returns a string and that string is the name um but what we need to do is after public we need to add this word override just to tell the compiler that now we're overriding the definition of name um that was up here so we're changing we're overwriting this definition and now this is the actual definition of name and you know it's very similar for the remainder of these methods so i'm just going to go ahead and do that so here we've implemented all the um all the view functions in the erc20 token interface that basically tell us the full details of our currency so these tell us the critical details of our currency and this last function balance of basically looks inside the balances mapping and asks how much money does the address owner have associated with it so now we've defined all those easy view functions we can define the more juicy functions that remain in the erc20 token interface so let me copy these and what i'm actually going to do is i'm going to implement the transfer from function first so what we're going to do is let's remove this keyword and we're going to say we're overriding now this function is going to transfer from an address underscore from and it's going to send to an address underscore 2 and the amount of value it's going to send is underscore value so before we can do that we need to require that from actually has enough money to send so we're gonna need that the balance for his address is greater than or equal to the value that he's trying to send and then if that's true balances his balance goes down by value so minus equals value this is the same as saying balances underscore from equals balances underscore from minus underscore value it's the same thing as that and then similarly we need to increase the balance of the guy who's receiving the money so plus equals value and there was a transfer of currency so we need to emit a transfer event to record this in the blockchain that we sent money from underscore from to underscore two and the amount was underscore value and then finally we're going to return true if the transfer was successful to conform to the token standard now this function transfer that they've defined in the standard you'll see is actually really similar to transfer from in fact it's just a specific case where transfer now transfers from whoever is calling the smart contract on the blockchain to the address that they target whereas transfer from allows the person who's calling the contract on the blockchain to transfer money from somebody else's address to somebody else's address so you see that transfer is just a specific instance of transfer from so we can actually save ourselves sometime and have our code look slightly cleaner by simply saying that transfer returns transfer from and now instead of having a from we're just going to be message.sender to the address to and the address value so we've saved ourselves from having to basically copy these these lines again in this transfer function now these last two functions approve an allowance so what approve does is it approves somebody else to spend a value from your account i don't really want my currency to have this so what i'm going to do is i'm going to keep these methods to conform to the to the standard but i'm just going to return true and it'll do nothing um you know for your coins you should obviously if you want to implement this you should feel free to go ahead and implement something cool here and then similarly for allowance i don't really want third parties um being allowed to spend money from other people's wallets so i'm just gonna say return zero no one's allowed to spend money from other people's wallets now i'll add two more functions so the first function is going to be mint and it's going to take an argument amount and it's going to return a boolean if the minting was successful and what it requires is that whoever is initiating this request has to be the mentor of the currency and then if that's true we can simply say that their balance the balance of the minter increases by the amount minted and the total supply of the currency also increases by the amount minted and we're gonna return it true if everything worked properly now you can also implement a more dictatorial version of this called confiscate so mint will expand the money supply and confiscate can shrink it and of course for confiscate now you can have a target to steal money from so you can say address target and you went amount public return bool to return a boolean once again and you're gonna have to require that only the minter can do the confiscating and since i don't want people to have negative balances in my currency i'm going to say if the balance of the target is greater than or equal to the amount that i'm trying to confiscate from him then simply reduces balance by that amount and of course reduce the total supply by that amount also however [Music] if it's not high enough then simply what we're going to do first is we're going to simply basically take all he has in his account so we're going to say total supply minus equals balances of the target so all his money has been removed and now we basically empty his account and then finally return true if your dictatorial plans have worked and you have seized people's money so now we have a way to inject new currency into the money supply as well as to remove new currency by force now we've got some solid basic functionality for our token which will allow us to transfer it and mint it as well as remove tokens from circulation now we can go ahead and actually try compiling our smart contract and deploying it to the test blockchain so to compile you click this button oops not this button sorry you click this button the solidity compiler and you see here that my compiler is 0.8.6 which matches the 0.8.6 up here and i'm basically going to go ahead and compile and you see it came up with the seven there's some warnings here you can typically ignore those the best code will obviously not have warnings but just to get started you can ignore them but this is great because now what it means is our contract is ready to go and we can deploy it so to do that first we're going to change the environment from this javascript vm to now an injected web 3 and you'll see that these things change automatically because what it does is if you go here now you'll see that metamask says it's connected to remix.ethereum.org since i've done this process before my account was automatically connected what you might need to do is after you've selected web3 you might need to click meta mask and then manually initiate a connection if you haven't done it before but alright we're connected now and you'll notice that i'm invoking the contract as the 0xd1 address which is which is my address and what i'm going to say is we're going to deploy the token token let's go and deploy it so metamask opened it's asking me for gas fees but i don't care because this is a test blockchain so i can spend as much ethereum as necessary and so i go ahead and confirm it and you can see that my contract deployment is in progress and now my contract's been deployed and so it shows up here in metamask as well as here in remix however so you can see that i've lost ethereum since i deployed the contract but i don't actually have my token show up so what happened well what we actually need to do is we need to click add token and now you see that we need the token contract address so this token contract address refers to the address on the blockchain of the smart contract that we just deployed to create our currency so we actually remix is very nice because it gives us this contract address we don't have to go searching for it and it's right here in deployed contract and you can click this button to copy it to keyboard and you can go back to metamask so let's go back to add token and i'm going to copy it and you'll see that it automatically populated the token symbol field tk and it said that our token decimal is zero so what i'm gonna do is i'm gonna click next and it says i have a hundred tk there we go so now look it shows me with a hundred tk so we've managed to deploy our token onto the gurley test network and the process for the ethereum main net would be exactly the same it's just that you'd have to buy some real ether to deploy um to deploy your token and to see if we can try send it you can go to metamask and you can click send and here's a friend of mine who i sent some currency to before so i'm just going to send him some fake currency now and let's see how much should i send him let me send him 10 tokens and you know these gas prices we don't really care about because we're on the test network so it's all free but if you're designing a real blockchain token on the real ethereum blockchain gas prices are something that of course you have to keep in mind but all right here is our transaction let's confirm it and now we're sending some tokens to our friend and there we go we've sent our token so you see metamask tells us we've sent away 10 tokens exactly as we'd said to the address that we wanted to send it to and if you check your assets you see that i actually do have only 90 tokens now the final thing we can check now is those other two methods that we created whereby we can increase the money supply as well as destroy the money supply so to be able to play with those mint and confiscate methods we need to go back to remix and we go to our deployed contract and we click this arrow and you see all these functions that we define and in particular what we want to do is suppose let's say we want to make 10 coins so that we get our 100 back so i'm going to put 10 over here and i'm going to click mint auto mask opened up again it's saying you want to do a transaction are you willing to pay the gas fees yes i am we've initiated the transaction it's pending and there we go we've minted some currency so our transaction is done and if we see we're back to 100 tokens and now suppose i wanted to confiscate my own wealth what i can do is i can go down to confiscate and i can click the arrow and i can copy my own address in and i can try take back the ether that i just gave myself and we're gonna go through the same drill and my contracts deployed so now we'll just wait till it runs and now my contract is finished running and lo and behold i am back at 90 tokens so that was it guys thank you so much for watching and i hope you had fun learning about how to make your own ethereum token the token we implemented was a fairly basic one with the ability to transfer ownership between different people as well as creating and destroying tokens but i hope you saw that there's really a lot of scope to go wild and let your imagination run free and really implement a lot of cool stuff so i hope this video encouraged you to go out there and try this yourself indulge that central banker within you and i'll leave i'll put this file that we wrote up on my github and i'll leave a link to that in the description so that you can use this as a template to play around in your own ethereum token adventures and yeah thank you so much for watching
Info
Channel: Jyotirmai Singh
Views: 2,594
Rating: undefined out of 5
Keywords:
Id: bgz4jahgaW0
Channel Id: undefined
Length: 29min 38sec (1778 seconds)
Published: Tue Aug 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.