> For the complete documentation index, see [llms.txt](https://rangersprotocol.gitbook.io/technical-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rangersprotocol.gitbook.io/technical-documentation/smart-contract/using-hardhat.md).

# Using Hardhat

Hardhat is an Ethereum development environment. It could interact with Ethereum's API and then deploy smart contracts into the Rangers Protocol. The following content introduces how to configure hardhat on your device, and how to compile and deploy smart contracts.

## Install hardhat&#x20;

{% hint style="warning" %}
Note: If you are a Windows user, it is highly recommended to use the Windows Subsystem for Linux (as known as [WSL2](https://docs.microsoft.com/en-us/windows/wsl/install)) for a better operating experience.
{% endhint %}

The following tutorial is based on Ubuntu and guides you through the process of successfully setting up a hardhat project and deploying smart contracts on Rangers Protocol.

Using hardhat to access Rangers Protocol is also a good option. Before installing hardhat, Make sure you have git and node.js installed. Otherwise follow [this instruction](https://hardhat.org/tutorial/setting-up-the-environment).

* Git&#x20;
* Node.js

Once you have these fundamental applications installed, create a new folder that is the running space for the Hardhat project.

#### Step 1&#x20;

Initialize an npm project as shown below.

```
npm init --yes 
```

<div align="left"><img src="https://lh3.googleusercontent.com/Cc1IoLQRAZr1XpoUB8Qhof4-revDXODWQKyeud4FvF2-IBW9QK5bUAoW6AQjZ9A63_FjbaJOjpXnuYJhsi1yI2OqzRLtzC7l7GjdV-FgKuihwA-wiyBbw-iF6PJYN66Yoiq4v1he-K-Oh6z8yjGKqGA" alt=""></div>

#### Step 2

In the terminal, go to the new folder and use the npm package manager to install hardhat, enter the command:

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

![](https://lh5.googleusercontent.com/LsN_GyzmcMmlL7bIfxCA8ohoJ2KJxP6Tk2wDDDyWO5-IOJ35MFNstvJgPe__M6bCtBGSZA2OpaJxWQRA54Ev3tDDm9tXTIrWamRvVZMKgr4ckydLntSbhUv6p54AUgrGc0nfEgkhV2vucFhE9EV28UE)

#### Step 3&#x20;

Once installed, run the hardhat project in the same directory and enter the command:

```
npx hardhat
```

![](https://lh6.googleusercontent.com/J1Kj1namIORnnCEQiJEyPfPqp7hOnXApB7QupsegXaDJcJNbhthihDQ9HCKEfbhGLAnJt3wy9NpGtSWrd6eRIPn3zVWEqko7XZAmd327vN3GENpk6nJiZkb8NIxRGYj6SWELWp_KAmCt_9TKR6o3F9U)

![](https://lh4.googleusercontent.com/7vH2YM-OqbziQ3BIFJZE0rLfkaGZWGn7NjkdqgCW3ITarkHT-NgjbtZKVN1Nw8tvJ5xey1TQlIys86bR2xx8-ThqP0ReE63vulSHT9hyJ8ejMq8BGyqBDGing5og25NdgNJJYvnX6p5zW4GAwlkHMo0)

#### Step 4&#x20;

After opening “hardhat.config.js”, copy the following code to update “hardhat.config.js”, they will allow you to interact with Rangers Protocol and test your contract.

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

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.9",
   defaultNetwork: "rangers",
  networks: {
    hardhat: {
    },
    rangers: {
      url: "https://robin.rangersprotocol.com/api/jsonrpc",
      accounts: [process.env.PRIVATE_KEY]
    }
  },
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  },
};

```

{% hint style="info" %}
**Note**：Please choose the appropriate solidity version. You could attain more information about Rangers network configuration on\
<https://doc.rangersprotocol.com/network.html>
{% endhint %}

#### Step 5

Create a .env file to store your private key . You can follow Export private key&#x20;

![](https://lh5.googleusercontent.com/z8dwhd7oxpUbbERkm5PZnIVldLud5LwN9f2J0KAZxaVjb1kthOK_OL6i9hJY09yBNe2wmJgOcXl7c0BSbL1-PPHkM_Q4y7-pdqNlYByMZDcKEvn8Il_3UtmE3_7ggOuetDqd_8CdRzsQEQDBiR-I8Ro)

#### Step 6&#x20;

Compile smart contract

```
npx hardhat compile
```

<div align="left"><img src="https://lh4.googleusercontent.com/Nu7XYjRwIrjob35HpZlAlEPhNXii1OX3LB3Itf5cO6h5QHWHMsFPsb5nI7RTVf0Qq1hOnyXLnZ0dtGcgOw1xubSJfVL_Gq-WYPXcyzXPqB-tErcOOhTZAcEaejjpQcCMUEUickogzcb43NhupkUYbvQ" alt=""></div>

#### Step 7&#x20;

Deploy smart contract

```
npx hardhat run scripts/sample-script.js —network rangers
```

![](https://lh3.googleusercontent.com/YPAWv5KCLKw4lIR-dV3F11WoNRr3DxHAI24GggQJsxZulZR8BSamhpLjwBslgz8FC6JZaZ7A-gMTBLqRIEpI2xFYEIWI5EY88ruvOAMrPDPhAfVeoISmIXxyA8OP4w8o3hD7J-NsVu_WJLHfaQdqAt0)

You can check [Rangers scan](https://scan.rangersprotocol.com/) later to verify if your contract is successfully deployed on Rangers protocol.

Congratulations on completing your first hardhat deployment of a smart contract!！


---

# 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://rangersprotocol.gitbook.io/technical-documentation/smart-contract/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.
