Skip to content
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
7 changes: 0 additions & 7 deletions clijs/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,8 @@ abstract class BaseCommand extends Command {
}

protected async finally(err: Error | undefined): Promise<unknown> {
this.closeConnection();
return super.finally(err);
}

protected closeConnection() {
this.rpcClient?.closeConnection();
this.cometaClient?.closeConnection();
this.faucetClient?.closeConnection();
}
}

export { BaseCommand };
2 changes: 1 addition & 1 deletion explorer_frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@loadable/component": "^5.15.3",
"@nilfoundation/niljs": "workspace:*",
"@nilfoundation/smart-contracts": "workspace:*",
"@nilfoundation/ui-kit": "^2.5.28",
"@nilfoundation/ui-kit": "2.5.28",
"@noble/curves": "^1.4.0",
"@replit/codemirror-lang-solidity": "^6.0.2",
"@trpc/client": "10.38.5",
Expand Down
3 changes: 1 addition & 2 deletions niljs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@
"@nilfoundation/smart-contracts": "workspace:*",
"@noble/curves": "^1.4.0",
"@noble/hashes": "1.7.2",
"@open-rpc/client-js": "^1.8.1",
"@rollup/plugin-json": "^6.1.0",
"@scure/bip39": "^1.3.0",
"@types/isomorphic-fetch": "^0.0.39",
"abitype": "^1.0.2",
"events": "^3.3.0",
"isomorphic-fetch": "^3.0.0",
"json-rpc-2.0": "1.7.0",
"tiny-invariant": "^1.3.3",
"ts-essentials": "^10.0.2",
"viem": "^2.16.3",
Expand Down
31 changes: 3 additions & 28 deletions niljs/src/clients/BaseClient.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import type { RequestArguments } from "@open-rpc/client-js/build/ClientInterface.js";
import type { ITransport } from "../transport/types/ITransport.js";
import { assertIsValidShardId } from "../utils/assert.js";
import { RPCClient } from "./RPCCleint.js";
import type { IClientBaseConfig } from "./types/Configs.js";

/**
* BaseClient is the base class for any client tasked with interacting with =nil;
* @class BaseClient
* @typedef {BaseClient}
*/
class BaseClient {
/**
* The ITransport to be used in the client. See {@link ITransport}.
*
* @readonly
* @type {ITransport}
*/
readonly transport: ITransport;

class BaseClient extends RPCClient {
/**
* The ID of the shard with which the client needs to interact.
* The shard with this ID will be used in every call made by the client.
Expand All @@ -31,26 +22,10 @@ class BaseClient {
* @param {IClientBaseConfig} config The config to be used in the client. It contains the transport and the shard ID. See {@link IClientBaseConfig}.
*/
constructor(config: IClientBaseConfig) {
this.transport = config.transport;
super(config);
this.shardId = config.shardId;
}

/**
* Closes the connection to the network.
*/
public closeConnection() {
this.transport.closeConnection();
}

/**
* Sends a request.
* @param requestObject The request object. It contains the request method and parameters.
* @returns The response.
*/
protected async request<T>(requestObject: RequestArguments): Promise<T> {
return this.transport.request(requestObject);
}

/**
* Returns the shard ID.
* @returns The shard ID.
Expand Down
51 changes: 51 additions & 0 deletions niljs/src/clients/RPCCleint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { JSONRPCClient, type JSONRPCRequest } from "json-rpc-2.0";
import type { ITransport } from "../transport/types/ITransport.js";
import type { IRPCClientConfig } from "./types/Configs.js";
import type { JSONRPCRequestArguments } from "./types/RPC.js";

/**
* RPCClient is used for creating RPC requests.
* @class RPCClient
*/
class RPCClient {
/**
* The JSON-RPC client to be used in the client. See {@link JSONRPCClient}.
* It is used as an entity that creates requests and parses responses.
* Request logic can be implemented in the transport layer.
*
* @private
* @type {JSONRPCClient}
*/
private requester: JSONRPCClient;

/**
* The ITransport to be used in the client. See {@link ITransport}.
*
* @readonly
* @type {ITransport}
*/
readonly transport: ITransport;

/**
* Creates an instance of RPCClient.
* @constructor
* @param {IRPCClientConfig} config The config to be used in the client. It contains the transport and the shard ID. See {@link IClientBaseConfig}.
*/
constructor(config: IRPCClientConfig) {
this.transport = config.transport;
this.requester = new JSONRPCClient((req: JSONRPCRequest) =>
this.transport.request(req, this.requester),
);
}

/**
* Sends a request.
* @param requestObject The request object. It contains the request method and parameters.
* @returns The response.
*/
protected async request<T>(requestObject: JSONRPCRequestArguments): Promise<T> {
return await this.requester.request(requestObject.method, requestObject.params);
}
}

export { RPCClient };
22 changes: 13 additions & 9 deletions niljs/src/clients/types/Configs.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import type { ISigner } from "../../signers/types/ISigner.js";
import type { ITransport } from "../../transport/types/ITransport.js";

/**
* The client configuration that is shared between public and private clients.
*/
type IClientBaseConfig = {
/**
* The ID of the shard with which the client interacts.
* @example 1
*/
shardId?: number;
type IRPCClientConfig = {
/**
* The transport is used to send requests to the network.
* @example
Expand All @@ -22,6 +14,17 @@ type IClientBaseConfig = {
transport: ITransport;
};

/**
* The client configuration that is shared between public and private clients.
*/
type IClientBaseConfig = {
/**
* The ID of the shard with which the client interacts.
* @example 1
*/
shardId?: number;
} & IRPCClientConfig;

/**
* The type representing the config for the public client.
*/
Expand Down Expand Up @@ -63,4 +66,5 @@ export type {
ISmartAccountClientConfig,
FaucetClientConfig,
CometaClientConfig,
IRPCClientConfig,
};
15 changes: 15 additions & 0 deletions niljs/src/clients/types/RPC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { JSONRPCID } from "json-rpc-2.0";

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
type JSONRPCParams = Array<any> | object;

/**
* Represents a JSON-RPC request arguments.
*/
type JSONRPCRequestArguments = {
method: string;
params?: JSONRPCParams;
id?: JSONRPCID;
};

export type { JSONRPCRequestArguments };
28 changes: 0 additions & 28 deletions niljs/src/rpc/rpcClient.test.ts

This file was deleted.

39 changes: 0 additions & 39 deletions niljs/src/rpc/rpcClient.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ test("Smart account self deploy test", async ({ expect }) => {

test("Deploy through smart account", async ({ expect }) => {
const fn = vi.fn();
fn.mockReturnValue({});

const client = new PublicClient({
transport: new MockTransport(fn),
Expand Down
Loading