From 6c0a82834425caf59d237b611cf4810a08a94c1f Mon Sep 17 00:00:00 2001 From: sebastijankuzner Date: Mon, 2 Dec 2024 12:15:33 +0100 Subject: [PATCH 1/4] Add net_version --- packages/api-evm/source/actions/net-version.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 packages/api-evm/source/actions/net-version.ts diff --git a/packages/api-evm/source/actions/net-version.ts b/packages/api-evm/source/actions/net-version.ts new file mode 100644 index 000000000..93447c02c --- /dev/null +++ b/packages/api-evm/source/actions/net-version.ts @@ -0,0 +1,17 @@ +import { injectable } from "@mainsail/container"; +import { Contracts } from "@mainsail/contracts"; + +@injectable() +export class NetVersion implements Contracts.Api.RPC.Action { + public readonly name: string = "net_version"; + + public readonly schema = { + $id: `jsonRpc_${this.name}`, + maxItems: 0, + type: "array", + }; + + public async handle(parameters: []): Promise { + return ""; + } +} From 0f02c76e26704f4bc3cc774c41cba74fe4df1730 Mon Sep 17 00:00:00 2001 From: sebastijankuzner Date: Mon, 2 Dec 2024 13:59:35 +0100 Subject: [PATCH 2/4] Register handler --- packages/api-evm/source/actions/index.ts | 1 + packages/api-evm/source/service-provider.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/packages/api-evm/source/actions/index.ts b/packages/api-evm/source/actions/index.ts index 4f0690810..a86021d69 100644 --- a/packages/api-evm/source/actions/index.ts +++ b/packages/api-evm/source/actions/index.ts @@ -12,5 +12,6 @@ export * from "./eth-get-uncle-count-by-block-hash.js"; export * from "./eth-get-uncle-count-by-block-number.js"; export * from "./net-listening.js"; export * from "./net-peer-count.js"; +export * from "./net-version.js"; export * from "./web3-client-version.js"; export * from "./web3-sha3.js"; diff --git a/packages/api-evm/source/service-provider.ts b/packages/api-evm/source/service-provider.ts index cfea7f2fa..80b82a218 100644 --- a/packages/api-evm/source/service-provider.ts +++ b/packages/api-evm/source/service-provider.ts @@ -17,6 +17,7 @@ import { EthGetUncleCountByBlockNumber, NetListeningAction, NetPeerCountAction, + NetVersion, Web3ClientVersionAction, Web3Sha3, } from "./actions/index.js"; @@ -83,6 +84,7 @@ export class ServiceProvider extends AbstractServiceProvider { this.app.resolve(EthGetUncleCountByBlockNumber), this.app.resolve(NetListeningAction), this.app.resolve(NetPeerCountAction), + this.app.resolve(NetVersion), this.app.resolve(Web3ClientVersionAction), this.app.resolve(Web3Sha3), ]; From f874e8771349621407a106659e59f3d1ccd76def Mon Sep 17 00:00:00 2001 From: sebastijankuzner Date: Mon, 2 Dec 2024 14:01:36 +0100 Subject: [PATCH 3/4] Return nethash --- packages/api-evm/source/actions/net-version.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/api-evm/source/actions/net-version.ts b/packages/api-evm/source/actions/net-version.ts index 93447c02c..9f90f168c 100644 --- a/packages/api-evm/source/actions/net-version.ts +++ b/packages/api-evm/source/actions/net-version.ts @@ -1,8 +1,11 @@ -import { injectable } from "@mainsail/container"; -import { Contracts } from "@mainsail/contracts"; +import { inject, injectable } from "@mainsail/container"; +import { Contracts, Identifiers } from "@mainsail/contracts"; @injectable() export class NetVersion implements Contracts.Api.RPC.Action { + @inject(Identifiers.Cryptography.Configuration) + private readonly configuration!: Contracts.Crypto.Configuration; + public readonly name: string = "net_version"; public readonly schema = { @@ -12,6 +15,6 @@ export class NetVersion implements Contracts.Api.RPC.Action { }; public async handle(parameters: []): Promise { - return ""; + return this.configuration.get("network.nethash"); } } From 74337d1e8823037b8c5e22ea59aeb82a4250dbea Mon Sep 17 00:00:00 2001 From: sebastijankuzner Date: Mon, 2 Dec 2024 14:04:54 +0100 Subject: [PATCH 4/4] Add test --- .../source/actions/net-version.test.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 packages/api-evm/source/actions/net-version.test.ts diff --git a/packages/api-evm/source/actions/net-version.test.ts b/packages/api-evm/source/actions/net-version.test.ts new file mode 100644 index 000000000..0628565c0 --- /dev/null +++ b/packages/api-evm/source/actions/net-version.test.ts @@ -0,0 +1,46 @@ +import { Identifiers } from "@mainsail/contracts"; +import { Validator } from "@mainsail/validation"; + +import { describe, Sandbox } from "../../../test-framework/source"; +import { NetVersion } from "./index.js"; + +describe<{ + sandbox: Sandbox; + action: NetVersion; + validator: Validator; +}>("NetVersion", ({ beforeEach, it, assert }) => { + const version = "0.0.1"; + + beforeEach(async (context) => { + context.sandbox = new Sandbox(); + + context.sandbox.app.bind(Identifiers.Cryptography.Configuration).toConstantValue({ + get: () => "nethash", + }); + + context.action = context.sandbox.app.resolve(NetVersion); + context.validator = context.sandbox.app.resolve(Validator); + }); + + it("should have a name", ({ action }) => { + assert.equal(action.name, "net_version"); + }); + + it("schema should be array with 0 parameters", ({ action, validator }) => { + assert.equal(action.schema, { + $id: "jsonRpc_net_version", + maxItems: 0, + type: "array", + }); + + validator.addSchema(action.schema); + + assert.undefined(validator.validate("jsonRpc_net_version", []).errors); + assert.defined(validator.validate("jsonRpc_net_version", [1]).errors); + assert.defined(validator.validate("jsonRpc_net_version", {}).errors); + }); + + it("should return the web3 client version", async ({ action }) => { + assert.equal(await action.handle([]), `nethash`); + }); +});