Hardhat Polkadot
Introduction
Section titled “Introduction”Hardhat is a flexible development environment for building, testing, and deploying smart contracts. With the @parity/hardhat-polkadot plugin, you can compile Solidity contracts to PVM bytecode and deploy them to Polkadot Hub. This page demonstrates how to set up a Hardhat project for PVM development.
Prerequisites
Section titled “Prerequisites”Before setting up Hardhat, make sure the following are installed:
- Node.js version 22.5+ and npm version 10.9.0+ to avoid issues with the Polkadot plugin
- A package manager like npm, pnpm, or yarn
Initialize a Hardhat Project
Section titled “Initialize a Hardhat Project”-
Create a directory to hold your project files:
Terminal window mkdir hardhat-pvm-examplecd hardhat-pvm-example -
Initialize a new npm project:
Terminal window npm init -y -
Install the Polkadot plugin for Hardhat:
Terminal window npm install --save-dev @parity/hardhat-polkadot@0.3.0 -
Install the resolc compiler (required for compiling Solidity to PVM):
Terminal window npm install --save-dev @parity/resolc@1.5.0 -
Initialize a Hardhat project:
Terminal window npx hardhat-polkadot initFollow the project creation wizard. Your project will be created with three main folders:
contracts: Stores your Solidity smart contracts.ignition: Contains deployment modules for safely deploying your contracts to various networks.test: Contains test files that validate contract functionality.
-
Add the following folder to the
.gitignorefile if it is not already there:Terminal window echo '/ignition/deployments/' >> .gitignore -
Complete the setup by installing all dependencies:
Terminal window npm install
Compile Your Contract
Section titled “Compile Your Contract”The plugin will compile your Solidity contracts (version 0.8.0 and higher) to PVM bytecode using the resolc compiler:
npx hardhat compileAfter successful compilation, you'll see the artifacts generated in the artifacts directory:
ls artifacts/contracts/*.sol/You should see JSON files containing the contract ABIs and bytecodes for the contracts you compiled.
Test Your Contract Locally
Section titled “Test Your Contract Locally”Hardhat provides a local testing environment through the hardhat-polkadot plugin, which spins up a local Substrate node with an ETH-RPC adapter.
Start a Local Node
Section titled “Start a Local Node”To obtain the dev-node and eth-rpc binaries, check this release and download the binaries for your platform, then update the paths in your config.
Once you have set up the binaries, start your local testing node:
npx hardhat nodeThis command launches a local node with the ETH-RPC adapter. By default, the Substrate node runs on localhost:8000, and the ETH-RPC adapter on localhost:8545.
The output will be something like this:
Starting server at 127.0.0.1:8000 ../bin/substrate-node --rpc-port=8000 --dev Starting the Eth RPC Adapter at 127.0.0.1:8545 ../bin/eth-rpc --node-rpc-url=ws://localhost:8000 --dev 2025-05-29 13:00:32 Running in --dev mode, RPC CORS has been disabled. 2025-05-29 13:00:32 Running in --dev mode, RPC CORS has been disabled. 2025-05-29 13:00:32 🌐 Connecting to node at: ws://localhost:8000 ... 2025-05-29 13:00:32 Substrate Node 2025-05-29 13:00:32 ✌️ version 3.0.0-dev-f73c228b7a1 2025-05-29 13:00:32 ❤️ by Parity Technologies <admin@parity.io>, 2017-2025 2025-05-29 13:00:32 📋 Chain specification: Development 2025-05-29 13:00:32 🏷 Node name: electric-activity-4221 2025-05-29 13:00:32 👤 Role: AUTHORITY 2025-05-29 13:00:32 💾 Database: RocksDb at /var/folders/f4/7rdt2m9d7j361dm453cpggbm0000gn/T/substrateOaoecu/chains/dev/db/full 2025-05-29 13:00:36 [0] 💸 generated 1 npos voters, 1 from validators and 0 nominators ...
Run Tests
Section titled “Run Tests”To run your contract tests:
npx hardhat testDeploy to Local Node
Section titled “Deploy to Local Node”Before deploying to a live network, you can deploy your contract to a local node using Ignition modules:
-
Ensure your local node is running:
Terminal window npx hardhat node -
In a new terminal window, deploy the contract using Ignition:
Terminal window npx hardhat ignition deploy ./ignition/modules/MyToken.js --network localNode
Deploy to Polkadot Hub TestNet
Section titled “Deploy to Polkadot Hub TestNet”After testing locally, you can deploy to the Polkadot Hub TestNet:
-
Fund your deployment account with PAS test tokens from the Polkadot faucet.
-
Set your private key as a configuration variable:
Terminal window npx hardhat vars set PRIVATE_KEY -
Verify your private key is set:
Terminal window npx hardhat vars get PRIVATE_KEY -
Deploy your contract:
Terminal window npx hardhat ignition deploy ./ignition/modules/MyToken.js --network polkadotTestnet
Common Issues
Section titled “Common Issues”- Compilation fails with resolc errors: Ensure you have
@parity/resolc@1.5.0installed and the version is specified in yourhardhat.config.js - Deployment hangs on local node: You might need to set
ignition.requiredConfirmations: 1in your config file - Binary permission issues: Run
chmod +x /path/to/your/binaryand on macOS, usexattr -d com.apple.quarantine /path/to/your/binary
Where to Go Next
Section titled “Where to Go Next”-
Deploy a Basic Contract
Ready to start using Hardhat with PVM? Learn how to compile, test, and deploy a basic contract.
-
Deploy an ERC-20
Walk through deploying a fully-functional ERC-20 to Polkadot Hub using Hardhat.
-
Hardhat Documentation
Learn more about Hardhat's advanced features and best practices.
-
Hardhat Polkadot Plugin
Explore the full capabilities of the Hardhat Polkadot plugin.