From 49bfd8b8797d4d1c000d4a80d7a4443ac0edc4ed Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Fri, 30 Apr 2021 12:58:58 +0800 Subject: [PATCH 1/3] Add getConnectionCount rpc --- .../__tests__/category/net.test.ts | 41 +++++++++++++++++++ .../jellyfish-api-core/src/category/net.ts | 21 ++++++++++ packages/jellyfish-api-core/src/index.ts | 3 ++ website/docs/jellyfish/api/net.md | 24 +++++++++++ 4 files changed, 89 insertions(+) create mode 100644 packages/jellyfish-api-core/__tests__/category/net.test.ts create mode 100644 packages/jellyfish-api-core/src/category/net.ts create mode 100644 website/docs/jellyfish/api/net.md diff --git a/packages/jellyfish-api-core/__tests__/category/net.test.ts b/packages/jellyfish-api-core/__tests__/category/net.test.ts new file mode 100644 index 0000000000..e722c642bb --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/net.test.ts @@ -0,0 +1,41 @@ +import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../container_adapter_client' + +describe('non masternode', () => { + const container = new RegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.waitForReady() + }) + + afterAll(async () => { + await container.stop() + }) + + it('should getConnectionCount', async () => { + const count = await client.net.getConnectionCount() + expect(count).toBeGreaterThanOrEqual(0) + }) +}) + +describe('masternode', () => { + const container = new MasterNodeRegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() + }) + + afterAll(async () => { + await container.stop() + }) + + it('should getConnectionCount', async () => { + const count = await client.net.getConnectionCount() + expect(count).toBeGreaterThanOrEqual(0) + }) +}) diff --git a/packages/jellyfish-api-core/src/category/net.ts b/packages/jellyfish-api-core/src/category/net.ts new file mode 100644 index 0000000000..86f399dd78 --- /dev/null +++ b/packages/jellyfish-api-core/src/category/net.ts @@ -0,0 +1,21 @@ +import { ApiClient } from '../.' + +/** + * Net RPCs for DeFi Blockchain + */ +export class Net { + private readonly client: ApiClient + + constructor (client: ApiClient) { + this.client = client + } + + /** + * Returns the number of connections to other nodes. + * + * @return {Promise} + */ + async getConnectionCount (): Promise { + return await this.client.call('getconnectioncount', [], 'number') + } +} diff --git a/packages/jellyfish-api-core/src/index.ts b/packages/jellyfish-api-core/src/index.ts index 8086b0e7ed..6cabc931a7 100644 --- a/packages/jellyfish-api-core/src/index.ts +++ b/packages/jellyfish-api-core/src/index.ts @@ -1,6 +1,7 @@ import { Precision, PrecisionPath } from '@defichain/jellyfish-json' import { Blockchain } from './category/blockchain' import { Mining } from './category/mining' +import { Net } from './category/net' import { RawTx } from './category/rawtx' import { Wallet } from './category/wallet' import { PoolPair } from './category/poolpair' @@ -10,6 +11,7 @@ export * from '@defichain/jellyfish-json' export * as blockchain from './category/blockchain' export * as mining from './category/mining' +export * as net from './category/net' export * as rawtx from './category/rawtx' export * as wallet from './category/wallet' export * as poolpair from './category/poolpair' @@ -21,6 +23,7 @@ export * as token from './category/token' export abstract class ApiClient { public readonly blockchain = new Blockchain(this) public readonly mining = new Mining(this) + public readonly net = new Net(this) public readonly rawtx = new RawTx(this) public readonly wallet = new Wallet(this) public readonly poolpair = new PoolPair(this) diff --git a/website/docs/jellyfish/api/net.md b/website/docs/jellyfish/api/net.md new file mode 100644 index 0000000000..4377cdde7b --- /dev/null +++ b/website/docs/jellyfish/api/net.md @@ -0,0 +1,24 @@ +--- +id: net +title: Net API +sidebar_label: Net API +slug: /jellyfish/api/net +--- + +```js +import {Client} from '@defichain/jellyfish' +const client = new Client() + +// Using client.net. +const something = await client.net.method() +``` + +## getConnectionCount + +Returns the number of connections to other nodes. + +```ts title="client.net.getConnectionCount()" +interface net { + getConnectionCount (): Promise +} +``` From 1acabc13867af040a7ad0b743f4c34b141d655d5 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Fri, 30 Apr 2021 14:07:47 +0800 Subject: [PATCH 2/3] Some minor fixes --- packages/jellyfish-api-core/src/category/net.ts | 2 +- website/docs/jellyfish/api/net.md | 2 +- website/sidebars.js | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/net.ts b/packages/jellyfish-api-core/src/category/net.ts index 86f399dd78..4a6fa2724d 100644 --- a/packages/jellyfish-api-core/src/category/net.ts +++ b/packages/jellyfish-api-core/src/category/net.ts @@ -15,7 +15,7 @@ export class Net { * * @return {Promise} */ - async getConnectionCount (): Promise { + async getConnectionCount (): Promise { return await this.client.call('getconnectioncount', [], 'number') } } diff --git a/website/docs/jellyfish/api/net.md b/website/docs/jellyfish/api/net.md index 4377cdde7b..7b390e588f 100644 --- a/website/docs/jellyfish/api/net.md +++ b/website/docs/jellyfish/api/net.md @@ -19,6 +19,6 @@ Returns the number of connections to other nodes. ```ts title="client.net.getConnectionCount()" interface net { - getConnectionCount (): Promise + getConnectionCount (): Promise } ``` diff --git a/website/sidebars.js b/website/sidebars.js index 2748b9b58e..2aaad84342 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -10,6 +10,7 @@ module.exports = { items: [ 'jellyfish/api/blockchain', 'jellyfish/api/mining', + 'jellyfish/api/net', 'jellyfish/api/rawtx', 'jellyfish/api/wallet', 'jellyfish/api/poolpair', From 633243146a1b7eadbbb88137b5e9d7b2c71d4e96 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Fri, 30 Apr 2021 14:09:45 +0800 Subject: [PATCH 3/3] Minor fix --- packages/jellyfish-api-core/src/category/net.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/src/category/net.ts b/packages/jellyfish-api-core/src/category/net.ts index 4a6fa2724d..1df13aa488 100644 --- a/packages/jellyfish-api-core/src/category/net.ts +++ b/packages/jellyfish-api-core/src/category/net.ts @@ -13,7 +13,7 @@ export class Net { /** * Returns the number of connections to other nodes. * - * @return {Promise} + * @return {Promise} */ async getConnectionCount (): Promise { return await this.client.call('getconnectioncount', [], 'number')