EVM with Hardhat

Bitroot EVM Smart Contract Development with Hardhat

This tutorial will guide you through setting up Hardhat for Bitroot EVM development and using OpenZeppelin contracts to build secure, standardized smart contracts. We’ll cover environment setup, contract creation, deployment, and show how to leverage OpenZeppelin’s pre-built components.

Table of Contents

  • Prerequisites

  • Setting Up Your Development Environment

  • Configuring Hardhat for Bitroot EVM

  • Using OpenZeppelin Contracts

  • Creating and Deploying an ERC20 Token

  • Creating and Deploying an ERC721 NFT

  • Implementing an Upgradeable Smart Contract

  • Testing Your Smart Contracts

  • Deploying to Bitroot Testnet and Mainnet

Prerequisites

Before we begin, ensure you have the following installed:

  • Node.js  (v16.0.0 or later)

  • npm  (v7.0.0 or later) or yarn 

  • A code editor (VS Code recommended)

  • Basic knowledge of Solidity and JavaScript

Setting Up Your Development Environment

Let’s create a new project and set up Hardhat:

After installation, initialize a new Hardhat project:

When prompted, select “Create a JavaScript project” and follow the setup instructions.

Configuring Hardhat for Bitroot EVM

Next, we’ll need to configure Hardhat to work with the Bitroot EVM. Update your hardhat.config.js file:

Create a .env file in your project root to store your private key:

Add .env to your .gitignore file to prevent committing sensitive information.

Using OpenZeppelin Contracts

OpenZeppelin provides a library of secure, tested smart contract components that you can use to build your applications. First, let’s install the OpenZeppelin Contracts package:

For upgradeable contracts, also install:

Update your hardhat.config.js to import the upgrades plugin:

Creating and Deploying an ERC20 Token

Let’s create a simple ERC20 token using OpenZeppelin contracts. Create a new file in the contracts directory called BitrootToken.sol:

Now, create a deployment script in the scripts directory called deploy-bitroot-token.js:

To deploy the token to the Bitroot testnet:

Creating and Deploying an ERC721 NFT

Now, let’s create an ERC721 NFT contract. Create a new file BitrootNFT.sol in the contracts directory:

Create a deployment script deploy-bitroot-nft.js:

Deploy the NFT contract to the Bitroot testnet:

Testing Your Smart Contracts

Hardhat makes it easy to test your contracts before deploying them. Create a test file test/bitroot-token-test.js:

Run your tests with:

Deploying to Bitroot Testnet and Mainnet

Once you’ve tested your contracts, you can deploy them to the Bitroot testnet or mainnet. To deploy, you’ll need:

  1. BRT tokens in your wallet for gas fees

  2. Your private key in the .env file

Deploy to the testnet:

Deploy to the mainnet (only when you’re ready for production):

Last updated