From 856c846ed4073df5ea1815edce1429a62657d345 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Fri, 16 Apr 2021 10:00:39 +0800 Subject: [PATCH 01/15] wip --- .../__tests__/category/account.test.ts | 49 +++++++++ .../src/category/account.ts | 100 ++++++++++++++++++ packages/jellyfish-api-core/src/index.ts | 3 + 3 files changed, 152 insertions(+) create mode 100644 packages/jellyfish-api-core/__tests__/category/account.test.ts create mode 100644 packages/jellyfish-api-core/src/category/account.ts diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts new file mode 100644 index 0000000000..efb9601477 --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -0,0 +1,49 @@ +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() + }) + + describe('listAccounts', () => { + it('should listAccounts', async () => { + await waitForExpect(async () => { + const accounts = await client.account.listAccounts() + console.log('accounts: ', accounts) + }) + }) + }) +}) + +describe.only('masternode', () => { + const container = new MasterNodeRegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.waitForReady() + }) + + afterAll(async () => { + await container.stop() + }) + + describe('listAccounts', () => { + it('should listAccounts', async () => { + await waitForExpect(async () => { + const accounts = await client.account.listAccounts() + console.log('accounts: ', accounts) + }) + }) + }) +}) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts new file mode 100644 index 0000000000..385852564e --- /dev/null +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -0,0 +1,100 @@ +import { ApiClient } from '../.' + +/** + * Account related RPC calls for DeFiChain + */ +export class Account { + private readonly client: ApiClient + + constructor (client: ApiClient) { + this.client = client + } + + /** + * Returns information about all accounts on chain + * + * @param pagination + * @param pagination.start + * @param pagination.including_start + * @param pagination.limit + * @param verbose default = true, otherwise limited objects are listed + * @param indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param isMineOnly get balances about all accounts belonging to the wallet + * @return Promise + */ + async listAccounts ( + pagination: AccountPagination = { + start: '', + including_start: true, + limit: 100 + }, verbose = true, indexedAmounts = false, isMineOnly = false + ): Promise { + return await this.client.call('listaccounts', [{}, verbose, indexedAmounts, isMineOnly], 'number') + } + + /** + * Returns information about account + * + * @param owner adress in base58/bech32/hex encoding + * @param pagination + * @param pagination.start + * @param pagination.including_start + * @param pagination.limit + * @param indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @return Promise + */ + async getAccount (owner: string, pagination: AccountPagination = { + start: '', + including_start: true, + limit: 100 + }, indexedAmounts = false): Promise { + return await this.client.call('getaccount', [owner, pagination, indexedAmounts], 'number') + } + + /** + * Returns the balances of all accounts that belong to the wallet + * @param pagination + * @param pagination.start + * @param pagination.including_start + * @param pagination.limit + * @param indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param symbolLookup use token symbols in output, default = false + * @return + */ + async getTokenBalances ( + pagination: AccountPagination = { + start: '', + including_start: true, + limit: 100 + }, indexedAmounts = false, symbolLookup = false + ): Promise { + return await this.client.call('gettokenbalances', [pagination, indexedAmounts, symbolLookup], 'number') + } + + /** + * Returns information about account history + * + * @param owner + * @param options + * @param options.maxBlockHeight + * @param options.depth + * @param options.no_rewards + * @param options.token + * @param options.txtype + * @param options.limit + * @return + */ + async listAccountHistory (): Promise { + return await this.client.call('listaccounthistory', [], 'number') + } +} + +export interface AccountPagination { + start: string + including_start: boolean + limit: number +} + +// export interface IAccount { + +// } diff --git a/packages/jellyfish-api-core/src/index.ts b/packages/jellyfish-api-core/src/index.ts index 4e3d5b29c0..1beb96ada1 100644 --- a/packages/jellyfish-api-core/src/index.ts +++ b/packages/jellyfish-api-core/src/index.ts @@ -2,11 +2,13 @@ import { Precision, PrecisionMapping } from '@defichain/jellyfish-json' import { Blockchain } from './category/blockchain' import { Mining } from './category/mining' import { Wallet } from './category/wallet' +import { Account } from './category/account' export * from '@defichain/jellyfish-json' export * from './category/blockchain' export * from './category/mining' export * from './category/wallet' +export * from './category/account' /** * ApiClient; a protocol agnostic DeFiChain node client, RPC calls are separated into their category. @@ -15,6 +17,7 @@ export abstract class ApiClient { public readonly blockchain = new Blockchain(this) public readonly mining = new Mining(this) public readonly wallet = new Wallet(this) + public readonly account = new Account(this) /** * A promise based procedure call handling From 077fd036ea35725e5fbdd3538f1ee22066ce6d9d Mon Sep 17 00:00:00 2001 From: websterlcl Date: Mon, 19 Apr 2021 10:05:22 +0800 Subject: [PATCH 02/15] add jsdoc and interface --- .../src/category/account.ts | 81 ++++++++++--------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 385852564e..e91e502506 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -13,34 +13,34 @@ export class Account { /** * Returns information about all accounts on chain * - * @param pagination - * @param pagination.start - * @param pagination.including_start - * @param pagination.limit - * @param verbose default = true, otherwise limited objects are listed - * @param indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @param isMineOnly get balances about all accounts belonging to the wallet + * @param {AccountPagination=} pagination + * @param {string} pagination.start + * @param {boolean} pagination.including_start + * @param {number} pagination.limit + * @param {boolean} verbose default = true, otherwise limited objects are listed + * @param {boolean} indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {boolean} isMineOnly get balances about all accounts belonging to the wallet * @return Promise */ async listAccounts ( pagination: AccountPagination = { - start: '', - including_start: true, + // start: '', + including_start: false, limit: 100 }, verbose = true, indexedAmounts = false, isMineOnly = false ): Promise { - return await this.client.call('listaccounts', [{}, verbose, indexedAmounts, isMineOnly], 'number') + return await this.client.call('listaccounts', [pagination, verbose, indexedAmounts, isMineOnly], 'number') } /** * Returns information about account * - * @param owner adress in base58/bech32/hex encoding - * @param pagination - * @param pagination.start - * @param pagination.including_start - * @param pagination.limit - * @param indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {string} owner address in base58/bech32/hex encoding + * @param {AccountPagination} pagination + * @param {string} pagination.start + * @param {boolean} pagination.including_start + * @param {number} pagination.limit + * @param {boolean} indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) * @return Promise */ async getAccount (owner: string, pagination: AccountPagination = { @@ -53,12 +53,13 @@ export class Account { /** * Returns the balances of all accounts that belong to the wallet - * @param pagination - * @param pagination.start - * @param pagination.including_start - * @param pagination.limit - * @param indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @param symbolLookup use token symbols in output, default = false + * + * @param {AccountPagination} pagination + * @param {string} pagination.start + * @param {boolean} pagination.including_start + * @param {number} pagination.limit + * @param {boolean} indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {boolean} symbolLookup use token symbols in output, default = false * @return */ async getTokenBalances ( @@ -74,27 +75,35 @@ export class Account { /** * Returns information about account history * - * @param owner - * @param options - * @param options.maxBlockHeight - * @param options.depth - * @param options.no_rewards - * @param options.token - * @param options.txtype - * @param options.limit + * @param {string=} owner + * @param {AccountHistoryOptions=} options + * @param {number=} options.maxBlockHeight Optional height to iterate from (downto genesis block), (default = chaintip). + * @param {number=} options.depth Maximum depth, from the genesis block is the default + * @param {boolean=} options.no_rewards Filter out rewards + * @param {string=} options.token Filter by token + * @param {string=} options.txtype Filter by transaction type, supported letter from 'CRTMNnpuslrUbBG + * @param {number=} options.limit Maximum number of records to return, 100 by default * @return */ - async listAccountHistory (): Promise { - return await this.client.call('listaccounthistory', [], 'number') + async listAccountHistory (owner?: string, options?: AccountHistoryOptions): Promise { + return await this.client.call('listaccounthistory', [owner, options], 'number') } } export interface AccountPagination { - start: string - including_start: boolean - limit: number + start?: string + including_start?: boolean + limit?: number } // export interface IAccount { - // } + +export interface AccountHistoryOptions { + maxBlockHeight: number + depth: number + no_rewards: boolean + token: string + txtype: string + limit: number +} From 6e104aa1443923dc4ec71f9b1503bdf53f4e5188 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Mon, 26 Apr 2021 17:18:45 +0800 Subject: [PATCH 03/15] test listaccounts --- .../__tests__/category/account.test.ts | 247 +++++++++++++++++- .../src/category/account.ts | 111 ++++---- 2 files changed, 309 insertions(+), 49 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index efb9601477..6253a22546 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.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 { AccountOwner, AccountAmount } from '../../src' describe('non masternode', () => { const container = new RegTestContainer() @@ -25,13 +26,30 @@ describe('non masternode', () => { }) }) -describe.only('masternode', () => { +describe('masternode', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) + async function utxosToAccount (): Promise { + const address = await container.call('getnewaddress') + const payload: { [key: string]: string } = {} + payload[address] = '100@0' + await container.call('utxostoaccount', [payload]) + await container.generate(1) + } + + // async function mintTokens (symbol: string): Promise { + + // await container.call('minttokens', [`2000@${symbol}`]) + + // await container.generate(1) + // } + beforeAll(async () => { await container.start() await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() + await container.waitForWalletBalanceGTE(300) }) afterAll(async () => { @@ -39,11 +57,236 @@ describe.only('masternode', () => { }) describe('listAccounts', () => { + beforeAll(async () => { + await utxosToAccount() + await utxosToAccount() + await utxosToAccount() + }) + it('should listAccounts', async () => { await waitForExpect(async () => { const accounts = await client.account.listAccounts() - console.log('accounts: ', accounts) + expect(accounts.length).toBeGreaterThan(0) + }) + + const accounts = await client.account.listAccounts() + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toBe('string') + expect(typeof account.owner === 'object').toBe(true) + expect(typeof (account.owner as AccountOwner).asm).toBe('string') + expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') + expect((account.owner as AccountOwner).type).toBe('scripthash') + expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) + expect(account.amount).toBe('100.00000000@DFI') + } + }) + + it('should listAccounts with pagination start and including_start', async () => { + let accounts: any[] = [] + + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) }) + + const pagination = { + start: accounts[0].owner.key, + including_start: true + } + + accounts = await client.account.listAccounts(pagination) + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toBe('string') + expect(typeof account.owner === 'object').toBe(true) + expect(typeof (account.owner as AccountOwner).asm).toBe('string') + expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') + expect((account.owner as AccountOwner).type).toBe('scripthash') + expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) + expect(account.amount).toBe('100.00000000@DFI') + } + }) + + it('should listAccounts with pagination.limit', async () => { + let accounts: any[] = [] + + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + const pagination = { + limit: 2 + } + accounts = await client.account.listAccounts(pagination) + expect(accounts.length).toBe(2) + }) + + it('should listAccounts with verbose false', async () => { + await waitForExpect(async () => { + const accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + const accounts = await client.account.listAccounts({}, false) + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toBe('string') + expect(typeof account.owner).toBe('string') + expect(account.amount).toBe('100.00000000@DFI') + } + }) + + it('should listAccounts with indexed_amounts true', async () => { + await waitForExpect(async () => { + const accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + const options = { + indexedAmounts: true + } + + const accounts = await client.account.listAccounts({}, true, options) + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toBe('string') + expect(typeof account.owner === 'object').toBe(true) + expect(typeof (account.owner as AccountOwner).asm).toBe('string') + expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') + expect((account.owner as AccountOwner).type).toBe('scripthash') + expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) + + expect(typeof account.amount === 'object').toBe(true) + expect((account.amount as AccountAmount)['0'].toString()).toBe('100') + } + }) + + it('should listAccounts with isMineOnly true', async () => { + await waitForExpect(async () => { + const accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + const options = { + isMineOnly: true + } + + const accounts = await client.account.listAccounts({}, true, options) + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toBe('string') + expect(typeof account.owner === 'object').toBe(true) + expect(typeof (account.owner as AccountOwner).asm).toBe('string') + expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') + expect((account.owner as AccountOwner).type).toBe('scripthash') + expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) + + expect(typeof account.owner === 'object').toBe(true) + expect(typeof (account.amount as AccountAmount)['0']).toBe('string') + } }) }) + + describe.only('getAccount', () => { + beforeAll(async () => { + await utxosToAccount() + }) + + it.only('should getAccount', async () => { + let accounts: any[] = [] + + await waitForExpect(async () => { + accounts = await client.account.listAccounts({}, false) + expect(accounts.length).toBeGreaterThan(0) + }) + + const account = await client.account.getAccount(accounts[0].owner) + console.log('account: ', account) + // expect(account.length).toBeGreaterThan(0) + // expect(account[0]).toBe('100.00000000@DFI') + }) + + it('should getAccount with pagination start and including_start', async () => { + let accounts: any[] = [] + + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + const pagination = { + start: accounts[0].owner.key, + including_start: true + } + + const account = await client.account.getAccount(accounts[0].owner, pagination) + console.log('account 1: ', account) + + expect(account.length).toBeGreaterThan(0) + expect(account[0]).toBe('100.00000000@DFI') + }) + + it('should getAccount with pagination.limit', async () => { + let accounts: any[] = [] + + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + const pagination = { + limit: 2 + } + const account = await client.account.getAccount(accounts[0].owner, pagination) + console.log('account 2: ', account) + }) + + it('should getAccount with indexedAmount true', async () => { + let accounts: any[] = [] + + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + const options = { + indexedAmounts: true + } + + const account = await client.account.getAccount(accounts[0].owner, {}, options) + console.log('account 3: ', account) + }) + }) + + describe('getTokenBalances', () => { + beforeAll(async () => { + await utxosToAccount() + }) + + it('should getTokenBalances', async () => { + let accounts: any[] = [] + + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + const tokenBalances = await client.account.getTokenBalances(accounts[0].owner) + console.log('tokenBalances: ', tokenBalances) + }) + }) + + // describe('listAccountHistory', () => { + // beforeAll(async () => { + // await utxosToAccount() + // await utxosToAccount() + // await utxosToAccount() + // }) + + // it('should listAccountHistory', async () => { + + // }) + // }) }) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index e91e502506..7b238d34f1 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -13,22 +13,22 @@ export class Account { /** * Returns information about all accounts on chain * - * @param {AccountPagination=} pagination - * @param {string} pagination.start - * @param {boolean} pagination.including_start - * @param {number} pagination.limit - * @param {boolean} verbose default = true, otherwise limited objects are listed - * @param {boolean} indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @param {boolean} isMineOnly get balances about all accounts belonging to the wallet - * @return Promise + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit] + * @param {boolean} [verbose=true] default = true, otherwise limited objects are listed + * @param {ListAccountOptions} [options] default = true, otherwise limited objects are listed + * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {boolean} [options.isMineOnly=false] get balances about all accounts belonging to the wallet + * @return {Promise} */ async listAccounts ( - pagination: AccountPagination = { - // start: '', - including_start: false, - limit: 100 - }, verbose = true, indexedAmounts = false, isMineOnly = false - ): Promise { + pagination: AccountPagination = {}, + verbose = true, + options: ListAccountOptions = {} + ): Promise { + const { indexedAmounts = false, isMineOnly = false } = options return await this.client.call('listaccounts', [pagination, verbose, indexedAmounts, isMineOnly], 'number') } @@ -36,38 +36,32 @@ export class Account { * Returns information about account * * @param {string} owner address in base58/bech32/hex encoding - * @param {AccountPagination} pagination - * @param {string} pagination.start - * @param {boolean} pagination.including_start - * @param {number} pagination.limit - * @param {boolean} indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @return Promise + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit] + * @param {GetAccountOptions} [options] + * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @return {Promise} */ - async getAccount (owner: string, pagination: AccountPagination = { - start: '', - including_start: true, - limit: 100 - }, indexedAmounts = false): Promise { - return await this.client.call('getaccount', [owner, pagination, indexedAmounts], 'number') + async getAccount (owner: string, pagination: AccountPagination = {}, options: GetAccountOptions = {}): Promise { + return await this.client.call('getaccount', [owner, pagination, options], 'number') } /** * Returns the balances of all accounts that belong to the wallet * - * @param {AccountPagination} pagination - * @param {string} pagination.start - * @param {boolean} pagination.including_start - * @param {number} pagination.limit - * @param {boolean} indexedAmounts format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @param {boolean} symbolLookup use token symbols in output, default = false + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit] + * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {boolean} [options] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false * @return */ async getTokenBalances ( - pagination: AccountPagination = { - start: '', - including_start: true, - limit: 100 - }, indexedAmounts = false, symbolLookup = false + pagination: AccountPagination = {}, indexedAmounts = false, symbolLookup = false ): Promise { return await this.client.call('gettokenbalances', [pagination, indexedAmounts, symbolLookup], 'number') } @@ -75,14 +69,14 @@ export class Account { /** * Returns information about account history * - * @param {string=} owner - * @param {AccountHistoryOptions=} options - * @param {number=} options.maxBlockHeight Optional height to iterate from (downto genesis block), (default = chaintip). - * @param {number=} options.depth Maximum depth, from the genesis block is the default - * @param {boolean=} options.no_rewards Filter out rewards - * @param {string=} options.token Filter by token - * @param {string=} options.txtype Filter by transaction type, supported letter from 'CRTMNnpuslrUbBG - * @param {number=} options.limit Maximum number of records to return, 100 by default + * @param {string} [owner] + * @param {AccountHistoryOptions} [options] + * @param {number} [options.maxBlockHeight] Optional height to iterate from (downto genesis block), (default = chaintip). + * @param {number} [options.depth] Maximum depth, from the genesis block is the default + * @param {boolean} [options.no_rewards] Filter out rewards + * @param {string} [options.token] Filter by token + * @param {string} [options.txtype] Filter by transaction type, supported letter from 'CRTMNnpuslrUbBG + * @param {number} [options.limit] Maximum number of records to return, 100 by default * @return */ async listAccountHistory (owner?: string, options?: AccountHistoryOptions): Promise { @@ -96,8 +90,31 @@ export interface AccountPagination { limit?: number } -// export interface IAccount { -// } +export interface AccountResult { + key: string + owner: string | AccountOwner + amount: string | AccountAmount +} + +export interface AccountOwner { + asm: string + reqSigs: number + type: string + addresses: string[] +} + +export interface AccountAmount { + [id: string]: string +} + +export interface ListAccountOptions { + indexedAmounts?: boolean + isMineOnly?: boolean +} + +export interface GetAccountOptions { + indexedAmounts?: boolean +} export interface AccountHistoryOptions { maxBlockHeight: number From a00493f364a54457d1806bf466a5dc1203457160 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Mon, 26 Apr 2021 23:51:36 +0800 Subject: [PATCH 04/15] wip --- .../__tests__/category/account.test.ts | 395 ++++++++++++++---- .../src/category/account.ts | 112 ++++- 2 files changed, 417 insertions(+), 90 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index 6253a22546..d1bea06cc1 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -1,68 +1,71 @@ -import { RegTestContainer, MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../container_adapter_client' import waitForExpect from 'wait-for-expect' -import { AccountOwner, AccountAmount } from '../../src' +import { AccountOwner, AccountAmount, TokenBalances } from '../../src' -describe('non masternode', () => { - const container = new RegTestContainer() +describe('masternode', () => { + const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) beforeAll(async () => { await container.start() await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() + await container.waitForWalletBalanceGTE(300) + + await setup() }) afterAll(async () => { await container.stop() }) - describe('listAccounts', () => { - it('should listAccounts', async () => { - await waitForExpect(async () => { - const accounts = await client.account.listAccounts() - console.log('accounts: ', accounts) - }) - }) - }) -}) + async function setup (): Promise { + const address = await utxosToAccount() -describe('masternode', () => { - const container = new MasterNodeRegTestContainer() - const client = new ContainerAdapterClient(container) + const symbol = 'DLITE' + await createToken(symbol) + await accountToAccount(address, symbol) + } - async function utxosToAccount (): Promise { + async function utxosToAccount (): Promise { const address = await container.call('getnewaddress') + const payload: { [key: string]: string } = {} - payload[address] = '100@0' + payload[address] = '10000@0' await container.call('utxostoaccount', [payload]) + await container.generate(1) - } - // async function mintTokens (symbol: string): Promise { + return address + } - // await container.call('minttokens', [`2000@${symbol}`]) + async function createToken (symbol: string): Promise { + const address = await container.call('getnewaddress') + const metadata = { + symbol, + name: symbol, + isDAT: true, + mintable: true, + tradeable: true, + collateralAddress: address + } + await container.call('createtoken', [metadata]) + await container.generate(1) - // await container.generate(1) - // } + await container.call('minttokens', [`500@${symbol}`]) + await container.generate(1) + } - beforeAll(async () => { - await container.start() - await container.waitForReady() - await container.waitForWalletCoinbaseMaturity() - await container.waitForWalletBalanceGTE(300) - }) + async function accountToAccount (from: string, symbol: string): Promise { + const to = await container.call('getnewaddress') - afterAll(async () => { - await container.stop() - }) + await container.call('accounttoaccount', [from, { [to]: `5@${symbol}` }]) - describe('listAccounts', () => { - beforeAll(async () => { - await utxosToAccount() - await utxosToAccount() - await utxosToAccount() - }) + await container.generate(1) + } + describe.only('listAccounts', () => { it('should listAccounts', async () => { await waitForExpect(async () => { const accounts = await client.account.listAccounts() @@ -78,7 +81,7 @@ describe('masternode', () => { expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') expect((account.owner as AccountOwner).type).toBe('scripthash') expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) - expect(account.amount).toBe('100.00000000@DFI') + expect(typeof account.amount).toBe('string') // eg: 10.00000000@DFI } }) @@ -87,15 +90,18 @@ describe('masternode', () => { await waitForExpect(async () => { accounts = await client.account.listAccounts() + console.log('accounts: ', accounts) expect(accounts.length).toBeGreaterThan(0) }) const pagination = { - start: accounts[0].owner.key, + start: accounts[2].owner.key, including_start: true } accounts = await client.account.listAccounts(pagination) + console.log('accounts 2: ', accounts) + for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') @@ -104,7 +110,7 @@ describe('masternode', () => { expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') expect((account.owner as AccountOwner).type).toBe('scripthash') expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) - expect(account.amount).toBe('100.00000000@DFI') + expect(typeof account.amount).toBe('string') // eg: 10.00000000@DFI } }) @@ -134,7 +140,7 @@ describe('masternode', () => { const account = accounts[i] expect(typeof account.key).toBe('string') expect(typeof account.owner).toBe('string') - expect(account.amount).toBe('100.00000000@DFI') + expect(typeof account.amount).toBe('string') // eg: 10.00000000@DFI } }) @@ -189,23 +195,20 @@ describe('masternode', () => { }) }) - describe.only('getAccount', () => { - beforeAll(async () => { - await utxosToAccount() - }) - - it.only('should getAccount', async () => { + describe('getAccount', () => { + it('should getAccount', async () => { let accounts: any[] = [] await waitForExpect(async () => { - accounts = await client.account.listAccounts({}, false) + accounts = await client.account.listAccounts() expect(accounts.length).toBeGreaterThan(0) }) + console.log('accounts: ', accounts) - const account = await client.account.getAccount(accounts[0].owner) + const account = await client.account.getAccount(accounts[0].owner.addresses[0]) console.log('account: ', account) // expect(account.length).toBeGreaterThan(0) - // expect(account[0]).toBe('100.00000000@DFI') + // expect(account[0]).toBe('10.00000000@DFI') }) it('should getAccount with pagination start and including_start', async () => { @@ -221,11 +224,11 @@ describe('masternode', () => { including_start: true } - const account = await client.account.getAccount(accounts[0].owner, pagination) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) console.log('account 1: ', account) expect(account.length).toBeGreaterThan(0) - expect(account[0]).toBe('100.00000000@DFI') + expect(account[0]).toBe('10.00000000@DFI') }) it('should getAccount with pagination.limit', async () => { @@ -239,7 +242,7 @@ describe('masternode', () => { const pagination = { limit: 2 } - const account = await client.account.getAccount(accounts[0].owner, pagination) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) console.log('account 2: ', account) }) @@ -255,38 +258,286 @@ describe('masternode', () => { indexedAmounts: true } - const account = await client.account.getAccount(accounts[0].owner, {}, options) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, options) console.log('account 3: ', account) + expect(typeof account).toBe('object') + expect(typeof account[0]).toBe('number') }) }) describe('getTokenBalances', () => { - beforeAll(async () => { - await utxosToAccount() + it('should getTokenBalances', async () => { + await waitForExpect(async () => { + const tokenBalances: number[] = await client.account.getTokenBalances() + console.log('tokenBalances: ', tokenBalances) + expect(tokenBalances.length).toBeGreaterThan(0) + expect(tokenBalances[0]).toBe(60) + }) }) - it('should getTokenBalances', async () => { - let accounts: any[] = [] + it('should getTokenBalances with pagination start and including_start', async () => { + await waitForExpect(async () => { + const pagination = { + start: '', + including_start: true + } + const tokenBalances: number[] = await client.account.getTokenBalances(pagination) + console.log('tokenBalances: ', tokenBalances) + // expect(tokenBalances.length).toBeGreaterThan(0) + // expect(tokenBalances[0]).toBe(0) + }) + }) + it('should getTokenBalances with pagination limit', async () => { await waitForExpect(async () => { - accounts = await client.account.listAccounts() - expect(accounts.length).toBeGreaterThan(0) + const pagination = { + limit: 2 + } + const tokenBalances: number[] = await client.account.getTokenBalances(pagination) + console.log('tokenBalances: ', tokenBalances) + // expect(tokenBalances.length).toBeGreaterThan(0) + // expect(tokenBalances[0]).toBe(0) }) + }) - const tokenBalances = await client.account.getTokenBalances(accounts[0].owner) - console.log('tokenBalances: ', tokenBalances) + it('should getTokenBalances with indexedAmounts true', async () => { + await waitForExpect(async () => { + const tokenBalances: TokenBalances = await client.account.getTokenBalances({}, true) + console.log('tokenBalances: ', tokenBalances) + expect(typeof tokenBalances === 'object').toBe(true) + expect(tokenBalances['0']).toBe(60) + }) + }) + + it('should getTokenBalances with symbolLookup', async () => { + await waitForExpect(async () => { + const options = { + symbolLookup: true + } + const tokenBalances: string[] = await client.account.getTokenBalances({}, false, options) + console.log('tokenBalances: ', tokenBalances) + expect(tokenBalances.length).toBeGreaterThan(0) + expect(tokenBalances[0]).toBe('60.00000000@DFI') + }) }) }) - // describe('listAccountHistory', () => { - // beforeAll(async () => { - // await utxosToAccount() - // await utxosToAccount() - // await utxosToAccount() - // }) + describe('listAccountHistory', () => { + it('should listAccountHistory', async () => { + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory() + console.log('accountHistories: ', accountHistories.length) + expect(accountHistories.length).toBeGreaterThan(0) + }) + + const accountHistories = await client.account.listAccountHistory() + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toBe('string') + expect(typeof accountHistory.blockHeight).toBe('number') + expect(typeof accountHistory.blockHash).toBe('string') + expect(typeof accountHistory.blockTime).toBe('number') + expect(typeof accountHistory.type).toBe('string') // UtxosToAccount, sent, receive + expect(typeof accountHistory.txn).toBe('number') + expect(typeof accountHistory.txid).toBe('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toBe('string') // [ '10.00000000@DFI' ] + } + }) + + it('should listAccountHistory with owner "all"', async () => { + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory('all') + console.log('accountHistories: ', accountHistories) + expect(accountHistories.length).toBeGreaterThan(0) + }) - // it('should listAccountHistory', async () => { + const accountHistories = await client.account.listAccountHistory('all') + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toBe('string') + expect(typeof accountHistory.blockHeight).toBe('number') + expect(typeof accountHistory.blockHash).toBe('string') + expect(typeof accountHistory.blockTime).toBe('number') + expect(typeof accountHistory.type).toBe('string') + expect(typeof accountHistory.txn).toBe('number') + expect(typeof accountHistory.txid).toBe('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toBe('string') + } + }) + + it('should listAccountHistory with owner CScript', async () => { + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory() + expect(accountHistories.length).toBeGreaterThan(0) + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toBe('string') + expect(typeof accountHistory.blockHeight).toBe('number') + expect(typeof accountHistory.blockHash).toBe('string') + expect(typeof accountHistory.blockTime).toBe('number') + expect(typeof accountHistory.type).toBe('string') // UtxosToAccount + expect(typeof accountHistory.txn).toBe('number') + expect(typeof accountHistory.txid).toBe('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toBe('number') + } + }) + }) + + it('should listAccountHistory with owner address', async () => { + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory() + expect(accountHistories.length).toBeGreaterThan(0) + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toBe('string') + expect(typeof accountHistory.blockHeight).toBe('number') + expect(typeof accountHistory.blockHash).toBe('string') + expect(typeof accountHistory.blockTime).toBe('number') + expect(typeof accountHistory.type).toBe('string') // UtxosToAccount + expect(typeof accountHistory.txn).toBe('number') + expect(typeof accountHistory.txid).toBe('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toBe('number') + } + }) + }) - // }) - // }) + it('should listAccountHistory with options maxBlockHeight', async () => { + await waitForExpect(async () => { + const options = { + maxBlockHeight: 100 + } + + const accountHistories = await client.account.listAccountHistory('mine', options) + expect(accountHistories.length).toBeGreaterThan(0) + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toBe('string') + expect(typeof accountHistory.blockHeight).toBe('number') + expect(typeof accountHistory.blockHash).toBe('string') + expect(typeof accountHistory.blockTime).toBe('number') + expect(typeof accountHistory.type).toBe('string') // UtxosToAccount + expect(typeof accountHistory.txn).toBe('number') + expect(typeof accountHistory.txid).toBe('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toBe('number') + } + }) + }) + + it('should listAccountHistory with options depth', async () => { + await waitForExpect(async () => { + const options = { + depth: 1 + } + + const accountHistories = await client.account.listAccountHistory('mine', options) + expect(accountHistories.length).toBeGreaterThan(0) + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toBe('string') + expect(typeof accountHistory.blockHeight).toBe('number') + expect(typeof accountHistory.blockHash).toBe('string') + expect(typeof accountHistory.blockTime).toBe('number') + expect(typeof accountHistory.type).toBe('string') // UtxosToAccount + expect(typeof accountHistory.txn).toBe('number') + expect(typeof accountHistory.txid).toBe('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toBe('number') + } + }) + }) + + it('should listAccountHistory with options no_rewards', async () => { + await waitForExpect(async () => { + const options = { + no_rewards: true + } + + const accountHistories = await client.account.listAccountHistory('mine', options) + expect(accountHistories.length).toBeGreaterThan(0) + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toBe('string') + expect(typeof accountHistory.blockHeight).toBe('number') + expect(typeof accountHistory.blockHash).toBe('string') + expect(typeof accountHistory.blockTime).toBe('number') + expect(typeof accountHistory.type).toBe('string') // UtxosToAccount + expect(typeof accountHistory.txn).toBe('number') + expect(typeof accountHistory.txid).toBe('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toBe('number') + } + }) + }) + + it('should listAccountHistory with options token', async () => { + await waitForExpect(async () => { + const options = { + token: '' + } + + const accountHistories = await client.account.listAccountHistory('mine', options) + expect(accountHistories.length).toBeGreaterThan(0) + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toBe('string') + expect(typeof accountHistory.blockHeight).toBe('number') + expect(typeof accountHistory.blockHash).toBe('string') + expect(typeof accountHistory.blockTime).toBe('number') + expect(typeof accountHistory.type).toBe('string') // UtxosToAccount + expect(typeof accountHistory.txn).toBe('number') + expect(typeof accountHistory.txid).toBe('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toBe('number') + } + }) + }) + + it('should listAccountHistory with options txtype', async () => { + await waitForExpect(async () => { + const options = { + txtype: '' + } + + const accountHistories = await client.account.listAccountHistory('mine', options) + expect(accountHistories.length).toBeGreaterThan(0) + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toBe('string') + expect(typeof accountHistory.blockHeight).toBe('number') + expect(typeof accountHistory.blockHash).toBe('string') + expect(typeof accountHistory.blockTime).toBe('number') + expect(typeof accountHistory.type).toBe('string') // UtxosToAccount + expect(typeof accountHistory.txn).toBe('number') + expect(typeof accountHistory.txid).toBe('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toBe('number') + } + }) + }) + + it('should listAccountHistory with options limit', async () => { + await waitForExpect(async () => { + const options = { + limit: 1 + } + + const accountHistories = await client.account.listAccountHistory('mine', options) + expect(accountHistories.length).toBe(1) + }) + }) + }) }) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 7b238d34f1..43a0e789fd 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -44,42 +44,99 @@ export class Account { * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) * @return {Promise} */ - async getAccount (owner: string, pagination: AccountPagination = {}, options: GetAccountOptions = {}): Promise { - return await this.client.call('getaccount', [owner, pagination, options], 'number') + async getAccount (owner: string, pagination: AccountPagination = {}, options: GetAccountOptions = {}): Promise { + const { indexedAmounts = false } = options + return await this.client.call('getaccount', [owner, pagination, indexedAmounts], 'number') } /** * Returns the balances of all accounts that belong to the wallet + * Output format: number[], eg: [60] * * @param {AccountPagination} [pagination] * @param {string} [pagination.start] * @param {boolean} [pagination.including_start] * @param {number} [pagination.limit] * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @param {boolean} [options] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {GetTokenBalancesOptions} [options] * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false - * @return + * @return {Promise} */ - async getTokenBalances ( - pagination: AccountPagination = {}, indexedAmounts = false, symbolLookup = false - ): Promise { + getTokenBalances ( + pagination?: AccountPagination, indexedAmounts?: false, options?: GetTokenBalancesOptions + ): Promise + + /** + * Returns the balances in of all accounts that belong to the wallet + * Output format: {tokenId: amount}, eg: {'0': 60} + * + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit] + * @param {boolean} [indexedAmounts=true] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {GetTokenBalancesOptions} [options] + * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false + * @return {Promise} + */ + getTokenBalances ( + pagination?: AccountPagination, indexedAmounts?: true, options?: GetTokenBalancesOptions, + ): Promise + + /** + * Returns the balances of all accounts that belong to the wallet + * Output format: ['60.00000000@DFI'] + * + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit] + * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {GetTokenBalancesOptions} [options] + * @param {boolean} [options.symbolLookup=true] use token symbols in output, default = false + * @return {Promise} + */ + getTokenBalances ( + pagination?: AccountPagination, indexedAmounts?: false, options?: { symbolLookup: true }, + ): Promise + + /** + * Returns the balances of all accounts that belong to the wallet + * + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit] + * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {GetTokenBalancesOptions} [options] + * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false + * @return {Promise} + */ + async getTokenBalances ( + pagination: AccountPagination = {}, + indexedAmounts = false, + options: GetTokenBalancesOptions = { + symbolLookup: false + } + ): Promise { + const { symbolLookup } = options return await this.client.call('gettokenbalances', [pagination, indexedAmounts, symbolLookup], 'number') } /** * Returns information about account history * - * @param {string} [owner] + * @param {string} [owner='mine'] single account ID (CScript or address) or reserved words 'mine' to list history for all owned accounts or 'all' to list whole DB * @param {AccountHistoryOptions} [options] - * @param {number} [options.maxBlockHeight] Optional height to iterate from (downto genesis block), (default = chaintip). + * @param {number} [options.maxBlockHeight='chaintip'] Optional height to iterate from (downto genesis block), (default = chaintip). * @param {number} [options.depth] Maximum depth, from the genesis block is the default * @param {boolean} [options.no_rewards] Filter out rewards * @param {string} [options.token] Filter by token * @param {string} [options.txtype] Filter by transaction type, supported letter from 'CRTMNnpuslrUbBG - * @param {number} [options.limit] Maximum number of records to return, 100 by default - * @return + * @param {number} [options.limit=100] Maximum number of records to return, 100 by default + * @return {Promise} */ - async listAccountHistory (owner?: string, options?: AccountHistoryOptions): Promise { + async listAccountHistory (owner = 'mine', options: AccountHistoryOptions = {}): Promise { return await this.client.call('listaccounthistory', [owner, options], 'number') } } @@ -116,11 +173,30 @@ export interface GetAccountOptions { indexedAmounts?: boolean } +export interface TokenBalances { + [id: string]: number +} + +export interface GetTokenBalancesOptions { + symbolLookup?: boolean +} + +export interface AccountHistory { + owner: string + blockHeight: number + blockHash: string + blockTime: number + type: string + txn: number + txid: string + amounts: number[] +} + export interface AccountHistoryOptions { - maxBlockHeight: number - depth: number - no_rewards: boolean - token: string - txtype: string - limit: number + maxBlockHeight?: number + depth?: number + no_rewards?: boolean + token?: string + txtype?: string + limit?: number } From 9aaa080319b1776856a83a5347256ebcbcc88886 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Tue, 27 Apr 2021 00:24:21 +0800 Subject: [PATCH 05/15] apply generic type on listaccounts --- .../__tests__/category/account.test.ts | 72 +++++++++---------- .../src/category/account.ts | 10 +-- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index d1bea06cc1..7ae8f8183a 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -32,10 +32,11 @@ describe('masternode', () => { const address = await container.call('getnewaddress') const payload: { [key: string]: string } = {} - payload[address] = '10000@0' + payload[address] = '300@0' await container.call('utxostoaccount', [payload]) await container.generate(1) + await container.call('clearmempool') return address } @@ -52,9 +53,11 @@ describe('masternode', () => { } await container.call('createtoken', [metadata]) await container.generate(1) + await container.call('clearmempool') await container.call('minttokens', [`500@${symbol}`]) await container.generate(1) + await container.call('clearmempool') } async function accountToAccount (from: string, symbol: string): Promise { @@ -63,6 +66,7 @@ describe('masternode', () => { await container.call('accounttoaccount', [from, { [to]: `5@${symbol}` }]) await container.generate(1) + await container.call('clearmempool') } describe.only('listAccounts', () => { @@ -89,7 +93,7 @@ describe('masternode', () => { let accounts: any[] = [] await waitForExpect(async () => { - accounts = await client.account.listAccounts() + accounts = await client.account.listAccounts() console.log('accounts: ', accounts) expect(accounts.length).toBeGreaterThan(0) }) @@ -99,43 +103,38 @@ describe('masternode', () => { including_start: true } - accounts = await client.account.listAccounts(pagination) + accounts = await client.account.listAccounts(pagination) console.log('accounts 2: ', accounts) for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') expect(typeof account.owner === 'object').toBe(true) - expect(typeof (account.owner as AccountOwner).asm).toBe('string') - expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') - expect((account.owner as AccountOwner).type).toBe('scripthash') - expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) + expect(typeof account.owner.asm).toBe('string') + expect(typeof account.owner.reqSigs).toBe('number') + expect(account.owner.type).toBe('scripthash') + expect(account.owner.addresses.length).toBeGreaterThan(0) expect(typeof account.amount).toBe('string') // eg: 10.00000000@DFI } }) it('should listAccounts with pagination.limit', async () => { - let accounts: any[] = [] - await waitForExpect(async () => { - accounts = await client.account.listAccounts() - expect(accounts.length).toBeGreaterThan(0) + const pagination = { + limit: 2 + } + const accounts = await client.account.listAccounts(pagination) + expect(accounts.length).toBe(2) }) - - const pagination = { - limit: 2 - } - accounts = await client.account.listAccounts(pagination) - expect(accounts.length).toBe(2) }) it('should listAccounts with verbose false', async () => { await waitForExpect(async () => { - const accounts = await client.account.listAccounts() + const accounts = await client.account.listAccounts() expect(accounts.length).toBeGreaterThan(0) }) - const accounts = await client.account.listAccounts({}, false) + const accounts = await client.account.listAccounts({}, false) for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') @@ -146,7 +145,7 @@ describe('masternode', () => { it('should listAccounts with indexed_amounts true', async () => { await waitForExpect(async () => { - const accounts = await client.account.listAccounts() + const accounts = await client.account.listAccounts() expect(accounts.length).toBeGreaterThan(0) }) @@ -154,24 +153,24 @@ describe('masternode', () => { indexedAmounts: true } - const accounts = await client.account.listAccounts({}, true, options) + const accounts = await client.account.listAccounts({}, true, options) for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') expect(typeof account.owner === 'object').toBe(true) - expect(typeof (account.owner as AccountOwner).asm).toBe('string') - expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') - expect((account.owner as AccountOwner).type).toBe('scripthash') - expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) + expect(typeof (account.owner).asm).toBe('string') + expect(typeof (account.owner).reqSigs).toBe('number') + expect(account.owner.type).toBe('scripthash') + expect(account.owner.addresses.length).toBeGreaterThan(0) expect(typeof account.amount === 'object').toBe(true) - expect((account.amount as AccountAmount)['0'].toString()).toBe('100') + expect(typeof account.amount['0']).toBe('string') } }) it('should listAccounts with isMineOnly true', async () => { await waitForExpect(async () => { - const accounts = await client.account.listAccounts() + const accounts = await client.account.listAccounts() expect(accounts.length).toBeGreaterThan(0) }) @@ -179,23 +178,22 @@ describe('masternode', () => { isMineOnly: true } - const accounts = await client.account.listAccounts({}, true, options) + const accounts = await client.account.listAccounts({}, true, options) for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') expect(typeof account.owner === 'object').toBe(true) - expect(typeof (account.owner as AccountOwner).asm).toBe('string') - expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') - expect((account.owner as AccountOwner).type).toBe('scripthash') - expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) + expect(typeof (account.owner).asm).toBe('string') + expect(typeof (account.owner).reqSigs).toBe('number') + expect((account.owner).type).toBe('scripthash') + expect((account.owner).addresses.length).toBeGreaterThan(0) - expect(typeof account.owner === 'object').toBe(true) - expect(typeof (account.amount as AccountAmount)['0']).toBe('string') + expect(typeof account.amount).toBe('string') // eg: 10.00000000@DFI } }) }) - describe('getAccount', () => { + describe.skip('getAccount', () => { it('should getAccount', async () => { let accounts: any[] = [] @@ -265,7 +263,7 @@ describe('masternode', () => { }) }) - describe('getTokenBalances', () => { + describe.skip('getTokenBalances', () => { it('should getTokenBalances', async () => { await waitForExpect(async () => { const tokenBalances: number[] = await client.account.getTokenBalances() @@ -322,7 +320,7 @@ describe('masternode', () => { }) }) - describe('listAccountHistory', () => { + describe.skip('listAccountHistory', () => { it('should listAccountHistory', async () => { await waitForExpect(async () => { const accountHistories = await client.account.listAccountHistory() diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 43a0e789fd..5a9e2d9f4c 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -23,11 +23,11 @@ export class Account { * @param {boolean} [options.isMineOnly=false] get balances about all accounts belonging to the wallet * @return {Promise} */ - async listAccounts ( + async listAccounts ( pagination: AccountPagination = {}, verbose = true, options: ListAccountOptions = {} - ): Promise { + ): Promise>> { const { indexedAmounts = false, isMineOnly = false } = options return await this.client.call('listaccounts', [pagination, verbose, indexedAmounts, isMineOnly], 'number') } @@ -147,10 +147,10 @@ export interface AccountPagination { limit?: number } -export interface AccountResult { +export interface AccountResult { key: string - owner: string | AccountOwner - amount: string | AccountAmount + owner: T // string | AccountOwner + amount: U // string | AccountAmount } export interface AccountOwner { From 87fb59e11dd2514fc213d2e5dd1709a4d023bf1b Mon Sep 17 00:00:00 2001 From: websterlcl Date: Tue, 27 Apr 2021 14:36:51 +0800 Subject: [PATCH 06/15] add doc, refine test --- .../__tests__/category/account.test.ts | 300 +++++++----------- .../src/category/account.ts | 4 +- website/docs/jellyfish/api/account.md | 128 ++++++++ 3 files changed, 253 insertions(+), 179 deletions(-) create mode 100644 website/docs/jellyfish/api/account.md diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index 7ae8f8183a..b25286c76e 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -21,27 +21,25 @@ describe('masternode', () => { }) async function setup (): Promise { - const address = await utxosToAccount() + await utxosToAccount(50) + await utxosToAccount(60) + await utxosToAccount(190) - const symbol = 'DLITE' - await createToken(symbol) - await accountToAccount(address, symbol) + const symbol = 'DBTC' + const address = await createToken(symbol, 200) + await accountToAccount(symbol, 5, address) } - async function utxosToAccount (): Promise { + async function utxosToAccount (amount: number): Promise { const address = await container.call('getnewaddress') const payload: { [key: string]: string } = {} - payload[address] = '300@0' + payload[address] = `${amount.toString()}@0` await container.call('utxostoaccount', [payload]) - - await container.generate(1) - await container.call('clearmempool') - - return address + await container.generate(25) } - async function createToken (symbol: string): Promise { + async function createToken (symbol: string, amount: number): Promise { const address = await container.call('getnewaddress') const metadata = { symbol, @@ -52,24 +50,23 @@ describe('masternode', () => { collateralAddress: address } await container.call('createtoken', [metadata]) - await container.generate(1) - await container.call('clearmempool') + await container.generate(25) + + await container.call('minttokens', [`${amount.toString()}@${symbol}`]) + await container.generate(25) - await container.call('minttokens', [`500@${symbol}`]) - await container.generate(1) - await container.call('clearmempool') + return address } - async function accountToAccount (from: string, symbol: string): Promise { + async function accountToAccount (symbol: string, amount: number, from: string): Promise { const to = await container.call('getnewaddress') - await container.call('accounttoaccount', [from, { [to]: `5@${symbol}` }]) + await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) - await container.generate(1) - await container.call('clearmempool') + await container.generate(25) } - describe.only('listAccounts', () => { + describe('listAccounts', () => { it('should listAccounts', async () => { await waitForExpect(async () => { const accounts = await client.account.listAccounts() @@ -85,27 +82,24 @@ describe('masternode', () => { expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') expect((account.owner as AccountOwner).type).toBe('scripthash') expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toBe('string') // eg: 10.00000000@DFI + expect(typeof account.amount).toBe('string') // 10.00000000@DFI } }) - it('should listAccounts with pagination start and including_start', async () => { + it.skip('should listAccounts with pagination start and including_start', async () => { let accounts: any[] = [] await waitForExpect(async () => { accounts = await client.account.listAccounts() - console.log('accounts: ', accounts) expect(accounts.length).toBeGreaterThan(0) }) const pagination = { - start: accounts[2].owner.key, + start: accounts[1].owner.key, including_start: true } accounts = await client.account.listAccounts(pagination) - console.log('accounts 2: ', accounts) - for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') @@ -114,7 +108,7 @@ describe('masternode', () => { expect(typeof account.owner.reqSigs).toBe('number') expect(account.owner.type).toBe('scripthash') expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toBe('string') // eg: 10.00000000@DFI + expect(typeof account.amount).toBe('string') // 10.00000000@DFI } }) @@ -139,7 +133,7 @@ describe('masternode', () => { const account = accounts[i] expect(typeof account.key).toBe('string') expect(typeof account.owner).toBe('string') - expect(typeof account.amount).toBe('string') // eg: 10.00000000@DFI + expect(typeof account.amount).toBe('string') // 10.00000000@DFI } }) @@ -164,7 +158,9 @@ describe('masternode', () => { expect(account.owner.addresses.length).toBeGreaterThan(0) expect(typeof account.amount === 'object').toBe(true) - expect(typeof account.amount['0']).toBe('string') + for (const k in account.amount) { + expect(typeof account.amount[k]).toBe('number') // [{'0': 100}] + } } }) @@ -187,13 +183,12 @@ describe('masternode', () => { expect(typeof (account.owner).reqSigs).toBe('number') expect((account.owner).type).toBe('scripthash') expect((account.owner).addresses.length).toBeGreaterThan(0) - - expect(typeof account.amount).toBe('string') // eg: 10.00000000@DFI + expect(typeof account.amount).toBe('string') // 10.00000000@DFI } }) }) - describe.skip('getAccount', () => { + describe('getAccount', () => { it('should getAccount', async () => { let accounts: any[] = [] @@ -201,15 +196,13 @@ describe('masternode', () => { accounts = await client.account.listAccounts() expect(accounts.length).toBeGreaterThan(0) }) - console.log('accounts: ', accounts) const account = await client.account.getAccount(accounts[0].owner.addresses[0]) - console.log('account: ', account) - // expect(account.length).toBeGreaterThan(0) - // expect(account[0]).toBe('10.00000000@DFI') + expect(account.length).toBeGreaterThan(0) + expect(typeof account[0]).toBe('string') // 10.00000000@DFI' }) - it('should getAccount with pagination start and including_start', async () => { + it.skip('should getAccount with pagination start and including_start', async () => { let accounts: any[] = [] await waitForExpect(async () => { @@ -223,13 +216,11 @@ describe('masternode', () => { } const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) - console.log('account 1: ', account) - expect(account.length).toBeGreaterThan(0) - expect(account[0]).toBe('10.00000000@DFI') + expect(typeof account[0]).toBe('string') // 10.00000000@DFI }) - it('should getAccount with pagination.limit', async () => { + it.skip('should getAccount with pagination.limit', async () => { let accounts: any[] = [] await waitForExpect(async () => { @@ -241,7 +232,7 @@ describe('masternode', () => { limit: 2 } const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) - console.log('account 2: ', account) + expect(account.length).toBe(2) }) it('should getAccount with indexedAmount true', async () => { @@ -257,23 +248,25 @@ describe('masternode', () => { } const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, options) - console.log('account 3: ', account) expect(typeof account).toBe('object') expect(typeof account[0]).toBe('number') }) }) - describe.skip('getTokenBalances', () => { + describe('getTokenBalances', () => { it('should getTokenBalances', async () => { await waitForExpect(async () => { const tokenBalances: number[] = await client.account.getTokenBalances() - console.log('tokenBalances: ', tokenBalances) expect(tokenBalances.length).toBeGreaterThan(0) - expect(tokenBalances[0]).toBe(60) }) + + const tokenBalances: number[] = await client.account.getTokenBalances() + for (let i = 0; i < tokenBalances.length; i += 1) { + expect(typeof tokenBalances[i]).toBe('string') // [ '300.00000000@0', '200.00000000@1' ] + } }) - it('should getTokenBalances with pagination start and including_start', async () => { + it.skip('should getTokenBalances with pagination start and including_start', async () => { await waitForExpect(async () => { const pagination = { start: '', @@ -281,46 +274,43 @@ describe('masternode', () => { } const tokenBalances: number[] = await client.account.getTokenBalances(pagination) console.log('tokenBalances: ', tokenBalances) - // expect(tokenBalances.length).toBeGreaterThan(0) - // expect(tokenBalances[0]).toBe(0) }) }) - it('should getTokenBalances with pagination limit', async () => { + it.skip('should getTokenBalances with pagination limit', async () => { await waitForExpect(async () => { const pagination = { limit: 2 } const tokenBalances: number[] = await client.account.getTokenBalances(pagination) console.log('tokenBalances: ', tokenBalances) - // expect(tokenBalances.length).toBeGreaterThan(0) - // expect(tokenBalances[0]).toBe(0) }) }) it('should getTokenBalances with indexedAmounts true', async () => { await waitForExpect(async () => { const tokenBalances: TokenBalances = await client.account.getTokenBalances({}, true) - console.log('tokenBalances: ', tokenBalances) expect(typeof tokenBalances === 'object').toBe(true) - expect(tokenBalances['0']).toBe(60) }) }) it('should getTokenBalances with symbolLookup', async () => { + const options = { + symbolLookup: true + } await waitForExpect(async () => { - const options = { - symbolLookup: true - } const tokenBalances: string[] = await client.account.getTokenBalances({}, false, options) - console.log('tokenBalances: ', tokenBalances) expect(tokenBalances.length).toBeGreaterThan(0) - expect(tokenBalances[0]).toBe('60.00000000@DFI') }) + + const tokenBalances: string[] = await client.account.getTokenBalances({}, false, options) + for (let i = 0; i < tokenBalances.length; i += 1) { + expect(typeof tokenBalances[i]).toBe('string') // [ '300.00000000@DFI', '200.00000000@DBTC' ] + } }) }) - describe.skip('listAccountHistory', () => { + describe('listAccountHistory', () => { it('should listAccountHistory', async () => { await waitForExpect(async () => { const accountHistories = await client.account.listAccountHistory() @@ -347,7 +337,7 @@ describe('masternode', () => { it('should listAccountHistory with owner "all"', async () => { await waitForExpect(async () => { const accountHistories = await client.account.listAccountHistory('all') - console.log('accountHistories: ', accountHistories) + console.log('accountHistories: ', accountHistories.length) expect(accountHistories.length).toBeGreaterThan(0) }) @@ -367,164 +357,121 @@ describe('masternode', () => { } }) - it('should listAccountHistory with owner CScript', async () => { + it.skip('should listAccountHistory with owner CScript', async () => { await waitForExpect(async () => { const accountHistories = await client.account.listAccountHistory() expect(accountHistories.length).toBeGreaterThan(0) - - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(typeof accountHistory.owner).toBe('string') - expect(typeof accountHistory.blockHeight).toBe('number') - expect(typeof accountHistory.blockHash).toBe('string') - expect(typeof accountHistory.blockTime).toBe('number') - expect(typeof accountHistory.type).toBe('string') // UtxosToAccount - expect(typeof accountHistory.txn).toBe('number') - expect(typeof accountHistory.txid).toBe('string') - expect(accountHistory.amounts.length).toBeGreaterThan(0) - expect(typeof accountHistory.amounts[0]).toBe('number') - } }) + + const accountHistories = await client.account.listAccountHistory() + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toBe('string') + expect(typeof accountHistory.blockHeight).toBe('number') + expect(typeof accountHistory.blockHash).toBe('string') + expect(typeof accountHistory.blockTime).toBe('number') + expect(typeof accountHistory.type).toBe('string') // UtxosToAccount + expect(typeof accountHistory.txn).toBe('number') + expect(typeof accountHistory.txid).toBe('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toBe('number') + } }) it('should listAccountHistory with owner address', async () => { + let address = '' + await waitForExpect(async () => { const accountHistories = await client.account.listAccountHistory() expect(accountHistories.length).toBeGreaterThan(0) - - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(typeof accountHistory.owner).toBe('string') - expect(typeof accountHistory.blockHeight).toBe('number') - expect(typeof accountHistory.blockHash).toBe('string') - expect(typeof accountHistory.blockTime).toBe('number') - expect(typeof accountHistory.type).toBe('string') // UtxosToAccount - expect(typeof accountHistory.txn).toBe('number') - expect(typeof accountHistory.txid).toBe('string') - expect(accountHistory.amounts.length).toBeGreaterThan(0) - expect(typeof accountHistory.amounts[0]).toBe('number') - } + address = accountHistories[0].owner }) + + const accountHistories = await client.account.listAccountHistory(address) + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(accountHistory.owner).toBe(address) + } }) it('should listAccountHistory with options maxBlockHeight', async () => { + const options = { + maxBlockHeight: 80 + } await waitForExpect(async () => { - const options = { - maxBlockHeight: 100 - } - const accountHistories = await client.account.listAccountHistory('mine', options) expect(accountHistories.length).toBeGreaterThan(0) - - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(typeof accountHistory.owner).toBe('string') - expect(typeof accountHistory.blockHeight).toBe('number') - expect(typeof accountHistory.blockHash).toBe('string') - expect(typeof accountHistory.blockTime).toBe('number') - expect(typeof accountHistory.type).toBe('string') // UtxosToAccount - expect(typeof accountHistory.txn).toBe('number') - expect(typeof accountHistory.txid).toBe('string') - expect(accountHistory.amounts.length).toBeGreaterThan(0) - expect(typeof accountHistory.amounts[0]).toBe('number') - } }) + + const accountHistories = await client.account.listAccountHistory('mine', options) + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(accountHistory.blockHeight).toBeLessThanOrEqual(80) + } }) - it('should listAccountHistory with options depth', async () => { + it.skip('should listAccountHistory with options depth', async () => { + const options = { + depth: 6 + } await waitForExpect(async () => { - const options = { - depth: 1 - } - const accountHistories = await client.account.listAccountHistory('mine', options) + console.log('accountHistories: ', accountHistories) expect(accountHistories.length).toBeGreaterThan(0) - - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(typeof accountHistory.owner).toBe('string') - expect(typeof accountHistory.blockHeight).toBe('number') - expect(typeof accountHistory.blockHash).toBe('string') - expect(typeof accountHistory.blockTime).toBe('number') - expect(typeof accountHistory.type).toBe('string') // UtxosToAccount - expect(typeof accountHistory.txn).toBe('number') - expect(typeof accountHistory.txid).toBe('string') - expect(accountHistory.amounts.length).toBeGreaterThan(0) - expect(typeof accountHistory.amounts[0]).toBe('number') - } }) }) it('should listAccountHistory with options no_rewards', async () => { + const options = { + no_rewards: true + } await waitForExpect(async () => { - const options = { - no_rewards: true - } - const accountHistories = await client.account.listAccountHistory('mine', options) expect(accountHistories.length).toBeGreaterThan(0) - - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(typeof accountHistory.owner).toBe('string') - expect(typeof accountHistory.blockHeight).toBe('number') - expect(typeof accountHistory.blockHash).toBe('string') - expect(typeof accountHistory.blockTime).toBe('number') - expect(typeof accountHistory.type).toBe('string') // UtxosToAccount - expect(typeof accountHistory.txn).toBe('number') - expect(typeof accountHistory.txid).toBe('string') - expect(accountHistory.amounts.length).toBeGreaterThan(0) - expect(typeof accountHistory.amounts[0]).toBe('number') - } }) + + const accountHistories = await client.account.listAccountHistory('mine', options) + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(accountHistory.txn).not.toBe('blockReward') + } }) it('should listAccountHistory with options token', async () => { + const options = { + token: 'DBTC' + } await waitForExpect(async () => { - const options = { - token: '' - } - const accountHistories = await client.account.listAccountHistory('mine', options) expect(accountHistories.length).toBeGreaterThan(0) + }) - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(typeof accountHistory.owner).toBe('string') - expect(typeof accountHistory.blockHeight).toBe('number') - expect(typeof accountHistory.blockHash).toBe('string') - expect(typeof accountHistory.blockTime).toBe('number') - expect(typeof accountHistory.type).toBe('string') // UtxosToAccount - expect(typeof accountHistory.txn).toBe('number') - expect(typeof accountHistory.txid).toBe('string') - expect(accountHistory.amounts.length).toBeGreaterThan(0) - expect(typeof accountHistory.amounts[0]).toBe('number') + const accountHistories = await client.account.listAccountHistory('mine', options) + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(accountHistory.amounts.length).toBeGreaterThan(0) + for (let j = 0; j < accountHistory.amounts.length; j += 1) { + const amount = accountHistory.amounts[j] + const symbol = amount.split('@')[1] + expect(symbol).toBe('DBTC') } - }) + } }) - it('should listAccountHistory with options txtype', async () => { + it.skip('should listAccountHistory with options txtype', async () => { + const options = { + txtype: 'AccountToAccount' // receive, sent, blockreward, AccountToAccount, UtxosToAccount, MintToken + } await waitForExpect(async () => { - const options = { - txtype: '' - } - const accountHistories = await client.account.listAccountHistory('mine', options) expect(accountHistories.length).toBeGreaterThan(0) - - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(typeof accountHistory.owner).toBe('string') - expect(typeof accountHistory.blockHeight).toBe('number') - expect(typeof accountHistory.blockHash).toBe('string') - expect(typeof accountHistory.blockTime).toBe('number') - expect(typeof accountHistory.type).toBe('string') // UtxosToAccount - expect(typeof accountHistory.txn).toBe('number') - expect(typeof accountHistory.txid).toBe('string') - expect(accountHistory.amounts.length).toBeGreaterThan(0) - expect(typeof accountHistory.amounts[0]).toBe('number') - } }) + + const accountHistories = await client.account.listAccountHistory('mine', options) + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(accountHistory.type).toBe('AccountToAccount') + } }) it('should listAccountHistory with options limit', async () => { @@ -532,7 +479,6 @@ describe('masternode', () => { const options = { limit: 1 } - const accountHistories = await client.account.listAccountHistory('mine', options) expect(accountHistories.length).toBe(1) }) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 5a9e2d9f4c..f80e7f6c25 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -161,7 +161,7 @@ export interface AccountOwner { } export interface AccountAmount { - [id: string]: string + [id: string]: number } export interface ListAccountOptions { @@ -189,7 +189,7 @@ export interface AccountHistory { type: string txn: number txid: string - amounts: number[] + amounts: string[] } export interface AccountHistoryOptions { diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md new file mode 100644 index 0000000000..688fe55553 --- /dev/null +++ b/website/docs/jellyfish/api/account.md @@ -0,0 +1,128 @@ +--- +id: account +title: Account API +sidebar_label: Account API +slug: /jellyfish/api/account +--- + +```js +import {Client} from '@defichain/jellyfish' +const client = new Client() +// Using client.account. +const something = await client.account.method() +``` + +## listPoolPairs + +Returns information about all accounts on chain + +```ts title="client.account.listAccounts()" +interface account { + async listAccounts ( + pagination: AccountPagination = {}, + verbose = true, + options: ListAccountOptions = {} + ): Promise>> +} + +interface AccountPagination { + start?: string + including_start?: boolean + limit?: number +} + +interface AccountResult { + key: string + owner: T // string | AccountOwner + amount: U // string | AccountAmount +} + +interface AccountOwner { + asm: string + reqSigs: number + type: string + addresses: string[] +} + +interface AccountAmount { + [id: string]: number +} + +interface ListAccountOptions { + indexedAmounts?: boolean + isMineOnly?: boolean +} +``` + +## getAccount + +Returns information about account + +```ts title="client.account.getAccount()" +interface account { + async getAccount (owner: string, pagination: AccountPagination = {}, options: GetAccountOptions = {}): Promise +} +``` + +## getTokenBalances + +Returns the balances of all accounts that belong to the wallet + +```ts title="client.account.getTokenBalances()" +interface account { + getTokenBalances (pagination?: AccountPagination, indexedAmounts?: false, options?: GetTokenBalancesOptions): Promise + getTokenBalances (pagination?: AccountPagination, indexedAmounts?: true, options?: GetTokenBalancesOptions): Promise + getTokenBalances (pagination?: AccountPagination, indexedAmounts?: false, options?: { symbolLookup: true }): Promise + async getTokenBalances ( + pagination: AccountPagination = {}, + indexedAmounts = false, + options: GetTokenBalancesOptions = { + symbolLookup: false + } + ): Promise +} + +interface TokenBalances { + [id: string]: number +} + +interface AccountPagination { + start?: string + including_start?: boolean + limit?: number +} + +interface GetTokenBalancesOptions { + symbolLookup?: boolean +} +``` + +## listAccountHistory + +Returns information about account history + +```ts title="client.account.listAccountHistory()" +interface account { + async listAccountHistory (owner = 'mine', options: AccountHistoryOptions = {}): Promise +} + +interface AccountHistory { + owner: string + blockHeight: number + blockHash: string + blockTime: number + type: string + txn: number + txid: string + amounts: string[] +} + +interface AccountHistoryOptions { + maxBlockHeight?: number + depth?: number + no_rewards?: boolean + token?: string + txtype?: string + limit?: number +} +``` \ No newline at end of file From 65cd4a4fa6d25f7c068a21c3077af1f8c1a73f78 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Tue, 27 Apr 2021 16:13:55 +0800 Subject: [PATCH 07/15] update doc and clean code --- .../__tests__/category/account.test.ts | 102 +++++++++--------- .../src/category/account.ts | 38 ++++--- website/docs/jellyfish/api/account.md | 34 ++++-- 3 files changed, 95 insertions(+), 79 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index b25286c76e..b203215674 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -1,7 +1,6 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../container_adapter_client' import waitForExpect from 'wait-for-expect' -import { AccountOwner, AccountAmount, TokenBalances } from '../../src' describe('masternode', () => { const container = new MasterNodeRegTestContainer() @@ -69,28 +68,30 @@ describe('masternode', () => { describe('listAccounts', () => { it('should listAccounts', async () => { await waitForExpect(async () => { - const accounts = await client.account.listAccounts() + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) expect(accounts.length).toBeGreaterThan(0) }) - const accounts = await client.account.listAccounts() + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) + for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') expect(typeof account.owner === 'object').toBe(true) - expect(typeof (account.owner as AccountOwner).asm).toBe('string') - expect(typeof (account.owner as AccountOwner).reqSigs).toBe('number') - expect((account.owner as AccountOwner).type).toBe('scripthash') - expect((account.owner as AccountOwner).addresses.length).toBeGreaterThan(0) + expect(typeof account.owner.asm).toBe('string') + expect(typeof account.owner.reqSigs).toBe('number') + expect(account.owner.type).toBe('scripthash') + expect(account.owner.addresses.length).toBeGreaterThan(0) expect(typeof account.amount).toBe('string') // 10.00000000@DFI } }) + // TODO(canonbrother): the index seems not working, total records 3, offset 2 expect 1 but return 3 it.skip('should listAccounts with pagination start and including_start', async () => { let accounts: any[] = [] await waitForExpect(async () => { - accounts = await client.account.listAccounts() + accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) expect(accounts.length).toBeGreaterThan(0) }) @@ -99,7 +100,8 @@ describe('masternode', () => { including_start: true } - accounts = await client.account.listAccounts(pagination) + accounts = await client.account.listAccounts(pagination, true, { indexedAmounts: false, isMineOnly: false }) + for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') @@ -117,18 +119,18 @@ describe('masternode', () => { const pagination = { limit: 2 } - const accounts = await client.account.listAccounts(pagination) + const accounts = await client.account.listAccounts(pagination, true, { indexedAmounts: false, isMineOnly: false }) expect(accounts.length).toBe(2) }) }) it('should listAccounts with verbose false', async () => { await waitForExpect(async () => { - const accounts = await client.account.listAccounts() + const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) expect(accounts.length).toBeGreaterThan(0) }) - const accounts = await client.account.listAccounts({}, false) + const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') @@ -139,15 +141,11 @@ describe('masternode', () => { it('should listAccounts with indexed_amounts true', async () => { await waitForExpect(async () => { - const accounts = await client.account.listAccounts() + const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) expect(accounts.length).toBeGreaterThan(0) }) - const options = { - indexedAmounts: true - } - - const accounts = await client.account.listAccounts({}, true, options) + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: true, isMineOnly: false }) for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') @@ -166,15 +164,11 @@ describe('masternode', () => { it('should listAccounts with isMineOnly true', async () => { await waitForExpect(async () => { - const accounts = await client.account.listAccounts() + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) expect(accounts.length).toBeGreaterThan(0) }) - const options = { - isMineOnly: true - } - - const accounts = await client.account.listAccounts({}, true, options) + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') @@ -193,20 +187,21 @@ describe('masternode', () => { let accounts: any[] = [] await waitForExpect(async () => { - accounts = await client.account.listAccounts() + accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) expect(accounts.length).toBeGreaterThan(0) }) - const account = await client.account.getAccount(accounts[0].owner.addresses[0]) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: false }) expect(account.length).toBeGreaterThan(0) expect(typeof account[0]).toBe('string') // 10.00000000@DFI' }) + // TODO(canonbrother): assume the pagination on test only works while getAccount return [amountA, amountB] but [amountA] it.skip('should getAccount with pagination start and including_start', async () => { let accounts: any[] = [] await waitForExpect(async () => { - accounts = await client.account.listAccounts() + accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) expect(accounts.length).toBeGreaterThan(0) }) @@ -215,23 +210,24 @@ describe('masternode', () => { including_start: true } - const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination, { indexedAmounts: false }) expect(account.length).toBeGreaterThan(0) expect(typeof account[0]).toBe('string') // 10.00000000@DFI }) + // TODO(canonbrother): assume the pagination on test only works while getAccount return [amountA, amountB] but [amountA] it.skip('should getAccount with pagination.limit', async () => { let accounts: any[] = [] await waitForExpect(async () => { - accounts = await client.account.listAccounts() + accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) expect(accounts.length).toBeGreaterThan(0) }) const pagination = { limit: 2 } - const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination, { indexedAmounts: false }) expect(account.length).toBe(2) }) @@ -239,28 +235,26 @@ describe('masternode', () => { let accounts: any[] = [] await waitForExpect(async () => { - accounts = await client.account.listAccounts() + accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) expect(accounts.length).toBeGreaterThan(0) }) - const options = { - indexedAmounts: true - } - - const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, options) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: true }) expect(typeof account).toBe('object') - expect(typeof account[0]).toBe('number') + for (const k in account) { + expect(typeof account[k]).toBe('number') + } }) }) describe('getTokenBalances', () => { it('should getTokenBalances', async () => { await waitForExpect(async () => { - const tokenBalances: number[] = await client.account.getTokenBalances() + const tokenBalances: number[] = await client.account.getTokenBalances({}, false, { symbolLookup: false }) expect(tokenBalances.length).toBeGreaterThan(0) }) - const tokenBalances: number[] = await client.account.getTokenBalances() + const tokenBalances: number[] = await client.account.getTokenBalances({}, false, { symbolLookup: false }) for (let i = 0; i < tokenBalances.length; i += 1) { expect(typeof tokenBalances[i]).toBe('string') // [ '300.00000000@0', '200.00000000@1' ] } @@ -272,38 +266,37 @@ describe('masternode', () => { start: '', including_start: true } - const tokenBalances: number[] = await client.account.getTokenBalances(pagination) + const tokenBalances: number[] = await client.account.getTokenBalances(pagination, false, { symbolLookup: false }) console.log('tokenBalances: ', tokenBalances) }) }) - it.skip('should getTokenBalances with pagination limit', async () => { + it('should getTokenBalances with pagination limit', async () => { await waitForExpect(async () => { - const pagination = { - limit: 2 - } - const tokenBalances: number[] = await client.account.getTokenBalances(pagination) - console.log('tokenBalances: ', tokenBalances) + const tokenBalances: number[] = await client.account.getTokenBalances({}, false, { symbolLookup: false }) + expect(tokenBalances.length).toBe(2) }) + const pagination = { + limit: 1 + } + const tokenBalances: number[] = await client.account.getTokenBalances(pagination, false, { symbolLookup: false }) + expect(tokenBalances.length).toBe(1) }) it('should getTokenBalances with indexedAmounts true', async () => { await waitForExpect(async () => { - const tokenBalances: TokenBalances = await client.account.getTokenBalances({}, true) + const tokenBalances = await client.account.getTokenBalances({}, true, { symbolLookup: false }) expect(typeof tokenBalances === 'object').toBe(true) }) }) it('should getTokenBalances with symbolLookup', async () => { - const options = { - symbolLookup: true - } await waitForExpect(async () => { - const tokenBalances: string[] = await client.account.getTokenBalances({}, false, options) + const tokenBalances: string[] = await client.account.getTokenBalances({}, false, { symbolLookup: true }) expect(tokenBalances.length).toBeGreaterThan(0) }) - const tokenBalances: string[] = await client.account.getTokenBalances({}, false, options) + const tokenBalances: string[] = await client.account.getTokenBalances({}, false, { symbolLookup: true }) for (let i = 0; i < tokenBalances.length; i += 1) { expect(typeof tokenBalances[i]).toBe('string') // [ '300.00000000@DFI', '200.00000000@DBTC' ] } @@ -314,7 +307,6 @@ describe('masternode', () => { it('should listAccountHistory', async () => { await waitForExpect(async () => { const accountHistories = await client.account.listAccountHistory() - console.log('accountHistories: ', accountHistories.length) expect(accountHistories.length).toBeGreaterThan(0) }) @@ -337,7 +329,6 @@ describe('masternode', () => { it('should listAccountHistory with owner "all"', async () => { await waitForExpect(async () => { const accountHistories = await client.account.listAccountHistory('all') - console.log('accountHistories: ', accountHistories.length) expect(accountHistories.length).toBeGreaterThan(0) }) @@ -357,6 +348,7 @@ describe('masternode', () => { } }) + // TODO(canonbrother): generate CScript it.skip('should listAccountHistory with owner CScript', async () => { await waitForExpect(async () => { const accountHistories = await client.account.listAccountHistory() @@ -410,6 +402,7 @@ describe('masternode', () => { } }) + // TODO(canonbrother): check the confirmation?? it.skip('should listAccountHistory with options depth', async () => { const options = { depth: 6 @@ -458,6 +451,7 @@ describe('masternode', () => { } }) + // TODO(canonbrother): filter by txtype but its not working it.skip('should listAccountHistory with options txtype', async () => { const options = { txtype: 'AccountToAccount' // receive, sent, blockreward, AccountToAccount, UtxosToAccount, MintToken diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index f80e7f6c25..919e0c023e 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -1,5 +1,7 @@ import { ApiClient } from '../.' +type OwnerType = 'mine' | 'all' | string + /** * Account related RPC calls for DeFiChain */ @@ -21,8 +23,16 @@ export class Account { * @param {ListAccountOptions} [options] default = true, otherwise limited objects are listed * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) * @param {boolean} [options.isMineOnly=false] get balances about all accounts belonging to the wallet - * @return {Promise} + * @return {Promise>>} */ + listAccounts (pagination: AccountPagination, verbose: true, options: {indexedAmounts: false, isMineOnly: boolean}): Promise>> + + listAccounts (pagination: AccountPagination, verbose: false, options: {indexedAmounts: false, isMineOnly: boolean}): Promise>> + + listAccounts (pagination: AccountPagination, verbose: true, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> + + listAccounts (pagination: AccountPagination, verbose: false, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> + async listAccounts ( pagination: AccountPagination = {}, verbose = true, @@ -42,9 +52,13 @@ export class Account { * @param {number} [pagination.limit] * @param {GetAccountOptions} [options] * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @return {Promise} + * @return {Promise | AccountAmount} */ - async getAccount (owner: string, pagination: AccountPagination = {}, options: GetAccountOptions = {}): Promise { + getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: false }): Promise + + getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: true }): Promise + + async getAccount (owner: string, pagination: AccountPagination = {}, options: GetAccountOptions = {}): Promise { const { indexedAmounts = false } = options return await this.client.call('getaccount', [owner, pagination, indexedAmounts], 'number') } @@ -62,9 +76,7 @@ export class Account { * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false * @return {Promise} */ - getTokenBalances ( - pagination?: AccountPagination, indexedAmounts?: false, options?: GetTokenBalancesOptions - ): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise /** * Returns the balances in of all accounts that belong to the wallet @@ -79,9 +91,7 @@ export class Account { * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false * @return {Promise} */ - getTokenBalances ( - pagination?: AccountPagination, indexedAmounts?: true, options?: GetTokenBalancesOptions, - ): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise /** * Returns the balances of all accounts that belong to the wallet @@ -96,9 +106,7 @@ export class Account { * @param {boolean} [options.symbolLookup=true] use token symbols in output, default = false * @return {Promise} */ - getTokenBalances ( - pagination?: AccountPagination, indexedAmounts?: false, options?: { symbolLookup: true }, - ): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: true }): Promise /** * Returns the balances of all accounts that belong to the wallet @@ -112,13 +120,13 @@ export class Account { * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false * @return {Promise} */ - async getTokenBalances ( + async getTokenBalances ( pagination: AccountPagination = {}, indexedAmounts = false, options: GetTokenBalancesOptions = { symbolLookup: false } - ): Promise { + ): Promise { const { symbolLookup } = options return await this.client.call('gettokenbalances', [pagination, indexedAmounts, symbolLookup], 'number') } @@ -136,7 +144,7 @@ export class Account { * @param {number} [options.limit=100] Maximum number of records to return, 100 by default * @return {Promise} */ - async listAccountHistory (owner = 'mine', options: AccountHistoryOptions = {}): Promise { + async listAccountHistory (owner: OwnerType = 'mine', options: AccountHistoryOptions = {}): Promise { return await this.client.call('listaccounthistory', [owner, options], 'number') } } diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index 688fe55553..b59db618ae 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -18,7 +18,11 @@ Returns information about all accounts on chain ```ts title="client.account.listAccounts()" interface account { - async listAccounts ( + listAccounts (pagination: AccountPagination, verbose: true, options: {indexedAmounts: false, isMineOnly: boolean}): Promise>> + listAccounts (pagination: AccountPagination, verbose: false, options: {indexedAmounts: false, isMineOnly: boolean}): Promise>> + listAccounts (pagination: AccountPagination, verbose: true, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> + listAccounts (pagination: AccountPagination, verbose: false, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> + listAccounts ( pagination: AccountPagination = {}, verbose = true, options: ListAccountOptions = {} @@ -33,8 +37,8 @@ interface AccountPagination { interface AccountResult { key: string - owner: T // string | AccountOwner - amount: U // string | AccountAmount + owner: T // string | AccountOwner (if verbose true) + amount: U // string | AccountAmount (if options.indexedAmounts true) } interface AccountOwner { @@ -60,7 +64,15 @@ Returns information about account ```ts title="client.account.getAccount()" interface account { - async getAccount (owner: string, pagination: AccountPagination = {}, options: GetAccountOptions = {}): Promise + getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: false }): Promise + getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: true }): Promise + getAccount ( + owner: string, + pagination: AccountPagination = {}, + options: GetAccountOptions = { + indexedAmounts: false, + }, + ): Promise } ``` @@ -70,16 +82,16 @@ Returns the balances of all accounts that belong to the wallet ```ts title="client.account.getTokenBalances()" interface account { - getTokenBalances (pagination?: AccountPagination, indexedAmounts?: false, options?: GetTokenBalancesOptions): Promise - getTokenBalances (pagination?: AccountPagination, indexedAmounts?: true, options?: GetTokenBalancesOptions): Promise - getTokenBalances (pagination?: AccountPagination, indexedAmounts?: false, options?: { symbolLookup: true }): Promise - async getTokenBalances ( + getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: true }): Promise + getTokenBalances ( pagination: AccountPagination = {}, indexedAmounts = false, options: GetTokenBalancesOptions = { symbolLookup: false } - ): Promise + ): Promise } interface TokenBalances { @@ -103,9 +115,11 @@ Returns information about account history ```ts title="client.account.listAccountHistory()" interface account { - async listAccountHistory (owner = 'mine', options: AccountHistoryOptions = {}): Promise + listAccountHistory (owner: OwnerType = 'mine', options: AccountHistoryOptions = {}): Promise } +type OwnerType = 'mine' | 'all' | string + interface AccountHistory { owner: string blockHeight: number From d32f4a6782c57416d274bff5df586d51bcb13ac1 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Tue, 27 Apr 2021 16:34:08 +0800 Subject: [PATCH 08/15] clean --- .../src/category/account.ts | 48 ++++--------------- 1 file changed, 8 insertions(+), 40 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 919e0c023e..d96cdb86a3 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -54,8 +54,11 @@ export class Account { * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) * @return {Promise | AccountAmount} */ + + // ['60.00000000@DFI'] getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: false }): Promise + // {'0': 60} getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: true }): Promise async getAccount (owner: string, pagination: AccountPagination = {}, options: GetAccountOptions = {}): Promise { @@ -65,7 +68,6 @@ export class Account { /** * Returns the balances of all accounts that belong to the wallet - * Output format: number[], eg: [60] * * @param {AccountPagination} [pagination] * @param {string} [pagination.start] @@ -74,52 +76,18 @@ export class Account { * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) * @param {GetTokenBalancesOptions} [options] * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false - * @return {Promise} + * @return {Promise} */ + + // [60] getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise - /** - * Returns the balances in of all accounts that belong to the wallet - * Output format: {tokenId: amount}, eg: {'0': 60} - * - * @param {AccountPagination} [pagination] - * @param {string} [pagination.start] - * @param {boolean} [pagination.including_start] - * @param {number} [pagination.limit] - * @param {boolean} [indexedAmounts=true] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @param {GetTokenBalancesOptions} [options] - * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false - * @return {Promise} - */ + // {'0': 60} getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise - /** - * Returns the balances of all accounts that belong to the wallet - * Output format: ['60.00000000@DFI'] - * - * @param {AccountPagination} [pagination] - * @param {string} [pagination.start] - * @param {boolean} [pagination.including_start] - * @param {number} [pagination.limit] - * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @param {GetTokenBalancesOptions} [options] - * @param {boolean} [options.symbolLookup=true] use token symbols in output, default = false - * @return {Promise} - */ + // ['60.00000000@DFI'] getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: true }): Promise - /** - * Returns the balances of all accounts that belong to the wallet - * - * @param {AccountPagination} [pagination] - * @param {string} [pagination.start] - * @param {boolean} [pagination.including_start] - * @param {number} [pagination.limit] - * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @param {GetTokenBalancesOptions} [options] - * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false - * @return {Promise} - */ async getTokenBalances ( pagination: AccountPagination = {}, indexedAmounts = false, From 9a714bc5e49d3ad5e09cd9f73a1bf53d4384c81c Mon Sep 17 00:00:00 2001 From: websterlcl Date: Wed, 28 Apr 2021 11:38:48 +0800 Subject: [PATCH 09/15] overload getTokenBalances, making getaccount return 2 amounts fixture correctly --- .../__tests__/category/account.test.ts | 147 +++++++++--------- .../src/category/account.ts | 25 +-- website/docs/jellyfish/api/account.md | 17 +- 3 files changed, 96 insertions(+), 93 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index b203215674..ffe346b03b 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -24,9 +24,14 @@ describe('masternode', () => { await utxosToAccount(60) await utxosToAccount(190) - const symbol = 'DBTC' - const address = await createToken(symbol, 200) - await accountToAccount(symbol, 5, address) + const from = await container.call('getnewaddress') + await createToken(from, 'DBTC', 200) + + const to = await accountToAccount('DBTC', 5, from) + await accountToAccount('DBTC', 18, from, to) + + await createToken(from, 'DETH', 200) + await accountToAccount('DETH', 46, from) } async function utxosToAccount (amount: number): Promise { @@ -34,12 +39,10 @@ describe('masternode', () => { const payload: { [key: string]: string } = {} payload[address] = `${amount.toString()}@0` - await container.call('utxostoaccount', [payload]) await container.generate(25) } - async function createToken (symbol: string, amount: number): Promise { - const address = await container.call('getnewaddress') + async function createToken (address: string, symbol: string, amount: number): Promise { const metadata = { symbol, name: symbol, @@ -53,16 +56,15 @@ describe('masternode', () => { await container.call('minttokens', [`${amount.toString()}@${symbol}`]) await container.generate(25) - - return address } - async function accountToAccount (symbol: string, amount: number, from: string): Promise { - const to = await container.call('getnewaddress') + async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise { + const to = _to !== '' ? _to : await container.call('getnewaddress') await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) - await container.generate(25) + + return to } describe('listAccounts', () => { @@ -86,8 +88,7 @@ describe('masternode', () => { } }) - // TODO(canonbrother): the index seems not working, total records 3, offset 2 expect 1 but return 3 - it.skip('should listAccounts with pagination start and including_start', async () => { + it('should listAccounts with pagination start and including_start', async () => { let accounts: any[] = [] await waitForExpect(async () => { @@ -96,22 +97,12 @@ describe('masternode', () => { }) const pagination = { - start: accounts[1].owner.key, + start: accounts[accounts.length - 1].key, including_start: true } - accounts = await client.account.listAccounts(pagination, true, { indexedAmounts: false, isMineOnly: false }) - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toBe('string') - expect(typeof account.owner === 'object').toBe(true) - expect(typeof account.owner.asm).toBe('string') - expect(typeof account.owner.reqSigs).toBe('number') - expect(account.owner.type).toBe('scripthash') - expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toBe('string') // 10.00000000@DFI - } + const lastAccounts = await client.account.listAccounts(pagination, true, { indexedAmounts: false, isMineOnly: false }) + expect(lastAccounts.length).toBe(1) }) it('should listAccounts with pagination.limit', async () => { @@ -127,10 +118,12 @@ describe('masternode', () => { it('should listAccounts with verbose false', async () => { await waitForExpect(async () => { const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) + expect(accounts.length).toBeGreaterThan(0) }) const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) + for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') @@ -142,10 +135,12 @@ describe('masternode', () => { it('should listAccounts with indexed_amounts true', async () => { await waitForExpect(async () => { const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) + expect(accounts.length).toBeGreaterThan(0) }) const accounts = await client.account.listAccounts({}, true, { indexedAmounts: true, isMineOnly: false }) + for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') @@ -165,10 +160,12 @@ describe('masternode', () => { it('should listAccounts with isMineOnly true', async () => { await waitForExpect(async () => { const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) + expect(accounts.length).toBeGreaterThan(0) }) const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) + for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] expect(typeof account.key).toBe('string') @@ -191,32 +188,38 @@ describe('masternode', () => { expect(accounts.length).toBeGreaterThan(0) }) - const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: false }) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: false }) // [ '187.00000000@DBTC', '154.00000000@DETH' ] expect(account.length).toBeGreaterThan(0) - expect(typeof account[0]).toBe('string') // 10.00000000@DFI' + for (let i = 0; i < account.length; i += 1) { + expect(typeof account[i]).toBe('string') + } }) - // TODO(canonbrother): assume the pagination on test only works while getAccount return [amountA, amountB] but [amountA] - it.skip('should getAccount with pagination start and including_start', async () => { + it('should getAccount with pagination start and including_start', async () => { let accounts: any[] = [] + let beforeAccountCount = 0 await waitForExpect(async () => { accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) expect(accounts.length).toBeGreaterThan(0) + + const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: false }) + beforeAccountCount = account.length }) const pagination = { - start: accounts[0].owner.key, + start: beforeAccountCount, including_start: true } const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination, { indexedAmounts: false }) - expect(account.length).toBeGreaterThan(0) - expect(typeof account[0]).toBe('string') // 10.00000000@DFI + expect(account.length).toBeGreaterThan(beforeAccountCount - 1) + for (let i = 0; i < account.length; i += 1) { + expect(typeof account[i]).toBe('string') + } }) - // TODO(canonbrother): assume the pagination on test only works while getAccount return [amountA, amountB] but [amountA] - it.skip('should getAccount with pagination.limit', async () => { + it('should getAccount with pagination.limit', async () => { let accounts: any[] = [] await waitForExpect(async () => { @@ -225,10 +228,10 @@ describe('masternode', () => { }) const pagination = { - limit: 2 + limit: 1 } const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination, { indexedAmounts: false }) - expect(account.length).toBe(2) + expect(account.length).toBe(1) }) it('should getAccount with indexedAmount true', async () => { @@ -250,36 +253,43 @@ describe('masternode', () => { describe('getTokenBalances', () => { it('should getTokenBalances', async () => { await waitForExpect(async () => { - const tokenBalances: number[] = await client.account.getTokenBalances({}, false, { symbolLookup: false }) + const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: false }) expect(tokenBalances.length).toBeGreaterThan(0) }) - const tokenBalances: number[] = await client.account.getTokenBalances({}, false, { symbolLookup: false }) + const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: false }) for (let i = 0; i < tokenBalances.length; i += 1) { expect(typeof tokenBalances[i]).toBe('string') // [ '300.00000000@0', '200.00000000@1' ] } }) - it.skip('should getTokenBalances with pagination start and including_start', async () => { + it('should getTokenBalances with pagination start and including_start, id is number', async () => { + let id = '' + await waitForExpect(async () => { - const pagination = { - start: '', - including_start: true - } - const tokenBalances: number[] = await client.account.getTokenBalances(pagination, false, { symbolLookup: false }) - console.log('tokenBalances: ', tokenBalances) + const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: false }) // [ '300.00000000@0', '200.00000000@1' ] + expect(tokenBalances.length).toBeGreaterThan(0) + + id = tokenBalances[tokenBalances.length - 1].split('@')[1] }) + + const pagination = { + start: Number(id), + including_start: true + } + const tokenBalances = await client.account.getTokenBalances(pagination, false, { symbolLookup: false }) + expect(tokenBalances.length).toBe(1) }) it('should getTokenBalances with pagination limit', async () => { await waitForExpect(async () => { - const tokenBalances: number[] = await client.account.getTokenBalances({}, false, { symbolLookup: false }) + const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: false }) expect(tokenBalances.length).toBe(2) }) const pagination = { limit: 1 } - const tokenBalances: number[] = await client.account.getTokenBalances(pagination, false, { symbolLookup: false }) + const tokenBalances = await client.account.getTokenBalances(pagination, false, { symbolLookup: false }) expect(tokenBalances.length).toBe(1) }) @@ -292,11 +302,11 @@ describe('masternode', () => { it('should getTokenBalances with symbolLookup', async () => { await waitForExpect(async () => { - const tokenBalances: string[] = await client.account.getTokenBalances({}, false, { symbolLookup: true }) + const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: true }) expect(tokenBalances.length).toBeGreaterThan(0) }) - const tokenBalances: string[] = await client.account.getTokenBalances({}, false, { symbolLookup: true }) + const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: true }) for (let i = 0; i < tokenBalances.length; i += 1) { expect(typeof tokenBalances[i]).toBe('string') // [ '300.00000000@DFI', '200.00000000@DBTC' ] } @@ -348,25 +358,22 @@ describe('masternode', () => { } }) - // TODO(canonbrother): generate CScript - it.skip('should listAccountHistory with owner CScript', async () => { + it('should listAccountHistory with owner CScript', async () => { + let accounts: any[] = [] + await waitForExpect(async () => { - const accountHistories = await client.account.listAccountHistory() - expect(accountHistories.length).toBeGreaterThan(0) + accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) + expect(accounts.length).toBeGreaterThan(0) }) - const accountHistories = await client.account.listAccountHistory() + const { owner } = accounts[0] + const { hex, addresses } = owner + + const accountHistories = await client.account.listAccountHistory(hex) + for (let i = 0; i < accountHistories.length; i += 1) { const accountHistory = accountHistories[i] - expect(typeof accountHistory.owner).toBe('string') - expect(typeof accountHistory.blockHeight).toBe('number') - expect(typeof accountHistory.blockHash).toBe('string') - expect(typeof accountHistory.blockTime).toBe('number') - expect(typeof accountHistory.type).toBe('string') // UtxosToAccount - expect(typeof accountHistory.txn).toBe('number') - expect(typeof accountHistory.txid).toBe('string') - expect(accountHistory.amounts.length).toBeGreaterThan(0) - expect(typeof accountHistory.amounts[0]).toBe('number') + expect(addresses.includes(accountHistory.owner)).toBe(true) } }) @@ -402,15 +409,11 @@ describe('masternode', () => { } }) - // TODO(canonbrother): check the confirmation?? - it.skip('should listAccountHistory with options depth', async () => { - const options = { - depth: 6 - } + it('should listAccountHistory with options depth', async () => { await waitForExpect(async () => { - const accountHistories = await client.account.listAccountHistory('mine', options) - console.log('accountHistories: ', accountHistories) - expect(accountHistories.length).toBeGreaterThan(0) + const depth = 10 + const accountHistories = await client.account.listAccountHistory('mine', { depth }) + expect(accountHistories.length).toBe(depth + 1) // include zero index }) }) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index d96cdb86a3..bcec8d263b 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -47,7 +47,7 @@ export class Account { * * @param {string} owner address in base58/bech32/hex encoding * @param {AccountPagination} [pagination] - * @param {string} [pagination.start] + * @param {string | number} [pagination.start] * @param {boolean} [pagination.including_start] * @param {number} [pagination.limit] * @param {GetAccountOptions} [options] @@ -79,22 +79,23 @@ export class Account { * @return {Promise} */ - // [60] - getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise + // [ '300.00000000@0', '200.00000000@1' ] + getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise - // {'0': 60} + // { '0': 300, '1': 200 } getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise - // ['60.00000000@DFI'] + // [ '300.00000000@DFI', '200.00000000@DBTC' ] getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: true }): Promise + // { DFI: 300, DBTC: 200 } + getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: true }): Promise + async getTokenBalances ( - pagination: AccountPagination = {}, - indexedAmounts = false, - options: GetTokenBalancesOptions = { - symbolLookup: false - } - ): Promise { + pagination: AccountPagination, + indexedAmounts: boolean, + options: GetTokenBalancesOptions + ): Promise { const { symbolLookup } = options return await this.client.call('gettokenbalances', [pagination, indexedAmounts, symbolLookup], 'number') } @@ -118,7 +119,7 @@ export class Account { } export interface AccountPagination { - start?: string + start?: string | number including_start?: boolean limit?: number } diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index b59db618ae..855d33980e 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -30,7 +30,7 @@ interface account { } interface AccountPagination { - start?: string + start?: string | number including_start?: boolean limit?: number } @@ -82,16 +82,15 @@ Returns the balances of all accounts that belong to the wallet ```ts title="client.account.getTokenBalances()" interface account { - getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: true }): Promise - getTokenBalances ( - pagination: AccountPagination = {}, - indexedAmounts = false, - options: GetTokenBalancesOptions = { - symbolLookup: false - } - ): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: true }): Promise + async getTokenBalances ( + pagination: AccountPagination, + indexedAmounts: boolean, + options: GetTokenBalancesOptions, + ): Promise { } interface TokenBalances { From 498ea24b9acaa5f6f990b695a64059568f3bf6b0 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Wed, 28 Apr 2021 12:07:42 +0800 Subject: [PATCH 10/15] update account.md --- .../jellyfish-api-core/__tests__/category/account.test.ts | 2 +- website/docs/jellyfish/api/account.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index ffe346b03b..1c5a977fc9 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -454,7 +454,7 @@ describe('masternode', () => { } }) - // TODO(canonbrother): filter by txtype but its not working + // TODO(canonbrother): require customTx it.skip('should listAccountHistory with options txtype', async () => { const options = { txtype: 'AccountToAccount' // receive, sent, blockreward, AccountToAccount, UtxosToAccount, MintToken diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index 855d33980e..c71ac9fc30 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -90,7 +90,7 @@ interface account { pagination: AccountPagination, indexedAmounts: boolean, options: GetTokenBalancesOptions, - ): Promise { + ): Promise } interface TokenBalances { @@ -98,7 +98,7 @@ interface TokenBalances { } interface AccountPagination { - start?: string + start?: string | number including_start?: boolean limit?: number } From f076f138c7dfc796adbbd825f4ec4f26baf56c46 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Wed, 28 Apr 2021 20:23:32 +0800 Subject: [PATCH 11/15] update account in sidebar --- website/sidebars.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/website/sidebars.js b/website/sidebars.js index 2748b9b58e..40b8a0a040 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -13,7 +13,8 @@ module.exports = { 'jellyfish/api/rawtx', 'jellyfish/api/wallet', 'jellyfish/api/poolpair', - 'jellyfish/api/token' + 'jellyfish/api/token', + 'jellyfish/api/account' ] } ], From efb31acaa2872cf13a317ef50e4eceebcecaa7f2 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Wed, 28 Apr 2021 23:47:41 +0800 Subject: [PATCH 12/15] test listaccounthistory filter by txtype, add defaults on params, update account.md --- .../__tests__/category/account.test.ts | 16 ++++----- .../src/category/account.ts | 33 ++++++++++++------- website/docs/jellyfish/api/account.md | 27 ++++++++------- 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index 1c5a977fc9..25ef3eb0c3 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -213,7 +213,7 @@ describe('masternode', () => { } const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination, { indexedAmounts: false }) - expect(account.length).toBeGreaterThan(beforeAccountCount - 1) + expect(account.length).toBe(1) for (let i = 0; i < account.length; i += 1) { expect(typeof account[i]).toBe('string') } @@ -413,7 +413,7 @@ describe('masternode', () => { await waitForExpect(async () => { const depth = 10 const accountHistories = await client.account.listAccountHistory('mine', { depth }) - expect(accountHistories.length).toBe(depth + 1) // include zero index + expect(accountHistories.length).toBe(depth + 1) // plus 1 to include zero index }) }) @@ -454,20 +454,16 @@ describe('masternode', () => { } }) - // TODO(canonbrother): require customTx - it.skip('should listAccountHistory with options txtype', async () => { - const options = { - txtype: 'AccountToAccount' // receive, sent, blockreward, AccountToAccount, UtxosToAccount, MintToken - } + it('should listAccountHistory with options txtype', async () => { await waitForExpect(async () => { - const accountHistories = await client.account.listAccountHistory('mine', options) + const accountHistories = await client.account.listAccountHistory('mine', { txtype: 'M' }) expect(accountHistories.length).toBeGreaterThan(0) }) - const accountHistories = await client.account.listAccountHistory('mine', options) + const accountHistories = await client.account.listAccountHistory('mine', { txtype: 'M' }) for (let i = 0; i < accountHistories.length; i += 1) { const accountHistory = accountHistories[i] - expect(accountHistory.type).toBe('AccountToAccount') + expect(accountHistory.type).toBe('MintToken') } }) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index bcec8d263b..9c3ce9d03e 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -18,7 +18,7 @@ export class Account { * @param {AccountPagination} [pagination] * @param {string} [pagination.start] * @param {boolean} [pagination.including_start] - * @param {number} [pagination.limit] + * @param {number} [pagination.limit=100] * @param {boolean} [verbose=true] default = true, otherwise limited objects are listed * @param {ListAccountOptions} [options] default = true, otherwise limited objects are listed * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) @@ -34,9 +34,9 @@ export class Account { listAccounts (pagination: AccountPagination, verbose: false, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> async listAccounts ( - pagination: AccountPagination = {}, + pagination: AccountPagination = { limit: 100 }, verbose = true, - options: ListAccountOptions = {} + options: ListAccountOptions = { indexedAmounts: false, isMineOnly: false } ): Promise>> { const { indexedAmounts = false, isMineOnly = false } = options return await this.client.call('listaccounts', [pagination, verbose, indexedAmounts, isMineOnly], 'number') @@ -49,7 +49,7 @@ export class Account { * @param {AccountPagination} [pagination] * @param {string | number} [pagination.start] * @param {boolean} [pagination.including_start] - * @param {number} [pagination.limit] + * @param {number} [pagination.limit=100] * @param {GetAccountOptions} [options] * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) * @return {Promise | AccountAmount} @@ -61,7 +61,11 @@ export class Account { // {'0': 60} getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: true }): Promise - async getAccount (owner: string, pagination: AccountPagination = {}, options: GetAccountOptions = {}): Promise { + async getAccount ( + owner: string, + pagination: AccountPagination = { limit: 100 }, + options: GetAccountOptions = { indexedAmounts: false } + ): Promise { const { indexedAmounts = false } = options return await this.client.call('getaccount', [owner, pagination, indexedAmounts], 'number') } @@ -72,7 +76,7 @@ export class Account { * @param {AccountPagination} [pagination] * @param {string} [pagination.start] * @param {boolean} [pagination.including_start] - * @param {number} [pagination.limit] + * @param {number} [pagination.limit=100] * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) * @param {GetTokenBalancesOptions} [options] * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false @@ -92,9 +96,9 @@ export class Account { getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: true }): Promise async getTokenBalances ( - pagination: AccountPagination, - indexedAmounts: boolean, - options: GetTokenBalancesOptions + pagination: AccountPagination = { limit: 100 }, + indexedAmounts = false, + options: GetTokenBalancesOptions = { symbolLookup: false } ): Promise { const { symbolLookup } = options return await this.client.call('gettokenbalances', [pagination, indexedAmounts, symbolLookup], 'number') @@ -103,9 +107,9 @@ export class Account { /** * Returns information about account history * - * @param {string} [owner='mine'] single account ID (CScript or address) or reserved words 'mine' to list history for all owned accounts or 'all' to list whole DB + * @param {OwnerType} [owner='mine'] single account ID (CScript or address) or reserved words 'mine' to list history for all owned accounts or 'all' to list whole DB * @param {AccountHistoryOptions} [options] - * @param {number} [options.maxBlockHeight='chaintip'] Optional height to iterate from (downto genesis block), (default = chaintip). + * @param {number} [options.maxBlockHeight] Optional height to iterate from (downto genesis block), (default = chaintip). * @param {number} [options.depth] Maximum depth, from the genesis block is the default * @param {boolean} [options.no_rewards] Filter out rewards * @param {string} [options.token] Filter by token @@ -113,7 +117,12 @@ export class Account { * @param {number} [options.limit=100] Maximum number of records to return, 100 by default * @return {Promise} */ - async listAccountHistory (owner: OwnerType = 'mine', options: AccountHistoryOptions = {}): Promise { + async listAccountHistory ( + owner: OwnerType = 'mine', + options: AccountHistoryOptions = { + limit: 100 + } + ): Promise { return await this.client.call('listaccounthistory', [owner, options], 'number') } } diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index c71ac9fc30..c4915dcf54 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -23,10 +23,10 @@ interface account { listAccounts (pagination: AccountPagination, verbose: true, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> listAccounts (pagination: AccountPagination, verbose: false, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> listAccounts ( - pagination: AccountPagination = {}, + pagination: AccountPagination = { limit: 100 }, verbose = true, - options: ListAccountOptions = {} - ): Promise>> + options: ListAccountOptions = { indexedAmounts: false, isMineOnly: false } + ): Promise>> { } interface AccountPagination { @@ -68,10 +68,8 @@ interface account { getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: true }): Promise getAccount ( owner: string, - pagination: AccountPagination = {}, - options: GetAccountOptions = { - indexedAmounts: false, - }, + pagination: AccountPagination = { limit: 100 }, + options: GetAccountOptions = { indexedAmounts: false } ): Promise } ``` @@ -86,10 +84,10 @@ interface account { getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: true }): Promise getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: true }): Promise - async getTokenBalances ( - pagination: AccountPagination, - indexedAmounts: boolean, - options: GetTokenBalancesOptions, + getTokenBalances ( + pagination: AccountPagination = { limit: 100 }, + indexedAmounts = false, + options: GetTokenBalancesOptions = { symbolLookup: false} ): Promise } @@ -114,7 +112,12 @@ Returns information about account history ```ts title="client.account.listAccountHistory()" interface account { - listAccountHistory (owner: OwnerType = 'mine', options: AccountHistoryOptions = {}): Promise + async listAccountHistory ( + owner: OwnerType = 'mine', + options: AccountHistoryOptions = { + limit: 100 + } + ): Promise } type OwnerType = 'mine' | 'all' | string From ce01e5b636834cf9727f407274493248926c2ccc Mon Sep 17 00:00:00 2001 From: websterlcl Date: Thu, 29 Apr 2021 08:43:54 +0800 Subject: [PATCH 13/15] set amount type to bignumber --- .../__tests__/category/account.test.ts | 33 +++++++++++-------- .../src/category/account.ts | 19 +++++------ website/docs/jellyfish/api/account.md | 14 ++++---- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index 25ef3eb0c3..8a3a0d32d6 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -1,6 +1,7 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../container_adapter_client' import waitForExpect from 'wait-for-expect' +import BigNumber from 'bignumber.js' describe('masternode', () => { const container = new MasterNodeRegTestContainer() @@ -81,8 +82,8 @@ describe('masternode', () => { expect(typeof account.key).toBe('string') expect(typeof account.owner === 'object').toBe(true) expect(typeof account.owner.asm).toBe('string') - expect(typeof account.owner.reqSigs).toBe('number') - expect(account.owner.type).toBe('scripthash') + expect(account.owner.reqSigs instanceof BigNumber).toBe(true) + expect(typeof account.owner.type).toBe('string') expect(account.owner.addresses.length).toBeGreaterThan(0) expect(typeof account.amount).toBe('string') // 10.00000000@DFI } @@ -145,14 +146,14 @@ describe('masternode', () => { const account = accounts[i] expect(typeof account.key).toBe('string') expect(typeof account.owner === 'object').toBe(true) - expect(typeof (account.owner).asm).toBe('string') - expect(typeof (account.owner).reqSigs).toBe('number') - expect(account.owner.type).toBe('scripthash') + expect(typeof account.owner.asm).toBe('string') + expect(account.owner.reqSigs instanceof BigNumber).toBe(true) + expect(typeof account.owner.type).toBe('string') expect(account.owner.addresses.length).toBeGreaterThan(0) expect(typeof account.amount === 'object').toBe(true) for (const k in account.amount) { - expect(typeof account.amount[k]).toBe('number') // [{'0': 100}] + expect(account.amount[k] instanceof BigNumber).toBe(true) // [{'0': 100}] } } }) @@ -170,10 +171,10 @@ describe('masternode', () => { const account = accounts[i] expect(typeof account.key).toBe('string') expect(typeof account.owner === 'object').toBe(true) - expect(typeof (account.owner).asm).toBe('string') - expect(typeof (account.owner).reqSigs).toBe('number') - expect((account.owner).type).toBe('scripthash') - expect((account.owner).addresses.length).toBeGreaterThan(0) + expect(typeof account.owner.asm).toBe('string') + expect(account.owner.reqSigs instanceof BigNumber).toBe(true) + expect(typeof account.owner.type).toBe('string') + expect(account.owner.addresses.length).toBeGreaterThan(0) expect(typeof account.amount).toBe('string') // 10.00000000@DFI } }) @@ -188,7 +189,8 @@ describe('masternode', () => { expect(accounts.length).toBeGreaterThan(0) }) - const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: false }) // [ '187.00000000@DBTC', '154.00000000@DETH' ] + // [ '187.00000000@DBTC', '154.00000000@DETH' ] + const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: false }) expect(account.length).toBeGreaterThan(0) for (let i = 0; i < account.length; i += 1) { expect(typeof account[i]).toBe('string') @@ -212,8 +214,10 @@ describe('masternode', () => { including_start: true } + // [ '187.00000000@DBTC', '154.00000000@DETH' ] const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination, { indexedAmounts: false }) expect(account.length).toBe(1) + for (let i = 0; i < account.length; i += 1) { expect(typeof account[i]).toBe('string') } @@ -263,7 +267,7 @@ describe('masternode', () => { } }) - it('should getTokenBalances with pagination start and including_start, id is number', async () => { + it('should getTokenBalances with pagination start and including_start', async () => { let id = '' await waitForExpect(async () => { @@ -297,6 +301,9 @@ describe('masternode', () => { await waitForExpect(async () => { const tokenBalances = await client.account.getTokenBalances({}, true, { symbolLookup: false }) expect(typeof tokenBalances === 'object').toBe(true) + for (const k in tokenBalances) { + expect(tokenBalances[k] instanceof BigNumber).toBe(true) + } }) }) @@ -328,7 +335,7 @@ describe('masternode', () => { expect(typeof accountHistory.blockHeight).toBe('number') expect(typeof accountHistory.blockHash).toBe('string') expect(typeof accountHistory.blockTime).toBe('number') - expect(typeof accountHistory.type).toBe('string') // UtxosToAccount, sent, receive + expect(typeof accountHistory.type).toBe('string') expect(typeof accountHistory.txn).toBe('number') expect(typeof accountHistory.txid).toBe('string') expect(accountHistory.amounts.length).toBeGreaterThan(0) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 9c3ce9d03e..6a35410587 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -1,3 +1,4 @@ +import BigNumber from 'bignumber.js' import { ApiClient } from '../.' type OwnerType = 'mine' | 'all' | string @@ -39,7 +40,7 @@ export class Account { options: ListAccountOptions = { indexedAmounts: false, isMineOnly: false } ): Promise>> { const { indexedAmounts = false, isMineOnly = false } = options - return await this.client.call('listaccounts', [pagination, verbose, indexedAmounts, isMineOnly], 'number') + return await this.client.call('listaccounts', [pagination, verbose, indexedAmounts, isMineOnly], 'bignumber') } /** @@ -87,21 +88,21 @@ export class Account { getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise // { '0': 300, '1': 200 } - getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise // [ '300.00000000@DFI', '200.00000000@DBTC' ] getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: true }): Promise // { DFI: 300, DBTC: 200 } - getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: true }): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: true }): Promise async getTokenBalances ( pagination: AccountPagination = { limit: 100 }, indexedAmounts = false, options: GetTokenBalancesOptions = { symbolLookup: false } - ): Promise { + ): Promise { const { symbolLookup } = options - return await this.client.call('gettokenbalances', [pagination, indexedAmounts, symbolLookup], 'number') + return await this.client.call('gettokenbalances', [pagination, indexedAmounts, symbolLookup], 'bignumber') } /** @@ -141,13 +142,13 @@ export interface AccountResult { export interface AccountOwner { asm: string - reqSigs: number + reqSigs: BigNumber type: string addresses: string[] } export interface AccountAmount { - [id: string]: number + [id: string]: BigNumber } export interface ListAccountOptions { @@ -159,10 +160,6 @@ export interface GetAccountOptions { indexedAmounts?: boolean } -export interface TokenBalances { - [id: string]: number -} - export interface GetTokenBalancesOptions { symbolLookup?: boolean } diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index c4915dcf54..966530f536 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -26,7 +26,7 @@ interface account { pagination: AccountPagination = { limit: 100 }, verbose = true, options: ListAccountOptions = { indexedAmounts: false, isMineOnly: false } - ): Promise>> { + ): Promise>> } interface AccountPagination { @@ -43,13 +43,13 @@ interface AccountResult { interface AccountOwner { asm: string - reqSigs: number + reqSigs: BigNumber type: string addresses: string[] } interface AccountAmount { - [id: string]: number + [id: string]: BigNumber } interface ListAccountOptions { @@ -81,9 +81,9 @@ Returns the balances of all accounts that belong to the wallet ```ts title="client.account.getTokenBalances()" interface account { getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise - getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: true }): Promise - getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: true }): Promise + getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: true }): Promise getTokenBalances ( pagination: AccountPagination = { limit: 100 }, indexedAmounts = false, @@ -91,8 +91,8 @@ interface account { ): Promise } -interface TokenBalances { - [id: string]: number +interface AccountAmount { + [id: string]: BigNumber } interface AccountPagination { From ba53570ed9eeee70b97eeb804fe02425a11301d5 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Fri, 30 Apr 2021 16:22:04 +0800 Subject: [PATCH 14/15] test: replace utxostoaccount by container.generate(100), extract listaccounts as a func(), rpc: use optional params instead of defaut to fix coverage issue --- .../__tests__/category/account.test.ts | 145 +++++++++--------- .../src/category/account.ts | 132 +++++++++++++--- website/docs/jellyfish/api/account.md | 10 +- 3 files changed, 188 insertions(+), 99 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index 8a3a0d32d6..5bb58dd16f 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -21,9 +21,7 @@ describe('masternode', () => { }) async function setup (): Promise { - await utxosToAccount(50) - await utxosToAccount(60) - await utxosToAccount(190) + await container.generate(100) const from = await container.call('getnewaddress') await createToken(from, 'DBTC', 200) @@ -35,14 +33,6 @@ describe('masternode', () => { await accountToAccount('DETH', 46, from) } - async function utxosToAccount (amount: number): Promise { - const address = await container.call('getnewaddress') - - const payload: { [key: string]: string } = {} - payload[address] = `${amount.toString()}@0` - await container.generate(25) - } - async function createToken (address: string, symbol: string, amount: number): Promise { const metadata = { symbol, @@ -68,14 +58,22 @@ describe('masternode', () => { return to } + async function waitForListingAccounts (): Promise { + let accounts: any[] = [] + + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + return accounts + } + describe('listAccounts', () => { it('should listAccounts', async () => { - await waitForExpect(async () => { - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) - expect(accounts.length).toBeGreaterThan(0) - }) + await waitForListingAccounts() - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) + const accounts = await client.account.listAccounts() for (let i = 0; i < accounts.length; i += 1) { const account = accounts[i] @@ -90,19 +88,14 @@ describe('masternode', () => { }) it('should listAccounts with pagination start and including_start', async () => { - let accounts: any[] = [] - - await waitForExpect(async () => { - accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) - expect(accounts.length).toBeGreaterThan(0) - }) + const accounts = await waitForListingAccounts() const pagination = { start: accounts[accounts.length - 1].key, including_start: true } - const lastAccounts = await client.account.listAccounts(pagination, true, { indexedAmounts: false, isMineOnly: false }) + const lastAccounts = await client.account.listAccounts(pagination) expect(lastAccounts.length).toBe(1) }) @@ -111,17 +104,13 @@ describe('masternode', () => { const pagination = { limit: 2 } - const accounts = await client.account.listAccounts(pagination, true, { indexedAmounts: false, isMineOnly: false }) + const accounts = await client.account.listAccounts(pagination) expect(accounts.length).toBe(2) }) }) - it('should listAccounts with verbose false', async () => { - await waitForExpect(async () => { - const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) - - expect(accounts.length).toBeGreaterThan(0) - }) + it('should listAccounts with verbose false and indexed_amounts false', async () => { + await waitForListingAccounts() const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) @@ -133,12 +122,25 @@ describe('masternode', () => { } }) - it('should listAccounts with indexed_amounts true', async () => { - await waitForExpect(async () => { - const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) + it('should listAccounts with verbose false and indexed_amounts true', async () => { + await waitForListingAccounts() - expect(accounts.length).toBeGreaterThan(0) - }) + const accounts = await client.account.listAccounts({}, false, { indexedAmounts: true, isMineOnly: false }) + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toBe('string') + expect(typeof account.owner).toBe('string') + + expect(typeof account.amount === 'object').toBe(true) + for (const k in account.amount) { + expect(account.amount[k] instanceof BigNumber).toBe(true) // [{'0': 100}] + } + } + }) + + it('should listAccounts with verbose true and indexed_amounts true', async () => { + await waitForListingAccounts() const accounts = await client.account.listAccounts({}, true, { indexedAmounts: true, isMineOnly: false }) @@ -158,12 +160,25 @@ describe('masternode', () => { } }) - it('should listAccounts with isMineOnly true', async () => { - await waitForExpect(async () => { - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) + it('should listAccounts with verbose true and indexed_amounts false', async () => { + await waitForListingAccounts() - expect(accounts.length).toBeGreaterThan(0) - }) + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toBe('string') + expect(typeof account.owner === 'object').toBe(true) + expect(typeof account.owner.asm).toBe('string') + expect(account.owner.reqSigs instanceof BigNumber).toBe(true) + expect(typeof account.owner.type).toBe('string') + expect(account.owner.addresses.length).toBeGreaterThan(0) + expect(typeof account.amount).toBe('string') // 10.00000000@DFI + } + }) + + it('should listAccounts with isMineOnly true', async () => { + await waitForListingAccounts() const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) @@ -182,15 +197,10 @@ describe('masternode', () => { describe('getAccount', () => { it('should getAccount', async () => { - let accounts: any[] = [] - - await waitForExpect(async () => { - accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) - expect(accounts.length).toBeGreaterThan(0) - }) + const accounts = await waitForListingAccounts() // [ '187.00000000@DBTC', '154.00000000@DETH' ] - const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: false }) + const account = await client.account.getAccount(accounts[0].owner.addresses[0]) expect(account.length).toBeGreaterThan(0) for (let i = 0; i < account.length; i += 1) { expect(typeof account[i]).toBe('string') @@ -202,10 +212,10 @@ describe('masternode', () => { let beforeAccountCount = 0 await waitForExpect(async () => { - accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) + accounts = await client.account.listAccounts() expect(accounts.length).toBeGreaterThan(0) - const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: false }) + const account = await client.account.getAccount(accounts[0].owner.addresses[0]) beforeAccountCount = account.length }) @@ -215,7 +225,7 @@ describe('masternode', () => { } // [ '187.00000000@DBTC', '154.00000000@DETH' ] - const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination, { indexedAmounts: false }) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) expect(account.length).toBe(1) for (let i = 0; i < account.length; i += 1) { @@ -224,27 +234,17 @@ describe('masternode', () => { }) it('should getAccount with pagination.limit', async () => { - let accounts: any[] = [] - - await waitForExpect(async () => { - accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) - expect(accounts.length).toBeGreaterThan(0) - }) + const accounts = await waitForListingAccounts() const pagination = { limit: 1 } - const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination, { indexedAmounts: false }) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) expect(account.length).toBe(1) }) it('should getAccount with indexedAmount true', async () => { - let accounts: any[] = [] - - await waitForExpect(async () => { - accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) - expect(accounts.length).toBeGreaterThan(0) - }) + const accounts = await waitForListingAccounts() const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: true }) expect(typeof account).toBe('object') @@ -257,11 +257,11 @@ describe('masternode', () => { describe('getTokenBalances', () => { it('should getTokenBalances', async () => { await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: false }) + const tokenBalances = await client.account.getTokenBalances() expect(tokenBalances.length).toBeGreaterThan(0) }) - const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: false }) + const tokenBalances = await client.account.getTokenBalances() for (let i = 0; i < tokenBalances.length; i += 1) { expect(typeof tokenBalances[i]).toBe('string') // [ '300.00000000@0', '200.00000000@1' ] } @@ -271,7 +271,7 @@ describe('masternode', () => { let id = '' await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: false }) // [ '300.00000000@0', '200.00000000@1' ] + const tokenBalances = await client.account.getTokenBalances() // [ '300.00000000@0', '200.00000000@1' ] expect(tokenBalances.length).toBeGreaterThan(0) id = tokenBalances[tokenBalances.length - 1].split('@')[1] @@ -281,19 +281,19 @@ describe('masternode', () => { start: Number(id), including_start: true } - const tokenBalances = await client.account.getTokenBalances(pagination, false, { symbolLookup: false }) + const tokenBalances = await client.account.getTokenBalances(pagination) expect(tokenBalances.length).toBe(1) }) it('should getTokenBalances with pagination limit', async () => { await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: false }) + const tokenBalances = await client.account.getTokenBalances() expect(tokenBalances.length).toBe(2) }) const pagination = { limit: 1 } - const tokenBalances = await client.account.getTokenBalances(pagination, false, { symbolLookup: false }) + const tokenBalances = await client.account.getTokenBalances(pagination) expect(tokenBalances.length).toBe(1) }) @@ -366,12 +366,7 @@ describe('masternode', () => { }) it('should listAccountHistory with owner CScript', async () => { - let accounts: any[] = [] - - await waitForExpect(async () => { - accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) - expect(accounts.length).toBeGreaterThan(0) - }) + const accounts = await waitForListingAccounts() const { owner } = accounts[0] const { hex, addresses } = owner diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 6a35410587..477ed9214f 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -1,10 +1,15 @@ import BigNumber from 'bignumber.js' import { ApiClient } from '../.' +/** + * Single account ID (CScript or address) or reserved words, + * 'mine' to list history for all owned accounts or + * 'all' to list the whole DB + */ type OwnerType = 'mine' | 'all' | string /** - * Account related RPC calls for DeFiChain + * Account RPCs for DeFi Blockchain */ export class Account { private readonly client: ApiClient @@ -14,7 +19,8 @@ export class Account { } /** - * Returns information about all accounts on chain + * Get information about all accounts on chain + * Return account with object owner info, addresses and amount with tokenId * * @param {AccountPagination} [pagination] * @param {string} [pagination.start] @@ -24,14 +30,56 @@ export class Account { * @param {ListAccountOptions} [options] default = true, otherwise limited objects are listed * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) * @param {boolean} [options.isMineOnly=false] get balances about all accounts belonging to the wallet - * @return {Promise>>} + * @return {Promise>>} */ - listAccounts (pagination: AccountPagination, verbose: true, options: {indexedAmounts: false, isMineOnly: boolean}): Promise>> + listAccounts (pagination?: AccountPagination, verbose?: boolean, options?: ListAccountOptions): Promise>> + /** + * Get information about all accounts on chain + * Return account with hex-encoded owner info, addresses and amount with tokenId + * + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit=100] + * @param {boolean} [verbose=true] default = true, otherwise limited objects are listed + * @param {ListAccountOptions} [options] default = true, otherwise limited objects are listed + * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {boolean} [options.isMineOnly=false] get balances about all accounts belonging to the wallet + * @return {Promise>>} + */ listAccounts (pagination: AccountPagination, verbose: false, options: {indexedAmounts: false, isMineOnly: boolean}): Promise>> + /** + * Get information about all accounts on chain + * Return account with object owner info, addresses and object with indexed amount + * + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit=100] + * @param {boolean} [verbose=true] default = true, otherwise limited objects are listed + * @param {ListAccountOptions} [options] default = true, otherwise limited objects are listed + * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {boolean} [options.isMineOnly=false] get balances about all accounts belonging to the wallet + * @return {Promise>>} + */ listAccounts (pagination: AccountPagination, verbose: true, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> + /** + * Get information about all accounts on chain + * Return account with hex-encoded owner info, addresses and object with indexed amount + * + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit=100] + * @param {boolean} [verbose=true] default = true, otherwise limited objects are listed + * @param {ListAccountOptions} [options] default = true, otherwise limited objects are listed + * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {boolean} [options.isMineOnly=false] get balances about all accounts belonging to the wallet + * @return {Promise>>} + */ listAccounts (pagination: AccountPagination, verbose: false, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> async listAccounts ( @@ -44,7 +92,8 @@ export class Account { } /** - * Returns information about account + * Get information about account + * Return an object account with indexed amount * * @param {string} owner address in base58/bech32/hex encoding * @param {AccountPagination} [pagination] @@ -53,15 +102,25 @@ export class Account { * @param {number} [pagination.limit=100] * @param {GetAccountOptions} [options] * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) - * @return {Promise | AccountAmount} + * @return {Promise - - // {'0': 60} getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: true }): Promise + /** + * Get information about account + * Return an array of amount with tokenId + * + * @param {string} owner address in base58/bech32/hex encoding + * @param {AccountPagination} [pagination] + * @param {string | number} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit=100] + * @param {GetAccountOptions} [options] + * @param {boolean} [options.indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @return {Promise | AccountAmount} resolves as ['60.00000000@DFI'] + */ + getAccount (owner: string, pagination?: AccountPagination, options?: GetAccountOptions): Promise + async getAccount ( owner: string, pagination: AccountPagination = { limit: 100 }, @@ -72,7 +131,8 @@ export class Account { } /** - * Returns the balances of all accounts that belong to the wallet + * Get the balances of all accounts that belong to the wallet + * Return an array of amount with index * * @param {AccountPagination} [pagination] * @param {string} [pagination.start] @@ -81,19 +141,53 @@ export class Account { * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) * @param {GetTokenBalancesOptions} [options] * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false - * @return {Promise} + * @return {Promise} resolves as [ '300.00000000@0', '200.00000000@1' ] */ + getTokenBalances (pagination?: AccountPagination, indexedAmounts?: boolean, options?: GetTokenBalancesOptions): Promise - // [ '300.00000000@0', '200.00000000@1' ] - getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise - - // { '0': 300, '1': 200 } + /** + * Get the balances of all accounts that belong to the wallet + * Return object amount with index + * + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit=100] + * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {GetTokenBalancesOptions} [options] + * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false + * @return {Promise} resolves as { '0': 300, '1': 200 } + */ getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise - // [ '300.00000000@DFI', '200.00000000@DBTC' ] + /** + * Get the balances of all accounts that belong to the wallet + * Return array of amount with tokenId + * + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit=100] + * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {GetTokenBalancesOptions} [options] + * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false + * @return {Promise} resolves as [ '300.00000000@DFI', '200.00000000@DBTC' ] + */ getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: true }): Promise - // { DFI: 300, DBTC: 200 } + /** + * Get the balances of all accounts that belong to the wallet + * Return object amount with tokenId + * + * @param {AccountPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit=100] + * @param {boolean} [indexedAmounts=false] format of amount output, default = false (true: {tokenid:amount}, false: amount@tokenid) + * @param {GetTokenBalancesOptions} [options] + * @param {boolean} [options.symbolLookup=false] use token symbols in output, default = false + * @return {Promise} resolves as { DFI: 300, DBTC: 200 } + */ getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: true }): Promise async getTokenBalances ( diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index 966530f536..98854cddaf 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -12,13 +12,13 @@ const client = new Client() const something = await client.account.method() ``` -## listPoolPairs +## listAccounts Returns information about all accounts on chain ```ts title="client.account.listAccounts()" interface account { - listAccounts (pagination: AccountPagination, verbose: true, options: {indexedAmounts: false, isMineOnly: boolean}): Promise>> + listAccounts (pagination?: AccountPagination, verbose?: boolean, options?: ListAccountOptions): Promise>> listAccounts (pagination: AccountPagination, verbose: false, options: {indexedAmounts: false, isMineOnly: boolean}): Promise>> listAccounts (pagination: AccountPagination, verbose: true, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> listAccounts (pagination: AccountPagination, verbose: false, options: {indexedAmounts: true, isMineOnly: boolean}): Promise>> @@ -64,8 +64,8 @@ Returns information about account ```ts title="client.account.getAccount()" interface account { - getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: false }): Promise getAccount (owner: string, pagination: AccountPagination, options: { indexedAmounts: true }): Promise + getAccount (owner: string, pagination?: AccountPagination, options?: GetAccountOptions): Promise getAccount ( owner: string, pagination: AccountPagination = { limit: 100 }, @@ -80,7 +80,7 @@ Returns the balances of all accounts that belong to the wallet ```ts title="client.account.getTokenBalances()" interface account { - getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: false }): Promise + getTokenBalances (pagination?: AccountPagination, indexedAmounts?: boolean, options?: GetTokenBalancesOptions): Promise getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: false }): Promise getTokenBalances (pagination: AccountPagination, indexedAmounts: false, options: { symbolLookup: true }): Promise getTokenBalances (pagination: AccountPagination, indexedAmounts: true, options: { symbolLookup: true }): Promise @@ -88,7 +88,7 @@ interface account { pagination: AccountPagination = { limit: 100 }, indexedAmounts = false, options: GetTokenBalancesOptions = { symbolLookup: false} - ): Promise + ): Promise } interface AccountAmount { From 255ef4b38ec18c8291273d1d68b92a838fd93ec1 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Fri, 30 Apr 2021 16:42:12 +0800 Subject: [PATCH 15/15] remove duplicate default set value --- packages/jellyfish-api-core/src/category/account.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 477ed9214f..3ea3d49567 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -87,7 +87,7 @@ export class Account { verbose = true, options: ListAccountOptions = { indexedAmounts: false, isMineOnly: false } ): Promise>> { - const { indexedAmounts = false, isMineOnly = false } = options + const { indexedAmounts, isMineOnly } = options return await this.client.call('listaccounts', [pagination, verbose, indexedAmounts, isMineOnly], 'bignumber') }