How to Create Your Own Token using Solidity (Part 1) – Writing and Deploying the Smart Contract

In January 2022, I did a 30-day project on “Why Your Business Needs to Adapt to Blockchain”. This post is a part of it. To know what I covered, learned, and executed in the project, visit this page.

By now, you probably have read my previous posts (1, 2, 3, 4, 5) on the series “How to Create Your First dApp”. If you have, you would have some familiarity with smart contracts and the Solidity language.

Picking up from there, I created a token of my own. I demonstrate how I did that in this three-part blog series. Let’s get started!

The Smart Contract for Creating the Token

First off, I created a new smart contract.

vi contracts/Token.sol creates a new contract with the name Token.sol. I use the following code to create the token:

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import “hardhat/console.sol”;

contract Token {

  string public name = “Korak Bhaduri First Token”;

  string public symbol = “KB01”;

  uint public totalSupply = 1000;

  mapping(address => uint) balances;

  constructor() {

    balances[msg.sender] = totalSupply;

  }

  function transfer(address to, uint amount) external {

    require(balances[msg.sender] >= amount, “Insufficient tokens”);

    balances[msg.sender] -= amount;

    balances[to] += amount;

  }

  function balanceOf(address account) external view returns (uint) {

    return balances[account];

  }

}

The Token.sol contract is very similar to the Greeter.sol contract I created earlier. The token has a name and a symbol. Name is “Korak Bhaduri First Token” and the symbol I used is “KB01”. The total supply is 1000.

Mapping is used to store data in key => value format, i.e., key is any person’s address on the network and the value is how many tokens or the ‘balance’ they have.

The constructor tells that the person starting the contract will have the entire supply of the tokens. The creator holds all the KB01 tokens initially and they can distribute them.

The read function in this contract is the balanceOf function. It simply takes an account’s address and returns its balances.

The write function is the transfer function. The transfer requires a ‘to’ address and an amount. The only requirement is that the balance of the sender is more than what they are trying to send. If not, “Insufficient tokens” error pops up. The sender’s balance is reduced and the recipient’s balance increased.

I saved and quit using :wq

Next, I compiled it using npx hardhat compile.

Deploying the Token on the Node

I went back to the deploy.js file I edited earlier using vi scripts/deploy.js

I added a few lines in addition to those used for deploying the Greeter app to this file.

Similar to the Greeter.sol contract, I used getContractFactory to get the Token contract from the Contract Factory and deploy to deploy it.

Then, we await for the token to be deployed. Lastly, console.log is used to tell where exactly the token went. Saved and exited.

Next, I ran the files using npx hardhat run scripts/deploy.js –network localhost

As you can see, the Greeter and the Token are deployed at the specified addresses.

I copied both addresses for quick reference.

Greeter address: 0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9

Token address: 0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9

Next, I imported this token to Metamask. Part 2 of this series explains how to do that.