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)