Skip to content

HardHat guide

cfxdevkit edited this page Dec 19, 2024 · 2 revisions

Hardhat Overview and Usage Guide

This guide provides a concise introduction to Hardhat's structure and basic functionality. For more comprehensive documentation and tutorials, please refer to the official Hardhat resources:

Hardhat Documentation

Hardhat Tutorial


Repository Structure

When you create a new repository using the core-hardhat-template, the following folder structure is provided:

.
├── contracts
│   └── MyToken.sol
├── hardhat.config.ts
└── scripts
    ├── contracts.ts
    └── tasks

Here's what each folder or file is for:

contracts: Contains Solidity contracts. Your project logic is written here.

hardhat.config.ts: The main configuration file for Hardhat, specifying available networks and other required settings.

scripts: User scripts, the subfolder task contains some custom logic added to hardhat, the main folder contains the deployment script.


Steps to Deploy a Contract

This guide demonstrates how to deploy the provided MyToken.sol contract.

Prerequisite: Install Dependencies

Ensure all dependencies are installed by running:

npm install

1. Configure Your Wallet

⚠️ Warning: The default mnemonic is publicly known. Do not use it on public networks. Any funds transferred using this mnemonic will be at risk of being stolen. Use it only for local testing and development purposes.

Before deploying to public networks, ensure your wallet is configured and has sufficient CFX for deployment.

Add a new wallet with:

devkit wallet add

This launches an interactive interface for generating or entering a mnemonic passphrase. After creating a wallet, activate it with:

devkit wallet select

The wallet configuration is persistent, so this step is only required when adding or changing wallets.

For more wallet commands, run:

devkit wallet -h

Or refer to the Devkit CLI Wiki.


2. Start the Conflux Node

The Conflux blockchain has two partitions:

Core Space: The native partition, used in this guide. ESpace: An EVM-compatible partition.

To start the Conflux node, use:

hh node

After a few seconds, you should see output similar to this:

√ Node started successfully!
⠦ 12/18/2024, 9:21:52 PM | Core Block: 33 | Espace Block: 29 | Default Keystore | [f]: Faucet [q]: Quit

The block count will increment as the node processes blocks. You can leave this terminal open and use a new terminal to continue.


4. Deploy Your Contract

To deploy the contract first you need to compile the contract running:

hh compile

It will generate an output similar to this:

Downloading compiler 0.8.28
Compiled 1 Solidity file successfully (evm target: paris).

After the contract is compiled you can execute the deployment script:

hh run scripts/contracts.ts --network confluxCoreLocal

Note: if the parameter --network is not specified the script will fail

Example output:

MyToken address: NET2029:TYPE.CONTRACT:ACBVM2FA0U69UYFTAF6H4R5GM7HT5DXAJJGF1APXTJ
Initial supply of MyToken: 1000000
New supply of MyToken: 1500000

The contract address (e.g., NET2029:TYPE.CONTRACT:ACBVM2FA0U69UYFTAF6H4R5GM7HT5DXAJJGF1APXTJ) can be used in your frontend or client applications.

To deploy to other networks like testnet (confluxCoreTestnet) or mainnet (confluxCore), follow the same steps, ensuring you have sufficient funds:

hh balance --network confluxCoreTestnet

With these steps, you’re now ready to deploy smart contracts on Conflux Core!