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

feat(jellyfish-api-core): add amount format option for listAccountHistory RPC #1569

Merged
merged 14 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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