Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add account.historyCount RPC #265

Merged
merged 13 commits into from
May 26, 2021
80 changes: 79 additions & 1 deletion packages/jellyfish-api-core/__tests__/category/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
})
})
canonbrother marked this conversation as resolved.
Show resolved Hide resolved

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 () => {
siradji marked this conversation as resolved.
Show resolved Hide resolved
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
}
siradji marked this conversation as resolved.
Show resolved Hide resolved
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)
})
})
})
})
51 changes: 48 additions & 3 deletions packages/jellyfish-api-core/src/category/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
siradji marked this conversation as resolved.
Show resolved Hide resolved
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
*/
Expand Down Expand Up @@ -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
Expand All @@ -213,7 +235,7 @@ export class Account {
* @return {Promise<AccountHistory[]>}
*/
async listAccountHistory (
owner: OwnerType = 'mine',
owner: OwnerType | string = OwnerType.MINE,
options: AccountHistoryOptions = {
limit: 100
}
Expand All @@ -235,6 +257,23 @@ export class Account {
async utxosToAccount (payload: UtxosToAccountPayload, utxos: UtxosToAccountUTXO[] = []): Promise<string> {
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<number>} count of account history
*/
async historyCount (
owner: OwnerType | string = OwnerType.MINE,
options: AccountHistoryCountOptions = {}
): Promise<number> {
return await this.client.call('accounthistorycount', [owner, options], 'number')
}
}

export interface AccountPagination {
Expand Down Expand Up @@ -301,3 +340,9 @@ export interface UtxosToAccountUTXO {
txid: string
vout: number
}

export interface AccountHistoryCountOptions {
token?: string
txtype?: TxType | string
no_rewards?: boolean
}
51 changes: 49 additions & 2 deletions website/docs/jellyfish/api/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccountHistory[]>
}

type OwnerType = 'mine' | 'all' | string
enum OwnerType {
MINE = "mine",
ALL = "all"
}

interface AccountHistory {
owner: string
Expand Down Expand Up @@ -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<number>
}

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
}
```