Skip to content

Commit

Permalink
feat(evm-api): implement net_version (#792)
Browse files Browse the repository at this point in the history
* Add net_version

* Register handler

* Return nethash

* Add test
  • Loading branch information
sebastijankuzner authored Dec 2, 2024
1 parent 28ce419 commit 7a9b32b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/api-evm/source/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
46 changes: 46 additions & 0 deletions packages/api-evm/source/actions/net-version.test.ts
Original file line number Diff line number Diff line change
@@ -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`);
});
});
20 changes: 20 additions & 0 deletions packages/api-evm/source/actions/net-version.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
return this.configuration.get<string>("network.nethash");
}
}
2 changes: 2 additions & 0 deletions packages/api-evm/source/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
EthGetUncleCountByBlockNumber,
NetListeningAction,
NetPeerCountAction,
NetVersion,
Web3ClientVersionAction,
Web3Sha3,
} from "./actions/index.js";
Expand Down Expand Up @@ -83,6 +84,7 @@ export class ServiceProvider extends AbstractServiceProvider<Server> {
this.app.resolve(EthGetUncleCountByBlockNumber),
this.app.resolve(NetListeningAction),
this.app.resolve(NetPeerCountAction),
this.app.resolve(NetVersion),
this.app.resolve(Web3ClientVersionAction),
this.app.resolve(Web3Sha3),
];
Expand Down

0 comments on commit 7a9b32b

Please sign in to comment.