Create your cryptocurrency token
Intro to blockchain, smart contracts and ERC-20 tokens
3 weeks ago, I started the #100daysofWeb3 challenge. As a developer, I had never worked with web3 products and tools before. And what better task than creating my cryptocurrency token.
Honestly, it’s way easier than it looks!
With that, I decided to put together this article to outline the basic concepts of cryptocurrencies (basically it answers the questions I kept asking myself) and guide you through creating your own token on Ethereum.
What do I need to know before creating my token?
Blockchain & Cryptocurrencies
Blockchain is a shared and immutable ledger. On the blockchain, the information is distributed among multiple computers which work together to provide the underlying infrastructure.
Cryptocurrencies(crypto) are digital currencies that are secured by cryptographic functions. Most cryptocurrencies are based on blockchain technology. They are represented by entries onto the blockchain.
Bitcoin, the most popular cryptocurrency, is powered by blockchain technology. Basically, blockchain is the technology that makes bitcoin possible and has applications way beyond bitcoin.
Ethereum, Avalanche and Solana are other platforms that are built using blockchain technology. Different blockchain platforms prioritize different features such as decentralization, cost, speed, security...
Coin vs. Token
Cryptocurrencies can be crypto coins or crypto tokens. The two terms are often used interchangeably however they are two separate terms.
A coin exists on its blockchain. It is a digital representation of money for that blockchain. BTC, ETH or LTC are examples of cryptocurrency coins, they each have their own blockchain.
A token functions on top of an existing blockchain via smart contracts. Unlike a coin, a token usually has a utility associated with it. For example, Uniswap is an exchange protocol built on top of the Ethereum blockchain and has its token called UNI. UNI holders can vote on development proposals and decisions for the platform.
In short, coins have their own blockchain and are used as a medium of exchange whereas tokens live on top of a blockchain and can have different values or utilities.
Smart contracts
Smart contracts are just like contracts in the real world, except they are digital.
A smart contract is a self-executing computer program stored inside the blockchain. When the conditions of a smart contract are met, the code is automatically executed. This removes the intermediaries needed in real-life contracts and creates a trustless environment for users. Trustless means that the users do not need to trust each other because they can rely on the underlying code.
Some smart contract languages are Solidity, Vyper and Java.
Ethereum
Ethereum is an open-source, decentralized blockchain platform with smart contracts. It has its native cryptocurrency coin called Ether(ETH).
Solidity is an object-oriented, high-level language developed for creating smart contracts. It is the main programming language for Ethereum.
ERC-20
ERC stands "Ethereum request for comment" and 20 is the token identifier. It defines a set of rules and functions that apply to all tokens.
ERC-20 is a standard for smart contracts that create fungible tokens on the Ethereum blockchain. A fungible token means that all the tokens are exactly the same. If we each have a UNI token, there is no difference between my token and your token, they have the same type and value in each of our accounts.
There are more standard ERC smart contracts. For example, the standard for creating a non-fungible token(NFT) is ERC-721.
🙌 We have covered the basic concepts for cryptocurrency tokens, now let’s get to create one!
How to create a cryptocurrency token?
Let's do it together! In the project, we will build our token on Ethereum using Solidity. The type of smart contract we will use is ERC20.
Project Tools
Metamask Wallet: crypto wallet. It is a software wallet that you can use as a mobile app or browser extension.
Alchemy: ethereum developer platform. Alchemy has a full suite of products. We will be using the Alchemy API to interact with Alchemy's Ethereum infrastructure.
Hardhat: ethereum development environment. It comes as an npm package and allows you to create the basic setup you need for a smart contract.
OpenZepplin Contracts: library for secure smart contract implementations.
Prerequisites
IDE
I will be using VS Code for this project. If you would like to use VS Code you will need to add the solidity extension. Choose the IDE you like best, even a text editor will do.
Note: Remix Ide is a browser-based ide for Ethereum, its super convenient to get started with as you don't need any setup. (If you use Remix IDE the steps will be different becuase you can directly compile and deploy your code from the UI.)
Metamask Setup
If you don’t have Metamask already you can head over to their website to download the extension and follow the setup wizard to create an account.
Change your network to the Rinkeby Test Network.
Alchemy Setup
Sign up with Alchemy, you can use my referral link and then we can each get $100 of credit on the platform. The tools we will be using are free, but the credit can come in handy for other projects.
Once you log in, from the dashboard click "Create App." Give your app a name and a description. Select the "Rinkeby" network which is a test network.
Create your app and then click "View details" to get the API key which we will be using.
Hardhat Setup
You need node.js on your machine to use hardhat. Install the version for your pc.
Create your token
Create a project folder and head over to its directory.
mkdir my-token cd my-token
In your project directory create a hardhat project by running
npx hardhat
$ npx hardhat
Here is what you will see on the terminal:
You can click enter throughout the setup wizard and keep the default options.
Run the command to install the OpenZepplin implementation for smart contracts. It has the ERC-20 token standard which we can extend from.
npm install @openzeppelin/contracts
Under the 'contracts' folder create a new solidity file(file extension is .sol). This will be the file for our ERC-20 token.
Below you can see a sample ERC-20 token smart contract extend from OpenZepplin. I have added some comments to explain what's going on in the code. Copy and paste it onto your own solidity file.
//SPDX-License-Identifier: Unlicense pragma solidity ^0.8.4; // tells the solidity version to the complier import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; // inherit the erc20 contract import "hardhat/console.sol"; // built in hardhat local environment contract Eda is ERC20 { constructor(string memory name, string memory symbol) ERC20(name, symbol) { _mint(msg.sender, 100 * (10 ** 18)); // mint tokens: supply 100 tokens, 18 is the decimal } }
We don't need to do much as we are extending the functions from the OpenZepplin ERC20 Contract. All we are doing is minting the tokens, which basically means creating them.
Remember the Alchemy API key, we need to talk to the Rinkeby Test Network from our contract. Add your API key to the hardhat.config.json (see code snippet under step-7)
Add your metamask private key to hardhat.config.json. You can get this by clicking Account Details --> Export Private Key from your Metamask extension. The tokens will be added to this account
require("@nomiclabs/hardhat-waffle"); module.exports = { solidity: '0.8.4', // make sure that the solidity compiler version is the same as in your contract networks: { rinkeby: { // defining our test network url: '', //alchemy api key accounts: [' '], // metamask rinkeby account private key }, }, };
All the coding is complete 😀 Now we need to compile and deploy our contract.
Compile your smart contract by running
npx hardhat compile
. This command will create an 'artifacts' folder in your project directory. It will translate the solidity code into machine-executable code.npx hardhat compile
We need to take our contract from our local machine and put it onto the Rinkeby Test Network. For this simply create a deploy.js file under the 'scripts' folder. Copy and paste the content below onto your file.
async function main() { const Token = await hre.ethers.getContractFactory("Eda"); // hre: hardhat runtime environment const token = await Token.deploy("Eda Token", "Eda"); // call constructor: name and symbol console.log("Token address:", token.address); // print the address to the console } main() .then(() => process.exit(0)) / .catch((error) => { console.error(error); // print the error if there happens to be one process.exit(1); });
Run the command below to deploy the contract to the Rinkeby Test Network.
npx hardhat run scripts/deploy.js --network rinkeby
If everything is working, it should deploy the contract and print out the contract address to the terminal.
Copy your contract address and head over to the Rinkeby Testnet Explorer to view your contract.
You can add your tokens to your metamask account. By default, you will not be able to see them. Under Assets select 'Import Tokens' and add the contract address. Once you click submit you should be able to see your tokens.
Let me leave you with the Ethereum Developer Resources where you can find great resources to continue developing on Ethereum.
Thank you very much for your time, hope you enjoyed the article. If you want any "eda" tokens or have any feedback, please don't hesitate to reach out!