Skip to content

Feat/jvd/hardhat plugin #15

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

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3b9aa16
WIP: experimental Chainweb hardhat network support
larskuhtz Feb 7, 2025
9fec3e9
add hash as checksum to SPV proof mocks
larskuhtz Feb 11, 2025
11234e8
Await spv proofs to become available on target
larskuhtz Feb 11, 2025
f6ba66a
WIP: experimental Chainweb hardhat network support
larskuhtz Feb 7, 2025
0b22dca
add hash as checksum to SPV proof mocks
larskuhtz Feb 11, 2025
42f1e1a
Await spv proofs to become available on target
larskuhtz Feb 11, 2025
7e967d8
feat(hardhat-plugin): added plugin
javadkh2 Feb 11, 2025
ac0eccd
chore(plugin): structure folders
javadkh2 Feb 12, 2025
66a9f0a
Merge branch 'lars/hardhat-network' into feat/jvd/hardhat-plugin
javadkh2 Feb 12, 2025
6cfaedb
feat(plugin): EIP
javadkh2 Feb 12, 2025
b256ae2
fix: utils
javadkh2 Feb 12, 2025
dc25c50
fix(plugin): export all required functions
javadkh2 Feb 12, 2025
15930c5
Merge branch 'feat/jvd/plugin-refactored' into feat/jvd/hardhat-plugin
javadkh2 Feb 12, 2025
2258329
feat(plugin): start node inside plugin
javadkh2 Feb 12, 2025
9ab7e87
fix(plugin): add defaultNetwork
javadkh2 Feb 12, 2025
8d2d15f
feat(plugin): add default network
javadkh2 Feb 13, 2025
4547432
feat(plugin): use hardhat in-process client for chains
javadkh2 Feb 13, 2025
3012463
feat(plugin): add logging none | info | debug
javadkh2 Feb 13, 2025
cb55a32
feat(plugin): add logging none | info | debug
javadkh2 Feb 13, 2025
0c7c724
chore(plugin): clean up
javadkh2 Feb 13, 2025
d11bd97
chore(plugin): types
javadkh2 Feb 14, 2025
48e5278
plugin: add ts example
javadkh2 Feb 14, 2025
da760c2
feat(plugin): add supports for 3,10,20 chians + readme
javadkh2 Feb 15, 2025
52401ae
chore: lock file
javadkh2 Feb 15, 2025
8686f57
doc(plugin)
javadkh2 Feb 15, 2025
a946fa9
doc(plugin)
javadkh2 Feb 15, 2025
9541f29
doc(plugin)
javadkh2 Feb 15, 2025
db79027
Update README.md
Randynamic Feb 17, 2025
8317129
Update pnpm-workspace.yaml packages
javadkh2 Feb 17, 2025
2e9a7ab
Update skipped integration tests to match main
hswopeams Feb 18, 2025
02d023b
Add comments about switchChain and switchNetwork
hswopeams Feb 18, 2025
feab296
Clean up hardhat config file and add deploy.js script to make sure it…
hswopeams Feb 18, 2025
9280036
doc(plugin): update readme
javadkh2 Feb 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions hardhat-kadena-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "hardhat-kadena-plugin",
"private": true,
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"packageManager": "[email protected]",
"engines": {
"node": ">=18 <=23",
"npm": ">=8"
}
}
2 changes: 2 additions & 0 deletions hardhat-kadena-plugin/packages/hardhat-kadena/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
node_modules
149 changes: 149 additions & 0 deletions hardhat-kadena-plugin/packages/hardhat-kadena/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Hardhat Kadena Plugin

`hardhat_kadena` is a Hardhat plugin that allows developers to create a Chainweb network, switch between chains, and request SPV proofs.

## Installation

To install the plugin, run the following command: **NOT PUBLISHED YET**

```sh
npm install hardhat_kadena
```

## Usage

To use the plugin in your Hardhat project, import it in your Hardhat configuration file (`hardhat.config.ts` or `hardhat.config.js`):

```ts
import "hardhat_kadena";
```

Then, configure the plugin in the `hardhat.config.ts` file:

```ts
module.exports = {
...,
chainweb: {
chains: 3, // Number of chains in the Chainweb network
},
};
```

## Configuration

The plugin uses the following configuration options:

| Property | Type | Description |
|--------------|--------------------------------|-------------|
| `networkStem` | `string` (optional) | Specifies the network stem for Chainweb (default: `kadena_devnet_`). |
| `accounts` | `HardhatNetworkAccountsConfig` (optional) | Defines the accounts configuration for the network (default: Hardhat network accounts). |
| `chains` | `number` (optional) | Specifies the number of chains in the Chainweb network (default: `2`). |
| `graph` | `{ [key: number]: number[] }` (optional) | Defines the graph structure of the Chainweb network where keys represent chain IDs and values are arrays of connected chain IDs (default: Pearson graph). |
| `logging` | `"none" \| "info" \| "debug"` (optional) | Sets the logging level for debugging
purposes (default: `"info"`). |

## Graph
If you don’t provide a graph, the plugin automatically generates one for the chains using its built-in algorithm. Currently, it supports only 2, 3, 10, or 20 chains. If you need a different number of chains, you must explicitly pass the graph property

### Example Configuration

```ts
module.exports = {
solidity: "0.8.20",
chainweb: {
chains: 2,
},
};
```

## Networks

The plugin uses the Chainweb configuration and extends the Hardhat config by adding networks to it. All networks inherit the built-in Hardhat network config by default, except:
- `chainId`: Replaced by `676000 + chainIndex` (e.g., `676000, 676001, 676002, ...`).
- `chainwebChainId`: The chain index.
- `loggingEnabled`: `"true"` if the `logging` option is set to `"debug"` in the Chainweb config; otherwise, `"false"`.

### Override Network Configurations

If you want to override any option, you can add the network with the custom config in the `networks` section:

```ts
module.exports = {
solidity: "0.8.20",
networks: {
kadena_devnet_0: {
chainId: 123, // Use custom chainId for chain 0
},
},
chainweb: {
chains: 2,
graph: {
0: [1],
1: [0],
},
},
};
```

## Plugin API

The plugin adds a `chainweb` property to the Hardhat Runtime Environment (HRE):

```ts
export interface ChainwebPluginApi {
getProvider: (cid: number) => HardhatEthersProvider;
requestSpvProof: (targetChain: number, origin: Origin) => Promise<string>;
switchChain: (cid: number) => Promise<void>;
getChainIds: () => number[];
callChainIdContract: () => Promise<number>;
deployContractOnChains: DeployContractOnChains;
createTamperedProof: (targetChain: number, origin: Origin) => Promise<string>;
computeOriginHash: (origin: Origin) => string;
deployMocks: () => ReturnType<DeployContractOnChains>;
network: ChainwebNetwork;
}
```

| Function | Parameters | Return Type | Description |
|----------|------------|-------------|-------------|
| `getProvider` | `cid: number` | `HardhatEthersProvider` | Retrieves the provider for a specified chain. |
| `requestSpvProof` | `targetChain: number, origin: Origin` | `Promise<string>` | Requests an SPV proof for a cross-chain transaction. |
| `switchChain` | `cid: number` | `Promise<void>` | Switches the active chain. |
| `getChainIds` | None | `number[]` | Returns an array of available chain IDs. |
| `callChainIdContract` | None | `Promise<number>` | Calls a contract to get the chain ID. |
| `deployContractOnChains` | Varies | `DeployContractOnChains` | Deploys a contract on multiple chains. |
| `createTamperedProof` | `targetChain: number, origin: Origin` | `Promise<string>` | Creates a tampered SPV proof for testing purposes. |
| `computeOriginHash` | `origin: Origin` | `string` | Computes the hash of a transaction origin. |
| `deployMocks` | None | `ReturnType<DeployContractOnChains>` | Deploys mock contracts for testing. |
| `network` | None | `ChainwebNetwork` | Provides access to the Chainweb network object. |

### Example
```TS
import { chainweb } from "hardhat"

await chainweb.deployContractOnChains("SimpleToken") // deploy contract on all chains
await chainweb.switchChain(0); // configure hardhat to use chain 0
```

### Overloading `hardhat-switch-network`

This plugin overrides `switchNetwork` from `hardhat-switch-network` to load the correct Chainweb provider while also supporting switching by chain index. For example, `switchNetwork(1)` switches to chain 1 of Chainweb.

## Features

- Create a Chainweb network with configurable chain count and graph structure.
- Spin up and Switch between chains seamlessly.
- Request SPV proofs for cross-chain transactions.
- Configure logging levels for better debugging.
- Uses the Hardhat in-process network internally and creates multiple instances of it.

## Future Works

- Support external Chainweb configuration.
- Support multiple Chianweb
- Expose PRC Server

## License

This project is licensed under the MIT License.

15 changes: 15 additions & 0 deletions hardhat-kadena-plugin/packages/hardhat-kadena/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const globals = require("globals");
const pluginJs = require("@eslint/js");
const tseslint = require("typescript-eslint");

const checkFiles = (files) => (config) => config.map((c) => ({ ...c, files }));

/** @type {import('eslint').Linter.Config[]} */
const config = [
pluginJs.configs.recommended,
{ languageOptions: { globals: globals.browser } },
// somehow this plugin want to scan all files, but we only want to scan the files in the files array
...tseslint.configs.recommended,
];

module.exports = checkFiles(["src/**/*.{js,mjs,cjs,ts}"])(config);
63 changes: 63 additions & 0 deletions hardhat-kadena-plugin/packages/hardhat-kadena/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "hardhat-kadena",
"version": "1.0.0",
"description": "Hardhat plugin for Kadena's Chainweb network",
"author": "Lars Kuhtz <[email protected]>",
"contributors": [
"Lars Kuhtz <[email protected]>",
"Javad Khalilian <[email protected]>"
],
"main": "./lib/cjs/index.js",
"types": "./lib/cjs/index.d.ts",
"scripts": {
"build": "eslint && tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kadena-io/hardhat-kadena.git"
},
"keywords": [
"hardhat",
"kadena",
"chainweb",
"ethereum",
"hardhat-plugin"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/kadena-io/hardhat-kadena/issues"
},
"homepage": "https://github.com/kadena-io/hardhat-kadena#readme",
"dependencies": {
"@types/node": "^22.13.1",
"async-lock": "^1.4.1",
"hardhat-switch-network": "^1.2.0"
},
"peerDependencies": {
"@nomicfoundation/hardhat-ethers": "^3.0.8",
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"ethers": "^6.1.0",
"hardhat": "^2.22.18",
"mocha": "^11.1.0",
"chai": "^4.2.0",
"web3": "^4.16.0"
},
"devDependencies": {
"@nomicfoundation/hardhat-ethers": "^3.0.8",
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"ethers": "^6.1.0",
"hardhat": "^2.22.18",
"mocha": "^11.1.0",
"chai": "^4.2.0",
"web3": "^4.16.0",
"@eslint/js": "^9.20.0",
"@types/async-lock": "^1.4.2",
"@types/mocha": "^10.0.10",
"eslint": "^9.20.0",
"globals": "^15.14.0",
"prettier": "^3.5.0",
"typescript": "^5.7.3",
"typescript-eslint": "^8.24.0"
}
}
6 changes: 6 additions & 0 deletions hardhat-kadena-plugin/packages/hardhat-kadena/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "@nomicfoundation/hardhat-ethers";
import "hardhat-switch-network";
import "./type";
import "./plugin";
export { Origin } from "./utils/chainweb";
export { DeployedContractsOnChains } from "./utils";
Loading