Introduction

This article shows how to deploy a Solidity smart contract using Hardhat on any of Quai Network’s chains.

Prerequisites

To deploy single chain smart contracts on Quai, we’ll need a few tool-kits and dependencies. Here’s an overview of all of the dependencies we’ll be using:

NodeJSJavascript runtime environment. Use the LTS version.
hardhat-exampleA Hardhat project with sample contracts and deploy scripts for Quai Network.
Quais.jsA JavaScript library for interacting with Quai Network.

Environment Setup

Install Example Repository

Start by cloning the hardhat-example repository, navigating to the Solidity directory we’ll be using for this tutorial, and installing the dependencies via npm.

git clone https://github.com/dominant-strategies/hardhat-example.git
cd hardhat-example/Solidity
npm install

Smart Contracts

The Solidity/ directory comes with 2 sample contracts: ERC20.sol and ERC721.sol inside of the contracts/ directory. Both contracts are implementations derived from the Open Zeppelin library.

We’ll be using the ERC20.sol sample contract for this tutorial, but you can also add your own contracts or use contracts from other libraries.

The Quai Network EVM supports Solidity versions up to 0.8.20. Using a newer version of Solidity may result in errors when deploying smart contracts.

Environment Variables

We’ve included a sample environment file, .env.dist, file at the root of the hardhat-example repo to manage token details, private keys, and your RPC URL in a secure fashion.

The .env.dist file is a template file and should not be used as is. You should copy the .env.dist file to a new .env file.

This file lives at the root of the hardhat-example repository and serves as the config file for both the Solidity/ and SolidityX/ directories.

Copy the .env.dist file in the root to a new .env file in the repository root using the following command:

cp ../.env.dist ../.env

Open the .env file and add your private keys, RPC URLs, and token args for the contract you’d like to deploy. The .env file should look like this:

.env
# Unique Privkey for each deployment address
CYPRUS1_PK="0x3700000000000000000000000000000000000000000000000000000000000000" # pubkey starting with 0x00
CYPRUS2_PK="0x9400000000000000000000000000000000000000000000000000000000000000" # pubkey starting with 0x01
...more priv keys

# Chain ID (local: 1337, testnet and devnet: 9000)
CHAIN_ID="9000"

# RPC endpoint
RPC_URL="https://rpc.quai.network"

# Token Arguments
...more token args

The PK values must all be for unique addresses and correspond to the zone name, i.e. your CYPRUS1_PK should be the private key of your Cyprus1 address.

Further information on RPC endpoints can be found in the local network specifications section for local nodes and the devnet specifications section for remote nodes.

The hardhat-example repository uses the Quais SDK to configure network connections using only a single RPC URL. To learn more about how the SDK configures network providers, visit the SDK provider examples section.

After filling in your private keys, RPC URL, we’re now ready to securely consume them inside of hardhat.config.js.

Hardhat Configuration

Hardhat uses a hardhat.config.js file to configure smart contract deployments. The config file allows you to define deployment networks, tasks, compilers, etc.

hardhat-example contains a prebuilt hardhat.config.js file with configurations for deploying and verifying smart contracts on any shard in the network.

When deploying or verifying a contract, hardhat.config.js will pull your private keys and RPC URL from the .env file and use them to deploy and verify your contracts. You can also specify the Solidity version and compiler settings in the solidity object.

Deploy and Interact

1

Compile with Hardhat

Smart contract compilation with Hardhat is simple and can be done using npx in the CLI.

Compile all of the contracts inside the contracts/ directory with:

npx hardhat compile

Which should output:

Downloading compiler 0.8.20

Compiled 2 Solidity files successfully
2

Configure deployment scripts

Inside the scripts/ directory, you’ll find a deploy script for both the ERC20 and ERC721 contracts: deployERC20.js and deployERC721.js. For this tutorial, we’ll be deploying an ERC20 contract.

The deployERC20.js script pulls your network configuration from hardhat.config.js and your token arguments from the .env file at the root of the repository and uses them to deploy your contract.

Token arguments are consumed via the tokenArgs array:

const tokenArgs = [process.env.ERC20_NAME, process.env.ERC20_SYMBOL, quais.parseUnits(process.env.ERC20_INITIALSUPPLY)]

Your specified network configuration and deployment account is consumed inside of the provider and wallet variables in tandem with the compiled contract ABI and bytecode to create a new contract instance:

const provider = new quais.JsonRpcProvider(hre.network.config.url, undefined, { usePathing: true })
const wallet = new quais.Wallet(hre.network.config.accounts[0], provider)
const ERC20 = new quais.ContractFactory(ERC20Json.abi, ERC20Json.bytecode, wallet)

We’ll use these ideas to properly modify the token args and network specification to deploy our contract in the next step.

The deployERC721.js script functions in a similar manner, but with different contract arguments and a different contract ABI and bytecode. You can replicate this configuration for any contract you’d like to deploy.

3

Deploy your contract

The deploy script takes in a --network flag to specify the network you’d like to deploy to (available options can be found here). For this tutorial, we’ll be deploying to cyprus1.

npx hardhat run scripts/deployERC20.js --network cyprus1

Which should output:

Transaction broadcasted: 0x235fdeb85db5b6cee8da9780e2246907e8342751849f5ce3514847a5dffd916f
Contract deployed to:  0x00735E9B2c731Fd3eCC8129a3653ACb99dF969cC

Congratulations, you’ve now deployed a ERC20 token to Quai Network!

The ERC20.sol and ERC721.sol sample contracts are basic implementations of each token for example purposes. It is highly recommended to modify these contracts to fit your specific use case before deploying them for any production use.

4

Interact with the contract

Hardhat console does not currently offer support for interaction with smart contracts on Quai Network.

To interact with the contract, you can configure a simple script using the Quais SDK. The script below configures a Contract instance for the ERC20 token we deployed to 0x00735E9B2c731Fd3eCC8129a3653ACb99dF969cC to get the name, symbol, and total supply of the token.

getContractDetails.js
const quais = require('quais')
const ERC20Json = require('../artifacts/contracts/ERC20.sol/TestERC20.json')

async function getContractDetails() {
  // Config provider, wallet, and contract factory
  const provider = new quais.JsonRpcProvider(hre.network.config.url, undefined, { usePathing: true })
  const wallet = new quais.Wallet(hre.network.config.accounts[0], provider)
  const ERC20 = new quais.Contract("0x00735E9B2c731Fd3eCC8129a3653ACb99dF969cC", ERC20Json.abi, wallet)

  const tokenName = await ERC20.name()
  const tokenSymbol = await ERC20.symbol()
  const tokenDecimals = await ERC20.decimals()
  const tokenTotalSupply = await ERC20.totalSupply()
}

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

Summary

Now you have all the tools you need to create a simple Hardhat project, deploy, and interact with your own smart contracts.