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/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`); + }); +}); 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..9f90f168c --- /dev/null +++ b/packages/api-evm/source/actions/net-version.ts @@ -0,0 +1,20 @@ +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 = { + $id: `jsonRpc_${this.name}`, + maxItems: 0, + type: "array", + }; + + public async handle(parameters: []): Promise { + return this.configuration.get("network.nethash"); + } +} 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), ];