> For the complete documentation index, see [llms.txt](https://diary.duckchain.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://diary.duckchain.io/2.-users-and-developers/2.3-developer-hub/2.3.2-builder-guide/deploying-smart-contracts/using-hardhat.md).

# Using Hardhat

Guide on deploying a smart contract on DuckChain using **Hardhat**, a popular framework for deploying and verifying smart contracts.

**Initial Setup:**

1. Get some test TON from the DuckChain testnet faucet.
2. Install Hardhat and its dependencies:

   ```
   npm install --save-dev ethers hardhat @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers dotenv
   ```
3. Initialize the project:

   ```
   npx hardhat init
   ```
4. Open `hardhat.config.js` and add the following:

   ```
   require("dotenv").config();
   require("@nomicfoundation/hardhat-toolbox");

   module.exports = {
     solidity: "0.8.9",
     paths: {
       artifacts: "./src",
     },
     networks: {
       DuckChainTestnet: {
         url: `https://testnet-rpc.duckchain.io`,
         accounts: [process.env.ACCOUNT_PRIVATE_KEY],
       },
     },
   };
   ```

**Add Contract Code and Deployment Script:**

1. Create `Storage.sol` in the `contracts` folder with the following code:

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

   contract Storage {
       uint256 _count = 0;

       function set(uint256 count) public {
           _count = count;
       }

       function get() public view returns (uint256){
           return _count;
       }
   }
   ```
2. Create a deployment script `deploy-storage.js`:

   ```
   const hre = require("hardhat");

   async function main() {
       const deployedContract = await hre.ethers.deployContract("Storage");
       await deployedContract.waitForDeployment();
       console.log(
           `Storage contract deployed to https://testnet-scan.duckchain.io/address/${deployedContract.target}`
       );
   }

   main().catch((error) => {
       console.error(error);
       process.exitCode = 1;
   });
   ```
3. Install Hardhat toolbox if not already installed:

   ```
   npm install --save-dev @nomicfoundation/hardhat-toolbox
   ```
4. Compile and deploy the contract:

   ```
   npx hardhat compile
   npx hardhat run scripts/deploy-storage.js --network DuckChainTestnet
   ```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://diary.duckchain.io/2.-users-and-developers/2.3-developer-hub/2.3.2-builder-guide/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.
