Skip to content

Commit f0db4ee

Browse files
committed
create compile, deploy and install and config pages
1 parent 0c38cf0 commit f0db4ee

File tree

3 files changed

+354
-3
lines changed

3 files changed

+354
-3
lines changed
Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,116 @@
1-
TODO
1+
---
2+
title: Compile and Test Smart Contracts with Hardhat
3+
description: Learn how to compile Solidity contracts for PolkaVM compatibility and test them using Hardhat's testing framework on Polkadot Hub.
4+
categories: Smart Contracts, Tooling
5+
---
6+
7+
# Compile and Test Smart Contracts
8+
9+
## Compile Your Contract
10+
11+
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:
12+
13+
- **npm compiler**: Uses library [@parity/resolc](https://www.npmjs.com/package/@parity/resolc){target=\_blank} for simplicity and ease of use.
14+
- **Binary compiler**: Uses your local `resolc` binary directly for more control and configuration options.
15+
16+
To compile your project, follow these instructions:
17+
18+
1. Modify your Hardhat configuration file to specify which compilation process you will be using and activate the `polkavm` flag in the Hardhat network:
19+
20+
=== "npm Configuration"
21+
22+
```javascript title="hardhat.config.js" hl_lines="9-11 14"
23+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:14'
24+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:33:35'
25+
```
26+
27+
=== "Binary Configuration"
28+
29+
```javascript title="hardhat.config.js" hl_lines="9-14 17"
30+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/binary-hardhat.config.js:1:17'
31+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/binary-hardhat.config.js:36:38'
32+
```
33+
34+
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 `resolc` compiler, and download the latest version.
35+
36+
The default settings used can be found in the [`constants.ts`](https://github.com/paritytech/hardhat-polkadot/blob/v0.1.5/packages/hardhat-polkadot-resolc/src/constants.ts#L8-L23){target=\_blank} file of the `hardhat-polkadot` source code. You can change them according to your project needs. Generally, the recommended settings for optimized outputs are the following:
37+
38+
```javascript title="hardhat.config.js" hl_lines="4-10"
39+
resolc: {
40+
...
41+
settings: {
42+
optimizer: {
43+
enabled: true,
44+
parameters: 'z',
45+
fallbackOz: true,
46+
runs: 200,
47+
},
48+
standardJson: true,
49+
},
50+
...
51+
}
52+
```
53+
54+
You can check the [`ResolcConfig`](https://github.com/paritytech/hardhat-polkadot/blob/v0.1.5/packages/hardhat-polkadot-resolc/src/types.ts#L26){target=\_blank} for more information about compilation settings.
55+
56+
2. Compile the contract with Hardhat:
57+
58+
```bash
59+
npx hardhat compile
60+
```
61+
62+
3. After successful compilation, you'll see the artifacts generated in the `artifacts-pvm` directory:
63+
64+
```bash
65+
ls artifacts-pvm/contracts/*.sol/
66+
```
67+
68+
You should see JSON files containing the contract ABI and bytecode of the contracts you compiled.
69+
70+
## Set Up a Testing Environment
71+
72+
Hardhat allows you to spin up a local testing environment to test and validate your smart contract functionalities before deploying to live networks. The `hardhat-polkadot` plugin provides the possibility to spin up a local node with an ETH-RPC adapter for running local tests.
73+
74+
For complete isolation and control over the testing environment, you can configure Hardhat to work with a fresh local Substrate node. This approach is ideal when you want to test in a clean environment without any existing state or when you need specific node configurations.
75+
76+
Configure a local node setup by adding the node binary path along with the ETH-RPC adapter path:
77+
78+
```javascript title="hardhat.config.js" hl_lines="12-20"
79+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:4'
80+
81+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:6:7'
82+
...
83+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:12:24'
84+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:34:35'
85+
```
86+
87+
Replace `INSERT_PATH_TO_SUBSTRATE_NODE` and `INSERT_PATH_TO_ETH_RPC_ADAPTER` with the actual paths to your compiled binaries. The `dev: true` flag configures both the node and adapter for development mode. To obtain these binaries, check the [Installation](/develop/smart-contracts/local-development-node#install-the-substrate-node-and-eth-rpc-adapter){target=\_blank} section on the Local Development Node page.
88+
89+
!!! warning
90+
If you're using the default `hardhat.config.js` created by the `hardhat-polkadot` plugin, it includes a `forking` section pointing to the Polkadot Hub TestNet. When you run `npx hardhat node`, Hardhat will start a fork of that network. To use your local node instead, comment out the `forking` section; otherwise, `npx hardhat node` will continue to use the forked network even if a local node is defined in the configuration.
91+
92+
Once configured, start your chosen testing environment with:
93+
94+
```bash
95+
npx hardhat node
96+
```
97+
98+
This command will launch either the forked network or local node (depending on your configuration) along with the ETH-RPC adapter, providing you with a complete testing environment ready for contract deployment and interaction. By default, the Substrate node will be running on `localhost:8000` and the ETH-RPC adapter on `localhost:8545`.
99+
100+
The output will be something like this:
101+
102+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat-node-output.html'
103+
104+
## Test Your Contract
105+
106+
When testing your contract, be aware that [`@nomicfoundation/hardhat-toolbox/network-helpers`](https://hardhat.org/hardhat-network-helpers/docs/overview){target=\_blank} is not fully compatible with Polkadot Hub's available RPCs. Specifically, Hardhat-only helpers like `time` and `loadFixture` may not work due to missing RPC calls in the node. For more details, refer to the [Compatibility](https://github.com/paritytech/hardhat-polkadot/tree/main/packages/hardhat-polkadot-node#compatibility){target=\_blank} section in the `hardhat-revive` docs. You should avoid using helpers like `time` and `loadFixture` when writing tests.
107+
108+
To run your test:
109+
110+
1. Update the `hardhat.config.js` file accordingly to the [Set Up a Testing Environment](#set-up-a-testing-environment) section.
111+
112+
2. Execute the following command to run your tests:
113+
114+
```bash
115+
npx hardhat test
116+
```
Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,101 @@
1-
TODO
1+
---
2+
title: Deploy Smart Contracts with Hardhat
3+
description: Learn how to deploy smart contracts to Polkadot Hub using Hardhat, including local deployment and testnet deployment with Ignition modules.
4+
categories: Smart Contracts, Tooling
5+
---
6+
7+
# Deploy a Contract using Hardhat
8+
9+
## Deploy to a Local Node
10+
11+
Before deploying to a live network, you can deploy your contract to a local node using [Ignition](https://hardhat.org/ignition/docs/getting-started#overview){target=\_blank} modules:
12+
13+
1. Update the Hardhat configuration file to add the local network as a target for local deployment:
14+
15+
```javascript title="hardhat.config.js" hl_lines="13-16"
16+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:4'
17+
18+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:6:7'
19+
...
20+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:12:13'
21+
...
22+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:24:28'
23+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:33:35'
24+
```
25+
26+
2. Start a local node:
27+
28+
```bash
29+
npx hardhat node
30+
```
31+
32+
This command will spawn a local Substrate node along with the ETH-RPC adapter.
33+
34+
3. In a new terminal window, deploy the contract using Ignition:
35+
36+
```bash
37+
npx hardhat ignition deploy ./ignition/modules/MyToken.js --network localNode
38+
```
39+
40+
## Deploying to a Live Network
41+
42+
After testing your contract locally, you can deploy it to a live network. This guide will use the Polkadot Hub TestNet as the target network. Here's how to configure and deploy:
43+
44+
1. Fund your deployment account with enough tokens to cover gas fees. In this case, the needed tokens are PAS (on Polkadot Hub TestNet). You can use the [Polkadot faucet](https://faucet.polkadot.io/?parachain=1111){target=\_blank} to obtain testing tokens.
45+
46+
2. Export your private key and save it in your Hardhat environment:
47+
48+
```bash
49+
npx hardhat vars set PRIVATE_KEY "INSERT_PRIVATE_KEY"
50+
```
51+
52+
Replace `INSERT_PRIVATE_KEY` with your actual private key. For further details on private key exportation, refer to the article [How to export an account's private key](https://support.metamask.io/configure/accounts/how-to-export-an-accounts-private-key/){target=\_blank}.
53+
54+
!!! warning
55+
Never reveal your private key, otherwise anyone with access to it can control your wallet and steal your funds. Store it securely and never share it publicly or commit it to version control systems.
56+
57+
3. Check that your private key has been set up successfully by running:
58+
59+
```bash
60+
npx hardhat vars get PRIVATE_KEY
61+
```
62+
63+
4. Update your Hardhat configuration file with network settings for the Polkadot network you want to target:
64+
65+
```javascript title="hardhat.config.js" hl_lines="18-22"
66+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:1:4'
67+
68+
const { vars } = require('hardhat/config');
69+
70+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:6:7'
71+
...
72+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:12:13'
73+
...
74+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:24:24'
75+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:25:25'
76+
...
77+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:28:33'
78+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/hardhat.config.js:33:35'
79+
```
80+
81+
6. Deploy your contract using Ignition:
82+
83+
```bash
84+
npx hardhat ignition deploy ./ignition/modules/MyToken.js --network polkadotHubTestnet
85+
```
86+
87+
## Interacting with Your Contract
88+
89+
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.
90+
91+
For example, for the default `MyToken.sol` contract, you can use the following file that connects to the contract at its address and retrieves the `unlockTime`, which represents when funds can be withdrawn. The script converts this timestamp into a readable date and logs it. It then checks the contract's balance and displays it. Finally, it attempts to call the withdrawal function on the contract, but it catches and logs the error message if the withdrawal is not yet allowed (e.g., before `unlockTime`).
92+
93+
```javascript title="interact.js"
94+
--8<-- 'code/develop/smart-contracts/dev-environments/hardhat/interact.js'
95+
```
96+
97+
Run your interaction script:
98+
99+
```bash
100+
npx hardhat run scripts/interact.js --network polkadotHubTestnet
101+
```
Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,137 @@
1-
TODO
1+
---
2+
title: Install and Configure Hardhat for Polkadot Hub
3+
description: Learn how to install and configure Hardhat development environment for building smart contracts on Polkadot Hub with PolkaVM support.
4+
categories: Smart Contracts, Tooling
5+
---
6+
7+
# Hardhat
8+
9+
--8<-- 'text/smart-contracts/polkaVM-warning.md'
10+
11+
<div class="grid cards" markdown>
12+
- :octicons-code-16:{ .lg .middle } __Test and Deploy with Hardhat__
13+
14+
---
15+
16+
Master Solidity smart contract development with Hardhat. Learn testing, deployment, and network interaction in one comprehensive tutorial.
17+
18+
<br>
19+
[:octicons-arrow-right-24: Get Started](/tutorials/smart-contracts/launch-your-first-project/test-and-deploy-with-hardhat){target=\_blank}
20+
21+
</div>
22+
23+
--8<-- 'text/smart-contracts/code-size.md'
24+
25+
## Overview
26+
27+
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 Hub.
28+
29+
## Prerequisites
30+
31+
Before getting started, ensure you have:
32+
33+
- [Node.js](https://nodejs.org/){target=\_blank} (v16.0.0 or later) and npm installed.
34+
- Note: Use Node.js 22.5+ and npm version 10.9.0+ to avoid issues with the Polkadot plugin.
35+
- Basic understanding of Solidity programming.
36+
- Some PAS test tokens to cover transaction fees (easily obtainable from the [Polkadot faucet](https://faucet.polkadot.io/?parachain=1111){target=\_blank}). To learn how to get test tokens, check out the [Test Tokens](/develop/smart-contracts/connect-to-polkadot#test-tokens){target=\_blank} section.
37+
38+
## Set Up Hardhat
39+
40+
1. Create a new directory for your project and navigate into it:
41+
42+
```bash
43+
mkdir hardhat-example
44+
cd hardhat-example
45+
```
46+
47+
2. Initialize a new npm project:
48+
49+
```bash
50+
npm init -y
51+
```
52+
53+
3. To interact with Polkadot, Hardhat requires the following plugin to compile contracts to PolkaVM bytecode and to spawn a local node compatible with PolkaVM:
54+
55+
```bash
56+
npm install --save-dev @parity/[email protected]
57+
```
58+
59+
4. Create a Hardhat project:
60+
61+
```bash
62+
npx hardhat-polkadot init
63+
```
64+
65+
Select **Create a JavaScript project** when prompted and follow the instructions. After that, your project will be created with three main folders:
66+
67+
- **`contracts`**: Where your Solidity smart contracts live.
68+
- **`test`**: Contains your test files that validate contract functionality.
69+
- **`ignition`**: Deployment modules for safely deploying your contracts to various networks.
70+
71+
5. Add the following folder to the `.gitignore` file if it is not already there:
72+
73+
```bash
74+
echo '/ignition/deployments/' >> .gitignore
75+
```
76+
77+
6. Finish the setup by installing all the dependencies:
78+
79+
```bash
80+
npm install
81+
```
82+
83+
!!! note
84+
This last step is needed to set up the `hardhat-polkadot` plugin. It will install the `@parity/hardhat-polkadot` package and all its dependencies. In the future, the plugin will handle this automatically.
85+
86+
nments/hardhat/hardhat-node-output.html'
87+
88+
## Upgrading the Plugin
89+
90+
If you already have a Hardhat Polkadot project and want to upgrade to a newer version of the plugin, to avoid errors (for example, `Cannot find module 'run-container'`), you can clean your dependencies by running the following commands:
91+
92+
```bash
93+
rm -rf node_modules package-lock.json
94+
```
95+
96+
After that, you can upgrade the plugin to the latest version by running the following commands:
97+
98+
```bash
99+
npm install --save-dev @parity/hardhat-polkadot@latest
100+
npm install
101+
```
102+
103+
Consider using [Node.js](https://nodejs.org/){target=\_blank} 22.18+ and [npm](https://www.npmjs.com/){target=\_blank} version 10.9.0+ to avoid issues with the plugin.
104+
105+
## Where to Go Next
106+
107+
Hardhat provides a powerful environment for developing, testing, and deploying smart contracts on Polkadot Hub. Its plugin architecture allows seamless integration with PolkaVM through the `hardhat-resolc` and `hardhat-revive-node` plugins.
108+
109+
Explore more about smart contracts through these resources:
110+
111+
<div class="grid cards" markdown>
112+
113+
- <span class="badge guide">Guide</span> __Smart Contracts on Polkadot__
114+
115+
---
116+
117+
Dive into advanced smart contract concepts.
118+
119+
[:octicons-arrow-right-24: Get Started](/develop/smart-contracts/)
120+
121+
- <span class="badge external">External</span> __Hardhat Documentation__
122+
123+
---
124+
125+
Learn more about Hardhat's advanced features and best practices.
126+
127+
[:octicons-arrow-right-24: Get Started](https://hardhat.org/docs){target=\_blank}
128+
129+
- <span class="badge external">External</span> __OpenZeppelin Contracts__
130+
131+
---
132+
133+
Test your skills by deploying contracts with prebuilt templates.
134+
135+
[:octicons-arrow-right-24: Get Started](https://www.openzeppelin.com/solidity-contracts){target=\_blank}
136+
137+
</div>

0 commit comments

Comments
 (0)