From 9b961d799e0a7c17312b669f9cb0bd3b99866e13 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Thu, 27 May 2021 12:19:10 +0800 Subject: [PATCH 01/13] Refactor account rpcs into account folder --- .../category/account/getAccount.test.ts | 127 ++++++++++++ .../category/account/listAccounts.test.ts | 195 ++++++++++++++++++ 2 files changed, 322 insertions(+) create mode 100644 packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts create mode 100644 packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts diff --git a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts new file mode 100644 index 0000000000..34746e424d --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts @@ -0,0 +1,127 @@ +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' +import waitForExpect from 'wait-for-expect' + +describe('masternode', () => { + const container = new MasterNodeRegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() + + await setup() + }) + + afterAll(async () => { + await container.stop() + }) + + async function setup (): Promise { + const from = await container.call('getnewaddress') + await createToken(from, 'DBTC', 200) + + const to = await accountToAccount('DBTC', 5, from) + await accountToAccount('DBTC', 18, from, to) + + await createToken(from, 'DETH', 200) + await accountToAccount('DETH', 46, from) + } + + async function createToken (address: string, symbol: string, amount: number): Promise { + const metadata = { + symbol, + name: symbol, + isDAT: true, + mintable: true, + tradeable: true, + collateralAddress: address + } + await container.waitForWalletBalanceGTE(101) + await container.call('createtoken', [metadata]) + await container.generate(1) + + await container.call('minttokens', [`${amount.toString()}@${symbol}`]) + await container.generate(1) + } + + async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise { + const to = _to !== '' ? _to : await container.call('getnewaddress') + + await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) + await container.generate(1) + + return to + } + + async function waitForListingAccounts (): Promise { + let accounts: any[] = [] + + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + return accounts + } + + describe('getAccount', () => { + it('should getAccount', async () => { + const accounts = await waitForListingAccounts() + + // [ '187.00000000@DBTC', '154.00000000@DETH' ] + const account = await client.account.getAccount(accounts[0].owner.addresses[0]) + expect(account.length).toBeGreaterThan(0) + for (let i = 0; i < account.length; i += 1) { + expect(typeof account[i]).toStrictEqual('string') + } + }) + + it('should getAccount with pagination start and including_start', async () => { + let accounts: any[] = [] + let beforeAccountCount = 0 + + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + + const account = await client.account.getAccount(accounts[0].owner.addresses[0]) + beforeAccountCount = account.length + }) + + const pagination = { + start: beforeAccountCount, + including_start: true + } + + // [ '187.00000000@DBTC', '154.00000000@DETH' ] + const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) + expect(account.length).toStrictEqual(1) + + for (let i = 0; i < account.length; i += 1) { + expect(typeof account[i]).toStrictEqual('string') + } + }) + + it('should getAccount with pagination.limit', async () => { + const accounts = await waitForListingAccounts() + + const pagination = { + limit: 1 + } + const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) + expect(account.length).toStrictEqual(1) + }) + + it('should getAccount with indexedAmount true', async () => { + const accounts = await waitForListingAccounts() + + const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: true }) + expect(typeof account).toStrictEqual('object') + for (const k in account) { + expect(typeof account[k]).toStrictEqual('number') + } + }) + }) +}) diff --git a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts new file mode 100644 index 0000000000..3a12c96e93 --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts @@ -0,0 +1,195 @@ +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' +import waitForExpect from 'wait-for-expect' +import BigNumber from 'bignumber.js' + +describe('masternode', () => { + const container = new MasterNodeRegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() + + await setup() + }) + + afterAll(async () => { + await container.stop() + }) + + async function setup (): Promise { + const from = await container.call('getnewaddress') + await createToken(from, 'DBTC', 200) + + const to = await accountToAccount('DBTC', 5, from) + await accountToAccount('DBTC', 18, from, to) + + await createToken(from, 'DETH', 200) + await accountToAccount('DETH', 46, from) + } + + async function createToken (address: string, symbol: string, amount: number): Promise { + const metadata = { + symbol, + name: symbol, + isDAT: true, + mintable: true, + tradeable: true, + collateralAddress: address + } + await container.waitForWalletBalanceGTE(101) + await container.call('createtoken', [metadata]) + await container.generate(1) + + await container.call('minttokens', [`${amount.toString()}@${symbol}`]) + await container.generate(1) + } + + async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise { + const to = _to !== '' ? _to : await container.call('getnewaddress') + + await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) + await container.generate(1) + + return to + } + + async function waitForListingAccounts (): Promise { + let accounts: any[] = [] + + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) + }) + + return accounts + } + + describe('listAccounts', () => { + it('should listAccounts', async () => { + await waitForListingAccounts() + + const accounts = await client.account.listAccounts() + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner === 'object').toStrictEqual(true) + expect(typeof account.owner.asm).toStrictEqual('string') + expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) + expect(typeof account.owner.type).toStrictEqual('string') + expect(account.owner.addresses.length).toBeGreaterThan(0) + expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + } + }) + + it('should listAccounts with pagination start and including_start', async () => { + const accounts = await waitForListingAccounts() + + const pagination = { + start: accounts[accounts.length - 1].key, + including_start: true + } + + const lastAccounts = await client.account.listAccounts(pagination) + expect(lastAccounts.length).toStrictEqual(1) + }) + + it('should listAccounts with pagination.limit', async () => { + await waitForExpect(async () => { + const pagination = { + limit: 2 + } + const accounts = await client.account.listAccounts(pagination) + expect(accounts.length).toStrictEqual(2) + }) + }) + + it('should listAccounts with verbose false and indexed_amounts false', async () => { + await waitForListingAccounts() + + const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner).toStrictEqual('string') + expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + } + }) + + it('should listAccounts with verbose false and indexed_amounts true', async () => { + await waitForListingAccounts() + + const accounts = await client.account.listAccounts({}, false, { indexedAmounts: true, isMineOnly: false }) + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner).toStrictEqual('string') + + expect(typeof account.amount === 'object').toStrictEqual(true) + for (const k in account.amount) { + expect(account.amount[k] instanceof BigNumber).toStrictEqual(true) // [{'0': 100}] + } + } + }) + + it('should listAccounts with verbose true and indexed_amounts true', async () => { + await waitForListingAccounts() + + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: true, isMineOnly: false }) + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner === 'object').toStrictEqual(true) + expect(typeof account.owner.asm).toStrictEqual('string') + expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) + expect(typeof account.owner.type).toStrictEqual('string') + expect(account.owner.addresses.length).toBeGreaterThan(0) + + expect(typeof account.amount === 'object').toStrictEqual(true) + for (const k in account.amount) { + expect(account.amount[k] instanceof BigNumber).toStrictEqual(true) // [{'0': 100}] + } + } + }) + + it('should listAccounts with verbose true and indexed_amounts false', async () => { + await waitForListingAccounts() + + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner === 'object').toStrictEqual(true) + expect(typeof account.owner.asm).toStrictEqual('string') + expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) + expect(typeof account.owner.type).toStrictEqual('string') + expect(account.owner.addresses.length).toBeGreaterThan(0) + expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + } + }) + + it('should listAccounts with isMineOnly true', async () => { + await waitForListingAccounts() + + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner === 'object').toStrictEqual(true) + expect(typeof account.owner.asm).toStrictEqual('string') + expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) + expect(typeof account.owner.type).toStrictEqual('string') + expect(account.owner.addresses.length).toBeGreaterThan(0) + expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + } + }) + }) +}) From 40eb81f95c3192a68831c920ab08590c97faedb5 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Thu, 27 May 2021 15:03:48 +0800 Subject: [PATCH 02/13] Quick push --- .../category/account/getAccount.test.ts | 42 +----- .../account/getAccountHistoryCount.test.ts | 136 ++++++++++++++++++ .../category/account/getTokenBalances.test.ts | 104 ++++++++++++++ .../category/account/listAccounts.test.ts | 49 +------ 4 files changed, 248 insertions(+), 83 deletions(-) create mode 100644 packages/jellyfish-api-core/__tests__/category/account/getAccountHistoryCount.test.ts create mode 100644 packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts diff --git a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts index 34746e424d..cfd1a2263c 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts @@ -10,25 +10,13 @@ describe('masternode', () => { await container.start() await container.waitForReady() await container.waitForWalletCoinbaseMaturity() - - await setup() + await createToken(await container.call('getnewaddress'), 'DBTC', 200) }) afterAll(async () => { await container.stop() }) - async function setup (): Promise { - const from = await container.call('getnewaddress') - await createToken(from, 'DBTC', 200) - - const to = await accountToAccount('DBTC', 5, from) - await accountToAccount('DBTC', 18, from, to) - - await createToken(from, 'DETH', 200) - await accountToAccount('DETH', 46, from) - } - async function createToken (address: string, symbol: string, amount: number): Promise { const metadata = { symbol, @@ -38,7 +26,7 @@ describe('masternode', () => { tradeable: true, collateralAddress: address } - await container.waitForWalletBalanceGTE(101) + await container.waitForWalletBalanceGTE(100) await container.call('createtoken', [metadata]) await container.generate(1) @@ -46,29 +34,9 @@ describe('masternode', () => { await container.generate(1) } - async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise { - const to = _to !== '' ? _to : await container.call('getnewaddress') - - await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) - await container.generate(1) - - return to - } - - async function waitForListingAccounts (): Promise { - let accounts: any[] = [] - - await waitForExpect(async () => { - accounts = await client.account.listAccounts() - expect(accounts.length).toBeGreaterThan(0) - }) - - return accounts - } - describe('getAccount', () => { it('should getAccount', async () => { - const accounts = await waitForListingAccounts() + const accounts = await client.account.listAccounts() // [ '187.00000000@DBTC', '154.00000000@DETH' ] const account = await client.account.getAccount(accounts[0].owner.addresses[0]) @@ -105,7 +73,7 @@ describe('masternode', () => { }) it('should getAccount with pagination.limit', async () => { - const accounts = await waitForListingAccounts() + const accounts = await client.account.listAccounts() const pagination = { limit: 1 @@ -115,7 +83,7 @@ describe('masternode', () => { }) it('should getAccount with indexedAmount true', async () => { - const accounts = await waitForListingAccounts() + const accounts = await client.account.listAccounts() const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: true }) expect(typeof account).toStrictEqual('object') diff --git a/packages/jellyfish-api-core/__tests__/category/account/getAccountHistoryCount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getAccountHistoryCount.test.ts new file mode 100644 index 0000000000..d5c30c64ab --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/account/getAccountHistoryCount.test.ts @@ -0,0 +1,136 @@ +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' +import waitForExpect from 'wait-for-expect' +import { AccountHistoryCountOptions, TxType } from '../../../src/category/account' + +describe('masternode', () => { + const container = new MasterNodeRegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() + + await setup() + }) + + afterAll(async () => { + await container.stop() + }) + + async function setup (): Promise { + const from = await container.call('getnewaddress') + await createToken(from, 'DBTC', 200) + + const to = await accountToAccount('DBTC', 5, from) + await accountToAccount('DBTC', 18, from, to) + + await createToken(from, 'DETH', 200) + await accountToAccount('DETH', 46, from) + } + + async function createToken (address: string, symbol: string, amount: number): Promise { + const metadata = { + symbol, + name: symbol, + isDAT: true, + mintable: true, + tradeable: true, + collateralAddress: address + } + await container.waitForWalletBalanceGTE(101) + await container.call('createtoken', [metadata]) + await container.generate(1) + + await container.call('minttokens', [`${amount.toString()}@${symbol}`]) + await container.generate(1) + } + + async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise { + const to = _to !== '' ? _to : await container.call('getnewaddress') + + await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) + await container.generate(1) + + return to + } + + describe('accountHistoryCount', () => { + it('should get accountHistoryCount', async () => { + await waitForExpect(async () => { + const count = await client.account.historyCount() + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get accountHistoryCount with owner as all', async () => { + await waitForExpect(async () => { + const count = await client.account.historyCount('all') + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get accountHistoryCount with no_rewards option', async () => { + await waitForExpect(async () => { + const options: AccountHistoryCountOptions = { + no_rewards: true + } + const count = await client.account.historyCount('mine', options) + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get accountHistoryCount with token option', async () => { + await waitForExpect(async () => { + const options1: AccountHistoryCountOptions = { + token: 'DBTC' + } + const options2: AccountHistoryCountOptions = { + token: 'DETH' + } + const countWithDBTC = await client.account.historyCount('mine', options1) + const countWithDETH = await client.account.historyCount('mine', options2) + + expect(typeof countWithDBTC).toBe('number') + expect(typeof countWithDETH).toBe('number') + expect(countWithDBTC).toStrictEqual(5) + expect(countWithDETH).toStrictEqual(3) + }) + }) + + it('should get accountHistory with txtype option', async () => { + await waitForExpect(async () => { + const options: AccountHistoryCountOptions = { + txtype: TxType.MINT_TOKEN + } + const count = await client.account.historyCount('mine', options) + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get different count for different txtypes', async () => { + await waitForExpect(async () => { + const options1: AccountHistoryCountOptions = { + txtype: TxType.MINT_TOKEN + } + const options2: AccountHistoryCountOptions = { + txtype: TxType.POOL_SWAP + + } + const count1 = await client.account.historyCount('mine', options1) + const count2 = await client.account.historyCount('mine', options2) + + expect(count1 === count2).toStrictEqual(false) + }) + }) + }) +}) diff --git a/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts new file mode 100644 index 0000000000..b605f36ed8 --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts @@ -0,0 +1,104 @@ +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' +import waitForExpect from 'wait-for-expect' +import BigNumber from 'bignumber.js' + +describe('masternode', () => { + const container = new MasterNodeRegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() + await createToken(await container.call('getnewaddress'), 'DBTC', 200) + await createToken(await container.call('getnewaddress'), 'DETH', 200) + }) + + afterAll(async () => { + await container.stop() + }) + + async function createToken (address: string, symbol: string, amount: number): Promise { + const metadata = { + symbol, + name: symbol, + isDAT: true, + mintable: true, + tradeable: true, + collateralAddress: address + } + await container.waitForWalletBalanceGTE(101) + await container.call('createtoken', [metadata]) + await container.generate(1) + + await container.call('minttokens', [`${amount.toString()}@${symbol}`]) + await container.generate(1) + } + + describe('getTokenBalances', () => { + it('should getTokenBalances', async () => { + await waitForExpect(async () => { + const tokenBalances = await client.account.getTokenBalances() + expect(tokenBalances.length).toBeGreaterThan(0) + }) + + const tokenBalances = await client.account.getTokenBalances() + for (let i = 0; i < tokenBalances.length; i += 1) { + expect(typeof tokenBalances[i]).toStrictEqual('string') // [ '300.00000000@0', '200.00000000@1' ] + } + }) + + it('should getTokenBalances with pagination start and including_start', async () => { + let id = '' + + await waitForExpect(async () => { + const tokenBalances = await client.account.getTokenBalances() // [ '300.00000000@0', '200.00000000@1' ] + expect(tokenBalances.length).toBeGreaterThan(0) + + id = tokenBalances[tokenBalances.length - 1].split('@')[1] + }) + + const pagination = { + start: Number(id), + including_start: true + } + const tokenBalances = await client.account.getTokenBalances(pagination) + expect(tokenBalances.length).toStrictEqual(1) + }) + + it('should getTokenBalances with pagination limit', async () => { + await waitForExpect(async () => { + const tokenBalances = await client.account.getTokenBalances() + expect(tokenBalances.length).toStrictEqual(2) + }) + const pagination = { + limit: 1 + } + const tokenBalances = await client.account.getTokenBalances(pagination) + expect(tokenBalances.length).toStrictEqual(1) + }) + + it('should getTokenBalances with indexedAmounts true', async () => { + await waitForExpect(async () => { + const tokenBalances = await client.account.getTokenBalances({}, true, { symbolLookup: false }) + expect(typeof tokenBalances === 'object').toStrictEqual(true) + for (const k in tokenBalances) { + expect(tokenBalances[k] instanceof BigNumber).toStrictEqual(true) + } + }) + }) + + it('should getTokenBalances with symbolLookup', async () => { + await waitForExpect(async () => { + const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: true }) + expect(tokenBalances.length).toBeGreaterThan(0) + }) + + const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: true }) + for (let i = 0; i < tokenBalances.length; i += 1) { + expect(typeof tokenBalances[i]).toStrictEqual('string') // [ '300.00000000@DFI', '200.00000000@DBTC' ] + } + }) + }) +}) diff --git a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts index 3a12c96e93..6275dd18ba 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts @@ -11,25 +11,14 @@ describe('masternode', () => { await container.start() await container.waitForReady() await container.waitForWalletCoinbaseMaturity() - - await setup() + await createToken(await container.call('getnewaddress'), 'DBTC', 200) + await createToken(await container.call('getnewaddress'), 'DETH', 200) }) afterAll(async () => { await container.stop() }) - async function setup (): Promise { - const from = await container.call('getnewaddress') - await createToken(from, 'DBTC', 200) - - const to = await accountToAccount('DBTC', 5, from) - await accountToAccount('DBTC', 18, from, to) - - await createToken(from, 'DETH', 200) - await accountToAccount('DETH', 46, from) - } - async function createToken (address: string, symbol: string, amount: number): Promise { const metadata = { symbol, @@ -47,30 +36,8 @@ describe('masternode', () => { await container.generate(1) } - async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise { - const to = _to !== '' ? _to : await container.call('getnewaddress') - - await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) - await container.generate(1) - - return to - } - - async function waitForListingAccounts (): Promise { - let accounts: any[] = [] - - await waitForExpect(async () => { - accounts = await client.account.listAccounts() - expect(accounts.length).toBeGreaterThan(0) - }) - - return accounts - } - describe('listAccounts', () => { it('should listAccounts', async () => { - await waitForListingAccounts() - const accounts = await client.account.listAccounts() for (let i = 0; i < accounts.length; i += 1) { @@ -86,7 +53,7 @@ describe('masternode', () => { }) it('should listAccounts with pagination start and including_start', async () => { - const accounts = await waitForListingAccounts() + const accounts: any[] = await client.account.listAccounts() const pagination = { start: accounts[accounts.length - 1].key, @@ -108,8 +75,6 @@ describe('masternode', () => { }) it('should listAccounts with verbose false and indexed_amounts false', async () => { - await waitForListingAccounts() - const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) for (let i = 0; i < accounts.length; i += 1) { @@ -121,8 +86,6 @@ describe('masternode', () => { }) it('should listAccounts with verbose false and indexed_amounts true', async () => { - await waitForListingAccounts() - const accounts = await client.account.listAccounts({}, false, { indexedAmounts: true, isMineOnly: false }) for (let i = 0; i < accounts.length; i += 1) { @@ -138,8 +101,6 @@ describe('masternode', () => { }) it('should listAccounts with verbose true and indexed_amounts true', async () => { - await waitForListingAccounts() - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: true, isMineOnly: false }) for (let i = 0; i < accounts.length; i += 1) { @@ -159,8 +120,6 @@ describe('masternode', () => { }) it('should listAccounts with verbose true and indexed_amounts false', async () => { - await waitForListingAccounts() - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) for (let i = 0; i < accounts.length; i += 1) { @@ -176,8 +135,6 @@ describe('masternode', () => { }) it('should listAccounts with isMineOnly true', async () => { - await waitForListingAccounts() - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) for (let i = 0; i < accounts.length; i += 1) { From 691d81062e0f0c0a4c380623392b29df07b67fdf Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Thu, 27 May 2021 15:18:02 +0800 Subject: [PATCH 03/13] Move await container.waitForWalletBalanceGTE(101) to beforeAll --- .../__tests__/category/account/getAccount.test.ts | 2 +- .../__tests__/category/account/getTokenBalances.test.ts | 2 +- .../__tests__/category/account/listAccounts.test.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts index cfd1a2263c..fff90cd82d 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts @@ -10,6 +10,7 @@ describe('masternode', () => { await container.start() await container.waitForReady() await container.waitForWalletCoinbaseMaturity() + await container.waitForWalletBalanceGTE(100) await createToken(await container.call('getnewaddress'), 'DBTC', 200) }) @@ -26,7 +27,6 @@ describe('masternode', () => { tradeable: true, collateralAddress: address } - await container.waitForWalletBalanceGTE(100) await container.call('createtoken', [metadata]) await container.generate(1) diff --git a/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts index b605f36ed8..5c3256e2da 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts @@ -11,6 +11,7 @@ describe('masternode', () => { await container.start() await container.waitForReady() await container.waitForWalletCoinbaseMaturity() + await container.waitForWalletBalanceGTE(100) await createToken(await container.call('getnewaddress'), 'DBTC', 200) await createToken(await container.call('getnewaddress'), 'DETH', 200) }) @@ -28,7 +29,6 @@ describe('masternode', () => { tradeable: true, collateralAddress: address } - await container.waitForWalletBalanceGTE(101) await container.call('createtoken', [metadata]) await container.generate(1) diff --git a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts index 6275dd18ba..778b19c243 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts @@ -11,6 +11,7 @@ describe('masternode', () => { await container.start() await container.waitForReady() await container.waitForWalletCoinbaseMaturity() + await container.waitForWalletBalanceGTE(100) await createToken(await container.call('getnewaddress'), 'DBTC', 200) await createToken(await container.call('getnewaddress'), 'DETH', 200) }) @@ -28,7 +29,6 @@ describe('masternode', () => { tradeable: true, collateralAddress: address } - await container.waitForWalletBalanceGTE(101) await container.call('createtoken', [metadata]) await container.generate(1) From 3f3c4eef62cd88283a74d70a7ebc133079b888c1 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Thu, 27 May 2021 15:39:42 +0800 Subject: [PATCH 04/13] Refactor code --- .../category/account/getAccount.test.ts | 92 ++++---- .../category/account/getTokenBalances.test.ts | 102 +++++---- .../category/account/listAccounts.test.ts | 196 +++++++++--------- 3 files changed, 192 insertions(+), 198 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts index fff90cd82d..c7dbef795d 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts @@ -11,7 +11,7 @@ describe('masternode', () => { await container.waitForReady() await container.waitForWalletCoinbaseMaturity() await container.waitForWalletBalanceGTE(100) - await createToken(await container.call('getnewaddress'), 'DBTC', 200) + await createToken(await container.getNewAddress(), 'DBTC', 200) }) afterAll(async () => { @@ -34,62 +34,60 @@ describe('masternode', () => { await container.generate(1) } - describe('getAccount', () => { - it('should getAccount', async () => { - const accounts = await client.account.listAccounts() + it('should getAccount', async () => { + const accounts = await client.account.listAccounts() - // [ '187.00000000@DBTC', '154.00000000@DETH' ] - const account = await client.account.getAccount(accounts[0].owner.addresses[0]) - expect(account.length).toBeGreaterThan(0) - for (let i = 0; i < account.length; i += 1) { - expect(typeof account[i]).toStrictEqual('string') - } - }) + // [ '187.00000000@DBTC', '154.00000000@DETH' ] + const account = await client.account.getAccount(accounts[0].owner.addresses[0]) + expect(account.length).toBeGreaterThan(0) + for (let i = 0; i < account.length; i += 1) { + expect(typeof account[i]).toStrictEqual('string') + } + }) - it('should getAccount with pagination start and including_start', async () => { - let accounts: any[] = [] - let beforeAccountCount = 0 + it('should getAccount with pagination start and including_start', async () => { + let accounts: any[] = [] + let beforeAccountCount = 0 - await waitForExpect(async () => { - accounts = await client.account.listAccounts() - expect(accounts.length).toBeGreaterThan(0) + await waitForExpect(async () => { + accounts = await client.account.listAccounts() + expect(accounts.length).toBeGreaterThan(0) - const account = await client.account.getAccount(accounts[0].owner.addresses[0]) - beforeAccountCount = account.length - }) + const account = await client.account.getAccount(accounts[0].owner.addresses[0]) + beforeAccountCount = account.length + }) - const pagination = { - start: beforeAccountCount, - including_start: true - } + const pagination = { + start: beforeAccountCount, + including_start: true + } - // [ '187.00000000@DBTC', '154.00000000@DETH' ] - const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) - expect(account.length).toStrictEqual(1) + // [ '187.00000000@DBTC', '154.00000000@DETH' ] + const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) + expect(account.length).toStrictEqual(1) - for (let i = 0; i < account.length; i += 1) { - expect(typeof account[i]).toStrictEqual('string') - } - }) + for (let i = 0; i < account.length; i += 1) { + expect(typeof account[i]).toStrictEqual('string') + } + }) - it('should getAccount with pagination.limit', async () => { - const accounts = await client.account.listAccounts() + it('should getAccount with pagination.limit', async () => { + const accounts = await client.account.listAccounts() - const pagination = { - limit: 1 - } - const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) - expect(account.length).toStrictEqual(1) - }) + const pagination = { + limit: 1 + } + const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) + expect(account.length).toStrictEqual(1) + }) - it('should getAccount with indexedAmount true', async () => { - const accounts = await client.account.listAccounts() + it('should getAccount with indexedAmount true', async () => { + const accounts = await client.account.listAccounts() - const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: true }) - expect(typeof account).toStrictEqual('object') - for (const k in account) { - expect(typeof account[k]).toStrictEqual('number') - } - }) + const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: true }) + expect(typeof account).toStrictEqual('object') + for (const k in account) { + expect(typeof account[k]).toStrictEqual('number') + } }) }) diff --git a/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts index 5c3256e2da..39d1aba8d6 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts @@ -12,8 +12,8 @@ describe('masternode', () => { await container.waitForReady() await container.waitForWalletCoinbaseMaturity() await container.waitForWalletBalanceGTE(100) - await createToken(await container.call('getnewaddress'), 'DBTC', 200) - await createToken(await container.call('getnewaddress'), 'DETH', 200) + await createToken(await container.getNewAddress(), 'DBTC', 200) + await createToken(await container.getNewAddress(), 'DETH', 200) }) afterAll(async () => { @@ -36,69 +36,67 @@ describe('masternode', () => { await container.generate(1) } - describe('getTokenBalances', () => { - it('should getTokenBalances', async () => { - await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances() - expect(tokenBalances.length).toBeGreaterThan(0) - }) - + it('should getTokenBalances', async () => { + await waitForExpect(async () => { const tokenBalances = await client.account.getTokenBalances() - for (let i = 0; i < tokenBalances.length; i += 1) { - expect(typeof tokenBalances[i]).toStrictEqual('string') // [ '300.00000000@0', '200.00000000@1' ] - } + expect(tokenBalances.length).toBeGreaterThan(0) }) - it('should getTokenBalances with pagination start and including_start', async () => { - let id = '' + const tokenBalances = await client.account.getTokenBalances() + for (let i = 0; i < tokenBalances.length; i += 1) { + expect(typeof tokenBalances[i]).toStrictEqual('string') // [ '300.00000000@0', '200.00000000@1' ] + } + }) - await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances() // [ '300.00000000@0', '200.00000000@1' ] - expect(tokenBalances.length).toBeGreaterThan(0) + it('should getTokenBalances with pagination start and including_start', async () => { + let id = '' - id = tokenBalances[tokenBalances.length - 1].split('@')[1] - }) + await waitForExpect(async () => { + const tokenBalances = await client.account.getTokenBalances() // [ '300.00000000@0', '200.00000000@1' ] + expect(tokenBalances.length).toBeGreaterThan(0) - const pagination = { - start: Number(id), - including_start: true - } - const tokenBalances = await client.account.getTokenBalances(pagination) - expect(tokenBalances.length).toStrictEqual(1) + id = tokenBalances[tokenBalances.length - 1].split('@')[1] }) - it('should getTokenBalances with pagination limit', async () => { - await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances() - expect(tokenBalances.length).toStrictEqual(2) - }) - const pagination = { - limit: 1 - } - const tokenBalances = await client.account.getTokenBalances(pagination) - expect(tokenBalances.length).toStrictEqual(1) - }) + const pagination = { + start: Number(id), + including_start: true + } + const tokenBalances = await client.account.getTokenBalances(pagination) + expect(tokenBalances.length).toStrictEqual(1) + }) - it('should getTokenBalances with indexedAmounts true', async () => { - await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances({}, true, { symbolLookup: false }) - expect(typeof tokenBalances === 'object').toStrictEqual(true) - for (const k in tokenBalances) { - expect(tokenBalances[k] instanceof BigNumber).toStrictEqual(true) - } - }) + it('should getTokenBalances with pagination limit', async () => { + await waitForExpect(async () => { + const tokenBalances = await client.account.getTokenBalances() + expect(tokenBalances.length).toStrictEqual(2) }) + const pagination = { + limit: 1 + } + const tokenBalances = await client.account.getTokenBalances(pagination) + expect(tokenBalances.length).toStrictEqual(1) + }) - it('should getTokenBalances with symbolLookup', async () => { - await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: true }) - expect(tokenBalances.length).toBeGreaterThan(0) - }) + it('should getTokenBalances with indexedAmounts true', async () => { + await waitForExpect(async () => { + const tokenBalances = await client.account.getTokenBalances({}, true, { symbolLookup: false }) + expect(typeof tokenBalances === 'object').toStrictEqual(true) + for (const k in tokenBalances) { + expect(tokenBalances[k] instanceof BigNumber).toStrictEqual(true) + } + }) + }) + it('should getTokenBalances with symbolLookup', async () => { + await waitForExpect(async () => { const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: true }) - for (let i = 0; i < tokenBalances.length; i += 1) { - expect(typeof tokenBalances[i]).toStrictEqual('string') // [ '300.00000000@DFI', '200.00000000@DBTC' ] - } + expect(tokenBalances.length).toBeGreaterThan(0) }) + + const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: true }) + for (let i = 0; i < tokenBalances.length; i += 1) { + expect(typeof tokenBalances[i]).toStrictEqual('string') // [ '300.00000000@DFI', '200.00000000@DBTC' ] + } }) }) diff --git a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts index 778b19c243..7f62ced098 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts @@ -12,8 +12,8 @@ describe('masternode', () => { await container.waitForReady() await container.waitForWalletCoinbaseMaturity() await container.waitForWalletBalanceGTE(100) - await createToken(await container.call('getnewaddress'), 'DBTC', 200) - await createToken(await container.call('getnewaddress'), 'DETH', 200) + await createToken(await container.getNewAddress(), 'DBTC', 200) + await createToken(await container.getNewAddress(), 'DETH', 200) }) afterAll(async () => { @@ -36,117 +36,115 @@ describe('masternode', () => { await container.generate(1) } - describe('listAccounts', () => { - it('should listAccounts', async () => { - const accounts = await client.account.listAccounts() - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner === 'object').toStrictEqual(true) - expect(typeof account.owner.asm).toStrictEqual('string') - expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) - expect(typeof account.owner.type).toStrictEqual('string') - expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI - } - }) + it('should listAccounts', async () => { + const accounts = await client.account.listAccounts() + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner === 'object').toStrictEqual(true) + expect(typeof account.owner.asm).toStrictEqual('string') + expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) + expect(typeof account.owner.type).toStrictEqual('string') + expect(account.owner.addresses.length).toBeGreaterThan(0) + expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + } + }) - it('should listAccounts with pagination start and including_start', async () => { - const accounts: any[] = await client.account.listAccounts() + it('should listAccounts with pagination start and including_start', async () => { + const accounts: any[] = await client.account.listAccounts() - const pagination = { - start: accounts[accounts.length - 1].key, - including_start: true - } + const pagination = { + start: accounts[accounts.length - 1].key, + including_start: true + } - const lastAccounts = await client.account.listAccounts(pagination) - expect(lastAccounts.length).toStrictEqual(1) - }) + const lastAccounts = await client.account.listAccounts(pagination) + expect(lastAccounts.length).toStrictEqual(1) + }) - it('should listAccounts with pagination.limit', async () => { - await waitForExpect(async () => { - const pagination = { - limit: 2 - } - const accounts = await client.account.listAccounts(pagination) - expect(accounts.length).toStrictEqual(2) - }) + it('should listAccounts with pagination.limit', async () => { + await waitForExpect(async () => { + const pagination = { + limit: 2 + } + const accounts = await client.account.listAccounts(pagination) + expect(accounts.length).toStrictEqual(2) }) + }) - it('should listAccounts with verbose false and indexed_amounts false', async () => { - const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) + it('should listAccounts with verbose false and indexed_amounts false', async () => { + const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner).toStrictEqual('string') - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI - } - }) + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner).toStrictEqual('string') + expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + } + }) - it('should listAccounts with verbose false and indexed_amounts true', async () => { - const accounts = await client.account.listAccounts({}, false, { indexedAmounts: true, isMineOnly: false }) + it('should listAccounts with verbose false and indexed_amounts true', async () => { + const accounts = await client.account.listAccounts({}, false, { indexedAmounts: true, isMineOnly: false }) - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner).toStrictEqual('string') + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner).toStrictEqual('string') - expect(typeof account.amount === 'object').toStrictEqual(true) - for (const k in account.amount) { - expect(account.amount[k] instanceof BigNumber).toStrictEqual(true) // [{'0': 100}] - } + expect(typeof account.amount === 'object').toStrictEqual(true) + for (const k in account.amount) { + expect(account.amount[k] instanceof BigNumber).toStrictEqual(true) // [{'0': 100}] } - }) + } + }) - it('should listAccounts with verbose true and indexed_amounts true', async () => { - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: true, isMineOnly: false }) - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner === 'object').toStrictEqual(true) - expect(typeof account.owner.asm).toStrictEqual('string') - expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) - expect(typeof account.owner.type).toStrictEqual('string') - expect(account.owner.addresses.length).toBeGreaterThan(0) - - expect(typeof account.amount === 'object').toStrictEqual(true) - for (const k in account.amount) { - expect(account.amount[k] instanceof BigNumber).toStrictEqual(true) // [{'0': 100}] - } + it('should listAccounts with verbose true and indexed_amounts true', async () => { + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: true, isMineOnly: false }) + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner === 'object').toStrictEqual(true) + expect(typeof account.owner.asm).toStrictEqual('string') + expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) + expect(typeof account.owner.type).toStrictEqual('string') + expect(account.owner.addresses.length).toBeGreaterThan(0) + + expect(typeof account.amount === 'object').toStrictEqual(true) + for (const k in account.amount) { + expect(account.amount[k] instanceof BigNumber).toStrictEqual(true) // [{'0': 100}] } - }) + } + }) - it('should listAccounts with verbose true and indexed_amounts false', async () => { - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner === 'object').toStrictEqual(true) - expect(typeof account.owner.asm).toStrictEqual('string') - expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) - expect(typeof account.owner.type).toStrictEqual('string') - expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI - } - }) + it('should listAccounts with verbose true and indexed_amounts false', async () => { + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner === 'object').toStrictEqual(true) + expect(typeof account.owner.asm).toStrictEqual('string') + expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) + expect(typeof account.owner.type).toStrictEqual('string') + expect(account.owner.addresses.length).toBeGreaterThan(0) + expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + } + }) - it('should listAccounts with isMineOnly true', async () => { - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner === 'object').toStrictEqual(true) - expect(typeof account.owner.asm).toStrictEqual('string') - expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) - expect(typeof account.owner.type).toStrictEqual('string') - expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI - } - }) + it('should listAccounts with isMineOnly true', async () => { + const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) + + for (let i = 0; i < accounts.length; i += 1) { + const account = accounts[i] + expect(typeof account.key).toStrictEqual('string') + expect(typeof account.owner === 'object').toStrictEqual(true) + expect(typeof account.owner.asm).toStrictEqual('string') + expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) + expect(typeof account.owner.type).toStrictEqual('string') + expect(account.owner.addresses.length).toBeGreaterThan(0) + expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + } }) }) From ea8f4a0f76b0af83c3ecefb87a215fc93171aeb3 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Thu, 27 May 2021 15:45:15 +0800 Subject: [PATCH 05/13] Temporarily remove this file --- .../account/getAccountHistoryCount.test.ts | 136 ------------------ 1 file changed, 136 deletions(-) delete mode 100644 packages/jellyfish-api-core/__tests__/category/account/getAccountHistoryCount.test.ts diff --git a/packages/jellyfish-api-core/__tests__/category/account/getAccountHistoryCount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getAccountHistoryCount.test.ts deleted file mode 100644 index d5c30c64ab..0000000000 --- a/packages/jellyfish-api-core/__tests__/category/account/getAccountHistoryCount.test.ts +++ /dev/null @@ -1,136 +0,0 @@ -import { MasterNodeRegTestContainer } from '@defichain/testcontainers' -import { ContainerAdapterClient } from '../../container_adapter_client' -import waitForExpect from 'wait-for-expect' -import { AccountHistoryCountOptions, TxType } from '../../../src/category/account' - -describe('masternode', () => { - const container = new MasterNodeRegTestContainer() - const client = new ContainerAdapterClient(container) - - beforeAll(async () => { - await container.start() - await container.waitForReady() - await container.waitForWalletCoinbaseMaturity() - - await setup() - }) - - afterAll(async () => { - await container.stop() - }) - - async function setup (): Promise { - const from = await container.call('getnewaddress') - await createToken(from, 'DBTC', 200) - - const to = await accountToAccount('DBTC', 5, from) - await accountToAccount('DBTC', 18, from, to) - - await createToken(from, 'DETH', 200) - await accountToAccount('DETH', 46, from) - } - - async function createToken (address: string, symbol: string, amount: number): Promise { - const metadata = { - symbol, - name: symbol, - isDAT: true, - mintable: true, - tradeable: true, - collateralAddress: address - } - await container.waitForWalletBalanceGTE(101) - await container.call('createtoken', [metadata]) - await container.generate(1) - - await container.call('minttokens', [`${amount.toString()}@${symbol}`]) - await container.generate(1) - } - - async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise { - const to = _to !== '' ? _to : await container.call('getnewaddress') - - await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) - await container.generate(1) - - return to - } - - describe('accountHistoryCount', () => { - it('should get accountHistoryCount', async () => { - await waitForExpect(async () => { - const count = await client.account.historyCount() - - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) - }) - - it('should get accountHistoryCount with owner as all', async () => { - await waitForExpect(async () => { - const count = await client.account.historyCount('all') - - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) - }) - - it('should get accountHistoryCount with no_rewards option', async () => { - await waitForExpect(async () => { - const options: AccountHistoryCountOptions = { - no_rewards: true - } - const count = await client.account.historyCount('mine', options) - - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) - }) - - it('should get accountHistoryCount with token option', async () => { - await waitForExpect(async () => { - const options1: AccountHistoryCountOptions = { - token: 'DBTC' - } - const options2: AccountHistoryCountOptions = { - token: 'DETH' - } - const countWithDBTC = await client.account.historyCount('mine', options1) - const countWithDETH = await client.account.historyCount('mine', options2) - - expect(typeof countWithDBTC).toBe('number') - expect(typeof countWithDETH).toBe('number') - expect(countWithDBTC).toStrictEqual(5) - expect(countWithDETH).toStrictEqual(3) - }) - }) - - it('should get accountHistory with txtype option', async () => { - await waitForExpect(async () => { - const options: AccountHistoryCountOptions = { - txtype: TxType.MINT_TOKEN - } - const count = await client.account.historyCount('mine', options) - - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) - }) - - it('should get different count for different txtypes', async () => { - await waitForExpect(async () => { - const options1: AccountHistoryCountOptions = { - txtype: TxType.MINT_TOKEN - } - const options2: AccountHistoryCountOptions = { - txtype: TxType.POOL_SWAP - - } - const count1 = await client.account.historyCount('mine', options1) - const count2 = await client.account.historyCount('mine', options2) - - expect(count1 === count2).toStrictEqual(false) - }) - }) - }) -}) From 07f4ef0dcbe0344bca1e40c27b5e8c6efcab8e58 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Thu, 27 May 2021 17:45:39 +0800 Subject: [PATCH 06/13] Refactor code and add 2 rpcs --- .../category/account/getAccount.test.ts | 4 +- .../category/account/getTokenBalances.test.ts | 4 +- .../account/listAccountHistory.test.ts | 198 ++++++++++++++++++ .../category/account/listAccounts.test.ts | 10 +- .../category/account/utxosToAccount.test.ts | 50 +++++ 5 files changed, 256 insertions(+), 10 deletions(-) create mode 100644 packages/jellyfish-api-core/__tests__/category/account/listAccountHistory.test.ts create mode 100644 packages/jellyfish-api-core/__tests__/category/account/utxosToAccount.test.ts diff --git a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts index c7dbef795d..56fd0ef627 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts @@ -2,7 +2,7 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' import waitForExpect from 'wait-for-expect' -describe('masternode', () => { +describe('Account with DBTC', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) @@ -37,7 +37,6 @@ describe('masternode', () => { it('should getAccount', async () => { const accounts = await client.account.listAccounts() - // [ '187.00000000@DBTC', '154.00000000@DETH' ] const account = await client.account.getAccount(accounts[0].owner.addresses[0]) expect(account.length).toBeGreaterThan(0) for (let i = 0; i < account.length; i += 1) { @@ -62,7 +61,6 @@ describe('masternode', () => { including_start: true } - // [ '187.00000000@DBTC', '154.00000000@DETH' ] const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) expect(account.length).toStrictEqual(1) diff --git a/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts index 39d1aba8d6..a9985c42c9 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts @@ -3,7 +3,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import waitForExpect from 'wait-for-expect' import BigNumber from 'bignumber.js' -describe('masternode', () => { +describe('Account with DBTC and DETH', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) @@ -96,7 +96,7 @@ describe('masternode', () => { const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: true }) for (let i = 0; i < tokenBalances.length; i += 1) { - expect(typeof tokenBalances[i]).toStrictEqual('string') // [ '300.00000000@DFI', '200.00000000@DBTC' ] + expect(typeof tokenBalances[i]).toStrictEqual('string') } }) }) diff --git a/packages/jellyfish-api-core/__tests__/category/account/listAccountHistory.test.ts b/packages/jellyfish-api-core/__tests__/category/account/listAccountHistory.test.ts new file mode 100644 index 0000000000..0eaf94269d --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/account/listAccountHistory.test.ts @@ -0,0 +1,198 @@ +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' +import waitForExpect from 'wait-for-expect' + +describe('Account with DBTC', () => { + const container = new MasterNodeRegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() + await container.waitForWalletBalanceGTE(100) + await createToken(await container.getNewAddress(), 'DBTC', 200) + }) + + afterAll(async () => { + await container.stop() + }) + + async function createToken (address: string, symbol: string, amount: number): Promise { + const metadata = { + symbol, + name: symbol, + isDAT: true, + mintable: true, + tradeable: true, + collateralAddress: address + } + await container.call('createtoken', [metadata]) + await container.generate(1) + + await container.call('minttokens', [`${amount.toString()}@${symbol}`]) + await container.generate(1) + } + + it('should listAccountHistory', async () => { + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory() + expect(accountHistories.length).toBeGreaterThan(0) + }) + + const accountHistories = await client.account.listAccountHistory() + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toStrictEqual('string') + expect(typeof accountHistory.blockHeight).toStrictEqual('number') + expect(typeof accountHistory.blockHash).toStrictEqual('string') + expect(typeof accountHistory.blockTime).toStrictEqual('number') + expect(typeof accountHistory.type).toStrictEqual('string') + expect(typeof accountHistory.txn).toStrictEqual('number') + expect(typeof accountHistory.txid).toStrictEqual('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toStrictEqual('string') // [ '10.00000000@DFI' ] + } + }) + + it('should listAccountHistory with owner "all"', async () => { + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory('all') + expect(accountHistories.length).toBeGreaterThan(0) + }) + + const accountHistories = await client.account.listAccountHistory('all') + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(typeof accountHistory.owner).toStrictEqual('string') + expect(typeof accountHistory.blockHeight).toStrictEqual('number') + expect(typeof accountHistory.blockHash).toStrictEqual('string') + expect(typeof accountHistory.blockTime).toStrictEqual('number') + expect(typeof accountHistory.type).toStrictEqual('string') + expect(typeof accountHistory.txn).toStrictEqual('number') + expect(typeof accountHistory.txid).toStrictEqual('string') + expect(accountHistory.amounts.length).toBeGreaterThan(0) + expect(typeof accountHistory.amounts[0]).toStrictEqual('string') + } + }) + + it('should listAccountHistory with owner CScript', async () => { + const accounts: any[] = await client.account.listAccounts() + + const { owner } = accounts[0] + const { hex, addresses } = owner + + const accountHistories = await client.account.listAccountHistory(hex) + + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(addresses.includes(accountHistory.owner)).toStrictEqual(true) + } + }) + + it('should listAccountHistory with owner address', async () => { + let address = '' + + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory() + expect(accountHistories.length).toBeGreaterThan(0) + address = accountHistories[0].owner + }) + + const accountHistories = await client.account.listAccountHistory(address) + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(accountHistory.owner).toStrictEqual(address) + } + }) + + it('should listAccountHistory with options maxBlockHeight', async () => { + const options = { + maxBlockHeight: 80 + } + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory('mine', options) + expect(accountHistories.length).toBeGreaterThan(0) + }) + + const accountHistories = await client.account.listAccountHistory('mine', options) + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(accountHistory.blockHeight).toBeLessThanOrEqual(80) + } + }) + + it('should listAccountHistory with options depth', async () => { + await waitForExpect(async () => { + const depth = 10 + const height = await container.getBlockCount() + const accountHistories = await client.account.listAccountHistory('mine', { depth }) + + for (const accountHistory of accountHistories) { + expect(accountHistory.blockHeight).toBeGreaterThanOrEqual(height - depth) + } + }) + }) + + it('should listAccountHistory with options no_rewards', async () => { + const options = { + no_rewards: true + } + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory('mine', options) + expect(accountHistories.length).toBeGreaterThan(0) + }) + + const accountHistories = await client.account.listAccountHistory('mine', options) + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(accountHistory.txn).not.toStrictEqual('blockReward') + } + }) + + it('should listAccountHistory with options token', async () => { + const options = { + token: 'DBTC' + } + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory('mine', options) + expect(accountHistories.length).toBeGreaterThan(0) + }) + + const accountHistories = await client.account.listAccountHistory('mine', options) + 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') + } + } + }) + + it('should listAccountHistory with options txtype', async () => { + await waitForExpect(async () => { + const accountHistories = await client.account.listAccountHistory('mine', { txtype: 'M' }) + expect(accountHistories.length).toBeGreaterThan(0) + }) + + const accountHistories = await client.account.listAccountHistory('mine', { txtype: 'M' }) + for (let i = 0; i < accountHistories.length; i += 1) { + const accountHistory = accountHistories[i] + expect(accountHistory.type).toStrictEqual('MintToken') + } + }) + + it('should listAccountHistory with options limit', async () => { + await waitForExpect(async () => { + const options = { + limit: 1 + } + const accountHistories = await client.account.listAccountHistory('mine', options) + expect(accountHistories.length).toStrictEqual(1) + }) + }) +}) diff --git a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts index 7f62ced098..e28f10738b 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts @@ -3,7 +3,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import waitForExpect from 'wait-for-expect' import BigNumber from 'bignumber.js' -describe('masternode', () => { +describe('Account with DBTC and DETH', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) @@ -47,7 +47,7 @@ describe('masternode', () => { expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) expect(typeof account.owner.type).toStrictEqual('string') expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + expect(typeof account.amount).toStrictEqual('string') } }) @@ -80,7 +80,7 @@ describe('masternode', () => { const account = accounts[i] expect(typeof account.key).toStrictEqual('string') expect(typeof account.owner).toStrictEqual('string') - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + expect(typeof account.amount).toStrictEqual('string') } }) @@ -129,7 +129,7 @@ describe('masternode', () => { expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) expect(typeof account.owner.type).toStrictEqual('string') expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + expect(typeof account.amount).toStrictEqual('string') } }) @@ -144,7 +144,7 @@ describe('masternode', () => { expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) expect(typeof account.owner.type).toStrictEqual('string') expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI + expect(typeof account.amount).toStrictEqual('string') } }) }) diff --git a/packages/jellyfish-api-core/__tests__/category/account/utxosToAccount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/utxosToAccount.test.ts new file mode 100644 index 0000000000..acaedba6cf --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/account/utxosToAccount.test.ts @@ -0,0 +1,50 @@ +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' +import { UtxosToAccountPayload } from '../../../src/category/account' + +describe('Account', () => { + 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 utxosToAccount', async () => { + const payload: UtxosToAccountPayload = {} + // NOTE(jingyi2811): Only support sending utxos to DFI account. + payload[await container.getNewAddress()] = '5@DFI' + payload[await container.getNewAddress()] = '5@DFI' + + const data = await client.account.utxosToAccount(payload) + + expect(typeof data).toStrictEqual('string') + expect(data.length).toStrictEqual(64) + }) + + it('should utxosToAccount with utxos', async () => { + const payload: UtxosToAccountPayload = {} + // NOTE(jingyi2811): Only support sending utxos to DFI account. + payload[await container.getNewAddress()] = '5@DFI' + payload[await container.getNewAddress()] = '5@DFI' + + const utxos = await container.call('listunspent') + const inputs = utxos.map((utxo: { txid: string, vout: number }) => { + return { + txid: utxo.txid, + vout: utxo.vout + } + }) + + const data = await client.account.utxosToAccount(payload, inputs) + + expect(typeof data).toStrictEqual('string') + expect(data.length).toStrictEqual(64) + }) +}) From 3e447c29d62c0dad9689d0765420df2e415d8b28 Mon Sep 17 00:00:00 2001 From: Siradji Date: Fri, 28 May 2021 09:02:10 +0100 Subject: [PATCH 07/13] Refactored accounthistoryCount.test.ts --- .../__tests__/category/account.test.ts | 80 +------------ .../category/account/accountHistory.test.ts | 113 ++++++++++++++++++ 2 files changed, 114 insertions(+), 79 deletions(-) create mode 100644 packages/jellyfish-api-core/__tests__/category/account/accountHistory.test.ts diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index 50fd4e0af6..700642a189 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -2,7 +2,7 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../container_adapter_client' import waitForExpect from 'wait-for-expect' import BigNumber from 'bignumber.js' -import { UtxosToAccountPayload, AccountHistoryCountOptions, TxType } from '../../src/category/account' +import { UtxosToAccountPayload } from '../../src/category/account' describe('masternode', () => { const container = new MasterNodeRegTestContainer() @@ -516,82 +516,4 @@ describe('masternode', () => { expect(data.length).toStrictEqual(64) }) }) - - describe('accountHistoryCount', () => { - it('should get accountHistoryCount', async () => { - await waitForExpect(async () => { - const count = await client.account.historyCount() - - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) - }) - - it('should get accountHistoryCount with owner as all', async () => { - await waitForExpect(async () => { - const count = await client.account.historyCount('all') - - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) - }) - - it('should get accountHistoryCount with no_rewards option', async () => { - await waitForExpect(async () => { - const options: AccountHistoryCountOptions = { - no_rewards: true - } - const count = await client.account.historyCount('mine', options) - - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) - }) - - it('should get accountHistoryCount with token option', async () => { - await waitForExpect(async () => { - const options1: AccountHistoryCountOptions = { - token: 'DBTC' - } - const options2: AccountHistoryCountOptions = { - token: 'DETH' - } - const countWithDBTC = await client.account.historyCount('mine', options1) - const countWithDETH = await client.account.historyCount('mine', options2) - - expect(typeof countWithDBTC).toBe('number') - expect(typeof countWithDETH).toBe('number') - expect(countWithDBTC).toStrictEqual(5) - expect(countWithDETH).toStrictEqual(3) - }) - }) - - it('should get accountHistory with txtype option', async () => { - await waitForExpect(async () => { - const options: AccountHistoryCountOptions = { - txtype: TxType.MINT_TOKEN - } - const count = await client.account.historyCount('mine', options) - - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) - }) - - it('should get different count for different txtypes', async () => { - await waitForExpect(async () => { - const options1: AccountHistoryCountOptions = { - txtype: TxType.MINT_TOKEN - } - const options2: AccountHistoryCountOptions = { - txtype: TxType.POOL_SWAP - - } - const count1 = await client.account.historyCount('mine', options1) - const count2 = await client.account.historyCount('mine', options2) - - expect(count1 === count2).toStrictEqual(false) - }) - }) - }) }) diff --git a/packages/jellyfish-api-core/__tests__/category/account/accountHistory.test.ts b/packages/jellyfish-api-core/__tests__/category/account/accountHistory.test.ts new file mode 100644 index 0000000000..e2d9cbb52e --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/account/accountHistory.test.ts @@ -0,0 +1,113 @@ +import waitForExpect from 'wait-for-expect' +import { AccountHistoryCountOptions, TxType } from '../../../src/category/account' +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' + +describe('accountHistoryCount', () => { + const container = new MasterNodeRegTestContainer() + const client = new ContainerAdapterClient(container) + + beforeAll(async () => { + await container.start() + await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() + await container.waitForWalletBalanceGTE(100) + await createToken(await container.getNewAddress(), 'DBTC', 200) + }) + + afterAll(async () => { + await container.stop() + }) + + async function createToken (address: string, symbol: string, amount: number): Promise { + const metadata = { + symbol, + name: symbol, + isDAT: true, + mintable: true, + tradeable: true, + collateralAddress: address + } + await container.call('createtoken', [metadata]) + await container.generate(1) + + await container.call('minttokens', [`${amount.toString()}@${symbol}`]) + await container.generate(1) + } + + it('should get accountHistoryCount', async () => { + await waitForExpect(async () => { + const count = await client.account.historyCount() + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get accountHistoryCount with owner as all', async () => { + await waitForExpect(async () => { + const count = await client.account.historyCount('all') + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get accountHistoryCount with no_rewards option', async () => { + await waitForExpect(async () => { + const options: AccountHistoryCountOptions = { + no_rewards: true + } + const count = await client.account.historyCount('mine', options) + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get accountHistoryCount with token option', async () => { + await waitForExpect(async () => { + const options1: AccountHistoryCountOptions = { + token: 'DBTC' + } + const options2: AccountHistoryCountOptions = { + token: 'DFI' + } + const countWithDBTC = await client.account.historyCount('mine', options1) + const countWithDFI = await client.account.historyCount('mine', options2) + + expect(typeof countWithDBTC).toBe('number') + expect(typeof countWithDFI).toBe('number') + expect(countWithDBTC).toStrictEqual(1) + expect(countWithDFI).toStrictEqual(111) + }) + }) + + it('should get accountHistory with txtype option', async () => { + await waitForExpect(async () => { + const options: AccountHistoryCountOptions = { + txtype: TxType.MINT_TOKEN + } + const count = await client.account.historyCount('mine', options) + + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) + }) + + it('should get different count for different txtypes', async () => { + await waitForExpect(async () => { + const options1: AccountHistoryCountOptions = { + txtype: TxType.MINT_TOKEN + } + const options2: AccountHistoryCountOptions = { + txtype: TxType.POOL_SWAP + + } + const count1 = await client.account.historyCount('mine', options1) + const count2 = await client.account.historyCount('mine', options2) + + expect(count1 === count2).toStrictEqual(false) + }) + }) +}) From bd654a48e6606bff7cf4a10f2158098a23b2502d Mon Sep 17 00:00:00 2001 From: Siradji Date: Fri, 28 May 2021 09:17:59 +0100 Subject: [PATCH 08/13] Renamed test file --- .../{accountHistory.test.ts => accountHistoryCount.test.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/jellyfish-api-core/__tests__/category/account/{accountHistory.test.ts => accountHistoryCount.test.ts} (100%) diff --git a/packages/jellyfish-api-core/__tests__/category/account/accountHistory.test.ts b/packages/jellyfish-api-core/__tests__/category/account/accountHistoryCount.test.ts similarity index 100% rename from packages/jellyfish-api-core/__tests__/category/account/accountHistory.test.ts rename to packages/jellyfish-api-core/__tests__/category/account/accountHistoryCount.test.ts From 30b848331f89cb8b4582c42caf5bfa52b3952424 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Fri, 28 May 2021 16:41:53 +0800 Subject: [PATCH 09/13] Minor --- .../category/account/accountHistory.test.ts | 146 ++++++++++-------- 1 file changed, 84 insertions(+), 62 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/accountHistory.test.ts b/packages/jellyfish-api-core/__tests__/category/account/accountHistory.test.ts index e2d9cbb52e..a94954881a 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/accountHistory.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/accountHistory.test.ts @@ -1,9 +1,9 @@ -import waitForExpect from 'wait-for-expect' -import { AccountHistoryCountOptions, TxType } from '../../../src/category/account' import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' +import waitForExpect from 'wait-for-expect' +import { AccountHistoryCountOptions, TxType } from '../../../src/category/account' -describe('accountHistoryCount', () => { +describe('masternode', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) @@ -11,14 +11,24 @@ describe('accountHistoryCount', () => { await container.start() await container.waitForReady() await container.waitForWalletCoinbaseMaturity() - await container.waitForWalletBalanceGTE(100) - await createToken(await container.getNewAddress(), 'DBTC', 200) + await setup() }) afterAll(async () => { await container.stop() }) + async function setup (): Promise { + const from = await container.call('getnewaddress') + await createToken(from, 'DBTC', 200) + + const to = await accountToAccount('DBTC', 5, from) + await accountToAccount('DBTC', 18, from, to) + + await createToken(from, 'DETH', 200) + await accountToAccount('DETH', 46, from) + } + async function createToken (address: string, symbol: string, amount: number): Promise { const metadata = { symbol, @@ -28,6 +38,7 @@ describe('accountHistoryCount', () => { tradeable: true, collateralAddress: address } + await container.waitForWalletBalanceGTE(101) await container.call('createtoken', [metadata]) await container.generate(1) @@ -35,79 +46,90 @@ describe('accountHistoryCount', () => { await container.generate(1) } - it('should get accountHistoryCount', async () => { - await waitForExpect(async () => { - const count = await client.account.historyCount() + async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise { + const to = _to !== '' ? _to : await container.call('getnewaddress') + + await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) + await container.generate(1) + + return to + } + + describe('accountHistoryCount', () => { + it('should get accountHistoryCount', async () => { + await waitForExpect(async () => { + const count = await client.account.historyCount() - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) }) - }) - it('should get accountHistoryCount with owner as all', async () => { - await waitForExpect(async () => { - const count = await client.account.historyCount('all') + it('should get accountHistoryCount with owner as all', async () => { + await waitForExpect(async () => { + const count = await client.account.historyCount('all') - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) }) - }) - it('should get accountHistoryCount with no_rewards option', async () => { - await waitForExpect(async () => { - const options: AccountHistoryCountOptions = { - no_rewards: true - } - const count = await client.account.historyCount('mine', options) + it('should get accountHistoryCount with no_rewards option', async () => { + await waitForExpect(async () => { + const options: AccountHistoryCountOptions = { + no_rewards: true + } + const count = await client.account.historyCount('mine', options) - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) }) - }) - it('should get accountHistoryCount with token option', async () => { - await waitForExpect(async () => { - const options1: AccountHistoryCountOptions = { - token: 'DBTC' - } - const options2: AccountHistoryCountOptions = { - token: 'DFI' - } - const countWithDBTC = await client.account.historyCount('mine', options1) - const countWithDFI = await client.account.historyCount('mine', options2) - - expect(typeof countWithDBTC).toBe('number') - expect(typeof countWithDFI).toBe('number') - expect(countWithDBTC).toStrictEqual(1) - expect(countWithDFI).toStrictEqual(111) + it('should get accountHistoryCount with token option', async () => { + await waitForExpect(async () => { + const options1: AccountHistoryCountOptions = { + token: 'DBTC' + } + const options2: AccountHistoryCountOptions = { + token: 'DETH' + } + const countWithDBTC = await client.account.historyCount('mine', options1) + const countWithDETH = await client.account.historyCount('mine', options2) + + expect(typeof countWithDBTC).toBe('number') + expect(typeof countWithDETH).toBe('number') + expect(countWithDBTC).toStrictEqual(5) + expect(countWithDETH).toStrictEqual(3) + }) }) - }) - it('should get accountHistory with txtype option', async () => { - await waitForExpect(async () => { - const options: AccountHistoryCountOptions = { - txtype: TxType.MINT_TOKEN - } - const count = await client.account.historyCount('mine', options) + it('should get accountHistory with txtype option', async () => { + await waitForExpect(async () => { + const options: AccountHistoryCountOptions = { + txtype: TxType.MINT_TOKEN + } + const count = await client.account.historyCount('mine', options) - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) + }) }) - }) - it('should get different count for different txtypes', async () => { - await waitForExpect(async () => { - const options1: AccountHistoryCountOptions = { - txtype: TxType.MINT_TOKEN - } - const options2: AccountHistoryCountOptions = { - txtype: TxType.POOL_SWAP + it('should get different count for different txtypes', async () => { + await waitForExpect(async () => { + const options1: AccountHistoryCountOptions = { + txtype: TxType.MINT_TOKEN + } + const options2: AccountHistoryCountOptions = { + txtype: TxType.POOL_SWAP - } - const count1 = await client.account.historyCount('mine', options1) - const count2 = await client.account.historyCount('mine', options2) + } + const count1 = await client.account.historyCount('mine', options1) + const count2 = await client.account.historyCount('mine', options2) - expect(count1 === count2).toStrictEqual(false) + expect(count1 === count2).toStrictEqual(false) + }) }) }) }) From 2bac5397421a92fd9ccf9ad4a3eb9fa65f153fd3 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Fri, 28 May 2021 16:55:53 +0800 Subject: [PATCH 10/13] Improve code --- .../__tests__/category/account.test.ts | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts index 700642a189..8fa9683a5a 100644 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account.test.ts @@ -2,7 +2,6 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../container_adapter_client' import waitForExpect from 'wait-for-expect' import BigNumber from 'bignumber.js' -import { UtxosToAccountPayload } from '../../src/category/account' describe('masternode', () => { const container = new MasterNodeRegTestContainer() @@ -482,38 +481,4 @@ describe('masternode', () => { }) }) }) - - describe('utxosToAccount', () => { - it('should utxosToAccount', async () => { - const payload: UtxosToAccountPayload = {} - // NOTE(jingyi2811): Only support sending utxos to DFI account. - payload[await container.getNewAddress()] = '5@DFI' - payload[await container.getNewAddress()] = '5@DFI' - - const data = await client.account.utxosToAccount(payload) - - expect(typeof data).toStrictEqual('string') - expect(data.length).toStrictEqual(64) - }) - - it('should utxosToAccount with utxos', async () => { - const payload: UtxosToAccountPayload = {} - // NOTE(jingyi2811): Only support sending utxos to DFI account. - payload[await container.getNewAddress()] = '5@DFI' - payload[await container.getNewAddress()] = '5@DFI' - - const utxos = await container.call('listunspent') - const inputs = utxos.map((utxo: { txid: string, vout: number }) => { - return { - txid: utxo.txid, - vout: utxo.vout - } - }) - - const data = await client.account.utxosToAccount(payload, inputs) - - expect(typeof data).toStrictEqual('string') - expect(data.length).toStrictEqual(64) - }) - }) }) From 6c5442e176afb64ff9bebd81b41a2ab68e5df45e Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Fri, 28 May 2021 17:02:22 +0800 Subject: [PATCH 11/13] Refactor code --- .../account/accountHistoryCount.test.ts | 118 +++++++++--------- .../category/account/accountToAccount.test.ts | 64 +++++----- .../category/account/utxosToAccount.test.ts | 6 +- 3 files changed, 92 insertions(+), 96 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/accountHistoryCount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/accountHistoryCount.test.ts index a94954881a..2df4747bfe 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/accountHistoryCount.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/accountHistoryCount.test.ts @@ -3,7 +3,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import waitForExpect from 'wait-for-expect' import { AccountHistoryCountOptions, TxType } from '../../../src/category/account' -describe('masternode', () => { +describe('Account with DBTC and DETH', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) @@ -55,81 +55,79 @@ describe('masternode', () => { return to } - describe('accountHistoryCount', () => { - it('should get accountHistoryCount', async () => { - await waitForExpect(async () => { - const count = await client.account.historyCount() + it('should get accountHistoryCount', async () => { + await waitForExpect(async () => { + const count = await client.account.historyCount() - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) }) + }) - it('should get accountHistoryCount with owner as all', async () => { - await waitForExpect(async () => { - const count = await client.account.historyCount('all') + it('should get accountHistoryCount with owner as all', async () => { + await waitForExpect(async () => { + const count = await client.account.historyCount('all') - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) }) + }) - it('should get accountHistoryCount with no_rewards option', async () => { - await waitForExpect(async () => { - const options: AccountHistoryCountOptions = { - no_rewards: true - } - const count = await client.account.historyCount('mine', options) + it('should get accountHistoryCount with no_rewards option', async () => { + await waitForExpect(async () => { + const options: AccountHistoryCountOptions = { + no_rewards: true + } + const count = await client.account.historyCount('mine', options) - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) }) + }) - it('should get accountHistoryCount with token option', async () => { - await waitForExpect(async () => { - const options1: AccountHistoryCountOptions = { - token: 'DBTC' - } - const options2: AccountHistoryCountOptions = { - token: 'DETH' - } - const countWithDBTC = await client.account.historyCount('mine', options1) - const countWithDETH = await client.account.historyCount('mine', options2) - - expect(typeof countWithDBTC).toBe('number') - expect(typeof countWithDETH).toBe('number') - expect(countWithDBTC).toStrictEqual(5) - expect(countWithDETH).toStrictEqual(3) - }) + it('should get accountHistoryCount with token option', async () => { + await waitForExpect(async () => { + const options1: AccountHistoryCountOptions = { + token: 'DBTC' + } + const options2: AccountHistoryCountOptions = { + token: 'DETH' + } + const countWithDBTC = await client.account.historyCount('mine', options1) + const countWithDETH = await client.account.historyCount('mine', options2) + + expect(typeof countWithDBTC).toBe('number') + expect(typeof countWithDETH).toBe('number') + expect(countWithDBTC).toStrictEqual(5) + expect(countWithDETH).toStrictEqual(3) }) + }) - it('should get accountHistory with txtype option', async () => { - await waitForExpect(async () => { - const options: AccountHistoryCountOptions = { - txtype: TxType.MINT_TOKEN - } - const count = await client.account.historyCount('mine', options) + it('should get accountHistory with txtype option', async () => { + await waitForExpect(async () => { + const options: AccountHistoryCountOptions = { + txtype: TxType.MINT_TOKEN + } + const count = await client.account.historyCount('mine', options) - expect(typeof count).toBe('number') - expect(count).toBeGreaterThanOrEqual(0) - }) + expect(typeof count).toBe('number') + expect(count).toBeGreaterThanOrEqual(0) }) + }) - it('should get different count for different txtypes', async () => { - await waitForExpect(async () => { - const options1: AccountHistoryCountOptions = { - txtype: TxType.MINT_TOKEN - } - const options2: AccountHistoryCountOptions = { - txtype: TxType.POOL_SWAP + it('should get different count for different txtypes', async () => { + await waitForExpect(async () => { + const options1: AccountHistoryCountOptions = { + txtype: TxType.MINT_TOKEN + } + const options2: AccountHistoryCountOptions = { + txtype: TxType.POOL_SWAP - } - const count1 = await client.account.historyCount('mine', options1) - const count2 = await client.account.historyCount('mine', options2) + } + const count1 = await client.account.historyCount('mine', options1) + const count2 = await client.account.historyCount('mine', options2) - expect(count1 === count2).toStrictEqual(false) - }) + expect(count1 === count2).toStrictEqual(false) }) }) }) diff --git a/packages/jellyfish-api-core/__tests__/category/account/accountToAccount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/accountToAccount.test.ts index 8667e78dd4..94c35080dc 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/accountToAccount.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/accountToAccount.test.ts @@ -3,7 +3,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import { RpcApiError } from '../../../src' import { BalanceTransferPayload, UTXO } from '../../../src/category/account' -describe('masternode', () => { +describe('Account with DBTC and DETH', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) @@ -60,46 +60,44 @@ describe('masternode', () => { return to } - describe('accountToAccount', () => { - it('should accountToAccount', async () => { - const payload: BalanceTransferPayload = {} - payload[await container.getNewAddress()] = '5@DFI' - payload[await container.getNewAddress()] = '5@DBTC' - payload[await container.getNewAddress()] = '5@DETH' + it('should accountToAccount', async () => { + const payload: BalanceTransferPayload = {} + payload[await container.getNewAddress()] = '5@DFI' + payload[await container.getNewAddress()] = '5@DBTC' + payload[await container.getNewAddress()] = '5@DETH' - const data = await client.account.accountToAccount(from, payload) + const data = await client.account.accountToAccount(from, payload) - expect(typeof data).toStrictEqual('string') - expect(data.length).toStrictEqual(64) - }) + expect(typeof data).toStrictEqual('string') + expect(data.length).toStrictEqual(64) + }) - it('should not accountToAccount for DFI coin if does not own the recipient address', async () => { - const promise = client.account.accountToAccount(from, { '2Mywjs9zEU4NtLknXQJZgozaxMvPn2Bb3qz': '5@DFI' }) + it('should not accountToAccount for DFI coin if does not own the recipient address', async () => { + const promise = client.account.accountToAccount(from, { '2Mywjs9zEU4NtLknXQJZgozaxMvPn2Bb3qz': '5@DFI' }) - await expect(promise).rejects.toThrow(RpcApiError) - await expect(promise).rejects.toThrow('The address (2Mywjs9zEU4NtLknXQJZgozaxMvPn2Bb3qz) is not your own address') - }) + await expect(promise).rejects.toThrow(RpcApiError) + await expect(promise).rejects.toThrow('The address (2Mywjs9zEU4NtLknXQJZgozaxMvPn2Bb3qz) is not your own address') + }) - it('should accountToAccount with utxos', async () => { - const { txid } = await container.fundAddress(from, 10) + it('should accountToAccount with utxos', async () => { + const { txid } = await container.fundAddress(from, 10) - const payload: BalanceTransferPayload = {} - payload[await container.getNewAddress()] = '5@DFI' - payload[await container.getNewAddress()] = '5@DBTC' - payload[await container.getNewAddress()] = '5@DETH' + const payload: BalanceTransferPayload = {} + payload[await container.getNewAddress()] = '5@DFI' + payload[await container.getNewAddress()] = '5@DBTC' + payload[await container.getNewAddress()] = '5@DETH' - const utxos = await container.call('listunspent') - const inputs: UTXO[] = utxos.filter((utxo: UTXO) => utxo.txid === txid).map((utxo: UTXO) => { - return { - txid: utxo.txid, - vout: utxo.vout - } - }) + const utxos = await container.call('listunspent') + const inputs: UTXO[] = utxos.filter((utxo: UTXO) => utxo.txid === txid).map((utxo: UTXO) => { + return { + txid: utxo.txid, + vout: utxo.vout + } + }) - const data = await client.account.accountToAccount(from, payload, { utxos: inputs }) + const data = await client.account.accountToAccount(from, payload, { utxos: inputs }) - expect(typeof data).toStrictEqual('string') - expect(data.length).toStrictEqual(64) - }) + expect(typeof data).toStrictEqual('string') + expect(data.length).toStrictEqual(64) }) }) diff --git a/packages/jellyfish-api-core/__tests__/category/account/utxosToAccount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/utxosToAccount.test.ts index acaedba6cf..f1b72c3696 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/utxosToAccount.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/utxosToAccount.test.ts @@ -1,6 +1,6 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' -import { UtxosToAccountPayload } from '../../../src/category/account' +import { BalanceTransferPayload } from '../../../src/category/account' describe('Account', () => { const container = new MasterNodeRegTestContainer() @@ -17,7 +17,7 @@ describe('Account', () => { }) it('should utxosToAccount', async () => { - const payload: UtxosToAccountPayload = {} + const payload: BalanceTransferPayload = {} // NOTE(jingyi2811): Only support sending utxos to DFI account. payload[await container.getNewAddress()] = '5@DFI' payload[await container.getNewAddress()] = '5@DFI' @@ -29,7 +29,7 @@ describe('Account', () => { }) it('should utxosToAccount with utxos', async () => { - const payload: UtxosToAccountPayload = {} + const payload: BalanceTransferPayload = {} // NOTE(jingyi2811): Only support sending utxos to DFI account. payload[await container.getNewAddress()] = '5@DFI' payload[await container.getNewAddress()] = '5@DFI' From 86fc19afee793081a3981c082cb43afdf3fdd056 Mon Sep 17 00:00:00 2001 From: Siradji Date: Tue, 1 Jun 2021 11:46:37 +0100 Subject: [PATCH 12/13] Renamed testing --- .../__tests__/category/account/accountHistoryCount.test.ts | 3 +-- .../__tests__/category/account/accountToAccount.test.ts | 2 +- .../__tests__/category/account/getAccount.test.ts | 2 +- .../__tests__/category/account/getTokenBalances.test.ts | 2 +- .../__tests__/category/account/listAccountHistory.test.ts | 2 +- .../__tests__/category/account/listAccounts.test.ts | 2 +- 6 files changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/accountHistoryCount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/accountHistoryCount.test.ts index 2df4747bfe..df44f5957d 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/accountHistoryCount.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/accountHistoryCount.test.ts @@ -3,7 +3,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import waitForExpect from 'wait-for-expect' import { AccountHistoryCountOptions, TxType } from '../../../src/category/account' -describe('Account with DBTC and DETH', () => { +describe('Account', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) @@ -50,7 +50,6 @@ describe('Account with DBTC and DETH', () => { const to = _to !== '' ? _to : await container.call('getnewaddress') await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) - await container.generate(1) return to } diff --git a/packages/jellyfish-api-core/__tests__/category/account/accountToAccount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/accountToAccount.test.ts index 94c35080dc..99da7c98a0 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/accountToAccount.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/accountToAccount.test.ts @@ -3,7 +3,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import { RpcApiError } from '../../../src' import { BalanceTransferPayload, UTXO } from '../../../src/category/account' -describe('Account with DBTC and DETH', () => { +describe('Account', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) diff --git a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts index 56fd0ef627..6e3e004639 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/getAccount.test.ts @@ -2,7 +2,7 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' import waitForExpect from 'wait-for-expect' -describe('Account with DBTC', () => { +describe('Account', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) diff --git a/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts b/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts index a9985c42c9..48435ccc2c 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/getTokenBalances.test.ts @@ -3,7 +3,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import waitForExpect from 'wait-for-expect' import BigNumber from 'bignumber.js' -describe('Account with DBTC and DETH', () => { +describe('Account', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) diff --git a/packages/jellyfish-api-core/__tests__/category/account/listAccountHistory.test.ts b/packages/jellyfish-api-core/__tests__/category/account/listAccountHistory.test.ts index 0eaf94269d..95d9ade803 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/listAccountHistory.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/listAccountHistory.test.ts @@ -2,7 +2,7 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' import waitForExpect from 'wait-for-expect' -describe('Account with DBTC', () => { +describe('Account', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) diff --git a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts index e28f10738b..de52af4977 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/listAccounts.test.ts @@ -3,7 +3,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import waitForExpect from 'wait-for-expect' import BigNumber from 'bignumber.js' -describe('Account with DBTC and DETH', () => { +describe('Account', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) From a7b81c3c67b1abcf6364b04b113d89bddf4e5dba Mon Sep 17 00:00:00 2001 From: Siradji Date: Tue, 1 Jun 2021 13:23:11 +0100 Subject: [PATCH 13/13] Removed old test file --- .../__tests__/category/account.test.ts | 484 ------------------ 1 file changed, 484 deletions(-) delete mode 100644 packages/jellyfish-api-core/__tests__/category/account.test.ts diff --git a/packages/jellyfish-api-core/__tests__/category/account.test.ts b/packages/jellyfish-api-core/__tests__/category/account.test.ts deleted file mode 100644 index 8fa9683a5a..0000000000 --- a/packages/jellyfish-api-core/__tests__/category/account.test.ts +++ /dev/null @@ -1,484 +0,0 @@ -import { MasterNodeRegTestContainer } from '@defichain/testcontainers' -import { ContainerAdapterClient } from '../container_adapter_client' -import waitForExpect from 'wait-for-expect' -import BigNumber from 'bignumber.js' - -describe('masternode', () => { - const container = new MasterNodeRegTestContainer() - const client = new ContainerAdapterClient(container) - - beforeAll(async () => { - await container.start() - await container.waitForReady() - await container.waitForWalletCoinbaseMaturity() - - await setup() - }) - - afterAll(async () => { - await container.stop() - }) - - async function setup (): Promise { - const from = await container.call('getnewaddress') - await createToken(from, 'DBTC', 200) - - const to = await accountToAccount('DBTC', 5, from) - await accountToAccount('DBTC', 18, from, to) - - await createToken(from, 'DETH', 200) - await accountToAccount('DETH', 46, from) - } - - async function createToken (address: string, symbol: string, amount: number): Promise { - const metadata = { - symbol, - name: symbol, - isDAT: true, - mintable: true, - tradeable: true, - collateralAddress: address - } - await container.waitForWalletBalanceGTE(101) - await container.call('createtoken', [metadata]) - await container.generate(1) - - await container.call('minttokens', [`${amount.toString()}@${symbol}`]) - await container.generate(1) - } - - async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise { - const to = _to !== '' ? _to : await container.call('getnewaddress') - - await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }]) - await container.generate(1) - - return to - } - - async function waitForListingAccounts (): Promise { - let accounts: any[] = [] - - await waitForExpect(async () => { - accounts = await client.account.listAccounts() - expect(accounts.length).toBeGreaterThan(0) - }) - - return accounts - } - - describe('listAccounts', () => { - it('should listAccounts', async () => { - await waitForListingAccounts() - - const accounts = await client.account.listAccounts() - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner === 'object').toStrictEqual(true) - expect(typeof account.owner.asm).toStrictEqual('string') - expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) - expect(typeof account.owner.type).toStrictEqual('string') - expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI - } - }) - - it('should listAccounts with pagination start and including_start', async () => { - const accounts = await waitForListingAccounts() - - const pagination = { - start: accounts[accounts.length - 1].key, - including_start: true - } - - const lastAccounts = await client.account.listAccounts(pagination) - expect(lastAccounts.length).toStrictEqual(1) - }) - - it('should listAccounts with pagination.limit', async () => { - await waitForExpect(async () => { - const pagination = { - limit: 2 - } - const accounts = await client.account.listAccounts(pagination) - expect(accounts.length).toStrictEqual(2) - }) - }) - - it('should listAccounts with verbose false and indexed_amounts false', async () => { - await waitForListingAccounts() - - const accounts = await client.account.listAccounts({}, false, { indexedAmounts: false, isMineOnly: false }) - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner).toStrictEqual('string') - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI - } - }) - - it('should listAccounts with verbose false and indexed_amounts true', async () => { - await waitForListingAccounts() - - const accounts = await client.account.listAccounts({}, false, { indexedAmounts: true, isMineOnly: false }) - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner).toStrictEqual('string') - - expect(typeof account.amount === 'object').toStrictEqual(true) - for (const k in account.amount) { - expect(account.amount[k] instanceof BigNumber).toStrictEqual(true) // [{'0': 100}] - } - } - }) - - it('should listAccounts with verbose true and indexed_amounts true', async () => { - await waitForListingAccounts() - - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: true, isMineOnly: false }) - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner === 'object').toStrictEqual(true) - expect(typeof account.owner.asm).toStrictEqual('string') - expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) - expect(typeof account.owner.type).toStrictEqual('string') - expect(account.owner.addresses.length).toBeGreaterThan(0) - - expect(typeof account.amount === 'object').toStrictEqual(true) - for (const k in account.amount) { - expect(account.amount[k] instanceof BigNumber).toStrictEqual(true) // [{'0': 100}] - } - } - }) - - it('should listAccounts with verbose true and indexed_amounts false', async () => { - await waitForListingAccounts() - - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: false }) - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner === 'object').toStrictEqual(true) - expect(typeof account.owner.asm).toStrictEqual('string') - expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) - expect(typeof account.owner.type).toStrictEqual('string') - expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI - } - }) - - it('should listAccounts with isMineOnly true', async () => { - await waitForListingAccounts() - - const accounts = await client.account.listAccounts({}, true, { indexedAmounts: false, isMineOnly: true }) - - for (let i = 0; i < accounts.length; i += 1) { - const account = accounts[i] - expect(typeof account.key).toStrictEqual('string') - expect(typeof account.owner === 'object').toStrictEqual(true) - expect(typeof account.owner.asm).toStrictEqual('string') - expect(account.owner.reqSigs instanceof BigNumber).toStrictEqual(true) - expect(typeof account.owner.type).toStrictEqual('string') - expect(account.owner.addresses.length).toBeGreaterThan(0) - expect(typeof account.amount).toStrictEqual('string') // 10.00000000@DFI - } - }) - }) - - describe('getAccount', () => { - it('should getAccount', async () => { - const accounts = await waitForListingAccounts() - - // [ '187.00000000@DBTC', '154.00000000@DETH' ] - const account = await client.account.getAccount(accounts[0].owner.addresses[0]) - expect(account.length).toBeGreaterThan(0) - for (let i = 0; i < account.length; i += 1) { - expect(typeof account[i]).toStrictEqual('string') - } - }) - - it('should getAccount with pagination start and including_start', async () => { - let accounts: any[] = [] - let beforeAccountCount = 0 - - await waitForExpect(async () => { - accounts = await client.account.listAccounts() - expect(accounts.length).toBeGreaterThan(0) - - const account = await client.account.getAccount(accounts[0].owner.addresses[0]) - beforeAccountCount = account.length - }) - - const pagination = { - start: beforeAccountCount, - including_start: true - } - - // [ '187.00000000@DBTC', '154.00000000@DETH' ] - const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) - expect(account.length).toStrictEqual(1) - - for (let i = 0; i < account.length; i += 1) { - expect(typeof account[i]).toStrictEqual('string') - } - }) - - it('should getAccount with pagination.limit', async () => { - const accounts = await waitForListingAccounts() - - const pagination = { - limit: 1 - } - const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination) - expect(account.length).toStrictEqual(1) - }) - - it('should getAccount with indexedAmount true', async () => { - const accounts = await waitForListingAccounts() - - const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: true }) - expect(typeof account).toStrictEqual('object') - for (const k in account) { - expect(typeof account[k]).toStrictEqual('number') - } - }) - }) - - describe('getTokenBalances', () => { - it('should getTokenBalances', async () => { - await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances() - expect(tokenBalances.length).toBeGreaterThan(0) - }) - - const tokenBalances = await client.account.getTokenBalances() - for (let i = 0; i < tokenBalances.length; i += 1) { - expect(typeof tokenBalances[i]).toStrictEqual('string') // [ '300.00000000@0', '200.00000000@1' ] - } - }) - - it('should getTokenBalances with pagination start and including_start', async () => { - let id = '' - - await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances() // [ '300.00000000@0', '200.00000000@1' ] - expect(tokenBalances.length).toBeGreaterThan(0) - - id = tokenBalances[tokenBalances.length - 1].split('@')[1] - }) - - const pagination = { - start: Number(id), - including_start: true - } - const tokenBalances = await client.account.getTokenBalances(pagination) - expect(tokenBalances.length).toStrictEqual(1) - }) - - it('should getTokenBalances with pagination limit', async () => { - await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances() - expect(tokenBalances.length).toStrictEqual(2) - }) - const pagination = { - limit: 1 - } - const tokenBalances = await client.account.getTokenBalances(pagination) - expect(tokenBalances.length).toStrictEqual(1) - }) - - it('should getTokenBalances with indexedAmounts true', async () => { - await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances({}, true, { symbolLookup: false }) - expect(typeof tokenBalances === 'object').toStrictEqual(true) - for (const k in tokenBalances) { - expect(tokenBalances[k] instanceof BigNumber).toStrictEqual(true) - } - }) - }) - - it('should getTokenBalances with symbolLookup', async () => { - await waitForExpect(async () => { - const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: true }) - expect(tokenBalances.length).toBeGreaterThan(0) - }) - - const tokenBalances = await client.account.getTokenBalances({}, false, { symbolLookup: true }) - for (let i = 0; i < tokenBalances.length; i += 1) { - expect(typeof tokenBalances[i]).toStrictEqual('string') // [ '300.00000000@DFI', '200.00000000@DBTC' ] - } - }) - }) - - describe('listAccountHistory', () => { - it('should listAccountHistory', async () => { - await waitForExpect(async () => { - const accountHistories = await client.account.listAccountHistory() - expect(accountHistories.length).toBeGreaterThan(0) - }) - - const accountHistories = await client.account.listAccountHistory() - - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(typeof accountHistory.owner).toStrictEqual('string') - expect(typeof accountHistory.blockHeight).toStrictEqual('number') - expect(typeof accountHistory.blockHash).toStrictEqual('string') - expect(typeof accountHistory.blockTime).toStrictEqual('number') - expect(typeof accountHistory.type).toStrictEqual('string') - expect(typeof accountHistory.txn).toStrictEqual('number') - expect(typeof accountHistory.txid).toStrictEqual('string') - expect(accountHistory.amounts.length).toBeGreaterThan(0) - expect(typeof accountHistory.amounts[0]).toStrictEqual('string') // [ '10.00000000@DFI' ] - } - }) - - it('should listAccountHistory with owner "all"', async () => { - await waitForExpect(async () => { - const accountHistories = await client.account.listAccountHistory('all') - expect(accountHistories.length).toBeGreaterThan(0) - }) - - const accountHistories = await client.account.listAccountHistory('all') - - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(typeof accountHistory.owner).toStrictEqual('string') - expect(typeof accountHistory.blockHeight).toStrictEqual('number') - expect(typeof accountHistory.blockHash).toStrictEqual('string') - expect(typeof accountHistory.blockTime).toStrictEqual('number') - expect(typeof accountHistory.type).toStrictEqual('string') - expect(typeof accountHistory.txn).toStrictEqual('number') - expect(typeof accountHistory.txid).toStrictEqual('string') - expect(accountHistory.amounts.length).toBeGreaterThan(0) - expect(typeof accountHistory.amounts[0]).toStrictEqual('string') - } - }) - - it('should listAccountHistory with owner CScript', async () => { - const accounts = await waitForListingAccounts() - - const { owner } = accounts[0] - const { hex, addresses } = owner - - const accountHistories = await client.account.listAccountHistory(hex) - - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(addresses.includes(accountHistory.owner)).toStrictEqual(true) - } - }) - - it('should listAccountHistory with owner address', async () => { - let address = '' - - await waitForExpect(async () => { - const accountHistories = await client.account.listAccountHistory() - expect(accountHistories.length).toBeGreaterThan(0) - address = accountHistories[0].owner - }) - - const accountHistories = await client.account.listAccountHistory(address) - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(accountHistory.owner).toStrictEqual(address) - } - }) - - it('should listAccountHistory with options maxBlockHeight', async () => { - const options = { - maxBlockHeight: 80 - } - await waitForExpect(async () => { - const accountHistories = await client.account.listAccountHistory('mine', options) - expect(accountHistories.length).toBeGreaterThan(0) - }) - - const accountHistories = await client.account.listAccountHistory('mine', options) - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(accountHistory.blockHeight).toBeLessThanOrEqual(80) - } - }) - - it('should listAccountHistory with options depth', async () => { - await waitForExpect(async () => { - const depth = 10 - const height = await container.getBlockCount() - const accountHistories = await client.account.listAccountHistory('mine', { depth }) - - for (const accountHistory of accountHistories) { - expect(accountHistory.blockHeight).toBeGreaterThanOrEqual(height - depth) - } - }) - }) - - it('should listAccountHistory with options no_rewards', async () => { - const options = { - no_rewards: true - } - await waitForExpect(async () => { - const accountHistories = await client.account.listAccountHistory('mine', options) - expect(accountHistories.length).toBeGreaterThan(0) - }) - - const accountHistories = await client.account.listAccountHistory('mine', options) - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(accountHistory.txn).not.toStrictEqual('blockReward') - } - }) - - it('should listAccountHistory with options token', async () => { - const options = { - token: 'DBTC' - } - await waitForExpect(async () => { - const accountHistories = await client.account.listAccountHistory('mine', options) - expect(accountHistories.length).toBeGreaterThan(0) - }) - - const accountHistories = await client.account.listAccountHistory('mine', options) - 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') - } - } - }) - - it('should listAccountHistory with options txtype', async () => { - await waitForExpect(async () => { - const accountHistories = await client.account.listAccountHistory('mine', { txtype: 'M' }) - expect(accountHistories.length).toBeGreaterThan(0) - }) - - const accountHistories = await client.account.listAccountHistory('mine', { txtype: 'M' }) - for (let i = 0; i < accountHistories.length; i += 1) { - const accountHistory = accountHistories[i] - expect(accountHistory.type).toStrictEqual('MintToken') - } - }) - - it('should listAccountHistory with options limit', async () => { - await waitForExpect(async () => { - const options = { - limit: 1 - } - const accountHistories = await client.account.listAccountHistory('mine', options) - expect(accountHistories.length).toStrictEqual(1) - }) - }) - }) -})