Create ERC20 token on Ethereum (the EASY way)

Investment Rookie
25 followers
6 Video Views·Oct 28, 2022
Have you ever dreamed of creating your own cryptocurrency?
If you try to fork 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 ERC20 token step by step on the Ethereum blockchain.
This is a beginner level project.
We are 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 Julien and on EtherBlocks, 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 ERC20 token is a virtual currency that lives on the Ethereum blockchain.
Contrary to the native coin of Ethereum, Ether, anybody can create his or her own ERC20 token.
When you create your token, you can allocate as many units to yourself.
Each token lives in its own smart contract.
If you never heard of smart contracts,
these are the small programs that run on the Ethereum blockchain.
ERC20 is a standard for tokens.
In theory, you could create a token that does not respect the ERC20 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 ERC20 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 decimals parameter would be 2.
If Ether was an ERC20 token,
the decimals parameter would 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 tokens 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 ERC20 tokens,
each spender has a maximum spending limit
after which it's not possible to spend anymore.
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.
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 JavaScript
but it's very misleading
because Solidity works very differently from 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 functions 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 Solidity, 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 deployed on Ethereum.
You don't need any permission.
This property of smart contracts is called composability.
In comparison, with JavaScript,
it's not possible to interact with some JavaScript code
running on another server unless it has an API
and it lets you access it.
The last big difference between Solidity and JavaScript
is that with Solidity,
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.
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 smart contracts 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.
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 going to call this mytoken.source.sol.
This is the extension for Solidity file.
And we're going to start with what we call the pragma statement.
You have this in every Solidity file.
And here, we specify the version of Solidity
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 smart 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, mytoken.
And after that, we could code from scratch
all the functions 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 implementations.
And the most popular one is the implementation of OpenZipling,
a popular library for Solidity.
This is the GitHub repo of OpenZipling.
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 ERC20, then in ERC20.sol.
Here, we have the standard implementation of ERC20 by OpenZipling.
So they define a name of the contract.
They define some variable here.
And here, we can see some function that we saw in the ERC20 standard,
like a 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 that we just copied.
So here, between a single quote, let's give the full URL to our smart contract.
And here, this is cut off on the video,
but I'm going to end the import statement by a single quote and a semicolon.
All right, so now we have imported the ERC20 implementation.
And next, we are going to make our token inherit from ERC20 of OpenZipling.
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 ERC20 of OpenZipling 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.
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 ERC20 of OpenZipling.
And there are two things to specify the name of our token.
So for example, let's do my token.
And then the symbol.
So usually, the symbol is three letters.
So it can be my MTN.
And then we're going to define an empty body between curly braces for the moment.
So just to be clear here, when you deploy my token,
so 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 repo of OpenZipling.
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 and symbol.
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 mint.
So let's scroll down.
Let's go down where we have this underscore mint.
And we have to specify the account that received the token and the amount.
So what's interesting here is 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 pass this underscore mint.
And so for the account, we're going to specify the address that
has deployed the contract.
So that's msg.sender.
This is a built-in 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 ERC-20 tokens.
So here we're going to multiply this by 10 power 18.
And we finish the statement with a semicolon.
And so now we're going to have 10,000 tokens that are minted 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 1.
And so in this case, I mint 10,000 tokens.
And that's fine.
But you might run into problem if you want to transfer some small quantity of tokens.
So usually we just leave this to 10 power 18.
All right.
So this is a very simple token.
And it would work to you as a 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 ERC20.
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 after.
So that means you don't have a fixed supply.
So in this case, you can create another function called mint.
And you're going to specify the recipient and the amount.
Make this external so that we can call it from outside smart contract.
And then we're going to call again underscore mint.
And we're going to mint.
We're going to forward the two parameters 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 depreciate 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 sender of this transaction is the admin.
Otherwise, the error message is only admin.
And if someone else tries to call this function, this request statement is going to fail.
And the transaction is going to be canceled.
So it'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 he's willing to burn his own to make all the other more valuable.
So that would be possible.
And so here, we're going to specify msg sender.
And we specify the amount.
And so underscore burn, just to be clear.
So this underscore burn function and underscore mint.
This is not part of the ERC20 standard.
It's just specific to the implementation of OpenZipling.
And if you want to do more customization.
Then you can inspect the code of ERC20 of OpenZipling.
And see if there are other functions that you want to use.
And also what you can do is you go to ERC20.
You go to extensions.
And you will see some example.
For example, ERC20 burnable.
So here, this is another way to do the burn.
You also have a burn from.
What else do we have?
You have ERC20 capped.
So I think for a maximum total supply.
I think you can mint up to a certain amount.
Yeah, here there's a required statement in the mint function.
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 ether for each of them.
Gas limit.
You don't touch it.
Here we see our contract.
mytoken.solve
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 ERC20.
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 orange button are the function.
Allow you to modify data.
And the blue one are the function to read data.
For example, let's try to get our balance.
So we go up.
We select.
We copy our address here.
Let's go down.
Balance off.
Let's paste this.
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 10 power 18.
And so let's try to.
Okay, 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 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 of.
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 10,000.
Balance of.
Okay, and after you can keep playing around with all these functions.
And when you want to deploy your spot 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 Metamask to Remix.
Then you select 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 spot contract for real.
In this video, I'll show you how to create an ERC20 token on Ethereum.
Everything you learn in this video is also applicable to BEP20 tokens.
On the Binance Smart Chain.
Binance Smart Chain uses the same technology as Ethereum.
And BEP20 is just another name for ERC20.
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 ERC20 token.
At some point, you will need to switch to a more robust tool like Truffle.
Truffle is a framework for creating smart contracts on Ethereum or Binance Smart Chain.
And a good follow-up to this video is to try to recreate your ERC20 token using the Truffle framework.
And for this, you can watch this tutorial on Truffle on my channel.
I will see you there.
