Create Your Own Token

Have you always wanted to create your own token but do not know how to do so? No worries! In this guide, I will show you step by step how you can easily set up your own token, and import it into Metamask (one of the most popular wallets out there). From there, you can transfer it to other people, or you can put it on a decentralized exchange such as Uniswap. If you want to know more about decentralized exchanges, or DEXs, check out my article here.

As you might already know by now, to have your own token on the Etehreum blockchain, you will need to use smart contracts. For creating our token we will have a contract that contains all the logic about token creation and distribution. The smart contract is written in Solidity, a programming language for writing smart contracts.

In order to write our smart contract, we will need a tool to help us write, compile, and debug our code. Here is where Remix is very handy. Remix is an IDE (Integrated Development Environment) that allows you to do exactly that, write, compile, and debug your smart contract code.

Prerequisites:

Having an Etehreum wallet. For this tutorial, we will use MetaMask wallet. Download the browser extension and set up your wallet here. Even if you already have another wallet provider, just use Metamask for now.

You will need some test Ether in your wallet. We will use Ropsten test network, therefore you will need some Ether from a Ropsten faucet. You can find one here. Simply paste your MetaMask address in there and you are about to get funded.

Steps:

1) Got to Remix (https://remix.ethereum.org/). You will see the following view:

First, a bit of housekeeping. The first time you open Remix, you will have a very simple example application. Under "contracts" you will find three files "1_Storage.sol", "2_Owner.sol", "3_Ballot.sol". Feel free to delete those, as we will write our own smart contract. Then under "scripts", there are two scripts "deploy_web3.js", and "deploy_ethers.js", you can delete those two as well. Then, under "tests", there is a "4_Ballot_test.sol" that we will not use as well, feel free to delete it. The last existing file is the README.txt, this one is used to provide the description for a project. Now that everything is clean, we can start creating our contract.

2) Go to the "contracts" folder and create a new file. Give the file the name of the token followed by ".sol". For example, I will name mine "DemoToken.sol". To create a new file, click on the Contract folder, then on the file symbol, and then add the name of your file.

3) Now it is time for the actual code:

1 //SPDX-License-Identifier: MIT

2 pragma solidity ^0.8.0;

3 import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

4 contract DemoToken is ERC20 {

5 constructor(string memory name, string memory symbol) ERC20(name, symbol) {

6 _mint(msg.sender, 600000 * (10 ** 18));

7 }

8 }

Yes, that's it! You can have your token with 8 lines of code. I have numbered them such that I can explain to you, line by line, what they represent. Please make sure you are not copying my numbering along with the code.

//SPDX-License-Identifier: MIT

The first line is a comment, it identifies the type of license for the code. It is called an "SPDX license identifier". Feel free to Google if you want to learn more about it.

pragma solidity ^0.8.0;

Next, on the second line, we tell our contract what version to use for the Solidity compiler. In other words, we enforce our compiler not to use a version lower than 0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

The 3rd line is where all the magic happens. Here we are importing the OppenZeppelin library (https://docs.openzeppelin.com/contracts/2.x/erc20). This library is used to develop secure smart contracts. In this example, we inherit all the functionalities from the ERC20 contract from the OppenZeppelin library. The advantage of using a well-known and audited library such as OppenZeppelin, is that we increase the security of our contract, and we simplify our work. If you have a look at the functions that the contract provides (and I encourage you to do so), you can see that it provides implementations for the following functions:

contract DemoToken is ERC20 {

On the 4th line, we define the name of our contract, DemoToekn, and we mention that it is a type of ERC20 token.

constructor(string memory name, string memory symbol) ERC20(name, symbol) {

Next, on the 5th line, we have the constructor. Here is where we pass the attributes of our contract such as the name and the token symbol.

_mint(msg.sender, 600000 * (10 ** 18));

On the 6th line, we call the _mint() function, and we pass two arguments. The first one, msg.sender is the address that calls the function, in this case, the owner of the contract. The second parameter is the total supply. Here we mint 600000 tokens. Feel free to put whatever number you want instead. This will represent the total supply of your token.

Wait, you've said we mint 600000 tokens, then why do we have 600000 * (10 ** 18)? First, we specify the total number of tokens that we want to mint. The second part is because of how fractions of Ether work. The smallest fraction of an Ether is Wei,1 Ether = 10 ^18 Wei. That is why, for our ERC20 compatible token, we have to multiply the total number of tokens that we want by 10 ^18.

And that's it, we are done with coding! Now it's time to deploy our contract and get those tokens on Metamask.

First make sure to save your code: CTRL + S.

Compile your code.

First, select from the Contract dropdown the name of your contract, in our case "DemoToken.sol"(1).   Then press the compile icon (2), and then Compile the contract (3).

Great, now we are ready to deploy our contract!

First, you need to select the "ENVIRONMENT" for your deployment (1). From the dropdown go and select "Injected Web 3". This will trigger your MetaMask wallet.  Log into your account, then select the Ropsten test network. For now, we will use Ropsten test network to test our contract without using real funds, instead, we use test ETH. Make sure your wallet has some funds before trying to deploy the contract.

After this, press the deploy icon (2). Then go to the CONTRACT dropdown (3) and select the name of your contract. Then, at step (4), open the drop-down from "Deploy". Here we will set the name and symbol for our contract.

In the "Name" field, put the name of your token, and then in the "Symbol" field, set the symbol for your token, I have chosen "DTK" for mine, feel free to choose yours creatively.

After you set these two, press "Transact". At this point, MetaMask will pop. Make sure one more time that you are on the Ropsten test network. And then press "Confirm". After you wait a bit, you will see a message saying "Transaction confirmed, view on Etherscan". You can press on the link and go on Ropsten Etherscan and verify the creation of your token.

Here you can see the address of your contract. Copy it to have it handy for later!

Now that our token has been deployed, and we have the tokens associated with our address, let's import them on MetaMask. Go to MetaMask, go to "Assets".

Scroll down, click on "Import Tokens".

For "Token Contract Address" paste the address of your contract that you copied from Etherscan. For token Symbol put the symbol of your token, and for decimals 18, then press "Add custom token".

Then press "Import Tokens".

After this, you should see your tokens in your MetaMask account. In my case, I now have 600000 DTK tokens.

From here you can send your tokens to your friends and other people! You did it! You have just created your own token! It's time to celebrate!

P.S: If you need an address to test sending some tokens, feel free to experiment with mine 0x7372d2Ed844704AdCC0b697973463391a80B0458 (these are tokens created on a test network, they do not have a real value).