From 79cd0dbb68945eb8835921c881bf62198859d70b Mon Sep 17 00:00:00 2001 From: iosh Date: Wed, 1 Jan 2025 19:44:28 +0800 Subject: [PATCH] chore: first version hardhat-node --- .changeset/warm-tables-relax.md | 5 ++++ packages/hardhat-node/package.json | 4 +-- packages/hardhat-node/src/errors.ts | 1 - packages/hardhat-node/src/tasks.ts | 25 +++++++---------- packages/hardhat-node/src/type-extensions.ts | 4 +-- .../default-config-project/hardhat.config.cjs | 5 ++++ packages/hardhat-node/test/taskNode.test.ts | 27 ++++++++++--------- 7 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 .changeset/warm-tables-relax.md delete mode 100644 packages/hardhat-node/src/errors.ts diff --git a/.changeset/warm-tables-relax.md b/.changeset/warm-tables-relax.md new file mode 100644 index 0000000..2edcb7f --- /dev/null +++ b/.changeset/warm-tables-relax.md @@ -0,0 +1,5 @@ +--- +"@civex/hardhat-node": patch +--- + +Added conflux node task to hardhat diff --git a/packages/hardhat-node/package.json b/packages/hardhat-node/package.json index 75a927a..3dd28f9 100644 --- a/packages/hardhat-node/package.json +++ b/packages/hardhat-node/package.json @@ -1,7 +1,6 @@ { "name": "@civex/hardhat-node", - "version": "0.0.1", - "private": true, + "version": "0.0.0", "main": "./dist/index.js", "types": "./dist/index.d.ts", "typings": "./dist/index.d.ts", @@ -10,6 +9,7 @@ "build": "tsc --project ./tsconfig.build.json", "test": "vitest --config ./vitest.config.ts dev" }, + "files": ["dist"], "keywords": [], "author": "", "license": "MIT", diff --git a/packages/hardhat-node/src/errors.ts b/packages/hardhat-node/src/errors.ts deleted file mode 100644 index 965a576..0000000 --- a/packages/hardhat-node/src/errors.ts +++ /dev/null @@ -1 +0,0 @@ -import { NomicLabsHardhatPluginError } from 'hardhat/plugins' diff --git a/packages/hardhat-node/src/tasks.ts b/packages/hardhat-node/src/tasks.ts index de1ca85..aa1078d 100644 --- a/packages/hardhat-node/src/tasks.ts +++ b/packages/hardhat-node/src/tasks.ts @@ -1,34 +1,29 @@ import { type Config, createServer } from '@xcfx/node' import { subtask, task, types } from 'hardhat/config' -import { HardhatPluginError } from 'hardhat/plugins' import { TASK_CONFLUX_NODE, TASK_CONFLUX_NODE_SERVER_READY } from './constants' subtask(TASK_CONFLUX_NODE_SERVER_READY) .addParam('nodeConfig', undefined, undefined, types.any) - .addParam('server', undefined, undefined, types.any) - .setAction(async ({ config }: { server: any; config: any }) => { - console.info(`Started HTTP server on port ${config.jsonrpcLocalHttpPort}`) + .addParam('confluxServer', undefined, undefined, types.any) + .setAction(async ({ nodeConfig }: { nodeConfig: any }) => { + console.info( + `Started HTTP server on port ${nodeConfig.jsonrpcLocalHttpPort}`, + ) }) task(TASK_CONFLUX_NODE, 'Starts a local conflux node').setAction( - async (_, { network, run }) => { - if (network.name !== 'conflux') { - throw new HardhatPluginError( - '@civex/hardhat-node', - 'network must be conflux', - ) - } + async (_, { config, run }) => { const nodeConfig: Config = { jsonrpcLocalHttpPort: 12539, - ...network.config, + ...config.conflux, } - const server = await createServer(nodeConfig) + const confluxServer = await createServer(nodeConfig) - await server.start() + await confluxServer.start() await run(TASK_CONFLUX_NODE_SERVER_READY, { nodeConfig: nodeConfig, - server, + confluxServer, }) }, ) diff --git a/packages/hardhat-node/src/type-extensions.ts b/packages/hardhat-node/src/type-extensions.ts index 397a97c..afce22c 100644 --- a/packages/hardhat-node/src/type-extensions.ts +++ b/packages/hardhat-node/src/type-extensions.ts @@ -3,11 +3,11 @@ import 'hardhat/types/runtime' import type { Config } from '@xcfx/node' declare module 'hardhat/types/config' { - interface NetworksConfig { + interface HardhatUserConfig { conflux?: Config } - interface NetworksUserConfig { + interface HardhatConfig { conflux?: Config } } diff --git a/packages/hardhat-node/test/default-config-project/hardhat.config.cjs b/packages/hardhat-node/test/default-config-project/hardhat.config.cjs index e5dd06a..da069fc 100644 --- a/packages/hardhat-node/test/default-config-project/hardhat.config.cjs +++ b/packages/hardhat-node/test/default-config-project/hardhat.config.cjs @@ -6,9 +6,14 @@ const config = { solidity: '0.8.19', networks: { conflux: { + url: 'http://127.0.0.1:12539', chainId: 1111, }, }, + conflux: { + chainId: 1111, + jsonrpcLocalHttpPort: 12539, + }, } module.exports = config diff --git a/packages/hardhat-node/test/taskNode.test.ts b/packages/hardhat-node/test/taskNode.test.ts index ba5e8ce..274dc2c 100644 --- a/packages/hardhat-node/test/taskNode.test.ts +++ b/packages/hardhat-node/test/taskNode.test.ts @@ -13,19 +13,22 @@ describe('default', () => { test('default', async () => { const hre = await import('hardhat') - hre.tasks[TASK_CONFLUX_NODE_SERVER_READY].setAction(async () => { - const result = await fetch( - `http://127.0.0.1:${config.jsonrpcLocalHttpPort}`, - { - headers: { - 'content-type': 'application/json', + hre.tasks[TASK_CONFLUX_NODE_SERVER_READY].setAction( + async ({ nodeConfig, confluxServer }) => { + const result = await fetch( + `http://127.0.0.1:${nodeConfig.jsonrpcLocalHttpPort}`, + { + headers: { + 'content-type': 'application/json', + }, + body: '{"id":1,"jsonrpc":"2.0","params":["earliest"],"method":"cfx_epochNumber"}', + method: 'POST', }, - body: '{"id":1,"jsonrpc":"2.0","params":["earliest"],"method":"cfx_epochNumber"}', - method: 'POST', - }, - ).then((res) => res.json() as Promise<{ result: string }>) - expect(result.result).toMatchInlineSnapshot() - }) + ).then((res) => res.json() as Promise<{ result: string }>) + expect(result.result).toMatchInlineSnapshot(`"0x0"`) + await confluxServer.stop() + }, + ) await hre.run(TASK_CONFLUX_NODE) })