Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(evm-api): implement net_version #792

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading