Skip to content

Commit

Permalink
Added getBalances RPC (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
aikchun authored Jun 23, 2021
1 parent cd2603e commit baf4782
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import { BigNumber } from 'bignumber.js'
import { WalletFlag, WalletBalances } from '../../../src/category/wallet'

// TODO(aikchun): Add behavior tests for untrusted_pending, immature, used. Currently unable to do multi-node testing
describe('getBalances on masternode', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

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

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

it('should getBalances', async () => {
const balances: WalletBalances = await client.wallet.getBalances()
expect(BigNumber.isBigNumber(balances.mine.trusted)).toStrictEqual(true)
expect(BigNumber.isBigNumber(balances.mine.untrusted_pending)).toStrictEqual(true)
expect(BigNumber.isBigNumber(balances.mine.immature)).toStrictEqual(true)
expect(typeof balances.mine.used).toStrictEqual('undefined')

expect(typeof balances.watchonly).toStrictEqual('undefined')
})

it('should show balances after sending the amount out', async () => {
const balanceBefore: WalletBalances = await client.wallet.getBalances()

await client.wallet.sendToAddress('bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2', 10000)
await container.generate(1)

const balanceAfter: WalletBalances = await client.wallet.getBalances()

expect(balanceBefore.mine.trusted.gt(balanceAfter.mine.trusted)).toStrictEqual(true)
})

it('test watchOnly', async () => {
await container.call('importaddress', ['bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2'])

const balances: WalletBalances = await client.wallet.getBalances()

expect(BigNumber.isBigNumber(balances.watchonly?.trusted)).toStrictEqual(true)
expect(BigNumber.isBigNumber(balances.watchonly?.untrusted_pending)).toStrictEqual(true)
expect(BigNumber.isBigNumber(balances.watchonly?.immature)).toStrictEqual(true)
})
})

describe('getBalances without masternode', () => {
const container = new RegTestContainer()
const client = new ContainerAdapterClient(container)

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

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

it('should getBalances.mine.trusted = 0', async () => {
const balances: WalletBalances = await client.wallet.getBalances()

expect(balances.mine.trusted.isEqualTo(new BigNumber('0'))).toStrictEqual(true)
})
})

describe('getBalances when wallet is set to avoid_reuse', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await client.wallet.setWalletFlag(WalletFlag.AVOID_REUSE)
await container.generate(1)
})

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

it('should have used', async () => {
const balances: WalletBalances = await client.wallet.getBalances()

expect(BigNumber.isBigNumber(balances.mine.trusted)).toStrictEqual(true)
expect(BigNumber.isBigNumber(balances.mine.untrusted_pending)).toStrictEqual(true)
expect(BigNumber.isBigNumber(balances.mine.immature)).toStrictEqual(true)
expect(BigNumber.isBigNumber(balances.mine.used)).toStrictEqual(true)

expect(typeof balances.watchonly).toStrictEqual('undefined')
})
})
27 changes: 27 additions & 0 deletions packages/jellyfish-api-core/src/category/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ export class Wallet {
return await this.client.call('getunconfirmedbalance', [false], 'bignumber')
}

/**
* Returns an object with all balances.
*
* @return {Promise<WalletBalances>}
*/
async getBalances (): Promise<WalletBalances> {
return await this.client.call('getbalances', [false], 'bignumber')
}

/**
* Get list of UTXOs in wallet.
*
Expand Down Expand Up @@ -472,3 +481,21 @@ export interface InWalletTransactionDetail {
fee: number
abandoned: boolean
}

export interface WalletBalances {
mine: WalletMineBalances
watchonly?: WalletWatchOnlyBalances
}

export interface WalletMineBalances {
trusted: BigNumber
untrusted_pending: BigNumber
immature: BigNumber
used?: BigNumber
}

export interface WalletWatchOnlyBalances {
trusted: BigNumber
untrusted_pending: BigNumber
immature: BigNumber
}
27 changes: 27 additions & 0 deletions website/docs/jellyfish/api/wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ Identical to getBalance to get untrusted pending balance.
```ts title="client.wallet.getUnconfirmedBalance()"
interface wallet {
getUnconfirmedBalance (): Promise<BigNumber>
```
## getBalances
Returns an object with all balances.
```ts title="client.wallet.getBalances()"
interface wallet {
getBalances (): Promise<WalletBalances>
}

interface WalletBalances {
mine: WalletMineBalances
watchonly?: WalletWatchOnlyBalances
}

interface WalletMineBalances {
trusted: BigNumber
untrusted_pending: BigNumber
immature: BigNumber
used?: BigNumber
}

interface WalletWatchOnlyBalances {
trusted: BigNumber
untrusted_pending: BigNumber
immature: BigNumber
}
```
Expand Down

0 comments on commit baf4782

Please sign in to comment.