# Deploy with Hardhat

### Deploying a Counter Contract with Hardhat

This guide will walk you through deploying a counter contract using Hardhat, a popular JavaScript-based development environment for Ethereum.

### 1. **Prerequisites**

Before you begin, ensure you have:

* Node.js installed (v12 or later)
* npm (comes with Node.js)
* A code editor (e.g., VS Code)
* MetaMask wallet and testnet tokens for deployment

### 2. **Install Hardhat**

Open your terminal and create a new project directory:

```bash
mkdir counter-project
cd counter-project
```

Initialize a new npm project:

```bash
npm init -y
```

Install Hardhat and required dependencies:

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

```bash
npm install --save-dev @nomicfoundation/hardhat-ignition
```

### 3. **Create a New Hardhat Project**

Run the Hardhat setup wizard:

```bash
npx hardhat
```

Choose “Create a JavaScript project” when prompted.

This will create a project structure like:

* `contracts/` - for Solidity contracts
* `igntion/` - for deployment scripts
* `test/` - for tests
* `hardhat.config.js` - configuration file

### 4. **Write Your Smart Contract**

Create a new file in the contracts directory, `Counter.sol`:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Counter { 
uint256 private count;   
function increment() public {        
count += 1;    
}     
function decrement() public {        
count -= 1;    
}     
function getCount() public view returns (uint256) {        
return count;    
}
}
```

### 5. **Compile the Smart Contract**

Compile your contracts with:

```bash
npx hardhat compile
```

You should see a success message if there are no errors.

### 6. **Write a Deployment Script**

Create a new file in the ignition directory, `Counter.js`:

{% code overflow="wrap" %}

```javascript
const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules"); module.exports = buildModule("CounterModule", (m) => { 
const counter = m.contract("Counter"); 
return { counter };
});
```

{% endcode %}

### **7. Configure Network Settings**

Create a `.env` file in your project root:

```
PRIVATE_KEY=your_private_key_here
```

Edit `hardhat.config.js`:

{% code overflow="wrap" %}

```javascript
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config(); 
module.exports = {  
solidity: "0.8.28",  
networks: {    
hardhat: {      
chainId: 31337,    
},    
lazai: {      
url: "https://testnet.lazai.network",      
chainId: 133718,      
accounts: [process.env.PRIVATE_KEY],    
},  
},
};
```

{% endcode %}

### &#x38;**. Deploy Your Contract**

**Local Deployment (Optional)**

Start the Hardhat local node in a separate terminal:

```bash
npx hardhat node
```

Deploy to local network:

```bash
npx hardhat ignition deploy ignition/modules/Counter.js --network  localhost
```

### D**eploy to LazAI Testnet**

Make sure to:

1. Get testnet tokens from the faucet
2. Add your private key to the `.env` file
3. Never share your private key

**Deploy to LazAI:**

```
npx hardhat ignition deploy ignition/modules/Counter.js --network lazai
```

**Test Setup**

Create `test/Counter.js`:

{% code overflow="wrap" %}

```javascript
const { expect } = require("chai"); 
describe("Counter", function () {  
it("Should increment the counter", async function () {  
const Counter = await ethers.getContractFactory("Counter");    
const counter = await Counter.deploy();    
await counter.deployed();     
await counter.increment();    
expect(await counter.getCount()).to.equal(1);
});
});
```

{% endcode %}

**Running Tests**

```sh
npx hardhat test
```


---

# 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.lazai.network/quickstart/deploy-your-first-smart-contract/deploy-with-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.
