From 116bcdb6feb5bcc122700819dd4c4e275914be5a Mon Sep 17 00:00:00 2001 From: Aik Chun Date: Thu, 3 Jun 2021 10:30:43 +0800 Subject: [PATCH] added getRpcInfo rpc (#330) --- .../category/server/getRpcInfo.test.ts | 24 +++++++++++++ .../jellyfish-api-core/src/category/server.ts | 28 +++++++++++++++ packages/jellyfish-api-core/src/index.ts | 3 ++ website/docs/jellyfish/api/server.md | 36 +++++++++++++++++++ website/sidebars.js | 3 +- 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 packages/jellyfish-api-core/__tests__/category/server/getRpcInfo.test.ts create mode 100644 packages/jellyfish-api-core/src/category/server.ts create mode 100644 website/docs/jellyfish/api/server.md diff --git a/packages/jellyfish-api-core/__tests__/category/server/getRpcInfo.test.ts b/packages/jellyfish-api-core/__tests__/category/server/getRpcInfo.test.ts new file mode 100644 index 0000000000..2cf97c2477 --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/server/getRpcInfo.test.ts @@ -0,0 +1,24 @@ +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' + +describe('Server on masternode', () => { + const container = new MasterNodeRegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.waitForReady() + }) + + afterAll(async () => { + await container.stop() + }) + + it('should getRpcInfo', async () => { + const info = await client.server.getRpcInfo() + expect(info.active_commands[0].method).toStrictEqual('getrpcinfo') + expect(info.active_commands[0].duration).toBeGreaterThanOrEqual(0) + expect(typeof info.logpath).toBe('string') + expect(info.logpath).toContain('regtest/debug.log') + }) +}) diff --git a/packages/jellyfish-api-core/src/category/server.ts b/packages/jellyfish-api-core/src/category/server.ts new file mode 100644 index 0000000000..389a98de32 --- /dev/null +++ b/packages/jellyfish-api-core/src/category/server.ts @@ -0,0 +1,28 @@ +import { ApiClient } from '../.' + +export class Server { + private readonly client: ApiClient + + constructor (client: ApiClient) { + this.client = client + } + + /** + * Returns details of the RPC server + * + * @return {Promise} + */ + async getRpcInfo (): Promise { + return await this.client.call('getrpcinfo', [], 'number') + } +} + +export interface ActiveCommand { + method: string + duration: number +} + +export interface RpcInfo { + active_commands: ActiveCommand[] + logpath: string +} diff --git a/packages/jellyfish-api-core/src/index.ts b/packages/jellyfish-api-core/src/index.ts index 6466339ef6..a250e83a35 100644 --- a/packages/jellyfish-api-core/src/index.ts +++ b/packages/jellyfish-api-core/src/index.ts @@ -7,6 +7,7 @@ import { Wallet } from './category/wallet' import { Account } from './category/account' import { PoolPair } from './category/poolpair' import { Token } from './category/token' +import { Server } from './category/server' export * from '@defichain/jellyfish-json' @@ -18,6 +19,7 @@ export * as wallet from './category/wallet' export * as poolpair from './category/poolpair' export * as token from './category/token' export * as account from './category/account' +export * as server from './category/server' /** * A protocol agnostic DeFiChain node client, RPC calls are separated into their category. @@ -31,6 +33,7 @@ export abstract class ApiClient { public readonly account = new Account(this) public readonly poolpair = new PoolPair(this) public readonly token = new Token(this) + public readonly server = new Server(this) /** * A promise based procedure call handling diff --git a/website/docs/jellyfish/api/server.md b/website/docs/jellyfish/api/server.md new file mode 100644 index 0000000000..7acf075306 --- /dev/null +++ b/website/docs/jellyfish/api/server.md @@ -0,0 +1,36 @@ +--- +id: server +title: Server API +sidebar_label: Server API +slug: /jellyfish/api/server +--- + +```js +import {Client} from '@defichain/jellyfish' +const client = new Client() + +// Using client.server. +const something = await client.server.method() +``` + +## getRpcInfo + +Returns details of the RPC server. +- `active_commands` a list of active commands +- `logpath` the complete path to the debug log + +```ts title="client.server.getRpcInfo()" +interface server { + getRpcInfo (): Promise +} + +interface RpcInfo { + active_commands: ActiveCommand[] + logpath: string +} + +interface ActiveCommand { + method: string + duration: number +} +``` diff --git a/website/sidebars.js b/website/sidebars.js index 04cdc6a6da..d8f9b543f9 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -15,7 +15,8 @@ module.exports = { 'jellyfish/api/wallet', 'jellyfish/api/poolpair', 'jellyfish/api/token', - 'jellyfish/api/account' + 'jellyfish/api/account', + 'jellyfish/api/server' ] } ],