From 1af169095b7eea95750182270bf2ddce88a9dd41 Mon Sep 17 00:00:00 2001 From: Fuxing Loh Date: Mon, 29 Mar 2021 16:45:33 +0800 Subject: [PATCH 1/4] moved interface moved below for each category --- .../__tests__/category/mining.test.ts | 8 ++-- .../jellyfish-api-core/src/category/mining.ts | 37 ++++++++++--------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/mining.test.ts b/packages/jellyfish-api-core/__tests__/category/mining.test.ts index 366403b9b6..41d263afce 100644 --- a/packages/jellyfish-api-core/__tests__/category/mining.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/mining.test.ts @@ -47,13 +47,13 @@ describe('masternode', () => { }) it('should getMintingInfo', async () => { - const info = await client.mining.getMintingInfo() - await waitForExpect(async () => { - const info = await container.getMintingInfo() - expect(info.blocks).toBeGreaterThan(1) + const info = await client.mining.getMintingInfo() + await expect(info.blocks).toBeGreaterThan(1) }) + const info = await client.mining.getMintingInfo() + expect(info.blocks).toBeGreaterThan(0) expect(info.currentblockweight).toBeGreaterThan(0) diff --git a/packages/jellyfish-api-core/src/category/mining.ts b/packages/jellyfish-api-core/src/category/mining.ts index 6ba79d88aa..6119811d0c 100644 --- a/packages/jellyfish-api-core/src/category/mining.ts +++ b/packages/jellyfish-api-core/src/category/mining.ts @@ -1,22 +1,5 @@ import { ApiClient } from '../.' -export interface MintingInfo { - blocks: number - currentblockweight?: number - currentblocktx?: number - difficulty: string - isoperator: boolean - masternodeid?: string - masternodeoperator?: string - masternodestate?: 'PRE_ENABLED' | 'ENABLED' | 'PRE_RESIGNED' | 'RESIGNED' | 'PRE_BANNED' | 'BANNED' - generate?: boolean - mintedblocks?: number - networkhashps: number - pooledtx: number - chain: 'main' | 'test' | 'regtest' | string - warnings: string -} - /** * Minting related RPC calls for DeFiChain */ @@ -46,3 +29,23 @@ export class Mining { return await this.client.call('getmintinginfo', [], 'number') } } + +/** + * Minting related information + */ +export interface MintingInfo { + blocks: number + currentblockweight?: number + currentblocktx?: number + difficulty: string + isoperator: boolean + masternodeid?: string + masternodeoperator?: string + masternodestate?: 'PRE_ENABLED' | 'ENABLED' | 'PRE_RESIGNED' | 'RESIGNED' | 'PRE_BANNED' | 'BANNED' + generate?: boolean + mintedblocks?: number + networkhashps: number + pooledtx: number + chain: 'main' | 'test' | 'regtest' | string + warnings: string +} From d4be3f08c0195e196bf6be98e452538981a6b92c Mon Sep 17 00:00:00 2001 From: Fuxing Loh Date: Mon, 29 Mar 2021 22:54:30 +0800 Subject: [PATCH 2/4] added getBlockchainInfo --- .../__tests__/category/blockchain.test.ts | 84 +++++++++++++++++++ .../src/category/blockchain.ts | 45 ++++++++++ packages/jellyfish-api-core/src/index.ts | 3 + 3 files changed, 132 insertions(+) create mode 100644 packages/jellyfish-api-core/__tests__/category/blockchain.test.ts create mode 100644 packages/jellyfish-api-core/src/category/blockchain.ts diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts new file mode 100644 index 0000000000..a4060fb6f4 --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -0,0 +1,84 @@ +import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../container_adapter_client' +import waitForExpect from 'wait-for-expect' + +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 getBlockchainInfo', async () => { + const info = await client.blockchain.getBlockchainInfo() + + expect(info.chain).toBe('regtest') + expect(info.blocks).toBe(0) + expect(info.headers).toBe(0) + + expect(info.bestblockhash.length).toBe(64) + expect(info.difficulty).toBeGreaterThan(0) + expect(info.mediantime).toBeGreaterThan(1550000000) + + expect(info.verificationprogress).toBe(1) + expect(info.initialblockdownload).toBe(true) + expect(info.chainwork.length).toBe(64) + expect(info.size_on_disk).toBeGreaterThan(0) + expect(info.pruned).toBe(false) + + expect(Object.keys(info.softforks).length).toBe(10) + + expect(info.softforks.amk.type).toBe('buried') + expect(info.softforks.amk.active).toBe(true) + expect(info.softforks.amk.height).toBe(0) + + expect(info.softforks.segwit.type).toBe('buried') + expect(info.softforks.segwit.active).toBe(true) + expect(info.softforks.segwit.height).toBe(0) + }) +}) + +describe('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 getBlockchainInfo', async () => { + await waitForExpect(async () => { + const info = await client.blockchain.getBlockchainInfo() + await expect(info.blocks).toBeGreaterThan(1) + }) + + const info = await client.blockchain.getBlockchainInfo() + + expect(info.chain).toBe('regtest') + expect(info.blocks).toBeGreaterThan(0) + expect(info.headers).toBeGreaterThan(0) + + expect(info.bestblockhash.length).toBe(64) + expect(info.difficulty).toBeGreaterThan(0) + expect(info.mediantime).toBeGreaterThan(1550000000) + + expect(info.verificationprogress).toBe(1) + expect(info.initialblockdownload).toBe(false) + expect(info.chainwork.length).toBe(64) + expect(info.size_on_disk).toBeGreaterThan(0) + expect(info.pruned).toBe(false) + + expect(Object.keys(info.softforks).length).toBe(10) + }) +}) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts new file mode 100644 index 0000000000..c9fa47aecd --- /dev/null +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -0,0 +1,45 @@ +import { ApiClient } from '../.' + +/** + * Blockchain related RPC calls for DeFiChain + */ +export class Blockchain { + private readonly client: ApiClient + + constructor (client: ApiClient) { + this.client = client + } + + /** + * Returns an object containing various state info regarding blockchain processing. + * @return Promise + */ + async getBlockchainInfo (): Promise { + return await this.client.call('getblockchaininfo', [], 'number') + } +} + +/** + * TODO(fuxingloh): defid prune=1 is not type supported yet + */ +export interface BlockchainInfo { + chain: 'main' | 'test' | 'regtest' | string + blocks: number + headers: number + bestblockhash: string + difficulty: number + mediantime: number + verificationprogress: number + initialblockdownload: boolean + chainwork: string + size_on_disk: number + pruned: boolean + softforks: { + [id: string]: { + type: 'buried' | 'bip9' + active: boolean + height: number + } + } + warnings: string +} diff --git a/packages/jellyfish-api-core/src/index.ts b/packages/jellyfish-api-core/src/index.ts index 0252c0aaca..35e6fc109e 100644 --- a/packages/jellyfish-api-core/src/index.ts +++ b/packages/jellyfish-api-core/src/index.ts @@ -1,8 +1,10 @@ import { Precision } from '@defichain/jellyfish-json' +import { Blockchain } from './category/blockchain' import { Mining } from './category/mining' import { Wallet } from './category/wallet' export * from '@defichain/jellyfish-json' +export * from './category/blockchain' export * from './category/mining' export * from './category/wallet' @@ -10,6 +12,7 @@ export * from './category/wallet' * ApiClient; a protocol agnostic DeFiChain node client, RPC calls are separated into their category. */ export abstract class ApiClient { + public readonly blockchain = new Blockchain(this) public readonly mining = new Mining(this) public readonly wallet = new Wallet(this) From 0faeb0ed48d904404d2dd8e9c8a9fc42ffac8138 Mon Sep 17 00:00:00 2001 From: Fuxing Loh Date: Mon, 29 Mar 2021 23:02:13 +0800 Subject: [PATCH 3/4] added docs for getBlockchainInfo --- .../src/category/blockchain.ts | 2 +- website/docs/jellyfish/api/blockchain.md | 46 +++++++++++++++++++ website/sidebars.js | 1 + 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 website/docs/jellyfish/api/blockchain.md diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index c9fa47aecd..e59494c65f 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -11,7 +11,7 @@ export class Blockchain { } /** - * Returns an object containing various state info regarding blockchain processing. + * Get various state info regarding blockchain processing. * @return Promise */ async getBlockchainInfo (): Promise { diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md new file mode 100644 index 0000000000..4b75b4f70a --- /dev/null +++ b/website/docs/jellyfish/api/blockchain.md @@ -0,0 +1,46 @@ +--- +id: blockchain +title: Blockchain API +sidebar_label: Blockchain API +slug: /jellyfish/api/blockchain +--- + +```js +import {Client} from '@defichain/jellyfish' +const client = new Client() + +// Using client.blockchain. +const something = await client.blockchain.method() +``` + +## getBlockchainInfo + +Get various state info regarding blockchain processing. + +```ts title="client.blockchain.getBlockchainInfo()" +interface blockchain { + getBlockchainInfo (): Promise +} + +interface BlockchainInfo { + chain: 'main' | 'test' | 'regtest' | string + blocks: number + headers: number + bestblockhash: string + difficulty: number + mediantime: number + verificationprogress: number + initialblockdownload: boolean + chainwork: string + size_on_disk: number + pruned: boolean + softforks: { + [id: string]: { + type: 'buried' | 'bip9' + active: boolean + height: number + } + } + warnings: string +} +``` diff --git a/website/sidebars.js b/website/sidebars.js index 8207df783c..d0ee0e9559 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -8,6 +8,7 @@ module.exports = { label: 'DeFi APIs', collapsed: false, items: [ + 'jellyfish/api/blockchain', 'jellyfish/api/mining', 'jellyfish/api/wallet' ] From 54b2e80b37cd9a5a6c4ba17e3b42925cbebec4d1 Mon Sep 17 00:00:00 2001 From: Fuxing Loh Date: Mon, 29 Mar 2021 23:12:55 +0800 Subject: [PATCH 4/4] removed keys test --- .../jellyfish-api-core/__tests__/category/blockchain.test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index a4060fb6f4..892509e92d 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -32,8 +32,6 @@ describe('non masternode', () => { expect(info.size_on_disk).toBeGreaterThan(0) expect(info.pruned).toBe(false) - expect(Object.keys(info.softforks).length).toBe(10) - expect(info.softforks.amk.type).toBe('buried') expect(info.softforks.amk.active).toBe(true) expect(info.softforks.amk.height).toBe(0) @@ -78,7 +76,5 @@ describe('masternode', () => { expect(info.chainwork.length).toBe(64) expect(info.size_on_disk).toBeGreaterThan(0) expect(info.pruned).toBe(false) - - expect(Object.keys(info.softforks).length).toBe(10) }) })