Skip to content

Commit

Permalink
feat(jellyfish-api-core): add amount format option for listAccountHis…
Browse files Browse the repository at this point in the history
…tory RPC (#1569)

* Updated the listAccountHistory api with new pagination key amountFormat + test

* Added a test to check default symbol format

* Update docker images

* Updated api and tests according to review

* Added latest master ain image

* Refactor amountFormat to format

* Update with latest ain image

Co-authored-by: surangap <[email protected]>
  • Loading branch information
chanakasameera and surangap authored Jun 30, 2022
1 parent 442676a commit bb46104
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/node/CATEGORIES/08-account.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ enum DfTxType {
NONE = '0'
}

enum Format {
ID = 'id',
SYMBOL = 'symbol'
}

interface AccountHistory {
owner: string
blockHeight: number
Expand All @@ -235,6 +240,7 @@ interface AccountHistoryOptions {
txtype?: DfTxType
limit?: number
txn?: number
format?: Format
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import waitForExpect from 'wait-for-expect'
import { DfTxType, BalanceTransferPayload, AccountResult, AccountOwner } from '../../../src/category/account'
import { DfTxType, BalanceTransferPayload, AccountResult, AccountOwner, Format } from '../../../src/category/account'

function createTokenForContainer (container: MasterNodeRegTestContainer) {
return async (address: string, symbol: string, amount: number) => {
Expand Down Expand Up @@ -303,6 +303,61 @@ describe('Account', () => {
}
})
})

it('should listAccountHistory with options format', async () => {
{ // amount format should be id
const options = {
token: 'DBTC',
format: Format.ID
}
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(accountHistory.amounts.length).toBeGreaterThan(0)
for (let j = 0; j < accountHistory.amounts.length; j += 1) {
const amount = accountHistory.amounts[j]
const id = amount.split('@')[1]
expect(id).toStrictEqual('1')
}
}
}

{ // amount format should be symbol
const options = {
token: 'DBTC',
format: Format.SYMBOL
}
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(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).toStrictEqual('DBTC')
}
}
}

{ // amount format default should be symbol
const options = {
token: 'DFI'
}
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(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).toStrictEqual('DFI')
}
}
}
})
})

describe('listAccountHistory', () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/jellyfish-api-core/src/category/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export enum DfTxType {
FUTURE_SWAP_REFUND = 'w'
}

/**
* Configure the format of amounts
* id - amount with the following 'id' -> <amount>@id
* symbol - amount with the following 'symbol' -> <amount>@symbol
*/
export enum Format {
ID = 'id',
SYMBOL = 'symbol'
}

export enum SelectionModeType {
PIE = 'pie',
CRUMBS = 'crumbs',
Expand Down Expand Up @@ -292,6 +302,7 @@ export class Account {
* @param {DfTxType} [options.txtype] Filter by transaction type. See DfTxType.
* @param {number} [options.limit=100] Maximum number of records to return, 100 by default
* @param {number} [options.txn] Order in block, unlimited by default
* @param {Format} [options.format] Set the return amount format, Format.SYMBOL by default
* @return {Promise<AccountHistory[]>}
*/
async listAccountHistory (
Expand Down Expand Up @@ -511,6 +522,7 @@ export interface AccountHistoryOptions {
txtype?: DfTxType
limit?: number
txn?: number
format?: Format
}

export interface AccountHistoryCountOptions {
Expand Down

0 comments on commit bb46104

Please sign in to comment.