# Using Hardhat

Hardhat is an Ethereum development environment for flexible, extensible, and fast smart contract development.

You can use Hardhat to edit, compile, debug, and deploy your smart contracts to Ancient8.

## Using Hardhat

This section will guide you through deploying an NFT smart contract (ERC-721) on the Ancient8 test network using [Hardhat](https://hardhat.org/).

### Objectives <a href="#objectives" id="objectives"></a>

By the end of this guide you should be able to do the following:

* Setup Hardhat for Ancient8 Testnet
* Create an NFT smart contract for Ancient8 Testnet
* Compile a smart contract for Ancient8 Testnet
* Deploy a smart contract to Ancient8 Testnet
* Interact with a smart contract deployed on Ancient8 Testnet

### Prerequisites <a href="#prerequisites" id="prerequisites"></a>

#### Node v18+

This guide requires you have Node version 18+ installed.

#### Wallet funds?

Deploying contracts to the blockchain requires a gas fee. Therefore, you will need to fund your wallet with **Sepolia ETH** to cover those gas fees.

### Creating a project <a href="#creating-a-project" id="creating-a-project"></a>

Before you can begin deploying smart contracts to Ancient8 Testnet, you need to set up your development environment by creating a Node.js project.

To create a new Node.js project, run:

```
npm init --y
```

Next, you will need to install Hardhat and create a new Hardhat project

To install Hardhat, run:

```
npm install --save-dev hardhat
```

To create a new Hardhat project, run:

```
npx hardhat
```

Select Create a TypeScript project then press enter to confirm the project root.

Select y for both adding a .gitignore and loading the sample project. It will take a moment for the project setup process to complete.

### Configuring Hardhat with Ancient8 <a href="#configuring-hardhat-with-base" id="configuring-hardhat-with-base"></a>

In order to deploy smart contracts to the Ancient8 Testnet, you will need to configure your Hardhat project and add the Ancient8 Testnet.

To configure Hardhat to use Ancient8 Testnet, add Ancient8 Testnet as a network to your project's `hardhat.config.ts` file:

```javascript
import { HardhatUserConfig } from 'hardhat/config';
import '@nomicfoundation/hardhat-toolbox';

require('dotenv').config();

const config: HardhatUserConfig = {
  solidity: {
    version: '0.8.17',
  },
  networks: {
    // for testnet
    'a8-testnet': {
      url: 'https://rpc-testnet.ancient8.gg',
      accounts: [process.env.WALLET_KEY as string],
      gasPrice: 1000000000,
    },
  },
  defaultNetwork: 'a8-testnet',
};

export default config;
```

#### Install Hardhat toolbox[​](https://docs.base.org/guides/deploy-smart-contracts#install-hardhat-toolbox) <a href="#install-hardhat-toolbox" id="install-hardhat-toolbox"></a>

The above configuration uses the `@nomicfoundation/hardhat-toolbox` plugin to bundle all the commonly used packages and Hardhat plugins recommended to start developing with Hardhat.

To install `@nomicfoundation/hardhat-toolbox`, run:

```
npm install --save-dev @nomicfoundation/hardhat-toolbox
```

#### Loading environment variables <a href="#loading-environment-variables" id="loading-environment-variables"></a>

The above configuration also uses [dotenv](https://www.npmjs.com/package/dotenv) to load the `WALLET_KEY` environment variable from a `.env` file to `process.env.WALLET_KEY`. You should use a similar method to avoid hardcoding your private keys within your source code.

To install `dotenv`, run:

```
npm install --save-dev dotenv
```

Once you have `dotenv` installed, you can create a `.env` file with the following content:

```
WALLET_KEY=<YOUR_PRIVATE_KEY>
```

Substituting `<YOUR_PRIVATE_KEY>` with the private key for your wallet.

{% hint style="warning" %}
**CAUTION**

`WALLET_KEY` is the private key of the wallet to use when deploying a contract. For instructions on how to get your private key from Metamask, visit the [Metamask documentation](https://support.metamask.io/hc/en-us/articles/360015289632-How-to-export-an-account-s-private-key). **It is critical that you do NOT commit this to a public repo**
{% endhint %}

### Compiling the smart contract <a href="#compiling-the-smart-contract" id="compiling-the-smart-contract"></a>

Below is a simple NFT smart contract (ERC-721) written in the Solidity programming language:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract NFT is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private currentTokenId;

    constructor() ERC721("NFT Name", "NFT") {}

    function mint(address recipient)
        public
        returns (uint256)
    {
        currentTokenId.increment();
        uint256 tokenId = currentTokenId.current();
        _safeMint(recipient, tokenId);
        return tokenId;
    }
}
```

The Solidity code above defines a smart contract named `NFT`. The code uses the `ERC721` interface provided by the [OpenZeppelin Contracts library](https://docs.openzeppelin.com/contracts/4.x/) to create an NFT smart contract. OpenZeppelin allows developers to leverage battle-tested smart contract implementations that adhere to official ERC standards.

To add the OpenZeppelin Contracts library to your project, run:

```sh
npm install --save @openzeppelin/contracts
```

In your project, delete the `contracts/Lock.sol` contract that was generated with the project and add the above code in a new file called `contracts/NFT.sol`. (You can also delete the `test/Lock.ts` test file, but you should add your own tests ASAP!).

To compile the contract using Hardhat, run:

```shell
npx hardhat compile
```

### Deploying the smart contract <a href="#deploying-the-smart-contract" id="deploying-the-smart-contract"></a>

Once your contract has been successfully compiled, you can deploy the contract to the Ancient8  Testnet.

To deploy the contract to the Ancient8 Testnet, you'll need to modify the `scripts/deploy.ts` in your project:

```typescript
import { ethers } from 'hardhat';

async function main() {
  const nft = await ethers.deployContract('NFT');

  await nft.waitForDeployment();

  console.log('NFT Contract Deployed at ' + nft.target);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
```

You'll also need **Sepolia ETH** in your wallet. See the [prerequisites](#prerequisites) if you haven't done that yet. Otherwise, the deployment attempt will fail.

Finally, run:

```bash
npx hardhat run scripts/deploy.ts 
```

The contract will be deployed on the Ancient8 Testnet. You can view the deployment status and contract by using a [block explorer](https://testnet.a8scan.io) and searching for the address returned by your deploy script. If you've deployed an exact copy of the NFT contract above, it will already be verified and you'll be able to read and write to the contract using the web interface.

{% hint style="info" %}
**INFO**

If you'd like to deploy to mainnet, you'll modify the command like so:

```
npx hardhat run scripts/deploy.ts --network ancient8-mainnet
```

{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ancient8.gg/building-on-ancient8/deploying-smart-contracts/using-hardhat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
