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 {