Developing, Testing, and Deploying an ERC20 Token on Mantle Testnet using Hardhat and Ethers.js
Emmanuel Akanji
Posted on June 12, 2023
Introduction
In this article, we will guide you through the process of creating and deploying an ERC20 token called Mantlecoin (MTNL) on the Mantle blockchain using Hardhat and Ethers.js. We will cover the necessary prerequisites, environment settings, and provide a step-by-step guide on how to create, test, and deploy your token.
Mantle Blockchain: A Brief History and Overview
Mantle is a blockchain platform designed to provide a scalable and secure infrastructure for decentralized applications (dApps). It offers a unique set of features, including:
High throughput: Mantle's consensus algorithm allows for fast transaction processing and low latency, making it suitable for a wide range of applications.
Security: The platform employs advanced cryptographic techniques to ensure the security and integrity of the network.
Interoperability: Mantle supports cross-chain communication, enabling seamless interaction between different blockchain networks.
Developer-friendly: The platform provides a comprehensive set of tools and libraries to simplify the development and deployment of dApps.
Prerequisites
Node.js 14.8+ or higher
npm (version 6 or higher)
Basic knowledge of JavaScript
Additionally, to interact with the Mantle testnet, it is recommended to have some BIT tokens in your wallet. You can obtain BIT tokens by following the instructions provided by the Mantle testnet faucet.
Setting up the project
First, create a new directory for your project and navigate to it in your terminal. Then, run the following command to initialize a new Hardhat project:
npx hardhat init
This command will create a basic Hardhat project structure with the necessary files and folders.
Installing dependencies
Once you have set up your project, the next step is to install the necessary dependencies.
npm install --save-dev @nomiclabs/hardhat-ethers ethers @openzeppelin/contracts chai
Here's a breakdown of what each dependency represents:
@nomiclabs/hardhat-ethers: This is Hardhat's Ethers.js plugin, which provides integration between Hardhat and the Ethers.js library.
ethers: This library is an essential tool for interacting with Ethereum networks. It offers a simple and intuitive interface for working with smart contracts and handling Ethereum transactions.
@openzeppelin/contracts: This library provides a collection of secure and audited smart contracts, including the ERC20 implementation. Using these contracts can save you time and ensure the reliability of our Mantle token.
chai: This package is a popular assertion library for JavaScript. It is often used in conjunction with testing frameworks like Hardhat to write assertions and make test assertions more readable and expressive.
Configuring Hardhat
Open the hardhat.config.js file and add the following code to enable the Ethers.js plugin:
require("@nomiclabs/hardhat-ethers");
module.exports = {
solidity: "0.8.4",
};
Writing the Mantlecoin contract
In your project directory, create a new file called
Mantlecoin.sol
inside the contracts folder and add the following code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Mantlecoin is ERC20 {
constructor(uint256 initialSupply) ERC20("Mantlecoin", "MTNL") {
_mint(msg.sender, initialSupply);
}
}
In this code snippet, we import the ERC20 contract from the OpenZeppelin Contracts library and create a new contract called Mantlecoin that inherits from ERC20.
The constructor takes an initialSupply argument and calls the parent constructor with the token name "Mantlecoin" and the ticker symbol "MTNL". Finally, we mint the initial supply of tokens and assign them to the contract deployer
Compiling the contract
To compile the Mantlecoin contract, run the following command in your terminal:
npx hardhat compile
This command will compile the contract and generate the necessary artifacts in the artifacts folder.
Writing Tests for Mantlecoin
To ensure that our token works as expected, let's write some tests. Create a new file called Mantlecoin.test.js
inside the test folder and add the following code:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Mantlecoin", function () {
let Mantlecoin, mantlecoin, owner, addr1, addr2;
beforeEach(async () => {
Mantlecoin = await ethers.getContractFactory("Mantlecoin");
[owner, addr1, addr2] = await ethers.getSigners();
mantlecoin = await Mantlecoin.deploy(1000000);
});
it("Should mint the initial supply to the owner", async () => {
const ownerBalance = await mantlecoin.balanceOf(owner.address);
expect(ownerBalance).to.equal(1000000);
});
it("Should transfer tokens between accounts", async () => {
await mantlecoin.transfer(addr1.address, 500);
const addr1Balance = await mantlecoin.balanceOf(addr1.address);
expect(addr1Balance).to.equal(500);
});
});
In this test file, we import the required libraries and set up a beforeEach hook to deploy a new instance of the Mantlecoin contract before each test. We then write two tests: one to check that the initial supply is minted to the owner, and another to test token transfers between accounts.
Running the tests
To run the tests, execute the following command in your terminal:
npx hardhat test
This command will run the tests and display the results in the terminal.
Deploying the Contract to the Mantle Testnet
To deploy our contract to the Mantle testnet, we need to update our hardhat.config.js
file with the appropriate network configuration. Add the following code to your hardhat.config.js file:
require("@nomiclabs/hardhat-ethers");
module.exports = {
solidity: "0.8.4",
networks: {
localhost: {
url: "http://127.0.0.1:8545",
},
mantleTestnet: {
url: "https://rpc.testnet.mantle.xyz/",
chainId: 5001,
accounts: [process.env.PRIVATE_KEY],
},
},
};
Replace process.env.PRIVATE_KEY
with your private key or use an environment variable to store it securely.
Now, create a new file called deploy.js
in the scripts folder and add the following code:
async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
const Mantlecoin = await ethers.getContractFactory("Mantlecoin");
const mantlecoin = await Mantlecoin.deploy(1000000);
console.log("Mantlecoin address:", mantlecoin.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
This script deploys the Mantlecoin contract with an initial supply of 1,000,000 tokens.
To deploy the contract, run the following command:
npx hardhat run --network mantle scripts/deploy.js
This command will deploy the contract to the Mantle network and display the contract address in the terminal.
Conclusion
In this tutorial, we have provided a detailed, step-by-step guide to creating and deploying an ERC20 token called Mantlecoin (MTNL) using Hardhat and Ethers.js. By following this guide, you should now have a solid understanding of how to create, test, and deploy your own ERC20 tokens.
Posted on June 12, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024