Create your own ERC-20 Tokens in 10 mins
Ethereum is the largest and the most used blockchain that runs around Smart Contracts and hosts a huge amount of dApps that are Decentralised Applications. Solidity runs on smart contracts. These decentralised applications and DeFi projects are based upon the Ethereum ERC-20 tokens. Thus, in order to create a Decentralised Application, we need to be aware of creation of our own tokens. Let’s create the token of our choice in just 10 mins.
ERC-20 Token
ERC-20 tokens are blockchain-based assets that have value and can be sent and received. The primary difference is that instead of running on their own blockchain, ERC-20 tokens are issued on the Ethereum network. In terms of implementation coding for ERC-20 tokens, the six basic coding functions are:
- total supply
- balance of
- allowance
- transfer
- approve
- transfer from
As we are just starting, we will directly use the format of Open Zeppelin which will help us make the code easier for a beginner.
Setting up Metamask Wallet
MetaMask wallet browser extension supports Chrome, Chromium and Firefox browsers. First, go to Google and search for Metamask. Download the extension from metamask.io. Once the installation is succeeded, it will greet you with a welcome screen. Click the “Get Started” button. Create your wallet, make a password and store the Secret Recovery Phrase in a safe place.
Now, go to Settings → Advanced → Scroll down until you see the “Show test networks” → Turn it on to enable testnets
Voila! You get all the available testnets. For this purpose, we are going to use Rinkeby Testnet. You can get some test Ether by requesting for it on faucet.rinkeby.io.
Remix
Remix IDE allows developing, deploying and administering smart contracts for Ethereum like blockchains. It can also be used as a learning platform. It is very user friendly and helps beginners to know about all the basic things required to deploy the contract. First, we start by creating a file in the name of say, Token.sol in the File Explorers tab.
Solidity 0.6.8 introduces SPDX license identifiers so developers can specify the license the contract uses. SPDX license identifiers should be added to the top of contract files.The following identifier should be added to the top of your contract:
//SPDX-License-Identifier: MIT
We now need to write the version of solidity that we are going to be using:
pragma solidity ^0.8.1;
Open Zeppelin
This line imports the ERC-20
token standard from OpenZeppelin (OZ). OZ is an Ethereum security company. Among other things, OZ develops reference contracts for popular smart contract standards which are thoroughly tested and secure.
//import ERC20 token smart contract from OpenZeppelinimport "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
Create a Token Contract
This specifies a new contract, named NazToken, in our Solidity file. Also, it says that this contract is an instance of ERC20
. ERC20
in this case refers to the standard contract we imported from OpenZeppelin.
Essentially, we are extending the ERC20
standard contract we imported from OpenZeppelin. So all the functions and logic that is built into ERC20
is available for us to use, and we can add our own custom logic on top of it.
contract NazToken is ERC20 {}
Essentially, we created constructor
function that is called when the smart contract is first deployed. Within the constructor, we want two arguments from the user - _name
and _symbol
which specify the name and symbol of our cryptocurrency. Eg. name = Ethereum, symbol = ETH.
What happens after it is more interesting. Immediately after specifying the constructor function, we call ERC20(_name, _symbol)
.
The ERC20
contract we imported from OpenZeppelin has it's own constructor, which requires the name
and symbol
parameters. Since we are extending the ERC20 contract, we need to initialize the ERC20 contract when we deploy ours. So, as part of our constructor, we also need to call the constructor on the ERC20
contract.
Therefore, we are providing _name
and _symbol
variables to our contract, which we immediately pass on to the ERC20
constructor, thereby initializing the ERC20
smart contract.
_mint
is an internal
function within the ERC20
standard contract, which means that it can only be called by the contract itself. External users cannot call this function.
Since you as the developer want to receive some tokens when you deploy this contract, we call the _mint
function to mint some tokens to msg.sender
. 10000 * 10 ** 18
specifies that you want 10,000 full tokens to be minted to your address. So, finally our contract becomes:
contract NazToken is ERC20 { //we also want to call the contructor present inside the ERC20constructor(string memory _name, string memory _symbol) ERC20(_name, _symbol) { //Get some tokens for ourselves _mint(msg.sender, 10000 * (10 ** 18)); //this is our constructor }}
Compile and Deploy the Contract
Compile your contract by either pressing Save (CTRL + S on Windows, Command + S on Mac), or by going over to the Compiler
tab in Remix, selecting NToken.sol
, and hitting Compile
.
Now, Head over to the Deployer
tab in Remix.
Select the Injected Web3
environment (ensure you are on the Rinkeby Test Network), and connect your Metamask wallet.
Select the NToken.sol
contract, and enter values for the constructor arguments _name
and _symbol
.I want my token name to be Ninu and the symbol to be NINU. Click Transact
and approve the transaction from Metamask to deploy your contract!
When deployed, the contract should show up under the Deployed Contracts
section. Click the Copy Address
button to copy the contract address. Go to Rinkeby Etherscan and search for your contract address and you should see it there!
Import and View token on Metamask
You may notice that even though you minted tokens to your address, they don’t show up in Metamask. To do so:
- Copy your contract address
- Open Metamask and click
Import Tokens
in theAssets
tab - Click Add, and you will see your balance in Metamask!
Congratulations! You’ve successfully deployed and minted your own ERC20 token! Woohhooo!!
Contents distributed by Learn.Block6.tech
👉 Telegram — Fresh ideas
👉 Twitter — Latest articles