From 7edd2c9f764975922326867e1a562edc8d4ac73c Mon Sep 17 00:00:00 2001 From: websterlcl Date: Thu, 1 Apr 2021 23:07:07 +0800 Subject: [PATCH 1/8] getBlock, getBlockHash, getBlockCount, test and doc --- .../__tests__/category/blockchain.test.ts | 94 +++++++++++++--- .../src/category/blockchain.ts | 103 ++++++++++++++++++ website/docs/jellyfish/api/blockchain.md | 101 +++++++++++++++++ 3 files changed, 283 insertions(+), 15 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index 892509e92d..dd0599d4cf 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -1,6 +1,7 @@ import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../container_adapter_client' import waitForExpect from 'wait-for-expect' +import { BlockVerbo } from '../../src/category/blockchain' describe('non masternode', () => { const container = new RegTestContainer() @@ -55,26 +56,89 @@ describe('masternode', () => { await container.stop() }) - it('should getBlockchainInfo', async () => { - await waitForExpect(async () => { + describe('getBlockchainInfo', () => { + 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() - await expect(info.blocks).toBeGreaterThan(1) + + 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) }) + }) - const info = await client.blockchain.getBlockchainInfo() + describe('getBlock', () => { + const height: number = 1 + let blockHash: string = '' + let verbosity: number = 0 - expect(info.chain).toBe('regtest') - expect(info.blocks).toBeGreaterThan(0) - expect(info.headers).toBeGreaterThan(0) + beforeAll(async () => { + blockHash = await client.blockchain.getBlockHash(height) + }) - expect(info.bestblockhash.length).toBe(64) - expect(info.difficulty).toBeGreaterThan(0) - expect(info.mediantime).toBeGreaterThan(1550000000) + it('test getblock with verbo 0, should return hash', async () => { + verbosity = 0 + const data: BlockVerbo = await client.blockchain.getBlock(blockHash, verbosity) + expect(data).not.toBeNull() + }) - 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) + it('test getblock with verbo 1', async () => { + verbosity = 1 + const data: BlockVerbo = await client.blockchain.getBlock(blockHash, verbosity) + console.log('data: ', data) + expect(data).toHaveProperty('hash') + expect(data.confirmations).toStrictEqual(1) + expect(data.strippedsize).toStrictEqual(360) + expect(data.size).toStrictEqual(396) + expect(data.weight).toStrictEqual(1476) + expect(data.height).toStrictEqual(1) + expect(data.masternode).toStrictEqual('e86c027861cc0af423313f4152a44a83296a388eb51bf1a6dde9bd75bed55fb4') + expect(data.minter).toStrictEqual('mswsMVsyGMj1FzDMbbxw2QW3KvQAv2FKiy') + expect(data.mintedBlocks).toStrictEqual(1) + expect(data.stakeModifier).toStrictEqual('fdd82eafa32300653d3b2d9b98a6650b4b15fe2eb32cdd847d3bf2272514cfbf') + expect(data.version).toStrictEqual(536870912) + expect(data.versionHex).toStrictEqual('20000000') + expect(data.merkleroot).toStrictEqual('00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44') + expect(data.tx.length).toStrictEqual(1) + expect(data.tx[0]).toStrictEqual('00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44') + expect(data).toHaveProperty('time') + expect(data).toHaveProperty('mediantime') + expect(data.bits).toStrictEqual('207fffff') + expect(data.difficulty).toStrictEqual(4.656542373906925e-10) + expect(data.chainwork).toStrictEqual('0000000000000000000000000000000000000000000000000000000000000004') + expect(data.nTx).toStrictEqual(1) + expect(data.previousblockhash).toStrictEqual('0091f00915b263d08eba2091ba70ba40cea75242b3f51ea29f4a1b8d7814cd01') + }) + + it('test getblock with verbo2', async () => { + verbosity = 2 + const data: BlockVerbo = await client.blockchain.getBlock(blockHash, verbosity) + console.log('data: ', data) + // NOTE(canonbrother): The only diff between verbo1 and verbo2 is "tx" format + expect(data.tx.length).toStrictEqual(1) + expect(data.tx[0].vin[0].coinbase).toStrictEqual('5100') + expect(data.tx[0].vin[0].sequence).toStrictEqual(4294967295) + expect(data.tx[0].vout[0].value).toStrictEqual(38) + expect(data.tx[0].vout[0].n).toStrictEqual(0) + expect(data.tx[0].vout[0].scriptPubKey.asm).toStrictEqual('OP_DUP OP_HASH160 b36814fd26190b321aa985809293a41273cfe15e OP_EQUALVERIFY OP_CHECKSIG') + expect(data.tx[0].vout[0].scriptPubKey).toHaveProperty('hex') + expect(data.tx[0].vout[0].scriptPubKey.reqSigs).toStrictEqual(1) + expect(data.tx[0].vout[0].scriptPubKey.type).toStrictEqual('pubkeyhash') + expect(data.tx[0].vout[0].scriptPubKey.addresses[0]).toStrictEqual('mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU') + }) }) }) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index e59494c65f..e949c2f7b2 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -1,5 +1,8 @@ import { ApiClient } from '../.' +type BlockVerbo0 = 'string' +export type BlockVerbo = BlockVerbo2 | BlockVerbo1 | BlockVerbo0 + /** * Blockchain related RPC calls for DeFiChain */ @@ -17,6 +20,36 @@ export class Blockchain { async getBlockchainInfo (): Promise { return await this.client.call('getblockchaininfo', [], 'number') } + + /** + * Get a hash of block in best-block-chain at height provided. + * @param height + * @return Promise + */ + async getBlockHash (height: number): Promise { + return await this.client.call('getblockhash', [height], 'number') + } + + /** + * Get the height of the most-work fully-validated chain. + * @return Promise + */ + async getBlockCount (): Promise { + return await this.client.call('getblockcount', [], 'number') + } + + /** + * Get block data with particular header hash. + * @param blockHash + * @param verbosity optional, default is 1, 0 for hex encoded + * @return Promise + */ + async getBlock (blockHash: string, verbosity: number): Promise + async getBlock (blockHash: string, verbosity: number): Promise + async getBlock (blockHash: string, verbosity: number): Promise + async getBlock (blockHash: string, verbosity: number): Promise { + return await this.client.call('getblock', [blockHash, verbosity], 'number') + } } /** @@ -43,3 +76,73 @@ export interface BlockchainInfo { } warnings: string } + +export interface BlockBase { + hash: string + confirmations: number + strippedsize: number + size: number + weight: number + height: number + masternode: string + minter: string + mintedBlocks: any + stakeModifier: any + version: number + versionHex: string + merkleroot: string + time: number + mediantime: number + bits: string + difficulty: number + chainwork: string + nTx: number + previousblockhash: string + nextblockhash: string +} + +export interface BlockVerbo1 extends BlockBase { + tx: string[] +} + +export interface BlockVerbo2 extends BlockBase { + tx: RawTx[] +} + +export interface RawTx { + txid: string + hash: string + version: number + size: number + vsize: number + weight: number + locktime: number + vin: Vin[] + vout: Vout[] + hex: string +} + +export interface Vin { + coinbase: string + txid: string + vout: number + scriptSig: { + asm: string + hex: string + } + txinwitness: string[] + sequence: string +} + +export interface Vout { + value: number + n: number + scriptPubKey: { + asm: string + hex: string + type: string + reqSigs: number + addresses: string[] + tokenId: string + } +} diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index 4b75b4f70a..4737c2143d 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -44,3 +44,104 @@ interface BlockchainInfo { warnings: string } ``` + +## getBlock + +Get block data with particular header hash. + +```ts title="client.blockchain.getBlock()" +interface blockchain { + getBlock(): Promise; +} + +interface BlockBase { + hash: string + confirmations: number + strippedsize: number + size: number + weight: number + height: number + masternode: string + minter: string + mintedBlocks: any + stakeModifier: any + version: number + versionHex: string + merkleroot: string + time: number + mediantime: number + bits: string + difficulty: number + chainwork: string + nTx: number + previousblockhash: string + nextblockhash: string +} + +interface BlockVerbo1 extends BlockBase { + tx: string[] +} + +interface BlockVerbo2 extends BlockBase { + tx: RawTx[] +} + +interface RawTx { + txid: string + hash: string + version: number + size: number + vsize: number + weight: number + locktime: number + vin: Vin[] + vout: Vout[] + hex: string +} + +interface Vin { + coinbase: string + txid: string + vout: number + scriptSig: + asm: string + hex: string + } + txinwitness: string[] + sequence: string +} + +interface Vout { + value: number + n: number + scriptPubKey: + asm: string + hex: string + type: string + reqSigs: number + addresses: string[] + tokenId: string + } +} +``` + +## getBlockHash + +Get a hash of block in best-block-chain at height provided. + +```ts title="client.blockchain.getBlockHash()" +interface blockchain { + getBlockHash(height: number): Promise +} +``` + +## getBlockCount + +Get the height of the most-work fully-validated chain. + +```ts title="client.blockchain.getBlockCount()" +interface blockchain { + getBlockCount (): Promise +} +``` + From 01746ec6c6dd1b28c84af13bce1491a7368ee026 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Thu, 1 Apr 2021 23:36:29 +0800 Subject: [PATCH 2/8] getBlock verbo is optional, add test for getBlock without verbo --- .../__tests__/category/blockchain.test.ts | 5 ++++- packages/jellyfish-api-core/src/category/blockchain.ts | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index dd0599d4cf..76be94472a 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -99,7 +99,6 @@ describe('masternode', () => { it('test getblock with verbo 1', async () => { verbosity = 1 const data: BlockVerbo = await client.blockchain.getBlock(blockHash, verbosity) - console.log('data: ', data) expect(data).toHaveProperty('hash') expect(data.confirmations).toStrictEqual(1) expect(data.strippedsize).toStrictEqual(360) @@ -122,6 +121,10 @@ describe('masternode', () => { expect(data.chainwork).toStrictEqual('0000000000000000000000000000000000000000000000000000000000000004') expect(data.nTx).toStrictEqual(1) expect(data.previousblockhash).toStrictEqual('0091f00915b263d08eba2091ba70ba40cea75242b3f51ea29f4a1b8d7814cd01') + + // NOTE(canonbrother): Get block without verbo, the verbo default should be 1 + const dataTestWithoutVerbo: BlockVerbo = await client.blockchain.getBlock(blockHash) + expect(data).toStrictEqual(dataTestWithoutVerbo) }) it('test getblock with verbo2', async () => { diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index e949c2f7b2..0790d37cc4 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -44,10 +44,10 @@ export class Blockchain { * @param verbosity optional, default is 1, 0 for hex encoded * @return Promise */ - async getBlock (blockHash: string, verbosity: number): Promise - async getBlock (blockHash: string, verbosity: number): Promise - async getBlock (blockHash: string, verbosity: number): Promise - async getBlock (blockHash: string, verbosity: number): Promise { + async getBlock (blockHash: string, verbosity?: number): Promise + async getBlock (blockHash: string, verbosity?: number): Promise + async getBlock (blockHash: string, verbosity?: number): Promise + async getBlock (blockHash: string, verbosity?: number): Promise { return await this.client.call('getblock', [blockHash, verbosity], 'number') } } From 90063c2e57b08612b3a7bff5be988f50ee457c20 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Thu, 1 Apr 2021 23:39:03 +0800 Subject: [PATCH 3/8] update getBlock doc --- website/docs/jellyfish/api/blockchain.md | 53 ++++++++++++------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index 4737c2143d..e0f3d55725 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -6,11 +6,11 @@ slug: /jellyfish/api/blockchain --- ```js -import {Client} from '@defichain/jellyfish' -const client = new Client() +import { Client } from "@defichain/jellyfish"; +const client = new Client(); // Using client.blockchain. -const something = await client.blockchain.method() +const something = await client.blockchain.method(); ``` ## getBlockchainInfo @@ -19,29 +19,29 @@ Get various state info regarding blockchain processing. ```ts title="client.blockchain.getBlockchainInfo()" interface blockchain { - getBlockchainInfo (): Promise + 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 + 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 + type: "buried" | "bip9"; + active: boolean; + height: number; + }; + }; + warnings: string; } ``` @@ -51,7 +51,7 @@ Get block data with particular header hash. ```ts title="client.blockchain.getBlock()" interface blockchain { - getBlock(): Promise; + getBlock (blockHash: string, verbosity?: number): Promise; } interface BlockBase { @@ -103,7 +103,7 @@ interface Vin { coinbase: string txid: string vout: number - scriptSig: + scriptSig: asm: string hex: string } @@ -114,7 +114,7 @@ interface Vin { interface Vout { value: number n: number - scriptPubKey: + scriptPubKey: asm: string hex: string type: string @@ -131,7 +131,7 @@ Get a hash of block in best-block-chain at height provided. ```ts title="client.blockchain.getBlockHash()" interface blockchain { - getBlockHash(height: number): Promise + getBlockHash(height: number): Promise; } ``` @@ -141,7 +141,6 @@ Get the height of the most-work fully-validated chain. ```ts title="client.blockchain.getBlockCount()" interface blockchain { - getBlockCount (): Promise + getBlockCount(): Promise; } ``` - From f7b1bee36913c2d5b89c068328e3c4eec504d8a4 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Thu, 1 Apr 2021 23:40:18 +0800 Subject: [PATCH 4/8] revert formatted --- website/docs/jellyfish/api/blockchain.md | 53 ++++++++++++------------ 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index e0f3d55725..d1ffdad104 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -6,11 +6,11 @@ slug: /jellyfish/api/blockchain --- ```js -import { Client } from "@defichain/jellyfish"; -const client = new Client(); +import {Client} from '@defichain/jellyfish' +const client = new Client() // Using client.blockchain. -const something = await client.blockchain.method(); +const something = await client.blockchain.method() ``` ## getBlockchainInfo @@ -19,29 +19,29 @@ Get various state info regarding blockchain processing. ```ts title="client.blockchain.getBlockchainInfo()" interface blockchain { - getBlockchainInfo(): Promise; + 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; + 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; + type: 'buried' | 'bip9' + active: boolean + height: number + } + } + warnings: string } ``` @@ -51,7 +51,7 @@ Get block data with particular header hash. ```ts title="client.blockchain.getBlock()" interface blockchain { - getBlock (blockHash: string, verbosity?: number): Promise; + getBlock (blockHash: string, verbosity?: number): Promise } interface BlockBase { @@ -103,7 +103,7 @@ interface Vin { coinbase: string txid: string vout: number - scriptSig: + scriptSig: asm: string hex: string } @@ -114,7 +114,7 @@ interface Vin { interface Vout { value: number n: number - scriptPubKey: + scriptPubKey: asm: string hex: string type: string @@ -131,7 +131,7 @@ Get a hash of block in best-block-chain at height provided. ```ts title="client.blockchain.getBlockHash()" interface blockchain { - getBlockHash(height: number): Promise; + getBlockHash(height: number): Promise } ``` @@ -141,6 +141,7 @@ Get the height of the most-work fully-validated chain. ```ts title="client.blockchain.getBlockCount()" interface blockchain { - getBlockCount(): Promise; + getBlockCount (): Promise } ``` + From d4b3664012b7ee7580623e29b7da67754596e044 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Fri, 2 Apr 2021 10:41:27 +0800 Subject: [PATCH 5/8] fix rq: replace extends by generic, add getBlockCount and getBlockHash test --- .../__tests__/category/blockchain.test.ts | 101 ++++++++++-------- .../src/category/blockchain.ts | 25 ++--- 2 files changed, 62 insertions(+), 64 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index 76be94472a..243f1cd09f 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -1,7 +1,7 @@ import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../container_adapter_client' import waitForExpect from 'wait-for-expect' -import { BlockVerbo } from '../../src/category/blockchain' +import { Block, RawTx } from '../../src/category/blockchain' describe('non masternode', () => { const container = new RegTestContainer() @@ -82,66 +82,77 @@ describe('masternode', () => { }) describe('getBlock', () => { - const height: number = 1 let blockHash: string = '' - let verbosity: number = 0 beforeAll(async () => { - blockHash = await client.blockchain.getBlockHash(height) + blockHash = await client.blockchain.getBlockHash(1) }) it('test getblock with verbo 0, should return hash', async () => { - verbosity = 0 - const data: BlockVerbo = await client.blockchain.getBlock(blockHash, verbosity) + const data: string | Block = await client.blockchain.getBlock(blockHash, 0) expect(data).not.toBeNull() }) it('test getblock with verbo 1', async () => { - verbosity = 1 - const data: BlockVerbo = await client.blockchain.getBlock(blockHash, verbosity) - expect(data).toHaveProperty('hash') - expect(data.confirmations).toStrictEqual(1) - expect(data.strippedsize).toStrictEqual(360) - expect(data.size).toStrictEqual(396) - expect(data.weight).toStrictEqual(1476) - expect(data.height).toStrictEqual(1) - expect(data.masternode).toStrictEqual('e86c027861cc0af423313f4152a44a83296a388eb51bf1a6dde9bd75bed55fb4') - expect(data.minter).toStrictEqual('mswsMVsyGMj1FzDMbbxw2QW3KvQAv2FKiy') - expect(data.mintedBlocks).toStrictEqual(1) - expect(data.stakeModifier).toStrictEqual('fdd82eafa32300653d3b2d9b98a6650b4b15fe2eb32cdd847d3bf2272514cfbf') - expect(data.version).toStrictEqual(536870912) - expect(data.versionHex).toStrictEqual('20000000') - expect(data.merkleroot).toStrictEqual('00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44') - expect(data.tx.length).toStrictEqual(1) - expect(data.tx[0]).toStrictEqual('00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44') - expect(data).toHaveProperty('time') - expect(data).toHaveProperty('mediantime') - expect(data.bits).toStrictEqual('207fffff') - expect(data.difficulty).toStrictEqual(4.656542373906925e-10) - expect(data.chainwork).toStrictEqual('0000000000000000000000000000000000000000000000000000000000000004') - expect(data.nTx).toStrictEqual(1) - expect(data.previousblockhash).toStrictEqual('0091f00915b263d08eba2091ba70ba40cea75242b3f51ea29f4a1b8d7814cd01') + const data: string | Block = await client.blockchain.getBlock(blockHash, 1) + const block = data as Block + expect(block).toHaveProperty('hash') + expect(block.confirmations).toStrictEqual(2) + expect(block.strippedsize).toStrictEqual(360) + expect(block.size).toStrictEqual(396) + expect(block.weight).toStrictEqual(1476) + expect(block.height).toStrictEqual(1) + expect(block.masternode).toStrictEqual('e86c027861cc0af423313f4152a44a83296a388eb51bf1a6dde9bd75bed55fb4') + expect(block.minter).toStrictEqual('mswsMVsyGMj1FzDMbbxw2QW3KvQAv2FKiy') + expect(block.mintedBlocks).toStrictEqual(1) + expect(block.stakeModifier).toStrictEqual('fdd82eafa32300653d3b2d9b98a6650b4b15fe2eb32cdd847d3bf2272514cfbf') + expect(block.version).toStrictEqual(536870912) + expect(block.versionHex).toStrictEqual('20000000') + expect(block.merkleroot).toStrictEqual('00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44') + expect(block.tx.length).toStrictEqual(1) + expect(block.tx[0]).toStrictEqual('00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44') + expect(block).toHaveProperty('time') + expect(block).toHaveProperty('mediantime') + expect(block.bits).toStrictEqual('207fffff') + expect(block.difficulty).toStrictEqual(4.656542373906925e-10) + expect(block.chainwork).toStrictEqual('0000000000000000000000000000000000000000000000000000000000000004') + expect(block.nTx).toStrictEqual(1) + expect(block.previousblockhash).toStrictEqual('0091f00915b263d08eba2091ba70ba40cea75242b3f51ea29f4a1b8d7814cd01') // NOTE(canonbrother): Get block without verbo, the verbo default should be 1 - const dataTestWithoutVerbo: BlockVerbo = await client.blockchain.getBlock(blockHash) - expect(data).toStrictEqual(dataTestWithoutVerbo) + const dataWithoutVerbo: string | Block = await client.blockchain.getBlock(blockHash) + const blockWithoutVerbo = dataWithoutVerbo as Block + expect(blockWithoutVerbo).toStrictEqual(dataWithoutVerbo) }) it('test getblock with verbo2', async () => { - verbosity = 2 - const data: BlockVerbo = await client.blockchain.getBlock(blockHash, verbosity) - console.log('data: ', data) + const data: string | Block = await client.blockchain.getBlock(blockHash, 2) + const block = data as Block // NOTE(canonbrother): The only diff between verbo1 and verbo2 is "tx" format - expect(data.tx.length).toStrictEqual(1) - expect(data.tx[0].vin[0].coinbase).toStrictEqual('5100') - expect(data.tx[0].vin[0].sequence).toStrictEqual(4294967295) - expect(data.tx[0].vout[0].value).toStrictEqual(38) - expect(data.tx[0].vout[0].n).toStrictEqual(0) - expect(data.tx[0].vout[0].scriptPubKey.asm).toStrictEqual('OP_DUP OP_HASH160 b36814fd26190b321aa985809293a41273cfe15e OP_EQUALVERIFY OP_CHECKSIG') - expect(data.tx[0].vout[0].scriptPubKey).toHaveProperty('hex') - expect(data.tx[0].vout[0].scriptPubKey.reqSigs).toStrictEqual(1) - expect(data.tx[0].vout[0].scriptPubKey.type).toStrictEqual('pubkeyhash') - expect(data.tx[0].vout[0].scriptPubKey.addresses[0]).toStrictEqual('mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU') + expect(block.tx.length).toStrictEqual(1) + expect(block.tx[0].vin[0].coinbase).toStrictEqual('5100') + expect(block.tx[0].vin[0].sequence).toStrictEqual(4294967295) + expect(block.tx[0].vout[0].value).toStrictEqual(38) + expect(block.tx[0].vout[0].n).toStrictEqual(0) + expect(block.tx[0].vout[0].scriptPubKey.asm).toStrictEqual('OP_DUP OP_HASH160 b36814fd26190b321aa985809293a41273cfe15e OP_EQUALVERIFY OP_CHECKSIG') + expect(block.tx[0].vout[0].scriptPubKey).toHaveProperty('hex') + expect(block.tx[0].vout[0].scriptPubKey.reqSigs).toStrictEqual(1) + expect(block.tx[0].vout[0].scriptPubKey.type).toStrictEqual('pubkeyhash') + expect(block.tx[0].vout[0].scriptPubKey.addresses[0]).toStrictEqual('mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU') + }) + }) + + describe('getBlockHash', () => { + it('should getBlockHash', async () => { + const blockHash: string = await client.blockchain.getBlockHash(1) + expect(blockHash).not.toBeNull() + }) + }) + + describe('getBlockCount', () => { + it('should getBlockCount', async () => { + const blockCount: number = await client.blockchain.getBlockCount() + expect(blockCount).toStrictEqual(2) }) }) }) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index 0790d37cc4..cc878246dc 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -1,8 +1,5 @@ import { ApiClient } from '../.' -type BlockVerbo0 = 'string' -export type BlockVerbo = BlockVerbo2 | BlockVerbo1 | BlockVerbo0 - /** * Blockchain related RPC calls for DeFiChain */ @@ -42,12 +39,9 @@ export class Blockchain { * Get block data with particular header hash. * @param blockHash * @param verbosity optional, default is 1, 0 for hex encoded - * @return Promise + * @return Promise */ - async getBlock (blockHash: string, verbosity?: number): Promise - async getBlock (blockHash: string, verbosity?: number): Promise - async getBlock (blockHash: string, verbosity?: number): Promise - async getBlock (blockHash: string, verbosity?: number): Promise { + async getBlock (blockHash: string, verbosity?: 0 | 1 | 2): Promise> { return await this.client.call('getblock', [blockHash, verbosity], 'number') } } @@ -77,7 +71,7 @@ export interface BlockchainInfo { warnings: string } -export interface BlockBase { +export interface Block { hash: string confirmations: number strippedsize: number @@ -86,8 +80,8 @@ export interface BlockBase { height: number masternode: string minter: string - mintedBlocks: any - stakeModifier: any + mintedBlocks: number + stakeModifier: string version: number versionHex: string merkleroot: string @@ -96,19 +90,12 @@ export interface BlockBase { bits: string difficulty: number chainwork: string + tx: T[] nTx: number previousblockhash: string nextblockhash: string } -export interface BlockVerbo1 extends BlockBase { - tx: string[] -} - -export interface BlockVerbo2 extends BlockBase { - tx: RawTx[] -} - export interface RawTx { txid: string hash: string From f5c8204c840c7a38eb8ae5702f97effffb4a02b2 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Fri, 2 Apr 2021 11:46:24 +0800 Subject: [PATCH 6/8] update blockchain.md --- .../src/category/blockchain.ts | 2 +- website/docs/jellyfish/api/blockchain.md | 55 ++----------------- 2 files changed, 6 insertions(+), 51 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index cc878246dc..828cc0957c 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -39,7 +39,7 @@ export class Blockchain { * Get block data with particular header hash. * @param blockHash * @param verbosity optional, default is 1, 0 for hex encoded - * @return Promise + * @return Promise */ async getBlock (blockHash: string, verbosity?: 0 | 1 | 2): Promise> { return await this.client.call('getblock', [blockHash, verbosity], 'number') diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index d1ffdad104..ae48d43d30 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -51,10 +51,10 @@ Get block data with particular header hash. ```ts title="client.blockchain.getBlock()" interface blockchain { - getBlock (blockHash: string, verbosity?: number): Promise + async getBlock (blockHash: string, verbosity?: 0 | 1 | 2): Promise> } -interface BlockBase { +export interface Block { hash: string confirmations: number strippedsize: number @@ -63,8 +63,8 @@ interface BlockBase { height: number masternode: string minter: string - mintedBlocks: any - stakeModifier: any + mintedBlocks: number + stakeModifier: string version: number versionHex: string merkleroot: string @@ -73,56 +73,11 @@ interface BlockBase { bits: string difficulty: number chainwork: string + tx: T[] nTx: number previousblockhash: string nextblockhash: string } - -interface BlockVerbo1 extends BlockBase { - tx: string[] -} - -interface BlockVerbo2 extends BlockBase { - tx: RawTx[] -} - -interface RawTx { - txid: string - hash: string - version: number - size: number - vsize: number - weight: number - locktime: number - vin: Vin[] - vout: Vout[] - hex: string -} - -interface Vin { - coinbase: string - txid: string - vout: number - scriptSig: - asm: string - hex: string - } - txinwitness: string[] - sequence: string -} - -interface Vout { - value: number - n: number - scriptPubKey: - asm: string - hex: string - type: string - reqSigs: number - addresses: string[] - tokenId: string - } -} ``` ## getBlockHash From 8efbfb27302282201f30f3cedcafa39098bdddfc Mon Sep 17 00:00:00 2001 From: websterlcl Date: Fri, 2 Apr 2021 14:53:39 +0800 Subject: [PATCH 7/8] fix rq: test with signatures/pattern than exact, resolve generics with overloads --- .../__tests__/category/blockchain.test.ts | 47 +++++++++---------- .../src/category/blockchain.ts | 3 ++ website/docs/jellyfish/api/blockchain.md | 3 ++ 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index 243f1cd09f..a862edd0b1 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -89,54 +89,51 @@ describe('masternode', () => { }) it('test getblock with verbo 0, should return hash', async () => { - const data: string | Block = await client.blockchain.getBlock(blockHash, 0) - expect(data).not.toBeNull() + const block: string = await client.blockchain.getBlock(blockHash, 0) + expect(block).not.toBeNull() }) it('test getblock with verbo 1', async () => { - const data: string | Block = await client.blockchain.getBlock(blockHash, 1) - const block = data as Block + const block: Block = await client.blockchain.getBlock(blockHash, 1) expect(block).toHaveProperty('hash') - expect(block.confirmations).toStrictEqual(2) - expect(block.strippedsize).toStrictEqual(360) - expect(block.size).toStrictEqual(396) - expect(block.weight).toStrictEqual(1476) - expect(block.height).toStrictEqual(1) + expect(block.confirmations).toBeGreaterThanOrEqual(2) + expect(block.strippedsize).toBeGreaterThanOrEqual(360) + expect(block.size).toBeGreaterThanOrEqual(396) + expect(block.weight).toBeGreaterThanOrEqual(1476) + expect(block.height).toBeGreaterThanOrEqual(1) expect(block.masternode).toStrictEqual('e86c027861cc0af423313f4152a44a83296a388eb51bf1a6dde9bd75bed55fb4') expect(block.minter).toStrictEqual('mswsMVsyGMj1FzDMbbxw2QW3KvQAv2FKiy') - expect(block.mintedBlocks).toStrictEqual(1) + expect(block.mintedBlocks).toBeGreaterThanOrEqual(1) expect(block.stakeModifier).toStrictEqual('fdd82eafa32300653d3b2d9b98a6650b4b15fe2eb32cdd847d3bf2272514cfbf') - expect(block.version).toStrictEqual(536870912) + expect(block.version).toBeGreaterThanOrEqual(536870912) expect(block.versionHex).toStrictEqual('20000000') expect(block.merkleroot).toStrictEqual('00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44') - expect(block.tx.length).toStrictEqual(1) + expect(block.tx.length).toBeGreaterThanOrEqual(1) expect(block.tx[0]).toStrictEqual('00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44') expect(block).toHaveProperty('time') expect(block).toHaveProperty('mediantime') expect(block.bits).toStrictEqual('207fffff') - expect(block.difficulty).toStrictEqual(4.656542373906925e-10) + expect(block).toHaveProperty('difficulty') expect(block.chainwork).toStrictEqual('0000000000000000000000000000000000000000000000000000000000000004') - expect(block.nTx).toStrictEqual(1) + expect(block.nTx).toBeGreaterThanOrEqual(1) expect(block.previousblockhash).toStrictEqual('0091f00915b263d08eba2091ba70ba40cea75242b3f51ea29f4a1b8d7814cd01') // NOTE(canonbrother): Get block without verbo, the verbo default should be 1 - const dataWithoutVerbo: string | Block = await client.blockchain.getBlock(blockHash) - const blockWithoutVerbo = dataWithoutVerbo as Block - expect(blockWithoutVerbo).toStrictEqual(dataWithoutVerbo) + const blockWithoutVerbo: string = await client.blockchain.getBlock(blockHash) + expect(blockWithoutVerbo).toStrictEqual(block) }) it('test getblock with verbo2', async () => { - const data: string | Block = await client.blockchain.getBlock(blockHash, 2) - const block = data as Block + const block: Block = await client.blockchain.getBlock(blockHash, 2) // NOTE(canonbrother): The only diff between verbo1 and verbo2 is "tx" format - expect(block.tx.length).toStrictEqual(1) + expect(block.tx.length).toBeGreaterThanOrEqual(1) expect(block.tx[0].vin[0].coinbase).toStrictEqual('5100') - expect(block.tx[0].vin[0].sequence).toStrictEqual(4294967295) - expect(block.tx[0].vout[0].value).toStrictEqual(38) - expect(block.tx[0].vout[0].n).toStrictEqual(0) + expect(block.tx[0].vin[0].sequence).toBeGreaterThanOrEqual(4294967295) + expect(block.tx[0].vout[0].value).toBeGreaterThanOrEqual(38) + expect(block.tx[0].vout[0].n).toBeGreaterThanOrEqual(0) expect(block.tx[0].vout[0].scriptPubKey.asm).toStrictEqual('OP_DUP OP_HASH160 b36814fd26190b321aa985809293a41273cfe15e OP_EQUALVERIFY OP_CHECKSIG') expect(block.tx[0].vout[0].scriptPubKey).toHaveProperty('hex') - expect(block.tx[0].vout[0].scriptPubKey.reqSigs).toStrictEqual(1) + expect(block.tx[0].vout[0].scriptPubKey.reqSigs).toBeGreaterThanOrEqual(1) expect(block.tx[0].vout[0].scriptPubKey.type).toStrictEqual('pubkeyhash') expect(block.tx[0].vout[0].scriptPubKey.addresses[0]).toStrictEqual('mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU') }) @@ -152,7 +149,7 @@ describe('masternode', () => { describe('getBlockCount', () => { it('should getBlockCount', async () => { const blockCount: number = await client.blockchain.getBlockCount() - expect(blockCount).toStrictEqual(2) + expect(blockCount).toBeGreaterThanOrEqual(2) }) }) }) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index 828cc0957c..b4537598cb 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -41,6 +41,9 @@ export class Blockchain { * @param verbosity optional, default is 1, 0 for hex encoded * @return Promise */ + async getBlock (hash: string, verbosity?: 0): Promise + async getBlock (hash: string, verbosity?: 1): Promise> + async getBlock (hash: string, verbosity?: 2): Promise> async getBlock (blockHash: string, verbosity?: 0 | 1 | 2): Promise> { return await this.client.call('getblock', [blockHash, verbosity], 'number') } diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index ae48d43d30..ca77b4bab9 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -51,6 +51,9 @@ Get block data with particular header hash. ```ts title="client.blockchain.getBlock()" interface blockchain { + async getBlock (hash: string, verbosity?: 0): Promise + async getBlock (hash: string, verbosity?: 1): Promise> + async getBlock (hash: string, verbosity?: 2): Promise> async getBlock (blockHash: string, verbosity?: 0 | 1 | 2): Promise> } From 7daa3ff5f59a1be9f34ef5e16cba60c8ca7d712e Mon Sep 17 00:00:00 2001 From: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> Date: Mon, 5 Apr 2021 12:16:27 +0800 Subject: [PATCH 8/8] - isolated and removed all possible race conditions from test (#92) - renamed RawTx to Transaction since its actually Transaction in this case - documented each verbosity and make it non-optional, you have to specific verbosity. - updated website with the latest changes --- .../__tests__/category/blockchain.test.ts | 84 ++++++++++++------- .../src/category/blockchain.ts | 39 +++++++-- website/docs/jellyfish/api/blockchain.md | 46 +++++++++- 3 files changed, 128 insertions(+), 41 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts index a862edd0b1..8830480be0 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain.test.ts @@ -1,7 +1,7 @@ import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../container_adapter_client' import waitForExpect from 'wait-for-expect' -import { Block, RawTx } from '../../src/category/blockchain' +import { Block, Transaction } from '../../src' describe('non masternode', () => { const container = new RegTestContainer() @@ -82,72 +82,100 @@ describe('masternode', () => { }) describe('getBlock', () => { - let blockHash: string = '' + /** + * Wait for block hash to reach a certain height + */ + async function waitForBlockHash (height: number): Promise { + await waitForExpect(async () => { + const info = await client.blockchain.getBlockchainInfo() + await expect(info.blocks).toBeGreaterThan(height) + }) - beforeAll(async () => { - blockHash = await client.blockchain.getBlockHash(1) - }) + return await client.blockchain.getBlockHash(height) + } - it('test getblock with verbo 0, should return hash', async () => { - const block: string = await client.blockchain.getBlock(blockHash, 0) - expect(block).not.toBeNull() + it('should getBlock with verbosity 0 and return just a string that is serialized, hex-encoded data for block', async () => { + const blockHash = await waitForBlockHash(1) + const hash: string = await client.blockchain.getBlock(blockHash, 0) + + expect(hash).not.toBeNull() }) - it('test getblock with verbo 1', async () => { + it('should getBlock with verbosity 1 and return block with tx as hex', async () => { + const blockHash = await waitForBlockHash(1) const block: Block = await client.blockchain.getBlock(blockHash, 1) - expect(block).toHaveProperty('hash') + + expect(block.hash.length).toBe(64) + expect(block.confirmations).toBeGreaterThanOrEqual(2) expect(block.strippedsize).toBeGreaterThanOrEqual(360) + expect(block.size).toBeGreaterThanOrEqual(396) expect(block.weight).toBeGreaterThanOrEqual(1476) expect(block.height).toBeGreaterThanOrEqual(1) - expect(block.masternode).toStrictEqual('e86c027861cc0af423313f4152a44a83296a388eb51bf1a6dde9bd75bed55fb4') - expect(block.minter).toStrictEqual('mswsMVsyGMj1FzDMbbxw2QW3KvQAv2FKiy') + + expect(block.masternode.length).toBe(64) + expect(block.minter.length).toBe(34) // legacy address length + expect(block.mintedBlocks).toBeGreaterThanOrEqual(1) - expect(block.stakeModifier).toStrictEqual('fdd82eafa32300653d3b2d9b98a6650b4b15fe2eb32cdd847d3bf2272514cfbf') + expect(block.stakeModifier.length).toBe(64) expect(block.version).toBeGreaterThanOrEqual(536870912) expect(block.versionHex).toStrictEqual('20000000') - expect(block.merkleroot).toStrictEqual('00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44') + expect(block.merkleroot.length).toBe(64) + expect(block.tx.length).toBeGreaterThanOrEqual(1) - expect(block.tx[0]).toStrictEqual('00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44') - expect(block).toHaveProperty('time') - expect(block).toHaveProperty('mediantime') + expect(block.tx[0].length).toBe(64) + + expect(block.time).toBeGreaterThan(1) + expect(block.mediantime).toBeGreaterThan(1) + expect(block.bits).toStrictEqual('207fffff') - expect(block).toHaveProperty('difficulty') - expect(block.chainwork).toStrictEqual('0000000000000000000000000000000000000000000000000000000000000004') - expect(block.nTx).toBeGreaterThanOrEqual(1) - expect(block.previousblockhash).toStrictEqual('0091f00915b263d08eba2091ba70ba40cea75242b3f51ea29f4a1b8d7814cd01') + expect(block.difficulty).toBeGreaterThan(0) - // NOTE(canonbrother): Get block without verbo, the verbo default should be 1 - const blockWithoutVerbo: string = await client.blockchain.getBlock(blockHash) - expect(blockWithoutVerbo).toStrictEqual(block) + expect(block.chainwork.length).toBe(64) + expect(block.nTx).toBeGreaterThanOrEqual(1) + expect(block.previousblockhash.length).toBe(64) }) - it('test getblock with verbo2', async () => { - const block: Block = await client.blockchain.getBlock(blockHash, 2) - // NOTE(canonbrother): The only diff between verbo1 and verbo2 is "tx" format + it('should getBlock with verbosity 2 and return block with tx as RawText', async () => { + const blockHash = await waitForBlockHash(1) + const block: Block = await client.blockchain.getBlock(blockHash, 2) + expect(block.tx.length).toBeGreaterThanOrEqual(1) expect(block.tx[0].vin[0].coinbase).toStrictEqual('5100') expect(block.tx[0].vin[0].sequence).toBeGreaterThanOrEqual(4294967295) + expect(block.tx[0].vout[0].value).toBeGreaterThanOrEqual(38) expect(block.tx[0].vout[0].n).toBeGreaterThanOrEqual(0) + expect(block.tx[0].vout[0].scriptPubKey.asm).toStrictEqual('OP_DUP OP_HASH160 b36814fd26190b321aa985809293a41273cfe15e OP_EQUALVERIFY OP_CHECKSIG') expect(block.tx[0].vout[0].scriptPubKey).toHaveProperty('hex') expect(block.tx[0].vout[0].scriptPubKey.reqSigs).toBeGreaterThanOrEqual(1) expect(block.tx[0].vout[0].scriptPubKey.type).toStrictEqual('pubkeyhash') - expect(block.tx[0].vout[0].scriptPubKey.addresses[0]).toStrictEqual('mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU') + expect(block.tx[0].vout[0].scriptPubKey.addresses[0].length).toBe(34) }) }) describe('getBlockHash', () => { it('should getBlockHash', async () => { + await waitForExpect(async () => { + const info = await client.blockchain.getBlockchainInfo() + await expect(info.blocks).toBeGreaterThan(1) + }) + const blockHash: string = await client.blockchain.getBlockHash(1) expect(blockHash).not.toBeNull() + expect(blockHash.length).toBe(64) }) }) describe('getBlockCount', () => { it('should getBlockCount', async () => { + await waitForExpect(async () => { + const info = await client.blockchain.getBlockchainInfo() + await expect(info.blocks).toBeGreaterThan(1) + }) + const blockCount: number = await client.blockchain.getBlockCount() expect(blockCount).toBeGreaterThanOrEqual(2) }) diff --git a/packages/jellyfish-api-core/src/category/blockchain.ts b/packages/jellyfish-api-core/src/category/blockchain.ts index b4537598cb..762052fff9 100644 --- a/packages/jellyfish-api-core/src/category/blockchain.ts +++ b/packages/jellyfish-api-core/src/category/blockchain.ts @@ -37,15 +37,36 @@ export class Blockchain { /** * Get block data with particular header hash. - * @param blockHash - * @param verbosity optional, default is 1, 0 for hex encoded - * @return Promise + * Returns a string that is serialized, hex-encoded data for block 'hash' + * + * @param hash of the block + * @param verbosity 0 + * @return Promise + */ + getBlock (hash: string, verbosity: 0): Promise + + /** + * Get block data with particular header hash. + * Returns an Object with information about the block 'hash'. + * + * @param hash of the block + * @param verbosity 1 + * @return Promise> */ - async getBlock (hash: string, verbosity?: 0): Promise - async getBlock (hash: string, verbosity?: 1): Promise> - async getBlock (hash: string, verbosity?: 2): Promise> - async getBlock (blockHash: string, verbosity?: 0 | 1 | 2): Promise> { - return await this.client.call('getblock', [blockHash, verbosity], 'number') + getBlock (hash: string, verbosity: 1): Promise> + + /** + * Get block data with particular header hash. + * Returns an Object with information about block 'hash' and information about each transaction. + * + * @param hash of the block + * @param verbosity 2 + * @return Promise> + */ + getBlock (hash: string, verbosity: 2): Promise> + + async getBlock (hash: string, verbosity: 0 | 1 | 2): Promise> { + return await this.client.call('getblock', [hash, verbosity], 'number') } } @@ -99,7 +120,7 @@ export interface Block { nextblockhash: string } -export interface RawTx { +export interface Transaction { txid: string hash: string version: number diff --git a/website/docs/jellyfish/api/blockchain.md b/website/docs/jellyfish/api/blockchain.md index ca77b4bab9..08e614713e 100644 --- a/website/docs/jellyfish/api/blockchain.md +++ b/website/docs/jellyfish/api/blockchain.md @@ -51,10 +51,10 @@ Get block data with particular header hash. ```ts title="client.blockchain.getBlock()" interface blockchain { - async getBlock (hash: string, verbosity?: 0): Promise - async getBlock (hash: string, verbosity?: 1): Promise> - async getBlock (hash: string, verbosity?: 2): Promise> - async getBlock (blockHash: string, verbosity?: 0 | 1 | 2): Promise> + getBlock (hash: string, verbosity: 0): Promise + getBlock (hash: string, verbosity: 1): Promise> + getBlock (hash: string, verbosity: 2): Promise> + getBlock (hash: string, verbosity: 0 | 1 | 2): Promise> } export interface Block { @@ -81,6 +81,44 @@ export interface Block { previousblockhash: string nextblockhash: string } + +export interface Transaction { + txid: string + hash: string + version: number + size: number + vsize: number + weight: number + locktime: number + vin: Vin[] + vout: Vout[] + hex: string +} + +export interface Vin { + coinbase: string + txid: string + vout: number + scriptSig: { + asm: string + hex: string + } + txinwitness: string[] + sequence: string +} + +export interface Vout { + value: number + n: number + scriptPubKey: { + asm: string + hex: string + type: string + reqSigs: number + addresses: string[] + tokenId: string + } +} ``` ## getBlockHash