Alchemy: Create Your Own ERC20 Cryptocurrency

Learn how to create your own ERC20 Cryptocurrency.

What is ERC20?

ERC20 is a token contract that keeps track of fungible tokens, such as exchange currency, staking, or other things with an exact value. ERC20 tokens do not have special rights or behaviors associated with them like non-fungible tokens (NFTs) do.

An example of ERC20 tokens are Shiba Inu (SHIB), Chainlink (LINK), and Wrapped Bitcoin (WBTC).

Read below to learn how to create your own ERC20 Cryptocurrency.

Prerequisites:

1. First, download and install S3FS-FUSE on your local environment.

This tool is available on a Linux or macOS system.

2. Set up an Access Key file for use with S3FS-FUSE.

Set up a credentials file for S3FS at ${HOME}/.passwd-s3fs. You will need to save your Filebase Access and Secret keys to this file and give it owner permissions. You can do so with the following commands:

echo ACCESS_KEY_ID:SECRET_ACCESS_KEY > ${HOME}/.passwd-s3fs

chmod 600 ${HOME}/.passwd-s3fs

ACCESS_KEY_ID is your Filebase Access key, and SECRET_ACCESS_KEY is your Filebase Secret key. For more information on Filebase access keys, see here.

3. Mount your bucket.

You can mount a Filebase IPFS bucket with the command:

s3fs mybucket /path/to/mountpoint -o passwd_file=${HOME}/.passwd-s3fs -o url=https://s3.filebase.com

  • mybucket: name of your Filebase bucket

  • /path/to/mountpoint

4. Now, navigate into the mounted Filebase bucket.

Create a new folder for your project:

mkdir myFirstToken

5. Install Hardhat with the following command:

yarn add hardhat @nomiclabs/hardhat-waffle @nomiclabs/hardhat-ethers ethers ethereum-waffle

6. Initialize the Hardhat setup prompt with the command:

npx hardhat

Select ‘Create a Basic Sample Project’ > ‘Leave Default Root’ > ‘Add a .gitignore’.

7. Now, let’s head over to Alchemy and either sign up for an account or login.

8. From the Alchemy dashboard, we need to create an app.

Select the ‘Create App’ button to get started.

9. Create a new app. For our example, we called our app Token.

Set the chain to ‘Polygon’, and the network to ‘Mumbai’.

10. Then, from our App’s page, select ‘View Key’:

11. Copy your app’s HTTP Key:

12. Next, open the hardhat.config.js file in your project’s directory.

Add the following code into the module.exports section:

module.exports = {
  solidity: "0.8.4",
  networks:{
    mumbai:{
      url: 'ALCHEMY_HTTP_URL',
    }
  }
};

Replace ALCHEMY_HTTP_URL with your Alchemy HTTP key we previously copied.

13. Next, install dotenv:

yarn add dotenv

14. Then, copy your cryptowallet’s private key.

If you are using Metamask, follow the instructions found here.

15. Create a new file called .env.

This will house your wallet’s private key. If you plan to upload your project files to GitHub, we recommend adding this file to your .gitignore, which will prevent this file from being pushed to GitHub where your wallet’s private key will be accessible by others.

16. In your .env file, input the following variable:

PRIVATE_KEY=”CRYPTO WALLET PRIVATE KEY”

Replace the value with your wallet’s private key.

17. In your hardhat.config.js file, insert the following line at the top of the file:

require('dotenv').config()

Then in the module.exports section, add the following accounts: line:

module.exports = {
  solidity: "0.8.4",
  networks:{
    mumbai:{
      url: 'YOUR ALCHEMY POLYGON MUMBAI ENDPOINT',
      accounts: [`0x${process.env.PRIVATE_KEY}`]
    }
  }
};

Overall, your hardhat.config.js file should resemble the following:

require("@nomiclabs/hardhat-waffle");
require('dotenv').config()

task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

module.exports = {
  solidity: "0.8.4",
  networks:{
    mumbai:{
      url: 'YOUR ALCHEMY POLYGON MUMBAI ENDPOINT',
      accounts: [`0x${process.env.PRIVATE_KEY}`]
    }
  }
};

18. Create a new file called Token.sol. Input the following content into this new file:

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.0;

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

contract TokenName is ERC20{
    constructor(uint256 totalSupply) ERC20("TokenName", "TKN"){
        _mint(msg.sender, totalSupply);
    }
}

Replace the values to match your desired configuration:

  • TokenName: Token Name

  • TKN: Token Symbol

This contract uses a fixed supply of tokens, meaning the total amount of tokens available to be minted and traded is a fixed value.

19. In your project’s scripts directory, rename the file sample-script.js to deploy.js, and replace the existing content with the following:

const hre = require("hardhat");

async function main() {
  const TokenName = await hre.ethers.getContractFactory("TokenName");
  const tokenName = await TokenName.deploy("1000000000000000000000000000");

  await tokenName.deployed();

  console.log("Token deployed to:", devToken.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Replace TokenName and tokenName with the name of the token you configured in Token.sol.

20. Now we’re ready to compile and deploy our token on the Polygon Mumbai network.

Use the following command to compile:

npx hardhat compile

21. We’ll need some test MATIC to cover the gas fees of our transaction.

Navigate to the Polygon faucet, select the Mumbai network, and send some MATIC to your wallet address.

22. After you have MATIC in your wallet, deploy your token on the Mumbai network:

npx hardhat run ./scripts/deploy.js --network mumbai

Take note of the contract address that gets returned.

23. Now you can add your token to your Crypto Wallet.

This tutorial uses Metamask. If you are using another wallet provider, the workflow will differ.

24. Click on the Metamask browser extension.

Make sure you are on the Mumbai network, then scroll down to ‘Import Tokens’.

25. Enter your Token’s contract address that was returned when you deployed it and your Token’s symbol:

Then select ‘Add Custom Token’.

Congratulations! You’ve created your own Cryptocurrency!

If you have any questions, please join our Discord server, or send us an email at hello@filebase.com

Last updated