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 wallet.listtransactions RPC #2060

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions docs/node/CATEGORIES/05-wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,13 @@ interface wallet {
signMessage (address: string, message: string): Promise<string>
}
```

## listTransactions

List transactions based on the given criteria.

```ts title="client.wallet.listTransactions()"
interface wallet {
listTransactions (label: string = '*', count: number = 10, skip: number = 0, includeWatchOnly: boolean = true, excludeCustomTx: boolean = true): Promise<InWalletTransactionWithCategory[]>
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import { BIP125, InWalletTransactionCategory } from '@defichain/jellyfish-api-core/dist/category/wallet'
import { BigNumber } from '@defichain/jellyfish-json'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'

describe('listTransactions', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

const address = 'mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU'

beforeAll(async () => {
await container.start()
await container.waitForWalletCoinbaseMaturity()
})

afterAll(async () => {
await container.stop()
})

it('should listTransactions', async () => {
await client.wallet.sendToAddress(address, 0.0001)
await container.generate(1, address)

const inWalletTransactions = await client.wallet.listTransactions()

expect(inWalletTransactions.length).toBeGreaterThanOrEqual(1)
andyrobert3 marked this conversation as resolved.
Show resolved Hide resolved

for (const inWalletTransaction of inWalletTransactions) {
expect(typeof inWalletTransaction.address).toStrictEqual('string')
expect(typeof inWalletTransaction.txid).toStrictEqual('string')
andyrobert3 marked this conversation as resolved.
Show resolved Hide resolved
expect(inWalletTransaction.amount).toBeInstanceOf(BigNumber)

expect(typeof inWalletTransaction.confirmations).toStrictEqual('number')
expect(typeof inWalletTransaction.blockhash).toStrictEqual('string')
expect(typeof inWalletTransaction.blocktime).toStrictEqual('number')
expect(typeof inWalletTransaction.blockindex).toStrictEqual('number')
expect(typeof inWalletTransaction.time).toStrictEqual('number')
expect(typeof inWalletTransaction.timereceived).toStrictEqual('number')
expect(Object.values(BIP125).includes(inWalletTransaction['bip125-replaceable'])).toStrictEqual(true)

expect(Object.values(InWalletTransactionCategory).includes(inWalletTransaction.category)).toStrictEqual(true)
expect(typeof inWalletTransaction.label).toStrictEqual('string')
expect(typeof inWalletTransaction.vout).toStrictEqual('number')
}
})

it('should listTransactions with label set', async () => {
await client.wallet.sendToAddress(address, 0.0001)
await container.generate(1, address)

const inWalletTransactions = await client.wallet.listTransactions('owner')

inWalletTransactions.forEach((inWalletTransaction) => {
expect(inWalletTransaction.label).toStrictEqual('owner')
})

expect(inWalletTransactions.length).toBeGreaterThanOrEqual(1)

for (const inWalletTransaction of inWalletTransactions) {
expect(typeof inWalletTransaction.address).toStrictEqual('string')
expect(typeof inWalletTransaction.txid).toStrictEqual('string')
expect(inWalletTransaction.amount).toBeInstanceOf(BigNumber)

expect(typeof inWalletTransaction.confirmations).toStrictEqual('number')
expect(typeof inWalletTransaction.blockhash).toStrictEqual('string')
expect(typeof inWalletTransaction.blocktime).toStrictEqual('number')
expect(typeof inWalletTransaction.blockindex).toStrictEqual('number')
expect(typeof inWalletTransaction.time).toStrictEqual('number')
expect(typeof inWalletTransaction.timereceived).toStrictEqual('number')
expect(Object.values(BIP125).includes(inWalletTransaction['bip125-replaceable'])).toStrictEqual(true)

expect(Object.values(InWalletTransactionCategory).includes(inWalletTransaction.category)).toStrictEqual(true)
expect(typeof inWalletTransaction.label).toStrictEqual('string')
expect(typeof inWalletTransaction.vout).toStrictEqual('number')
}
})

it('should listTransactions with label set', async () => {
await client.wallet.sendToAddress(address, 0.0001)
await container.generate(1, address)

const inWalletTransactions = await client.wallet.listTransactions('owner')

inWalletTransactions.forEach((inWalletTransaction) => {
expect(inWalletTransaction.label).toStrictEqual('owner')
})

expect(inWalletTransactions.length).toBeGreaterThanOrEqual(1)

for (const inWalletTransaction of inWalletTransactions) {
expect(typeof inWalletTransaction.address).toStrictEqual('string')
expect(typeof inWalletTransaction.txid).toStrictEqual('string')
expect(inWalletTransaction.amount).toBeInstanceOf(BigNumber)

expect(typeof inWalletTransaction.confirmations).toStrictEqual('number')
expect(typeof inWalletTransaction.blockhash).toStrictEqual('string')
expect(typeof inWalletTransaction.blocktime).toStrictEqual('number')
expect(typeof inWalletTransaction.blockindex).toStrictEqual('number')
expect(typeof inWalletTransaction.time).toStrictEqual('number')
expect(typeof inWalletTransaction.timereceived).toStrictEqual('number')
expect(Object.values(BIP125).includes(inWalletTransaction['bip125-replaceable'])).toStrictEqual(true)

expect(Object.values(InWalletTransactionCategory).includes(inWalletTransaction.category)).toStrictEqual(true)
expect(typeof inWalletTransaction.label).toStrictEqual('string')
expect(typeof inWalletTransaction.vout).toStrictEqual('number')
}
})

it('should listTransactions with count = 5', async () => {
await client.wallet.sendToAddress(address, 0.0001)
await container.generate(1, address)

const inWalletTransactions = await client.wallet.listTransactions('*', 5)

expect(inWalletTransactions.length).toStrictEqual(5)

for (const inWalletTransaction of inWalletTransactions) {
expect(typeof inWalletTransaction.address).toStrictEqual('string')
expect(typeof inWalletTransaction.txid).toStrictEqual('string')
expect(inWalletTransaction.amount).toBeInstanceOf(BigNumber)

expect(typeof inWalletTransaction.confirmations).toStrictEqual('number')
expect(typeof inWalletTransaction.blockhash).toStrictEqual('string')
expect(typeof inWalletTransaction.blocktime).toStrictEqual('number')
expect(typeof inWalletTransaction.blockindex).toStrictEqual('number')
expect(typeof inWalletTransaction.time).toStrictEqual('number')
expect(typeof inWalletTransaction.timereceived).toStrictEqual('number')
expect(Object.values(BIP125).includes(inWalletTransaction['bip125-replaceable'])).toStrictEqual(true)

expect(Object.values(InWalletTransactionCategory).includes(inWalletTransaction.category)).toStrictEqual(true)
expect(typeof inWalletTransaction.label).toStrictEqual('string')
expect(typeof inWalletTransaction.vout).toStrictEqual('number')
}
})

it('should not listTransactions with count = -1', async () => {
await expect(client.wallet.listTransactions('*', -1)).rejects.toThrow('RpcApiError: \'Negative count\', code: -8, method: listtransactions')
})

it('should listTransactions with count = 0', async () => {
const inWalletTransactions = await client.wallet.listTransactions('*', 0)
expect(inWalletTransactions.length).toStrictEqual(0)
})

it('should listTransactions with includeWatchOnly false', async () => {
const inWalletTransactions = await client.wallet.listTransactions('*', 10, 0, false, undefined)

inWalletTransactions.forEach((inWalletTransaction) => {
expect(inWalletTransaction.address).toStrictEqual(address)
})

expect(inWalletTransactions.length).toBeGreaterThanOrEqual(1)

for (const inWalletTransaction of inWalletTransactions) {
expect(typeof inWalletTransaction.address).toStrictEqual('string')
expect(typeof inWalletTransaction.txid).toStrictEqual('string')
expect(inWalletTransaction.amount).toBeInstanceOf(BigNumber)

expect(typeof inWalletTransaction.confirmations).toStrictEqual('number')
expect(typeof inWalletTransaction.blockhash).toStrictEqual('string')
expect(typeof inWalletTransaction.blocktime).toStrictEqual('number')
expect(typeof inWalletTransaction.blockindex).toStrictEqual('number')
expect(typeof inWalletTransaction.time).toStrictEqual('number')
expect(typeof inWalletTransaction.timereceived).toStrictEqual('number')
expect(Object.values(BIP125).includes(inWalletTransaction['bip125-replaceable'])).toStrictEqual(true)

expect(Object.values(InWalletTransactionCategory).includes(inWalletTransaction.category)).toStrictEqual(true)
expect(typeof inWalletTransaction.label).toStrictEqual('string')
expect(typeof inWalletTransaction.vout).toStrictEqual('number')
}
})

it('should listTransactions with excludeCustomTx = true', async () => {
const inWalletTransactions = await client.wallet.listTransactions('*', 10, 0, undefined, true)

inWalletTransactions.forEach((inWalletTransaction) => {
expect(inWalletTransaction.address).toStrictEqual(address)
})

expect(inWalletTransactions.length).toBeGreaterThanOrEqual(1)

for (const inWalletTransaction of inWalletTransactions) {
expect(typeof inWalletTransaction.address).toStrictEqual('string')
expect(typeof inWalletTransaction.txid).toStrictEqual('string')
expect(inWalletTransaction.amount).toBeInstanceOf(BigNumber)

expect(typeof inWalletTransaction.confirmations).toStrictEqual('number')
expect(typeof inWalletTransaction.blockhash).toStrictEqual('string')
expect(typeof inWalletTransaction.blocktime).toStrictEqual('number')
expect(typeof inWalletTransaction.blockindex).toStrictEqual('number')
expect(typeof inWalletTransaction.time).toStrictEqual('number')
expect(typeof inWalletTransaction.timereceived).toStrictEqual('number')
expect(Object.values(BIP125).includes(inWalletTransaction['bip125-replaceable'])).toStrictEqual(true)

expect(Object.values(InWalletTransactionCategory).includes(inWalletTransaction.category)).toStrictEqual(true)
expect(typeof inWalletTransaction.label).toStrictEqual('string')
expect(typeof inWalletTransaction.vout).toStrictEqual('number')
}
})
})
35 changes: 35 additions & 0 deletions packages/jellyfish-api-core/src/category/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,21 @@ export class Wallet {
return await this.client.call('listwallets', [], 'number')
}

/**
* If a label name is provided, this will return only incoming transactions paying to addresses with the specified label.
* Returns up to 'count' most recent transactions skipping the first 'from' transactions.
*
* @param {string} label If set, should be a valid label name to return only incoming transactions with the specified label, or '*' to disable filtering and return all transactions. (default = '*')
* @param {string} count The number of transactions to return (default = 10)
* @param {string} skip The number of transactions to skip (default = 0)
* @param {boolean} includeWatchOnly Whether to include watch-only addresses (default = true)
* @param {boolean} excludeCustomTx False to include all transactions, otherwise exclude custom transactions (default = false)
* @return {Promise<Array<InWalletTransactionWithFlatDetails>>}
*/
async listTransactions (label: string = '*', count: number = 10, skip: number = 0, includeWatchOnly: boolean = true, excludeCustomTx: boolean = false): Promise<InWalletTransactionWithFlatDetails[]> {
andyrobert3 marked this conversation as resolved.
Show resolved Hide resolved
return await this.client.call('listtransactions', [label, count, skip, includeWatchOnly, excludeCustomTx], { amount: 'bignumber' })
}

/**
* Sign a message with the private key of an address
* Requires wallet to be unlocked for usage. Use `walletpassphrase` to unlock wallet.
Expand Down Expand Up @@ -494,6 +509,26 @@ export interface InWalletTransaction {
hex: string
}

export interface InWalletTransactionWithFlatDetails {
address: string
category: InWalletTransactionCategory
amount: BigNumber
label?: string
vout: number
fee?: number
confirmations: number
trusted: boolean
blockhash: string
blockindex: number
blocktime: number
txid: string
time: number
timereceived: number
comment?: string
'bip125-replaceable': BIP125
abandoned?: boolean
}

export interface InWalletTransactionDetail {
address: string
category: InWalletTransactionCategory
Expand Down