Last updated
Last updated
This tutorial is suitable for Conflux eSpace.
Note: Sandbox functionality is only available after logging in to ChainIDE via GitHub.
Open Sandbox
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.
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.
Next, if you take a look in the contracts/
folder, you'll see Lock.sol
:
To compile it, simply run:
If you take a look in the test/
folder, you'll see a test file:
You can run your tests with npx hardhat test
:
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
:
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 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.
If you created a TypeScript project, this task will also generate TypeScript bindings using .
Your project comes with tests that use , , and .
If you want other clients to access the Hardhat instance started above through a link, refer to to forward the HTTP 8545 port, and copy its address (for example: ).
This quick start provided fundamental knowledge about the Hardhat project lifecycle, but there is still much more to learn. Please continue reading the and and .
Use Hardhat to quickly build a dApp on Conflux eSpace