Skip to content

Hardhat Package

Hardhat is a development environment for Ethereum smart contract development. AI Scaffolding provides a pre-configured Hardhat setup to help you get started quickly with smart contract development.

Configuration Options

When adding the Hardhat package to your project, you'll be prompted to configure the following options:

Package Name

You can customize the name of your Hardhat package. By default, it's set to hardhat.

? Enter the name of the hardhat package: (hardhat)

This name will be used as the package name in your project's workspace, e.g., @your-project/hardhat.

Networks

Select which networks your Hardhat setup should support:

? Select networks: (Press <space> to select, <a> to toggle all, <i> to invert selection, and <enter> to proceed)
❯◉ Hardhat  
 Mainnet
 Sepolia
 SilentData

Options include:

  • Hardhat: Local development network (default)
  • Mainnet: Ethereum mainnet
  • Sepolia: Ethereum testnet
  • SilentData: For Silent Data rollup support

Ignition Deployer

Decide whether to include Hardhat Ignition for smart contract deployment:

? Do you want to add Ignition as deployer? (Y/n)

Hardhat Ignition is a declarative deployment system for smart contracts that makes deployments more reliable and easier to manage.

Project Structure

After creating a project with the Hardhat package, the following structure will be generated:

your-project/
└── packages/
    └── hardhat/
        ├── contracts/
        │   └── Lock.sol
        ├── test/
        │   └── Lock.test.ts
        ├── scripts/
        │   └── deploy.ts
        ├── tasks/
        │   └── accounts.ts
        ├── utils/
        │   └── index.ts
        ├── hardhat.config.ts
        ├── tsconfig.json
        ├── package.json
        ├── .env.example
        └── README.md

If you choose to include Ignition, the following directory will also be created:

your-project/
└── packages/
    └── hardhat/
        └── ignition/
            └── modules/
                └── Lock.ts

Environment Setup

Create a .env file in the packages/hardhat directory based on the provided .env.example file:

cd packages/hardhat
cp .env.example .env

Then configure the following environment variables:

# Your private key for deployments
PRIVATE_KEY=0xabc123...

# Network RPC URLs
MAINNET_RPC_URL=https://eth-mainnet.g.alchemy.com/v2/your-api-key
SEPOLIA_RPC_URL=https://eth-sepolia.g.alchemy.com/v2/your-api-key

# Silent Data specific variables (if using Silent Data)
SILENT_DATA_URL=https://api.silentdata.com/...
CHAIN_ID=1337

Common Commands

Once you've set up the Hardhat package, you can use the following commands from your project root:

pnpm
# Compile contracts
pnpm hardhat compile
 
# Run tests
pnpm hardhat test
 
# Start a local node
pnpm hardhat chain
 
# Deploy to local node using Ignition (if enabled)
pnpm hardhat deploy:local
 
# Deploy to Sepolia testnet
pnpm hardhat deploy --network sepolia

Integration with Frontend

When using Hardhat with Vite or Next.js in your project, the frontend packages are automatically configured to connect to the Hardhat node and deployed contracts. After deploying your contracts, you can access them from your frontend code using:

// Import the contract
import { Lock__factory } from '@your-project/hardhat/typechain-types';
 
// Connect to the contract instance
const lockContract = Lock__factory.connect(contractAddress, provider);
 
// Interact with the contract
const unlockTime = await lockContract.unlockTime();

Advanced Configuration

You can customize the Hardhat configuration in the hardhat.config.ts file. Some common configurations include:

Adding Custom Networks

// hardhat.config.ts
export default {
  networks: {
    // Add a custom network
    arbitrum: {
      url: process.env.ARBITRUM_RPC_URL,
      accounts: [process.env.PRIVATE_KEY],
    },
  },
};

Configuring Compiler Settings

// hardhat.config.ts
export default {
  solidity: {
    version: "0.8.20",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
};