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

Added getBalances RPC #362

Merged
merged 24 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
973f791
Created a new getBalances.test.ts
aikchun Jun 9, 2021
60ffdc8
Removed some assertions.
aikchun Jun 9, 2021
38f2352
Updated the documentation for getBalances.
aikchun Jun 9, 2021
b5135f6
Renamed the interface WalletBalance, WalletMineBalance and WalletWatc…
aikchun Jun 9, 2021
7b9918f
Added a failing test.
aikchun Jun 10, 2021
365e01e
Added a test to check for the balance after sending out some amount.
aikchun Jun 10, 2021
59e7f4d
committing a failing test to show immature balance changes.
aikchun Jun 11, 2021
c6940dd
Removed some tests that require multi-node testing for now. Added com…
aikchun Jun 11, 2021
cb016d1
Added behavioral tests for watchonly balances.
aikchun Jun 12, 2021
1ebbc27
Changed the assertion methodology for BigNumber type
aikchun Jun 12, 2021
08d6992
Change assertion to use the BigNumber API isEqualTo.
aikchun Jun 14, 2021
618d9f5
Added tests for the withTokens set to true
aikchun Jun 15, 2021
092218c
REmoved test regarding with_tokens as it is not standard in Bitcoin.
aikchun Jun 17, 2021
14164f1
Removed full-stop from method description.
aikchun Jun 21, 2021
e632c55
Changed the method docs to follow JSDoc convention for optional defau…
aikchun Jun 21, 2021
7d8f9da
Changed up the test style to test for the balances in watchonly
aikchun Jun 21, 2021
968b8d6
removed the .only cause
aikchun Jun 21, 2021
8b1d468
made consistent with the formatting.
aikchun Jun 21, 2021
fc9609b
Changed optional chaining to avoid no-non-null-assertion
aikchun Jun 21, 2021
9e6f22f
Removed the options of withTokens in the parameter. Updated the docs.
aikchun Jun 21, 2021
af6dfc7
Moved test where wallet is set to avoid_reuse into a separate describ…
aikchun Jun 21, 2021
9ff31e4
Fixed assert to compare balanceBefore and balanceAfter using BigNumbe…
aikchun Jun 21, 2021
c862b1a
Import BigNumber directly from dependency.
aikchun Jun 22, 2021
b999820
Moved wallet flag set to avoid_reuse into beforeAll
aikchun Jun 22, 2021
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
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 () => {
canonbrother marked this conversation as resolved.
Show resolved Hide resolved
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()
aikchun marked this conversation as resolved.
Show resolved Hide resolved
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.
fuxingloh marked this conversation as resolved.
Show resolved Hide resolved

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