diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index 6014deefc3..b92df0ec8a 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -2,7 +2,7 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../container_adapter_client' import waitForExpect from 'wait-for-expect' import BigNumber from 'bignumber.js' -import { UtxosToAccountPayload } from '../../src/category/account' +import { UtxosToAccountPayload, AccountHistoryCountOptions, TxType } from '../../src/category/account' describe('masternode', () => { const container = new MasterNodeRegTestContainer() @@ -516,4 +516,82 @@ describe('masternode', () => { expect(data.length).toBe(64) }) }) + + describe('accountHistoryCount', () => { + it('should get accountHistoryCount', async () => { + await waitForExpect(async () => { + const count = await client.account.historyCount() + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get accountHistoryCount with owner as all', async () => { + await waitForExpect(async () => { + const count = await client.account.historyCount('all') + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get accountHistoryCount with no_rewards option', async () => { + await waitForExpect(async () => { + const options: AccountHistoryCountOptions = { + no_rewards: true + } + const count = await client.account.historyCount('mine', options) + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get accountHistoryCount with token option', async () => { + await waitForExpect(async () => { + const options1: AccountHistoryCountOptions = { + token: 'DBTC' + } + const options2: AccountHistoryCountOptions = { + token: 'DETH' + } + const countWithDBTC = await client.account.historyCount('mine', options1) + const countWithDETH = await client.account.historyCount('mine', options2) + + expect(typeof countWithDBTC).toBe('number') + expect(typeof countWithDETH).toBe('number') + expect(countWithDBTC).toStrictEqual(5) + expect(countWithDETH).toStrictEqual(3) + }) + }) + + it('should get accountHistory with txtype option', async () => { + await waitForExpect(async () => { + const options: AccountHistoryCountOptions = { + txtype: TxType.MINT_TOKEN + } + const count = await client.account.historyCount('mine', options) + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get different count for different txtypes', async () => { + await waitForExpect(async () => { + const options1: AccountHistoryCountOptions = { + txtype: TxType.MINT_TOKEN + } + const options2: AccountHistoryCountOptions = { + txtype: TxType.POOL_SWAP + + } + const count1 = await client.account.historyCount('mine', options1) + const count2 = await client.account.historyCount('mine', options2) + + expect(count1 === count2).toStrictEqual(false) + }) + }) + }) }) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 6bd5fd11b8..a6140fdba9 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -6,8 +6,30 @@ import { ApiClient } from '../.' * - 'mine' to list history for all owned accounts or * - 'all' to list the whole DB */ -type OwnerType = 'mine' | 'all' | string +export enum OwnerType { + MINE = 'mine', + ALL = 'all' +} +export enum TxType { + MINT_TOKEN ='M', + POOL_SWAP = 's', + ADD_POOL_LIQUIDITY = 'l', + REMOVE_POOL_LIQUIDITY = 'r', + UTXOS_TO_ACCOUNT = 'U', + ACCOUNT_TO_UTXOS = 'b', + ACCOUNT_TO_ACCOUNT = 'B', + ANY_ACCOUNTS_TO_ACCOUNTS = 'a', + CREATE_MASTERNODE = 'C', + RESIGN_MASTERNODE = 'R', + CREATE_TOKEN = 'T', + UPDATE_TOKEN = 'N', + UPDATE_TOKEN_ANY = 'n', + CREATE_POOL_PAIR = 'p', + UPDATE_POOL_PAIR = 'u', + SET_GOV_VARIABLE = 'G', + AUTO_AUTH_PREP = 'A' +} /** * Account RPCs for DeFi Blockchain */ @@ -202,7 +224,7 @@ export class Account { /** * Returns information about account history * - * @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 {OwnerType | string} [owner=OwnerType.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 (down to genesis block), (default = chaintip). * @param {number} [options.depth] Maximum depth, from the genesis block is the default @@ -213,7 +235,7 @@ export class Account { * @return {Promise} */ async listAccountHistory ( - owner: OwnerType = 'mine', + owner: OwnerType | string = OwnerType.MINE, options: AccountHistoryOptions = { limit: 100 } @@ -235,6 +257,23 @@ export class Account { async utxosToAccount (payload: UtxosToAccountPayload, utxos: UtxosToAccountUTXO[] = []): Promise { return await this.client.call('utxostoaccount', [payload, utxos], 'number') } + + /** + * Returns count of account history + * + * @param {OwnerType | string} [owner=OwnerType.MINE] single account ID (CScript or address) or reserved words 'mine' to list history count for all owned accounts or 'all' to list whole DB + * @param {AccountHistoryCountOptions} [options] + * @param {boolean} [options.no_rewards] Filter out rewards + * @param {string} [options.token] Filter by token + * @param {TxType | string} [options.txtype] Filter by transaction type, supported letter from 'CRTMNnpuslrUbBG' + * @return {Promise} count of account history + */ + async historyCount ( + owner: OwnerType | string = OwnerType.MINE, + options: AccountHistoryCountOptions = {} + ): Promise { + return await this.client.call('accounthistorycount', [owner, options], 'number') + } } export interface AccountPagination { @@ -301,3 +340,9 @@ export interface UtxosToAccountUTXO { txid: string vout: number } + +export interface AccountHistoryCountOptions { + token?: string + txtype?: TxType | string + no_rewards?: boolean +} diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index 72b74558ae..f8fc1804ce 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -113,14 +113,17 @@ Returns information about account history ```ts title="client.account.listAccountHistory()" interface account { listAccountHistory ( - owner: OwnerType = 'mine', + owner: OwnerType | string = OwnerType.MINE, options: AccountHistoryOptions = { limit: 100 } ): Promise } -type OwnerType = 'mine' | 'all' | string +enum OwnerType { + MINE = "mine", + ALL = "all" +} interface AccountHistory { owner: string @@ -162,3 +165,47 @@ interface UtxosToAccountUTXO { vout: number } ``` + +## historyCount + +Returns count of account history + +```ts title="client.account.historyCount()" +interface account { + historyCount ( + owner: OwnerType | string = OwnerType.MINE, + options: AccountHistoryCountOptions = {} + ): Promise +} + +enum OwnerType { + MINE = "mine", + ALL = "all" +} + +enum TxType { + MINT_TOKEN = 'M', + POOL_SWAP = 's', + ADD_POOL_LIQUIDITY = 'l', + REMOVE_POOL_LIQUIDITY = 'r', + UTXOS_TO_ACCOUNT = 'U', + ACCOUNT_TO_UTXOS = 'b', + ACCOUNT_TO_ACCOUNT = 'B', + ANY_ACCOUNTS_TO_ACCOUNTS = 'a', + CREATE_MASTERNODE = 'C', + RESIGN_MASTERNODE = 'R', + CREATE_TOKEN = 'T', + UPDATE_TOKEN = 'N', + UPDATE_TOKEN_ANY = 'n', + CREATE_POOL_PAIR = 'p', + UPDATE_POOL_PAIR = 'u', + SET_GOV_VARIABLE = 'G', + AUTO_AUTH_PREP = 'A' +} + +interface AccountHistoryCountOptions { + token?: string + txtype?: TxType | string + no_rewards?: boolean +} +```