3. Using Hardhat
Use Hardhat to quickly build a dApp on ChainIDE.
1. Open Sandbox
Note: Sandbox functionality is only available after logging in to ChainIDE via GitHub.

Open Sandbox

2. Quick Start
We will explore the basics of creating a Hardhat project with a sample contract, tests of that contract, and a script to deploy it.
To create the sample project, run npx hardhat init in your project folder:
Let’s create the JavaScript or TypeScript project and go through these steps to compile, test and deploy the sample contract. We recommend using TypeScript, but if you are not familiar with it just pick JavaScript.
3. Running tasks
To first get a quick sense of what's available and what's going on, run npx hardhat in your project folder:
The list of available tasks includes the built-in ones and also those that came with any installed plugins. npx hardhat is your starting point to find out what tasks are available to run.
4. Compiling your contracts
Next, if you take a look in the contracts/ folder, you'll see Lock.sol:
To compile it, simply run:
If you created a TypeScript project, this task will also generate TypeScript bindings using TypeChain.
5. Testing your contracts
Your project comes with tests that use Mocha, Chai, and Ethers.js.
If you take a look in the test/ folder, you'll see a test file:
You can run your tests with npx hardhat test:
6. Deploying your contracts
Next, to deploy the contract we will use a Hardhat script.
Inside the scripts/ folder you will find a file with the following code:
You can run it using npx hardhat run scripts/deploy.ts:
7. Connecting a wallet or Dapp to Hardhat Network
By default, Hardhat will spin up a new in-memory instance of Hardhat Network on startup. It's also possible to run Hardhat Network in a standalone fashion so that external clients can connect to it. This could be a wallet, your Dapp front-end, or a script.
To run Hardhat Network in this way, run npx hardhat node:
This will expose a JSON-RPC interface to Hardhat Network. To use it connect your wallet or application to http://127.0.0.1:8545.
If you want other clients to access the Hardhat instance started above through a link, refer to ChainIDE - Port Forwarding to forward the HTTP 8545 port, and copy its address (for example: https://sandbox-1e82a87a951241179f98494a6dbb2617-ethereum-8545.uat-sandbox.chainide.com).

If you want to connect Hardhat to this node, for example to run a deployment script against it, you simply need to run it using --network localhost.
To try this, start a node with npx hardhat node and re-run the deployment script using the network option:
Congrats! You have created a project and compiled, tested and deployed a smart contract.
8. Continue learning
This quick start provided fundamental knowledge about the Hardhat project lifecycle, but there is still much more to learn. Please continue reading the official Hardhat documentation and Polygon: Using Hardhat.
Last updated
Was this helpful?