Skip to content

Commit

Permalink
chore: first version hardhat-node
Browse files Browse the repository at this point in the history
  • Loading branch information
iosh committed Jan 1, 2025
1 parent 255a08b commit 79cd0db
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-tables-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@civex/hardhat-node": patch
---

Added conflux node task to hardhat
4 changes: 2 additions & 2 deletions packages/hardhat-node/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -10,6 +9,7 @@
"build": "tsc --project ./tsconfig.build.json",
"test": "vitest --config ./vitest.config.ts dev"
},
"files": ["dist"],
"keywords": [],
"author": "",
"license": "MIT",
Expand Down
1 change: 0 additions & 1 deletion packages/hardhat-node/src/errors.ts

This file was deleted.

25 changes: 10 additions & 15 deletions packages/hardhat-node/src/tasks.ts
Original file line number Diff line number Diff line change
@@ -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,
})
},
)
4 changes: 2 additions & 2 deletions packages/hardhat-node/src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 15 additions & 12 deletions packages/hardhat-node/test/taskNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit 79cd0db

Please sign in to comment.