Skip to content

[FIX] - Update hardhat guide according to latest releases of hardhat-polkadot #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
// hardhat.config.js
require('@nomicfoundation/hardhat-toolbox');

require('@nomicfoundation/hardhat-network-helpers');
require('@nomicfoundation/hardhat-chai-matchers');
require('@nomicfoundation/hardhat-ethers');
require('hardhat-gas-reporter');
require('@nomicfoundation/hardhat-ignition');

require('hardhat-resolc');
require('hardhat-revive-node');

require('dotenv').config();
require('@parity/hardhat-polkadot');

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: '0.8.28',
// Remix Compiler
// Npm Compiler
resolc: {
version: '1.5.2',
compilerSource: 'remix',
settings: {
optimizer: {
enabled: false,
runs: 600,
},
evmVersion: 'istanbul',
},
compilerSource: 'npm',
},
// Binary Compiler
resolc: {
Expand Down Expand Up @@ -57,7 +41,7 @@ module.exports = {
polkavm: true,
url: `http://127.0.0.1:8545`,
},
westendAssetHub: {
westendHub: {
polkavm: true,
url: 'https://westend-asset-hub-eth-rpc.polkadot.io',
accounts: [process.env.PRIVATE_KEY],
Expand Down
171 changes: 58 additions & 113 deletions develop/smart-contracts/dev-environments/hardhat.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Use Hardhat with Asset Hub
description: Learn how to create, compile, test, and deploy smart contracts on Asset Hub using Hardhat, a powerful development environment for blockchain developers.
title: Use Hardhat with Polkadot
description: Learn how to create, compile, test, and deploy smart contracts on Polkadot using Hardhat, a powerful development environment for blockchain developers.
---

# Hardhat
Expand All @@ -19,7 +19,7 @@ description: Learn how to create, compile, test, and deploy smart contracts on A

## Overview

Hardhat is a robust development environment for Ethereum-compatible chains that makes smart contract development more efficient. This guide walks you through the essentials of using Hardhat to create, compile, test, and deploy smart contracts on Asset Hub.
Hardhat is a robust development environment for Ethereum-compatible chains that makes smart contract development more efficient. This guide walks you through the essentials of using Hardhat to create, compile, test, and deploy smart contracts on Polkadot.

## Prerequisites

Expand All @@ -28,7 +28,6 @@ Before getting started, ensure you have:
- [Node.js](https://nodejs.org/){target=\_blank} (v16.0.0 or later) and npm installed
- Basic understanding of Solidity programming
- Some WND test tokens to cover transaction fees (easily obtainable from the [Polkadot faucet](https://faucet.polkadot.io/westend?parachain=1000){target=\_blank}). To learn how to get test tokens, check out the [Test Tokens](/develop/smart-contracts/connect-to-asset-hub/#test-tokens){target=\_blank} section
- [MetaMask](https://metamask.io/){target=\_blank} installed and connected to [Westend Asset Hub](https://chainlist.org/chain/420420421){target=\_blank}. For more detailed instructions on connecting your wallet, see the [Connect Your Wallet](/develop/smart-contracts/connect-to-asset-hub/#connect-your-wallet){target=\_blank} section

## Setting Up Hardhat

Expand All @@ -48,13 +47,13 @@ Before getting started, ensure you have:
3. Install Hardhat and the required plugins:

```bash
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox
npm install --save-dev hardhat@"<2.23.0" @nomicfoundation/hardhat-toolbox
```

To interact with Asset Hub, Hardhat requires the [`hardhat-resolc`](https://www.npmjs.com/package/hardhat-resolc){target=\_blank} plugin to compile contracts to PolkaVM bytecode and the [`hardhat-revive-node`](https://www.npmjs.com/package/hardhat-revive-node){target=\_blank} plugin to spawn a local node compatible with PolkaVM.
To interact with Polkadot, Hardhat requires the followin plugins to compile contracts to PolkaVM bytecode and to spawn a local node compatible with PolkaVM.

```bash
npm install --save-dev hardhat-resolc hardhat-revive-node
npm install --save-dev @parity/hardhat-polkadot @parity/resolc
```

4. Create a Hardhat project:
Expand All @@ -69,47 +68,41 @@ Before getting started, ensure you have:
- **`test`** - contains your test files that validate contract functionality
- **`ignition`** - deployment modules for safely deploying your contracts to various networks

5. Update your Hardhat configuration file (`hardhat.config.js`) to include the plugins:
5. Update your Hardhat configuration file (`hardhat.config.js`) to include the `hardhat-polkadot` plugin:

```javascript title="hardhat.config.js" hl_lines="9-10"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:0:12'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:15:16'
```javascript title="hardhat.config.js" hl_lines="4-4"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:7'
// Additional configuration will be added later
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:66:66'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:50:50'
```

## Compiling Your Contract

The `hardhat-resolc` plugin will compile your Solidity contracts for Solidity versions `0.8.0` and higher to be PolkaVM compatible. When compiling your contract using the `hardhat-resolc` plugin, there are two ways to configure your compilation process:
The plugin will compile your Solidity contracts for Solidity versions `0.8.0` and higher to be PolkaVM compatible. When compiling your contract, there are two ways to configure your compilation process:

- **Remix compiler** - uses the Remix online compiler backend for simplicity and ease of use
- **Binary compiler** - uses the resolc binary directly for more control and configuration options
- **Npm compiler** - uses library [@parity/revive](https://www.npmjs.com/package/@parity/revive){target=\_blank} for simplicity and ease of use
- **Binary compiler** - uses your local `resolc` binary directly for more control and configuration options

To compile your project, follow these instructions:

1. Modify your Hardhat configuration file to specify which compilation process you will be using:
1. Modify your Hardhat configuration file to specify which compilation process you will be using and activate the `polkavm` flag in the hardhat network:

=== "Remix Configuration"
=== "Npm Configuration"

```javascript title="hardhat.config.js" hl_lines="15-25"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:0:12'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:15:17'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:19:29'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:43:45'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:65:66'
```javascript title="hardhat.config.js" hl_lines="10-13"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:13'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:50:50'
```

=== "Binary Configuration"

```javascript title="hardhat.config.js" hl_lines="15-26"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:0:12'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:15:17'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:31:42'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:43:45'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:64:66'
```javascript title="hardhat.config.js" hl_lines="10-21"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:8'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:14:26'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:50:50'
```

For the binary configuration, replace `INSERT_PATH_TO_RESOLC_COMPILER` with the proper path to the binary. For more information about its installation, check the [installation](https://github.com/paritytech/revive?tab=readme-ov-file#installation){target=\_blank} section of the `pallet-revive`.
For the binary configuration, replace `INSERT_PATH_TO_RESOLC_COMPILER` with the proper path to the binary. To obtain the binary, check the [releases](https://github.com/paritytech/revive/releases){target=\_blank} section of the `revive` compiler, and download the latest version.

2. Compile the contract with Hardhat:

Expand Down Expand Up @@ -143,7 +136,7 @@ npx hardhat test

## Deploying with a Local Node

Before deploying to a live network, you can deploy your contract to a local node using the [`hardhat-revive-node`](https://www.npmjs.com/package/hardhat-revive-node){target=\_blank} plugin and Ignition modules:
Before deploying to a live network, you can deploy your contract to a local node using Ignition modules:

!!! warning "Contract Size Limitation in Testing Environment"

Expand All @@ -153,85 +146,38 @@ Before deploying to a live network, you can deploy your contract to a local node

This limitation is established by Hardhat based on Ethereum's default contract size limits. While Hardhat can disable this limitation, technical constraints currently prevent it from being applied to the PolkaVM test environment.

1. First, ensure you have compiled a Substrate node and the ETH RPC adapter from the Polkadot SDK. Checkout the [compatible commit](https://github.com/paritytech/polkadot-sdk/commit/c29e72a8628835e34deb6aa7db9a78a2e4eabcee){target=\_blank} from the SDK and build the node and the ETH-RPC from source:
1. Update the Hardhat configuration file to add the local node as a target for local deployment:

```bash
git clone https://github.com/paritytech/polkadot-sdk.git
cd polkadot-sdk
git checkout {{ dependencies.repositories.polkadot_sdk_compatible_hardhat_node }}
```

Now, build the node and the ETH-RPC adapter. Note that this process might take a long time to complete:

```bash
# Build the substrate node
cargo build --release
# Build the eth-rpc adapter
cargo build -p pallet-revive-eth-rpc --bin eth-rpc --release
```
=== "Npm Configuration"

2. Update the Hardhat configuration file to add the local node as a target for local deployment:

=== "Remix Configuration"

```javascript title="hardhat.config.js" hl_lines="27-44"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:0:12'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:15:29'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:43:59'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:65:66'
```javascript title="hardhat.config.js" hl_lines="27-30"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:13'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:27:43'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:49:50'
```

=== "Binary Configuration"

```javascript title="hardhat.config.js" hl_lines="27-44"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:0:12'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:15:17'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:31:42'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:43:59'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:65:66'
```javascript title="hardhat.config.js" hl_lines="35-38"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:8'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:14:43'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:49:50'
```

Ensure to replace `INSERT_PATH_TO_SUBSTRATE_NODE` and `INSERT_PATH_TO_ETH_RPC_ADAPTER` with the proper paths to the compiled binaries. Since you compiled these from source using Rust's Cargo build system, you can find them at:

- Substrate node path - `polkadot-sdk/target/release/substrate-node`
- ETH-RPC adapter path - `polkadot-sdk/target/release/eth-rpc`

For example, if you cloned the polkadot-sdk repository to your home directory, the paths might look like:

```javascript
nodeBinaryPath: '/home/username/polkadot-sdk/target/release/substrate-node',
adapterBinaryPath: '/home/username/polkadot-sdk/target/release/eth-rpc',
```

3. Modify the Ignition modules, considering that the value of the pallet revive `block.timestamp` is returned in seconds. Check this [PR](https://github.com/paritytech/polkadot-sdk/pull/7792/files){target=\_blank} for more information. For example, for the default `ignition/modules/Lock.js` file, the needed modification should be:

```diff
- const JAN_1ST_2030 = 1893456000;
+ const JAN_1ST_2030 = 18934560000000;
```

???--- code "Modified `ignition/modules/Lock.js` file"

```js title="Lock.js"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/lock-ignition.js'
```

4. Start a local node:
2. Start a local node:

```bash
npx hardhat node-polkavm
npx hardhat node
```

This command will start a local PolkaVM node powered by the `hardhat-revive-node` plugin.
This command will spawn a local substrate node along with the ETC-RPC adapter.

5. In a new terminal window, deploy the contract using Ignition:
3. In a new terminal window, deploy the contract using Ignition:

```bash
npx hardhat ignition deploy ./ignition/modules/INSERT_IGNITION_MODULE_NAME.js --network localNode
npx hardhat ignition deploy ./ignition/modules/Lock.js --network localNode
```

Replace `INSERT_IGNITION_MODULE_NAME` with the proper name for your contract. You'll see deployment information, including the contract address.

## Deploying to a Live Network

After testing your contract locally, you can deploy it to a live network. This guide will use Westend Asset Hub as the target network. Here's how to configure and deploy:
Expand All @@ -252,44 +198,43 @@ After testing your contract locally, you can deploy it to a live network. This g
3. Install the [`dotenv`](https://www.npmjs.com/package/dotenv){target=\_blank} package to load the private key into your Hardhat configuration:

```bash
npm install dotenv
npm install --save-dev dotenv
```

4. Update your config to load it:

```javascript title="hardhat.config.js" hl_lines="12"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:0:16'
```javascript title="hardhat.config.js" hl_lines="6"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:4'

require('dotenv').config();

--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:6:7'
// The rest remains the same...
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:66:66'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:50:50'
```

5. Update your Hardhat configuration file with network settings for the Asset Hub network you want to target:
5. Update your Hardhat configuration file with network settings for the Polkadot network you want to target:

=== "Remix Configuration"
=== "Npm Configuration"

```javascript title="hardhat.config.js" hl_lines="44-48"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:0:12'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:15:29'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:43:66'
```javascript title="hardhat.config.js" hl_lines="31-35"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:13'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:27:50'
```

=== "Binary Configuration"

```javascript title="hardhat.config.js" hl_lines="44-48"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:0:12'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:15:17'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:31:42'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:43:66'
```javascript title="hardhat.config.js" hl_lines="39-43"
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:8'
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:14:50'
```

4. Deploy your contract using Ignition:
5. Deploy your contract using Ignition:

```bash
npx hardhat ignition deploy ./ignition/modules/INSERT_IGNITION_MODULE_NAME.js --network westendAssetHub
npx hardhat ignition deploy ./ignition/modules/Lock.js --network westendHub
```

Replace `INSERT_IGNITION_MODULE_NAME` with the proper name for your contract. You'll see deployment information, including the contract address.

## Interacting with Your Contract

Once deployed, you can create a script to interact with your contract. To do so, create a file called `scripts/interact.js` and add some logic to interact with the contract.
Expand All @@ -303,7 +248,7 @@ For example, for the default `Lock.sol` contract, you can use the following file
Run your interaction script:

```bash
npx hardhat run scripts/interact.js --network westendAssetHub
npx hardhat run scripts/interact.js --network westendHub
```

## Where to Go Next
Expand Down
Loading