Skip to content

Commit 991cf5f

Browse files
update hardhat documentation for web0 hackathon (#1227)
1 parent 9e55775 commit 991cf5f

File tree

7 files changed

+251
-577
lines changed

7 files changed

+251
-577
lines changed

.ai/categories/smart-contracts.md

Lines changed: 66 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -10427,10 +10427,10 @@ Before getting started, ensure you have:
1042710427
npm init -y
1042810428
```
1042910429

10430-
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:
10430+
3. To interact with Polkadot, Hardhat requires the following plugin to compile contracts and to spawn a local node for testing:
1043110431

1043210432
```bash
10433-
npm install --save-dev @parity/hardhat-polkadot@0.1.9
10433+
npm install --save-dev @parity/hardhat-polkadot@latest
1043410434
```
1043510435

1043610436
4. Create a Hardhat project:
@@ -10439,7 +10439,7 @@ Before getting started, ensure you have:
1043910439
npx hardhat-polkadot init
1044010440
```
1044110441

10442-
Select **Create a JavaScript project** when prompted and follow the instructions. After that, your project will be created with three main folders:
10442+
Follow the project creation wizard. Your project will be created with three main folders:
1044310443

1044410444
- **`contracts`**: Where your Solidity smart contracts live.
1044510445
- **`test`**: Contains your test files that validate contract functionality.
@@ -10462,143 +10462,83 @@ Before getting started, ensure you have:
1046210462

1046310463
## Compile Your Contract
1046410464

10465-
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:
10465+
The plugin will compile your Solidity contracts for Solidity versions `0.8.0` and higher. To compile your project, follow these instructions:
1046610466

10467-
- **npm compiler**: Uses library [@parity/resolc](https://www.npmjs.com/package/@parity/resolc){target=\_blank} for simplicity and ease of use.
10468-
- **Binary compiler**: Uses your local `resolc` binary directly for more control and configuration options.
10467+
1. Make sure your Hardhat configuration file looks like the following. Note that it may differ slightly based on the language choice made during the `init` step of setting up Hardhat:
1046910468

10470-
To compile your project, follow these instructions:
10471-
10472-
1. Modify your Hardhat configuration file to specify which compilation process you will be using and activate the `polkavm` flag in the Hardhat network:
10473-
10474-
=== "npm Configuration"
10475-
10476-
```javascript title="hardhat.config.js" hl_lines="9-11 14"
10477-
// hardhat.config.js
10478-
require('@nomicfoundation/hardhat-toolbox');
10479-
10480-
require('@parity/hardhat-polkadot');
10481-
10482-
/** @type import('hardhat/config').HardhatUserConfig */
10483-
module.exports = {
10484-
solidity: '0.8.28',
10485-
resolc: {
10486-
compilerSource: 'npm',
10469+
```javascript title="hardhat.config.js" hl_lines="5-7 19-21 25-27"
10470+
module.exports = {
10471+
solidity: '0.8.28',
10472+
networks: {
10473+
hardhat: {
10474+
polkadot: {
10475+
target: 'evm',
1048710476
},
10488-
networks: {
10489-
hardhat: {
10490-
polkavm: true,
10491-
},
10477+
nodeConfig: {
10478+
nodeBinaryPath: './bin/dev-node',
10479+
rpcPort: 8000,
10480+
dev: true,
1049210481
},
10493-
};
10494-
```
10495-
10496-
=== "Binary Configuration"
10497-
10498-
```javascript title="hardhat.config.js" hl_lines="9-14 17"
10499-
// hardhat.config.js
10500-
require('@nomicfoundation/hardhat-toolbox');
10501-
10502-
require('@parity/hardhat-polkadot');
10503-
10504-
/** @type import('hardhat/config').HardhatUserConfig */
10505-
module.exports = {
10506-
solidity: '0.8.28',
10507-
resolc: {
10508-
compilerSource: 'binary',
10509-
settings: {
10510-
compilerPath: 'INSERT_PATH_TO_RESOLC_COMPILER',
10511-
},
10482+
adapterConfig: {
10483+
adapterBinaryPath: './bin/eth-rpc',
10484+
dev: true,
1051210485
},
10513-
networks: {
10514-
hardhat: {
10515-
polkavm: true,
10516-
},
10486+
},
10487+
localNode: {
10488+
polkadot: {
10489+
target: 'evm',
1051710490
},
10518-
};
10519-
```
10520-
10521-
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.
10522-
10523-
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:
10524-
10525-
```javascript title="hardhat.config.js" hl_lines="4-10"
10526-
resolc: {
10527-
...
10528-
settings: {
10529-
optimizer: {
10530-
enabled: true,
10531-
parameters: 'z',
10532-
fallbackOz: true,
10533-
runs: 200,
10491+
url: `http://127.0.0.1:8545`,
10492+
},
10493+
polkadotHubTestnet: {
10494+
polkadot: {
10495+
target: 'evm',
10496+
},
10497+
url: 'https://testnet-passet-hub-eth-rpc.polkadot.io',
10498+
accounts: [vars.get('PRIVATE_KEY')],
1053410499
},
10535-
standardJson: true,
1053610500
},
10537-
...
10538-
}
10501+
};
10502+
1053910503
```
1054010504

10541-
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.
10505+
To obtain the `dev-node` and `eth-rpc` binaries required in `nodeConfig` and `adapterConfig` respectively, check this [release](https://github.com/paritytech/hardhat-polkadot/releases/tag/nodes-19071579107){target=\_blank} and download the binaries as per your development platform and update the paths in your Hardhat config.
10506+
10507+
!!! note
10508+
You might have to give executable permissions to the binaries:
10509+
```bash
10510+
chmod +x /path/to/your/binary
10511+
```
10512+
In macOS environments, binaries are sometimes quarantined. To remove this, run:
10513+
```bash
10514+
xattr -d com.apple.quarantine /path/to/your/binary
10515+
```
1054210516

1054310517
2. Compile the contract with Hardhat:
1054410518

1054510519
```bash
1054610520
npx hardhat compile
1054710521
```
1054810522

10549-
3. After successful compilation, you'll see the artifacts generated in the `artifacts-pvm` directory:
10523+
3. After successful compilation, you'll see the artifacts generated in the `artifacts` directory:
1055010524

1055110525
```bash
10552-
ls artifacts-pvm/contracts/*.sol/
10526+
ls artifacts/contracts/*.sol/
1055310527
```
1055410528

10555-
You should see JSON files containing the contract ABI and bytecode of the contracts you compiled.
10529+
You should see JSON files containing the contract ABIs and bytecodes for the contracts you compiled.
1055610530

1055710531
## Set Up a Testing Environment
1055810532

10559-
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.
10560-
10561-
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.
10562-
10563-
Configure a local node setup by adding the node binary path along with the ETH-RPC adapter path:
10533+
Hardhat lets you spin up a local testing environment to test and validate your smart contract functionality before deploying to live networks. The `hardhat-polkadot` plugin allows you to spin up a local node with an ETH-RPC adapter for running local tests.
1056410534

10565-
```javascript title="hardhat.config.js" hl_lines="12-20"
10566-
// hardhat.config.js
10567-
require('@nomicfoundation/hardhat-toolbox');
10568-
10569-
require('@parity/hardhat-polkadot');
10570-
/** @type import('hardhat/config').HardhatUserConfig */
10571-
module.exports = {
10572-
...
10573-
networks: {
10574-
hardhat: {
10575-
polkavm: true,
10576-
nodeConfig: {
10577-
nodeBinaryPath: 'INSERT_PATH_TO_SUBSTRATE_NODE',
10578-
rpcPort: 8000,
10579-
dev: true,
10580-
},
10581-
adapterConfig: {
10582-
adapterBinaryPath: 'INSERT_PATH_TO_ETH_RPC_ADAPTER',
10583-
dev: true,
10584-
},
10585-
},
10586-
},
10587-
};
10588-
```
10589-
10590-
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.
10591-
10592-
!!! warning
10593-
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.
10594-
10595-
Once configured, start your chosen testing environment with:
10535+
Once you have set up the binaries as per the [Compile Your Contract](#compile-your-contract) section, start your local testing node with:
1059610536

1059710537
```bash
1059810538
npx hardhat node
1059910539
```
1060010540

10601-
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`.
10541+
This command launches a local node with the ETH-RPC adapter, providing a complete testing environment ready for contract deployment and interaction. By default, the Substrate node runs on `localhost:8000`, and the ETH-RPC adapter on `localhost:8545`.
1060210542

1060310543
The output will be something like this:
1060410544

@@ -10629,7 +10569,7 @@ When testing your contract, be aware that [`@nomicfoundation/hardhat-toolbox/net
1062910569

1063010570
To run your test:
1063110571

10632-
1. Update the `hardhat.config.js` file accordingly to the [Set Up a Testing Environment](#set-up-a-testing-environment) section.
10572+
1. Update the `hardhat.config.js` file as per the [Compile Your Contract](#compile-your-contract) section.
1063310573

1063410574
2. Execute the following command to run your tests:
1063510575

@@ -10641,27 +10581,15 @@ To run your test:
1064110581

1064210582
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:
1064310583

10644-
1. Update the Hardhat configuration file to add the local network as a target for local deployment:
10645-
10646-
```javascript title="hardhat.config.js" hl_lines="13-16"
10647-
// hardhat.config.js
10648-
require('@nomicfoundation/hardhat-toolbox');
10584+
1. Make sure that the local network is added as a target in your Hardhat configuration file for local deployment:
1064910585

10650-
require('@parity/hardhat-polkadot');
10651-
/** @type import('hardhat/config').HardhatUserConfig */
10652-
module.exports = {
10653-
...
10654-
networks: {
10655-
hardhat: {
10656-
...
10657-
},
10658-
localNode: {
10659-
polkavm: true,
10660-
url: `http://127.0.0.1:8545`,
10661-
},
10586+
```javascript title="hardhat.config.js"
10587+
localNode: {
10588+
polkadot: {
10589+
target: 'evm',
10590+
},
10591+
url: `http://127.0.0.1:8545`,
1066210592
},
10663-
},
10664-
};
1066510593
```
1066610594

1066710595
2. Start a local node:
@@ -10701,33 +10629,16 @@ After testing your contract locally, you can deploy it to a live network. This g
1070110629
npx hardhat vars get PRIVATE_KEY
1070210630
```
1070310631

10704-
4. Update your Hardhat configuration file with network settings for the Polkadot network you want to target:
10705-
10706-
```javascript title="hardhat.config.js" hl_lines="18-22"
10707-
// hardhat.config.js
10708-
require('@nomicfoundation/hardhat-toolbox');
10709-
10710-
require('@parity/hardhat-polkadot');
10711-
const { vars } = require('hardhat/config');
10632+
4. Make sure the Polkadot Hub TestNet is added as a target in your Hardhat configuration file:
1071210633

10713-
/** @type import('hardhat/config').HardhatUserConfig */
10714-
module.exports = {
10715-
...
10716-
networks: {
10717-
hardhat: {
10718-
...
10719-
},
10720-
localNode: {
10721-
...
10722-
},
10723-
polkadotHubTestnet: {
10724-
polkavm: true,
10725-
url: 'https://testnet-passet-hub-eth-rpc.polkadot.io',
10726-
accounts: [vars.get('PRIVATE_KEY')],
10727-
},
10634+
```javascript title="hardhat.config.js"
10635+
polkadotHubTestnet: {
10636+
polkadot: {
10637+
target: 'evm',
10638+
},
10639+
url: 'https://testnet-passet-hub-eth-rpc.polkadot.io',
10640+
accounts: [vars.get('PRIVATE_KEY')],
1072810641
},
10729-
},
10730-
};
1073110642
```
1073210643

1073310644
6. Deploy your contract using Ignition:

0 commit comments

Comments
 (0)