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 +} +```