From 973f791f8bdfe47fe7fd42fe2f2519a0530aff61 Mon Sep 17 00:00:00 2001 From: aikchun Date: Wed, 9 Jun 2021 10:54:35 +0800 Subject: [PATCH 01/24] Created a new getBalances.test.ts file --- .../__tests__/category/wallet/getBalances.test.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts new file mode 100644 index 0000000000..e69de29bb2 From 60ffdc8481763ec39f76911a95c658f0ae9be572 Mon Sep 17 00:00:00 2001 From: aikchun Date: Wed, 9 Jun 2021 12:22:42 +0800 Subject: [PATCH 02/24] Removed some assertions. --- .../category/wallet/getBalances.test.ts | 42 +++++++++++++++++++ .../jellyfish-api-core/src/category/wallet.ts | 25 +++++++++++ 2 files changed, 67 insertions(+) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index e69de29bb2..52a40779c8 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -0,0 +1,42 @@ +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' +import { BigNumber } from '../../../src' +import { WalletFlag } from '../../../src/category/wallet' + +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 = await client.wallet.getBalances() + expect(balances.mine.trusted).toBeInstanceOf(BigNumber) + expect(balances.mine.untrusted_pending).toBeInstanceOf(BigNumber) + expect(balances.mine.immature).toBeInstanceOf(BigNumber) + expect(typeof balances.mine.used).toStrictEqual('undefined') + + expect(typeof balances.watchonly).toStrictEqual('undefined') + }) + + it('should have used in getBalances when wallet is set to avoid_reuse', async () => { + await client.wallet.setWalletFlag(WalletFlag.AVOID_REUSE) + await container.generate(1) + const balances = await client.wallet.getBalances() + + expect(balances.mine.trusted).toBeInstanceOf(BigNumber) + expect(balances.mine.untrusted_pending).toBeInstanceOf(BigNumber) + expect(balances.mine.immature).toBeInstanceOf(BigNumber) + expect(balances.mine.used).toBeInstanceOf(BigNumber) + + expect(typeof balances.watchonly).toStrictEqual('undefined') + }) +}) diff --git a/packages/jellyfish-api-core/src/category/wallet.ts b/packages/jellyfish-api-core/src/category/wallet.ts index 3b30ff1330..6e770a7964 100644 --- a/packages/jellyfish-api-core/src/category/wallet.ts +++ b/packages/jellyfish-api-core/src/category/wallet.ts @@ -72,6 +72,13 @@ export class Wallet { return await this.client.call('getunconfirmedbalance', [false], 'bignumber') } + /** + * Returns an object with all balances + */ + async getBalances (withTokens = false): Promise { + return await this.client.call('getbalances', [withTokens], 'bignumber') + } + /** * Get list of UTXOs in wallet. * @@ -472,3 +479,21 @@ export interface InWalletTransactionDetail { fee: number abandoned: boolean } + +export interface WalletBalance { + mine: WalletMineBalance + watchonly?: WalletWatchOnlyBalance +} + +export interface WalletMineBalance { + trusted: BigNumber + untrusted_pending: BigNumber + immature: BigNumber + used?: BigNumber +} + +export interface WalletWatchOnlyBalance { + trusted: BigNumber + untrusted_pending: BigNumber + immature: BigNumber +} From 38f235292b9c2f21fddfeca06f0c287cf72b3a00 Mon Sep 17 00:00:00 2001 From: aikchun Date: Wed, 9 Jun 2021 12:30:08 +0800 Subject: [PATCH 03/24] Updated the documentation for getBalances. --- .../jellyfish-api-core/src/category/wallet.ts | 3 +++ website/docs/jellyfish/api/wallet.md | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/packages/jellyfish-api-core/src/category/wallet.ts b/packages/jellyfish-api-core/src/category/wallet.ts index 6e770a7964..93cfb97987 100644 --- a/packages/jellyfish-api-core/src/category/wallet.ts +++ b/packages/jellyfish-api-core/src/category/wallet.ts @@ -74,6 +74,9 @@ export class Wallet { /** * Returns an object with all balances + * + * @param {boolean} withTokens = false, Include tokens balances; Default is 'false' for backward compatibility. + * @return {Promise} */ async getBalances (withTokens = false): Promise { return await this.client.call('getbalances', [withTokens], 'bignumber') diff --git a/website/docs/jellyfish/api/wallet.md b/website/docs/jellyfish/api/wallet.md index fa1d8bb4a6..b78c1afd6b 100644 --- a/website/docs/jellyfish/api/wallet.md +++ b/website/docs/jellyfish/api/wallet.md @@ -36,6 +36,33 @@ Identical to getBalance to get untrusted pending balance. ```ts title="client.wallet.getUnconfirmedBalance()" interface wallet { getUnconfirmedBalance (): Promise +``` + +## getBalances + +Returns an object with all balances. + +```ts title="client.wallet.getBalances()" +interface wallet { + getBalances(withTokens = false): Promise +} + +interface WalletBalance { + mine: WalletMineBalance + watchonly?: WalletWatchOnlyBalance +} + +interface WalletMineBalance { + trusted: BigNumber + untrusted_pending: BigNumber + immature: BigNumber + used?: BigNumber +} + +interface WalletWatchOnlyBalance { + trusted: BigNumber + untrusted_pending: BigNumber + immature: BigNumber } ``` From b5135f67f7c863bc7a84a67545ccb08ac5c5b0bd Mon Sep 17 00:00:00 2001 From: aikchun Date: Wed, 9 Jun 2021 14:09:38 +0800 Subject: [PATCH 04/24] Renamed the interface WalletBalance, WalletMineBalance and WalletWatchOnlyBalance to WalletBalances, WalletMineBalances and WalletWatchOnlyBalances respectively. --- packages/jellyfish-api-core/src/category/wallet.ts | 14 +++++++------- website/docs/jellyfish/api/wallet.md | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/wallet.ts b/packages/jellyfish-api-core/src/category/wallet.ts index 93cfb97987..f50a3a89f0 100644 --- a/packages/jellyfish-api-core/src/category/wallet.ts +++ b/packages/jellyfish-api-core/src/category/wallet.ts @@ -76,9 +76,9 @@ export class Wallet { * Returns an object with all balances * * @param {boolean} withTokens = false, Include tokens balances; Default is 'false' for backward compatibility. - * @return {Promise} + * @return {Promise} */ - async getBalances (withTokens = false): Promise { + async getBalances (withTokens = false): Promise { return await this.client.call('getbalances', [withTokens], 'bignumber') } @@ -483,19 +483,19 @@ export interface InWalletTransactionDetail { abandoned: boolean } -export interface WalletBalance { - mine: WalletMineBalance - watchonly?: WalletWatchOnlyBalance +export interface WalletBalances { + mine: WalletMineBalances + watchonly?: WalletWatchOnlyBalances } -export interface WalletMineBalance { +export interface WalletMineBalances { trusted: BigNumber untrusted_pending: BigNumber immature: BigNumber used?: BigNumber } -export interface WalletWatchOnlyBalance { +export interface WalletWatchOnlyBalances { trusted: BigNumber untrusted_pending: BigNumber immature: BigNumber diff --git a/website/docs/jellyfish/api/wallet.md b/website/docs/jellyfish/api/wallet.md index b78c1afd6b..9dbdf533da 100644 --- a/website/docs/jellyfish/api/wallet.md +++ b/website/docs/jellyfish/api/wallet.md @@ -44,22 +44,22 @@ Returns an object with all balances. ```ts title="client.wallet.getBalances()" interface wallet { - getBalances(withTokens = false): Promise + getBalances(withTokens = false): Promise } -interface WalletBalance { - mine: WalletMineBalance - watchonly?: WalletWatchOnlyBalance +interface WalletBalances { + mine: WalletMineBalances + watchonly?: WalletWatchOnlyBalances } -interface WalletMineBalance { +interface WalletMineBalances { trusted: BigNumber untrusted_pending: BigNumber immature: BigNumber used?: BigNumber } -interface WalletWatchOnlyBalance { +interface WalletWatchOnlyBalances { trusted: BigNumber untrusted_pending: BigNumber immature: BigNumber From 7b9918ff73d93076edb12dc8f4176f92eb1ffe50 Mon Sep 17 00:00:00 2001 From: aikchun Date: Thu, 10 Jun 2021 12:19:12 +0800 Subject: [PATCH 05/24] Added a failing test. --- .../category/wallet/getBalances.test.ts | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index 52a40779c8..67ed028369 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -1,4 +1,4 @@ -import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' import { BigNumber } from '../../../src' import { WalletFlag } from '../../../src/category/wallet' @@ -39,4 +39,40 @@ describe('getBalances on masternode', () => { expect(typeof balances.watchonly).toStrictEqual('undefined') }) + + it('should show balances after transfer', async () => { + const balance = await client.wallet.getBalances() + + const address = await client.wallet.getNewAddress() + + await client.wallet.sendToAddress(address, 10000) + await container.generate(1) + + const newBalance = await client.wallet.getBalances() + + console.log('balance.mine.trusted', balance.mine.trusted.toNumber()) + console.log('newBalance.mine.trusted', balance.mine.trusted.toNumber()) + + expect(newBalance.mine.trusted.toNumber() - balance.mine.trusted.toNumber()).toBeGreaterThan(10000) + }) +}) + +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 = await client.wallet.getBalances() + + expect(balances.mine.trusted.toNumber()).toStrictEqual(0) + }) }) From 365e01e5f6909f3704a18610f2254c2cd39acb0d Mon Sep 17 00:00:00 2001 From: aikchun Date: Thu, 10 Jun 2021 14:17:30 +0800 Subject: [PATCH 06/24] Added a test to check for the balance after sending out some amount. --- .../__tests__/category/wallet/getBalances.test.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index 67ed028369..a068f47ce5 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -40,20 +40,17 @@ describe('getBalances on masternode', () => { expect(typeof balances.watchonly).toStrictEqual('undefined') }) - it('should show balances after transfer', async () => { + it('should show balances after sending the amount out', async () => { const balance = await client.wallet.getBalances() - const address = await client.wallet.getNewAddress() + const address = 'bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2' await client.wallet.sendToAddress(address, 10000) await container.generate(1) const newBalance = await client.wallet.getBalances() - console.log('balance.mine.trusted', balance.mine.trusted.toNumber()) - console.log('newBalance.mine.trusted', balance.mine.trusted.toNumber()) - - expect(newBalance.mine.trusted.toNumber() - balance.mine.trusted.toNumber()).toBeGreaterThan(10000) + expect(balance.mine.trusted.toNumber() - newBalance.mine.trusted.toNumber()).toBeGreaterThan(10000) }) }) From 59e7f4d906ffc1f7f4a42c65fbc3861a805bfec8 Mon Sep 17 00:00:00 2001 From: aikchun Date: Fri, 11 Jun 2021 14:06:38 +0800 Subject: [PATCH 07/24] committing a failing test to show immature balance changes. --- .../__tests__/category/wallet/getBalances.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index a068f47ce5..ebd3deaa01 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -52,6 +52,16 @@ describe('getBalances on masternode', () => { expect(balance.mine.trusted.toNumber() - newBalance.mine.trusted.toNumber()).toBeGreaterThan(10000) }) + + it('should show immature changes', async () => { + const address = 'bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2' + + await client.wallet.sendToAddress(address, 1) + + const newBalance = await client.wallet.getBalances() + + expect(newBalance.mine.immature.toNumber()).toBeGreaterThanOrEqual(10000) + }) }) describe('getBalances without masternode', () => { From c6940dd150afc4b2e2f5236fed0e0db248c2efa5 Mon Sep 17 00:00:00 2001 From: aikchun Date: Fri, 11 Jun 2021 15:45:04 +0800 Subject: [PATCH 08/24] Removed some tests that require multi-node testing for now. Added comments for that. --- .../__tests__/category/wallet/getBalances.test.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index ebd3deaa01..e8fffe6302 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -3,6 +3,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import { BigNumber } from '../../../src' import { WalletFlag } 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) @@ -53,15 +54,7 @@ describe('getBalances on masternode', () => { expect(balance.mine.trusted.toNumber() - newBalance.mine.trusted.toNumber()).toBeGreaterThan(10000) }) - it('should show immature changes', async () => { - const address = 'bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2' - - await client.wallet.sendToAddress(address, 1) - - const newBalance = await client.wallet.getBalances() - - expect(newBalance.mine.immature.toNumber()).toBeGreaterThanOrEqual(10000) - }) + // TODO(aikchun) }) describe('getBalances without masternode', () => { From cb016d183bde548c2fabd1e24d988ced89ff3fb0 Mon Sep 17 00:00:00 2001 From: aikchun Date: Sat, 12 Jun 2021 16:10:14 +0800 Subject: [PATCH 09/24] Added behavioral tests for watchonly balances. --- .../__tests__/category/wallet/getBalances.test.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index e8fffe6302..da02b34ca9 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -1,7 +1,7 @@ import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' import { BigNumber } from '../../../src' -import { WalletFlag } from '../../../src/category/wallet' +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', () => { @@ -54,7 +54,18 @@ describe('getBalances on masternode', () => { expect(balance.mine.trusted.toNumber() - newBalance.mine.trusted.toNumber()).toBeGreaterThan(10000) }) - // TODO(aikchun) + it('test watchOnly', async () => { + await container.call('importaddress', ['bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2']) + const balances: WalletBalances = await client.wallet.getBalances() + + if (balances.watchonly != null) { + expect(balances.watchonly.trusted instanceof BigNumber).toStrictEqual(true) + expect(balances.watchonly.untrusted_pending instanceof BigNumber).toStrictEqual(true) + expect(balances.watchonly.immature instanceof BigNumber).toStrictEqual(true) + } else { + throw new Error('expected watchonly to be truthy') + } + }) }) describe('getBalances without masternode', () => { From 1ebbc2718be4f592868991b6aaa7967d7474c189 Mon Sep 17 00:00:00 2001 From: aikchun Date: Sat, 12 Jun 2021 16:18:00 +0800 Subject: [PATCH 10/24] Changed the assertion methodology for BigNumber type --- .../category/wallet/getBalances.test.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index da02b34ca9..847f247f09 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -19,10 +19,10 @@ describe('getBalances on masternode', () => { }) it('should getBalances', async () => { - const balances = await client.wallet.getBalances() - expect(balances.mine.trusted).toBeInstanceOf(BigNumber) - expect(balances.mine.untrusted_pending).toBeInstanceOf(BigNumber) - expect(balances.mine.immature).toBeInstanceOf(BigNumber) + const balances: WalletBalances = await client.wallet.getBalances() + expect(balances.mine.trusted instanceof BigNumber).toStrictEqual(true) + expect(balances.mine.untrusted_pending instanceof BigNumber).toStrictEqual(true) + expect(balances.mine.immature instanceof BigNumber).toStrictEqual(true) expect(typeof balances.mine.used).toStrictEqual('undefined') expect(typeof balances.watchonly).toStrictEqual('undefined') @@ -31,25 +31,25 @@ describe('getBalances on masternode', () => { it('should have used in getBalances when wallet is set to avoid_reuse', async () => { await client.wallet.setWalletFlag(WalletFlag.AVOID_REUSE) await container.generate(1) - const balances = await client.wallet.getBalances() + const balances: WalletBalances = await client.wallet.getBalances() - expect(balances.mine.trusted).toBeInstanceOf(BigNumber) - expect(balances.mine.untrusted_pending).toBeInstanceOf(BigNumber) - expect(balances.mine.immature).toBeInstanceOf(BigNumber) - expect(balances.mine.used).toBeInstanceOf(BigNumber) + expect(balances.mine.trusted instanceof BigNumber).toStrictEqual(true) + expect(balances.mine.untrusted_pending instanceof BigNumber).toStrictEqual(true) + expect(balances.mine.immature instanceof BigNumber).toStrictEqual(true) + expect(balances.mine.used instanceof BigNumber).toStrictEqual(true) expect(typeof balances.watchonly).toStrictEqual('undefined') }) it('should show balances after sending the amount out', async () => { - const balance = await client.wallet.getBalances() + const balance: WalletBalances = await client.wallet.getBalances() const address = 'bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2' await client.wallet.sendToAddress(address, 10000) await container.generate(1) - const newBalance = await client.wallet.getBalances() + const newBalance: WalletBalances = await client.wallet.getBalances() expect(balance.mine.trusted.toNumber() - newBalance.mine.trusted.toNumber()).toBeGreaterThan(10000) }) @@ -82,7 +82,7 @@ describe('getBalances without masternode', () => { }) it('should getBalances.mine.trusted = 0', async () => { - const balances = await client.wallet.getBalances() + const balances: WalletBalances = await client.wallet.getBalances() expect(balances.mine.trusted.toNumber()).toStrictEqual(0) }) From 08d6992f678f5f7ff99b4b42a76aada67659da7b Mon Sep 17 00:00:00 2001 From: aikchun Date: Mon, 14 Jun 2021 16:06:36 +0800 Subject: [PATCH 11/24] Change assertion to use the BigNumber API isEqualTo. --- .../__tests__/category/wallet/getBalances.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index 847f247f09..97150058e2 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -84,6 +84,6 @@ describe('getBalances without masternode', () => { it('should getBalances.mine.trusted = 0', async () => { const balances: WalletBalances = await client.wallet.getBalances() - expect(balances.mine.trusted.toNumber()).toStrictEqual(0) + expect(balances.mine.trusted.isEqualTo(new BigNumber('0'))).toStrictEqual(true) }) }) From 618d9f5d484d1acc6e9b117e207d6508372d8e94 Mon Sep 17 00:00:00 2001 From: aikchun Date: Tue, 15 Jun 2021 11:07:06 +0800 Subject: [PATCH 12/24] Added tests for the withTokens set to true --- .../category/wallet/getBalances.test.ts | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index 97150058e2..8dd5a8cd7e 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -20,9 +20,9 @@ describe('getBalances on masternode', () => { it('should getBalances', async () => { const balances: WalletBalances = await client.wallet.getBalances() - expect(balances.mine.trusted instanceof BigNumber).toStrictEqual(true) - expect(balances.mine.untrusted_pending instanceof BigNumber).toStrictEqual(true) - expect(balances.mine.immature instanceof BigNumber).toStrictEqual(true) + 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') @@ -33,25 +33,23 @@ describe('getBalances on masternode', () => { await container.generate(1) const balances: WalletBalances = await client.wallet.getBalances() - expect(balances.mine.trusted instanceof BigNumber).toStrictEqual(true) - expect(balances.mine.untrusted_pending instanceof BigNumber).toStrictEqual(true) - expect(balances.mine.immature instanceof BigNumber).toStrictEqual(true) - expect(balances.mine.used instanceof BigNumber).toStrictEqual(true) + 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') }) it('should show balances after sending the amount out', async () => { - const balance: WalletBalances = await client.wallet.getBalances() + const balanceBefore: WalletBalances = await client.wallet.getBalances() - const address = 'bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2' - - await client.wallet.sendToAddress(address, 10000) + await client.wallet.sendToAddress('bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2', 10000) await container.generate(1) - const newBalance: WalletBalances = await client.wallet.getBalances() + const balanceAfter: WalletBalances = await client.wallet.getBalances() - expect(balance.mine.trusted.toNumber() - newBalance.mine.trusted.toNumber()).toBeGreaterThan(10000) + expect(balanceBefore.mine.trusted.gt(balanceAfter.mine.trusted.toNumber())).toStrictEqual(true) }) it('test watchOnly', async () => { @@ -59,13 +57,35 @@ describe('getBalances on masternode', () => { const balances: WalletBalances = await client.wallet.getBalances() if (balances.watchonly != null) { - expect(balances.watchonly.trusted instanceof BigNumber).toStrictEqual(true) - expect(balances.watchonly.untrusted_pending instanceof BigNumber).toStrictEqual(true) - expect(balances.watchonly.immature instanceof BigNumber).toStrictEqual(true) + 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) } else { throw new Error('expected watchonly to be truthy') } }) + + it('should show balances with withTokens set to true', async () => { + const balances: WalletBalances = await client.wallet.getBalances(true) + + // balances.mine.trusted { '0': BigNumber { s: 1, e: 8, c: [ 199990075, 99995580000000 ] } } + + // balances.mine.untrusted_pending { '0': BigNumber { s: 1, e: 0, c: [ 0 ] } } + + // balances.mine.immature BigNumber { s: 1, e: 3, c: [ 3418, 34000000000000 ] } + + expect(BigNumber.isBigNumber(balances.mine.trusted)).toStrictEqual(false) + expect(BigNumber.isBigNumber(balances.mine.untrusted_pending)).toStrictEqual(false) + expect(BigNumber.isBigNumber(balances.mine.immature)).toStrictEqual(true) + + if (balances.watchonly != null) { // type guard + expect(BigNumber.isBigNumber(balances.watchonly.trusted)).toStrictEqual(false) + expect(BigNumber.isBigNumber(balances.watchonly.untrusted_pending)).toStrictEqual(false) + expect(BigNumber.isBigNumber(balances.watchonly.immature)).toStrictEqual(true) + } else { + throw new Error('expected balances.watchonly to be truthy') + } + }) }) describe('getBalances without masternode', () => { From 092218cc326f770d0b5d2096fc419dc7efc3b18e Mon Sep 17 00:00:00 2001 From: aikchun Date: Thu, 17 Jun 2021 14:57:34 +0800 Subject: [PATCH 13/24] REmoved test regarding with_tokens as it is not standard in Bitcoin. --- .../category/wallet/getBalances.test.ts | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index 8dd5a8cd7e..e9b6f829a4 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -64,28 +64,6 @@ describe('getBalances on masternode', () => { throw new Error('expected watchonly to be truthy') } }) - - it('should show balances with withTokens set to true', async () => { - const balances: WalletBalances = await client.wallet.getBalances(true) - - // balances.mine.trusted { '0': BigNumber { s: 1, e: 8, c: [ 199990075, 99995580000000 ] } } - - // balances.mine.untrusted_pending { '0': BigNumber { s: 1, e: 0, c: [ 0 ] } } - - // balances.mine.immature BigNumber { s: 1, e: 3, c: [ 3418, 34000000000000 ] } - - expect(BigNumber.isBigNumber(balances.mine.trusted)).toStrictEqual(false) - expect(BigNumber.isBigNumber(balances.mine.untrusted_pending)).toStrictEqual(false) - expect(BigNumber.isBigNumber(balances.mine.immature)).toStrictEqual(true) - - if (balances.watchonly != null) { // type guard - expect(BigNumber.isBigNumber(balances.watchonly.trusted)).toStrictEqual(false) - expect(BigNumber.isBigNumber(balances.watchonly.untrusted_pending)).toStrictEqual(false) - expect(BigNumber.isBigNumber(balances.watchonly.immature)).toStrictEqual(true) - } else { - throw new Error('expected balances.watchonly to be truthy') - } - }) }) describe('getBalances without masternode', () => { From 14164f1313d507a58618acfe63e4792c3ef2a227 Mon Sep 17 00:00:00 2001 From: aikchun Date: Mon, 21 Jun 2021 09:45:16 +0800 Subject: [PATCH 14/24] Removed full-stop from method description. --- website/docs/jellyfish/api/wallet.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/jellyfish/api/wallet.md b/website/docs/jellyfish/api/wallet.md index 9dbdf533da..478a21a9b4 100644 --- a/website/docs/jellyfish/api/wallet.md +++ b/website/docs/jellyfish/api/wallet.md @@ -40,7 +40,7 @@ interface wallet { ## getBalances -Returns an object with all balances. +Returns an object with all balances ```ts title="client.wallet.getBalances()" interface wallet { From e632c5593dca9d63ea0b445eec580f4ee90e57e1 Mon Sep 17 00:00:00 2001 From: aikchun Date: Mon, 21 Jun 2021 10:29:13 +0800 Subject: [PATCH 15/24] Changed the method docs to follow JSDoc convention for optional default values. --- packages/jellyfish-api-core/src/category/wallet.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/src/category/wallet.ts b/packages/jellyfish-api-core/src/category/wallet.ts index f50a3a89f0..1cd1d05b88 100644 --- a/packages/jellyfish-api-core/src/category/wallet.ts +++ b/packages/jellyfish-api-core/src/category/wallet.ts @@ -75,7 +75,7 @@ export class Wallet { /** * Returns an object with all balances * - * @param {boolean} withTokens = false, Include tokens balances; Default is 'false' for backward compatibility. + * @param {boolean} [withTokens=false] - Include tokens balances; Default is 'false' for backward compatibility. * @return {Promise} */ async getBalances (withTokens = false): Promise { From 7d8f9da9b107a47952225a32e2f8f38b61bbb406 Mon Sep 17 00:00:00 2001 From: aikchun Date: Mon, 21 Jun 2021 10:33:16 +0800 Subject: [PATCH 16/24] Changed up the test style to test for the balances in watchonly --- .../__tests__/category/wallet/getBalances.test.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index e9b6f829a4..d1d6f452af 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -52,16 +52,19 @@ describe('getBalances on masternode', () => { expect(balanceBefore.mine.trusted.gt(balanceAfter.mine.trusted.toNumber())).toStrictEqual(true) }) - it('test watchOnly', async () => { + it.only('test watchOnly', async () => { await container.call('importaddress', ['bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2']) + const balances: WalletBalances = await client.wallet.getBalances() - if (balances.watchonly != null) { + expect(balances.watchonly).toBeTruthy() + + if (balances.watchonly == null) { // type guard needed as `balances.watchonly` is optional + throw new Error('expected truthy value') + } else { 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) - } else { - throw new Error('expected watchonly to be truthy') } }) }) From 968b8d656518fd432733bfb933f176a7d3ce61cf Mon Sep 17 00:00:00 2001 From: aikchun Date: Mon, 21 Jun 2021 10:38:23 +0800 Subject: [PATCH 17/24] removed the .only cause --- .../__tests__/category/wallet/getBalances.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index d1d6f452af..d6ca3acdc3 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -52,7 +52,7 @@ describe('getBalances on masternode', () => { expect(balanceBefore.mine.trusted.gt(balanceAfter.mine.trusted.toNumber())).toStrictEqual(true) }) - it.only('test watchOnly', async () => { + it('test watchOnly', async () => { await container.call('importaddress', ['bcrt1q2tke5fa7wx26m684d7yuyt85rvjl36u6q8l6e2']) const balances: WalletBalances = await client.wallet.getBalances() From 8b1d468ad99ffdf5313d72ec78a8807b16b6072e Mon Sep 17 00:00:00 2001 From: aikchun Date: Mon, 21 Jun 2021 10:41:13 +0800 Subject: [PATCH 18/24] made consistent with the formatting. --- website/docs/jellyfish/api/wallet.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/jellyfish/api/wallet.md b/website/docs/jellyfish/api/wallet.md index 478a21a9b4..68c1b4696d 100644 --- a/website/docs/jellyfish/api/wallet.md +++ b/website/docs/jellyfish/api/wallet.md @@ -40,11 +40,11 @@ interface wallet { ## getBalances -Returns an object with all balances +Returns an object with all balances. ```ts title="client.wallet.getBalances()" interface wallet { - getBalances(withTokens = false): Promise + getBalances (withTokens = false): Promise } interface WalletBalances { From fc9609b703558e6864b683d66c1b5f139c67874f Mon Sep 17 00:00:00 2001 From: aikchun Date: Mon, 21 Jun 2021 12:37:39 +0800 Subject: [PATCH 19/24] Changed optional chaining to avoid no-non-null-assertion --- .../__tests__/category/wallet/getBalances.test.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index d6ca3acdc3..d5ad9e52b5 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -57,15 +57,9 @@ describe('getBalances on masternode', () => { const balances: WalletBalances = await client.wallet.getBalances() - expect(balances.watchonly).toBeTruthy() - - if (balances.watchonly == null) { // type guard needed as `balances.watchonly` is optional - throw new Error('expected truthy value') - } else { - 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) - } + 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) }) }) From 9e6f22faf53ab3f5d74f772b17ecc32d47425040 Mon Sep 17 00:00:00 2001 From: aikchun Date: Mon, 21 Jun 2021 13:03:48 +0800 Subject: [PATCH 20/24] Removed the options of withTokens in the parameter. Updated the docs. --- packages/jellyfish-api-core/src/category/wallet.ts | 7 +++---- website/docs/jellyfish/api/wallet.md | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/wallet.ts b/packages/jellyfish-api-core/src/category/wallet.ts index 1cd1d05b88..74f04a6889 100644 --- a/packages/jellyfish-api-core/src/category/wallet.ts +++ b/packages/jellyfish-api-core/src/category/wallet.ts @@ -73,13 +73,12 @@ export class Wallet { } /** - * Returns an object with all balances + * Returns an object with all balances. * - * @param {boolean} [withTokens=false] - Include tokens balances; Default is 'false' for backward compatibility. * @return {Promise} */ - async getBalances (withTokens = false): Promise { - return await this.client.call('getbalances', [withTokens], 'bignumber') + async getBalances (): Promise { + return await this.client.call('getbalances', [false], 'bignumber') } /** diff --git a/website/docs/jellyfish/api/wallet.md b/website/docs/jellyfish/api/wallet.md index 68c1b4696d..eb7082f9f1 100644 --- a/website/docs/jellyfish/api/wallet.md +++ b/website/docs/jellyfish/api/wallet.md @@ -44,7 +44,7 @@ Returns an object with all balances. ```ts title="client.wallet.getBalances()" interface wallet { - getBalances (withTokens = false): Promise + getBalances (): Promise } interface WalletBalances { From af6dfc7e2365f1b7c2ebde6868c72a57041c01b2 Mon Sep 17 00:00:00 2001 From: aikchun Date: Mon, 21 Jun 2021 13:29:49 +0800 Subject: [PATCH 21/24] Moved test where wallet is set to avoid_reuse into a separate describe block. --- .../category/wallet/getBalances.test.ts | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index d5ad9e52b5..681ec782c1 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -28,19 +28,6 @@ describe('getBalances on masternode', () => { expect(typeof balances.watchonly).toStrictEqual('undefined') }) - it('should have used in getBalances when wallet is set to avoid_reuse', async () => { - await client.wallet.setWalletFlag(WalletFlag.AVOID_REUSE) - await container.generate(1) - 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') - }) - it('should show balances after sending the amount out', async () => { const balanceBefore: WalletBalances = await client.wallet.getBalances() @@ -82,3 +69,31 @@ describe('getBalances without masternode', () => { 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() + }) + + afterAll(async () => { + await container.stop() + }) + + it('should have used', async () => { + await client.wallet.setWalletFlag(WalletFlag.AVOID_REUSE) + await container.generate(1) + 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') + }) +}) From 9ff31e4577291e50ba7939e525f4d4d13aabc2e3 Mon Sep 17 00:00:00 2001 From: aikchun Date: Mon, 21 Jun 2021 13:36:43 +0800 Subject: [PATCH 22/24] Fixed assert to compare balanceBefore and balanceAfter using BigNumber instead of converting it into number. --- .../__tests__/category/wallet/getBalances.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index 681ec782c1..5e5d299623 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -36,7 +36,7 @@ describe('getBalances on masternode', () => { const balanceAfter: WalletBalances = await client.wallet.getBalances() - expect(balanceBefore.mine.trusted.gt(balanceAfter.mine.trusted.toNumber())).toStrictEqual(true) + expect(balanceBefore.mine.trusted.gt(balanceAfter.mine.trusted)).toStrictEqual(true) }) it('test watchOnly', async () => { From c862b1ae28725722559a8c15119ad7a4adee9e5e Mon Sep 17 00:00:00 2001 From: aikchun Date: Tue, 22 Jun 2021 21:19:10 +0800 Subject: [PATCH 23/24] Import BigNumber directly from dependency. --- .../__tests__/category/wallet/getBalances.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index 5e5d299623..bb22364ed5 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -1,6 +1,6 @@ import { MasterNodeRegTestContainer, RegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' -import { BigNumber } from '../../../src' +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 From b99982028e69f77710147e5cfd30ebc40c1944d7 Mon Sep 17 00:00:00 2001 From: aikchun Date: Tue, 22 Jun 2021 21:20:25 +0800 Subject: [PATCH 24/24] Moved wallet flag set to avoid_reuse into beforeAll --- .../__tests__/category/wallet/getBalances.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts index bb22364ed5..1bd4cf7a29 100644 --- a/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/wallet/getBalances.test.ts @@ -78,6 +78,8 @@ describe('getBalances when wallet is set to avoid_reuse', () => { await container.start() await container.waitForReady() await container.waitForWalletCoinbaseMaturity() + await client.wallet.setWalletFlag(WalletFlag.AVOID_REUSE) + await container.generate(1) }) afterAll(async () => { @@ -85,8 +87,6 @@ describe('getBalances when wallet is set to avoid_reuse', () => { }) it('should have used', async () => { - await client.wallet.setWalletFlag(WalletFlag.AVOID_REUSE) - await container.generate(1) const balances: WalletBalances = await client.wallet.getBalances() expect(BigNumber.isBigNumber(balances.mine.trusted)).toStrictEqual(true)