Skip to content

Commit

Permalink
feat(jellyfish-api-core): Add multi-txtype, multi-address query suppo…
Browse files Browse the repository at this point in the history
…rt for `accounthistorycount` RPC (#1943)

<!--  Thanks for sending a pull request! -->

#### What this PR does / why we need it:
As stated in the title, this PR adds support to `accounthistorycount`
RPC to query with multiple txtypes/addresses.

We need this because with the current implementation we're only allowed
to fetch one address and/or one tx type at once thus needing to make
multiple RPC calls if we need to query history count of multple
txtypes/addresses.

Refer: [Defich/ain PR](DeFiCh/ain#1666)

Signed-off-by: Dilshan Madushanka <[email protected]>
  • Loading branch information
helloscoopa authored Jan 20, 2023
1 parent e07aad2 commit 4c8f2ba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/node/CATEGORIES/08-account.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ Returns count of account history
```ts title="client.account.historyCount()"
interface account {
historyCount (
owner: OwnerType | string = OwnerType.MINE,
owner: OwnerType | string | string[] = OwnerType.MINE,
options: AccountHistoryCountOptions = {}
): Promise<number>
}
Expand Down Expand Up @@ -315,6 +315,7 @@ enum DfTxType {
interface AccountHistoryCountOptions {
token?: string
txtype?: DfTxType
txtypes?: DfTxType[]
no_rewards?: boolean
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AccountHistoryCountOptions, DfTxType } from '../../../src/category/acco
describe('Account', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)
let from: string, to: string

beforeAll(async () => {
await container.start()
Expand All @@ -19,10 +20,10 @@ describe('Account', () => {
})

async function setup (): Promise<void> {
const from = await container.call('getnewaddress')
from = await container.call('getnewaddress')
await createToken(from, 'DBTC', 200)

const to = await accountToAccount('DBTC', 5, from)
to = await accountToAccount('DBTC', 5, from)
await accountToAccount('DBTC', 18, from, to)

await createToken(from, 'DETH', 200)
Expand Down Expand Up @@ -102,7 +103,7 @@ describe('Account', () => {
})
})

it('should get accountHistory with txtype option', async () => {
it('should get accountHistoryCount with txtype option', async () => {
await waitForExpect(async () => {
const options: AccountHistoryCountOptions = {
txtype: DfTxType.MINT_TOKEN
Expand All @@ -121,12 +122,27 @@ describe('Account', () => {
}
const options2: AccountHistoryCountOptions = {
txtype: DfTxType.POOL_SWAP

}
const count1 = await client.account.historyCount('mine', options1)
const count2 = await client.account.historyCount('mine', options2)

expect(count1 === count2).toStrictEqual(false)
})
})

it('should get accountHistoryCount for multiple txtypes at once', async () => {
const mintCount = await client.account.historyCount('mine', { txtype: DfTxType.MINT_TOKEN })
const poolSwapCount = await client.account.historyCount('mine', { txtype: DfTxType.POOL_SWAP })
const combinedCount = await client.account.historyCount('mine', { txtypes: [DfTxType.MINT_TOKEN, DfTxType.POOL_SWAP] })

expect(combinedCount).toStrictEqual(mintCount + poolSwapCount)
})

it('should get accountHistoryCount for multiple addresses at once', async () => {
const fromCount = await client.account.historyCount(from)
const toCount = await client.account.historyCount(to)
const combinedCount = await client.account.historyCount([from, to])

expect(combinedCount).toStrictEqual(fromCount + toCount)
})
})
8 changes: 5 additions & 3 deletions packages/jellyfish-api-core/src/category/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export class Account {
/**
* Returns information about account history
*
* @param {OwnerType | string | string[]} [owner=OwnerType.MINE] Single/multiple account ID(s) (CScript or address) or reserved words 'mine' to list history for all owned accounts or 'all' to list whole DB
* @param {OwnerType | string | string[]} [owner=OwnerType.MINE] Account ID(s) (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
Expand Down Expand Up @@ -337,15 +337,16 @@ export class Account {
/**
* 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 {OwnerType | string | string[]} [owner=OwnerType.MINE] Account ID(s) (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 {DfTxType} [options.txtype] Filter by transaction type. See DfTxType.
* @param {DfTxType[]} [options.txtypes] Filter by multiple transaction types. See DfTxType.
* @return {Promise<number>} count of account history
*/
async historyCount (
owner: OwnerType | string = OwnerType.MINE,
owner: OwnerType | string | string [] = OwnerType.MINE,
options: AccountHistoryCountOptions = {}
): Promise<number> {
return await this.client.call('accounthistorycount', [owner, options], 'number')
Expand Down Expand Up @@ -554,6 +555,7 @@ export interface AccountHistoryOptions {
export interface AccountHistoryCountOptions {
token?: string
txtype?: DfTxType
txtypes?: DfTxType[]
no_rewards?: boolean
}

Expand Down

0 comments on commit 4c8f2ba

Please sign in to comment.