From a2d327becdcdd5acf79bddbc3a9cd706ccb03cc6 Mon Sep 17 00:00:00 2001 From: Siradji Date: Mon, 31 May 2021 12:49:29 +0100 Subject: [PATCH 01/28] Quick save --- .../account/sendTokensToAddress.test.ts | 88 +++++++++++++++++++ .../src/category/account.ts | 22 +++++ 2 files changed, 110 insertions(+) create mode 100644 packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts new file mode 100644 index 0000000000..ad09cefe2a --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -0,0 +1,88 @@ +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' +import { SelectionModeType } from '../../../src/category/account' + +describe('SendTokenToAdress', () => { + 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 { + await createToken(await container.call('getnewaddress'), 'DETH', 200) + await createToken(await container.call('getnewaddress'), 'DBTC', 200) + await createToken(await container.call('getnewaddress'), 'ETH', 200) + } + + 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('utxostoaccount', [{ [address]: '100@0' }]) + await container.generate(1) + + await container.call('minttokens', [`${amount.toString()}@${symbol}`]) + await container.generate(1) + } + + it('should create a transaction with autoselect', async () => { + const to = await client.wallet.getNewAddress() + const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }) + + expect(typeof transactionHex).toStrictEqual('string') + }) + + it('should create a transaction with Pie selection Mode', async () => { + const to = await client.wallet.getNewAddress() + const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }, SelectionModeType.PIE) + + expect(typeof transactionHex).toStrictEqual('string') + }) + + it('should create a transaction with Forward selection mode', async () => { + const to = await client.wallet.getNewAddress() + const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }, SelectionModeType.FORWARD) + + expect(typeof transactionHex).toStrictEqual('string') + }) + + it('should create a transaction with Crumbs selections mode', async () => { + const to = await client.wallet.getNewAddress() + const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }, SelectionModeType.CRUMBS) + + expect(typeof transactionHex).toStrictEqual('string') + }) + + it('should create a transaction with multiple destination address tokens', async () => { + const to = await client.wallet.getNewAddress() + const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@ETH', '0.1@DBTC', '100@DETH'] }) + + expect(typeof transactionHex).toStrictEqual('string') + }) + + it('should create a transaction with multiple source address tokens', async () => { + const to = await client.wallet.getNewAddress() + const from = await client.wallet.getNewAddress() + const transactionHex = await client.account.sendTokensToAddress({ [from]: '2@DFI' }, { [to]: '10@DBTC' }) + + expect(typeof transactionHex).toStrictEqual('string') + }) +}) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 2899177242..5d6ba1f50e 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -31,6 +31,12 @@ export enum TxType { AUTO_AUTH_PREP = 'A' } +export enum SelectionModeType { + PIE = 'pie', + CRUMBS = 'crumbs', + FORWARD = 'forward' +} + type AccountRegexType = `${string}@${string}` /** @@ -294,6 +300,18 @@ export class Account { ): Promise { return await this.client.call('accounthistorycount', [owner, options], 'number') } + + /** + * Creates a transfer transaction from your accounts balances. + * + * @param {Address} from The source defi address is the key, the value is amount in amount amount@token format + * @param {Address} to The defi address is the key, the value is amount in amount amount@token format + * @param {SelectionModeTyoe} [selectionMode=SelectionModeType.PIE] account selection mode. If "from" param is empty, it will autoselect. + * @return {Promise} + */ + async sendTokensToAddress (from: Address, to: Address, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise { + return await this.client.call('sendtokenstoaddress', [from, to, selectionMode], 'number') + } } export interface AccountPagination { @@ -370,3 +388,7 @@ export interface AccountHistoryCountOptions { txtype?: TxType | string no_rewards?: boolean } + +export interface Address { + [key: string]: string | string[] +} From c8e693c38488a2797f87a97c3a0880e03437ec96 Mon Sep 17 00:00:00 2001 From: Siradji Date: Mon, 31 May 2021 15:59:13 +0100 Subject: [PATCH 02/28] Testing complete --- .../account/sendTokensToAddress.test.ts | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index ad09cefe2a..cf81287d53 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -2,7 +2,7 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' import { SelectionModeType } from '../../../src/category/account' -describe('SendTokenToAdress', () => { +describe('SendTokenToAddress', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) @@ -17,10 +17,12 @@ describe('SendTokenToAdress', () => { await container.stop() }) + let from: string + async function setup (): Promise { - await createToken(await container.call('getnewaddress'), 'DETH', 200) - await createToken(await container.call('getnewaddress'), 'DBTC', 200) - await createToken(await container.call('getnewaddress'), 'ETH', 200) + from = await container.call('getnewaddress') + await createToken(from, 'DBTC', 200) + await createToken(from, 'ETH', 200) } async function createToken (address: string, symbol: string, amount: number): Promise { @@ -43,7 +45,7 @@ describe('SendTokenToAdress', () => { await container.generate(1) } - it('should create a transaction with autoselect', async () => { + it('should create a transaction with autoselect (empty source address)', async () => { const to = await client.wallet.getNewAddress() const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }) @@ -64,7 +66,7 @@ describe('SendTokenToAdress', () => { expect(typeof transactionHex).toStrictEqual('string') }) - it('should create a transaction with Crumbs selections mode', async () => { + it('should create a transaction with Crumbs selection mode', async () => { const to = await client.wallet.getNewAddress() const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }, SelectionModeType.CRUMBS) @@ -73,16 +75,35 @@ describe('SendTokenToAdress', () => { it('should create a transaction with multiple destination address tokens', async () => { const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@ETH', '0.1@DBTC', '100@DETH'] }) + const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@ETH', '0.1@DBTC', '10@DFI'] }) expect(typeof transactionHex).toStrictEqual('string') }) - it('should create a transaction with multiple source address tokens', async () => { + it('should create a transaction with source address provided', async () => { const to = await client.wallet.getNewAddress() - const from = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [from]: '2@DFI' }, { [to]: '10@DBTC' }) + const transactionHex = await client.account.sendTokensToAddress({ [from]: '10@ETH' }, { [to]: '10@ETH' }) expect(typeof transactionHex).toStrictEqual('string') }) + + it('should create a transaction with multiple source address tokens provided', async () => { + const to = await client.wallet.getNewAddress() + const transactionHex = await client.account.sendTokensToAddress({ [from]: ['2@DBTC', '10@ETH'] }, { [to]: ['2@DBTC', '10@ETH'] }) + + expect(typeof transactionHex).toStrictEqual('string') + }) + + it('should fail and throw an exception if destination address param is empty', async () => { + const promise = client.account.sendTokensToAddress({}, {}) + + await expect(promise).rejects.toThrow('zero amounts in "to" param') + }) + + it('should fail and throw an exception when insufficient funds in wallet', async () => { + const to = await client.wallet.getNewAddress() + const promise = client.account.sendTokensToAddress({ [from]: '500@ETH' }, { [to]: '500@ETH' }) + + await expect(promise).rejects.toThrow("Execution test failed: not enough balance on owner's account, call utxostoaccount to increase it") + }) }) From 9094ed3922d7fc1840b9abe4097c9bcce11dfcea Mon Sep 17 00:00:00 2001 From: Siradji Date: Mon, 31 May 2021 16:11:19 +0100 Subject: [PATCH 03/28] Minor changes --- .../account/sendTokensToAddress.test.ts | 2 +- .../src/category/account.ts | 4 ++-- website/docs/jellyfish/api/account.md | 23 +++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index cf81287d53..6fbb6f374e 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -45,7 +45,7 @@ describe('SendTokenToAddress', () => { await container.generate(1) } - it('should create a transaction with autoselect (empty source address)', async () => { + it('should create a transaction with auto select (empty source address)', async () => { const to = await client.wallet.getNewAddress() const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 5d6ba1f50e..dcb47ae0d8 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -306,7 +306,7 @@ export class Account { * * @param {Address} from The source defi address is the key, the value is amount in amount amount@token format * @param {Address} to The defi address is the key, the value is amount in amount amount@token format - * @param {SelectionModeTyoe} [selectionMode=SelectionModeType.PIE] account selection mode. If "from" param is empty, it will autoselect. + * @param {SelectionModeType} [selectionMode=SelectionModeType.PIE] account selection mode. If "from" param is empty, it will auto select. * @return {Promise} */ async sendTokensToAddress (from: Address, to: Address, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise { @@ -390,5 +390,5 @@ export interface AccountHistoryCountOptions { } export interface Address { - [key: string]: string | string[] + [key: string]: AccountRegexType | AccountRegexType[] } diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index f3a3230043..acf67ee286 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -237,3 +237,26 @@ interface AccountHistoryCountOptions { no_rewards?: boolean } ``` + +## sendTokensToAddress + +Creates a transfer transaction from your accounts balances. + +```ts title="client.account.sendTokensToAddress()" + +interface Account { + sendTokensToAddress(from: Address, to: Address, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise +} + +type AccountRegexType = `${string}@${string}` + +enum SelectionModeType { + PIE = 'pie', + CRUMBS = 'crumbs', + FORWARD = 'forward' +} + +interface Address { + [key: string]: AccountRegexType | AccountRegexType[] +} +``` \ No newline at end of file From 1870cbdfd68131237e7f846c79b4e0cf792722ae Mon Sep 17 00:00:00 2001 From: Siradji Date: Mon, 31 May 2021 20:38:02 +0100 Subject: [PATCH 04/28] Removed unsued test --- .../__tests__/category/account/sendTokensToAddress.test.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 6fbb6f374e..3989bdde35 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -99,11 +99,4 @@ describe('SendTokenToAddress', () => { await expect(promise).rejects.toThrow('zero amounts in "to" param') }) - - it('should fail and throw an exception when insufficient funds in wallet', async () => { - const to = await client.wallet.getNewAddress() - const promise = client.account.sendTokensToAddress({ [from]: '500@ETH' }, { [to]: '500@ETH' }) - - await expect(promise).rejects.toThrow("Execution test failed: not enough balance on owner's account, call utxostoaccount to increase it") - }) }) From ec2b3e6b129d3fdc8fc658d9fb8ad6d899f621c5 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Tue, 1 Jun 2021 19:47:54 +0800 Subject: [PATCH 05/28] Fix flakiness or regression in blockchain.getChainTips --- .../__tests__/category/blockchain/getChainTips.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain/getChainTips.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain/getChainTips.test.ts index bce3d5d44e..f04db1aa2c 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain/getChainTips.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain/getChainTips.test.ts @@ -9,6 +9,7 @@ describe('ChainTips', () => { beforeAll(async () => { await container.start() await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() }) afterAll(async () => { From d18426c8dc452a18697faab9c55377558d9aa919 Mon Sep 17 00:00:00 2001 From: jingyi2811 Date: Tue, 1 Jun 2021 23:12:56 +0800 Subject: [PATCH 06/28] Revert back the code --- .../__tests__/category/blockchain/getChainTips.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain/getChainTips.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain/getChainTips.test.ts index f04db1aa2c..bce3d5d44e 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain/getChainTips.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain/getChainTips.test.ts @@ -9,7 +9,6 @@ describe('ChainTips', () => { beforeAll(async () => { await container.start() await container.waitForReady() - await container.waitForWalletCoinbaseMaturity() }) afterAll(async () => { From 4a30b0db749f652fbb78a0051eaf6d4d1621bc2a Mon Sep 17 00:00:00 2001 From: Siradji Date: Wed, 2 Jun 2021 08:32:50 +0100 Subject: [PATCH 07/28] Minor fixes --- .../category/account/sendTokensToAddress.test.ts | 10 +++++----- packages/jellyfish-api-core/src/category/account.ts | 2 +- website/docs/jellyfish/api/account.md | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 3989bdde35..8346d7ca40 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -47,28 +47,28 @@ describe('SendTokenToAddress', () => { it('should create a transaction with auto select (empty source address)', async () => { const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }) + const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@DFI'] }) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with Pie selection Mode', async () => { const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }, SelectionModeType.PIE) + const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@DFI'] }, SelectionModeType.PIE) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with Forward selection mode', async () => { const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }, SelectionModeType.FORWARD) + const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@DFI'] }, SelectionModeType.FORWARD) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with Crumbs selection mode', async () => { const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [to]: '2@DFI' }, SelectionModeType.CRUMBS) + const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@DFI'] }, SelectionModeType.CRUMBS) expect(typeof transactionHex).toStrictEqual('string') }) @@ -82,7 +82,7 @@ describe('SendTokenToAddress', () => { it('should create a transaction with source address provided', async () => { const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [from]: '10@ETH' }, { [to]: '10@ETH' }) + const transactionHex = await client.account.sendTokensToAddress({ [from]: ['10@ETH'] }, { [to]: ['10@ETH'] }) expect(typeof transactionHex).toStrictEqual('string') }) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index dcb47ae0d8..8033a93203 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -390,5 +390,5 @@ export interface AccountHistoryCountOptions { } export interface Address { - [key: string]: AccountRegexType | AccountRegexType[] + [key: string]: AccountRegexType[] } diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index acf67ee286..84ef184f31 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -245,7 +245,7 @@ Creates a transfer transaction from your accounts balances. ```ts title="client.account.sendTokensToAddress()" interface Account { - sendTokensToAddress(from: Address, to: Address, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise + sendTokensToAddress (from: Address, to: Address, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise } type AccountRegexType = `${string}@${string}` @@ -257,6 +257,6 @@ enum SelectionModeType { } interface Address { - [key: string]: AccountRegexType | AccountRegexType[] + [key: string]: AccountRegexType[] } ``` \ No newline at end of file From 12bfb1bf381a15b60b4546867e1f17c989a0e945 Mon Sep 17 00:00:00 2001 From: Siradji Date: Thu, 3 Jun 2021 08:50:19 +0100 Subject: [PATCH 08/28] Implemented suggestions from code review --- packages/jellyfish-api-core/src/category/account.ts | 8 ++++---- website/docs/jellyfish/api/account.md | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 8033a93203..8fdf05d589 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -304,12 +304,12 @@ export class Account { /** * Creates a transfer transaction from your accounts balances. * - * @param {Address} from The source defi address is the key, the value is amount in amount amount@token format - * @param {Address} to The defi address is the key, the value is amount in amount amount@token format + * @param {AddressBalances} from The source defi address is the key, the value is amount in amount amount@token format + * @param {AddressBalances} to The defi address is the key, the value is amount in amount amount@token format * @param {SelectionModeType} [selectionMode=SelectionModeType.PIE] account selection mode. If "from" param is empty, it will auto select. * @return {Promise} */ - async sendTokensToAddress (from: Address, to: Address, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise { + async sendTokensToAddress (from: AddressBalances, to: AddressBalances, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise { return await this.client.call('sendtokenstoaddress', [from, to, selectionMode], 'number') } } @@ -389,6 +389,6 @@ export interface AccountHistoryCountOptions { no_rewards?: boolean } -export interface Address { +export interface AddressBalances { [key: string]: AccountRegexType[] } diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index 84ef184f31..1bcfb548e1 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -245,7 +245,7 @@ Creates a transfer transaction from your accounts balances. ```ts title="client.account.sendTokensToAddress()" interface Account { - sendTokensToAddress (from: Address, to: Address, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise + sendTokensToAddress (from: AddressBalances, to: AddressBalances, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise } type AccountRegexType = `${string}@${string}` @@ -256,7 +256,7 @@ enum SelectionModeType { FORWARD = 'forward' } -interface Address { +interface AddressBalances { [key: string]: AccountRegexType[] } ``` \ No newline at end of file From f0d96b9db54311d1e343dcef2b90f3879dfe8993 Mon Sep 17 00:00:00 2001 From: Siradji Date: Thu, 3 Jun 2021 09:11:05 +0100 Subject: [PATCH 09/28] Fixed lint issue --- packages/jellyfish-api-core/src/category/account.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 2523b5db46..e2481ee8bf 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -39,7 +39,6 @@ export enum SelectionModeType { type AccountRegexType = `${string}@${string}` - /** * Account RPCs for DeFi Blockchain */ From 02fd385cdccd1d72a73dc65df6295680fd5e164a Mon Sep 17 00:00:00 2001 From: Siradji Date: Thu, 3 Jun 2021 10:46:58 +0100 Subject: [PATCH 10/28] Implemented suggestions from code review --- .../account/sendTokensToAddress.test.ts | 21 +++++++------------ .../src/category/account.ts | 2 +- website/docs/jellyfish/api/account.md | 2 +- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 8346d7ca40..b2a08594f8 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -46,50 +46,43 @@ describe('SendTokenToAddress', () => { } it('should create a transaction with auto select (empty source address)', async () => { - const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@DFI'] }) + const transactionHex = await client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['2@DFI'] }) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with Pie selection Mode', async () => { - const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@DFI'] }, SelectionModeType.PIE) + const transactionHex = await client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['2@DFI'] }, SelectionModeType.PIE) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with Forward selection mode', async () => { - const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@DFI'] }, SelectionModeType.FORWARD) + const transactionHex = await client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['2@DFI'] }, SelectionModeType.FORWARD) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with Crumbs selection mode', async () => { - const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@DFI'] }, SelectionModeType.CRUMBS) + const transactionHex = await client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['2@DFI'] }, SelectionModeType.CRUMBS) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with multiple destination address tokens', async () => { - const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [to]: ['2@ETH', '0.1@DBTC', '10@DFI'] }) + const transactionHex = await client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['2@ETH', '0.1@DBTC', '10@DFI'] }) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with source address provided', async () => { - const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [from]: ['10@ETH'] }, { [to]: ['10@ETH'] }) + const transactionHex = await client.account.sendTokensToAddress({ [from]: ['10@ETH'] }, { [await client.wallet.getNewAddress()]: ['10@ETH'] }) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with multiple source address tokens provided', async () => { - const to = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [from]: ['2@DBTC', '10@ETH'] }, { [to]: ['2@DBTC', '10@ETH'] }) + const transactionHex = await client.account.sendTokensToAddress({ [from]: ['2@DBTC', '10@ETH'] }, { [await client.wallet.getNewAddress()]: ['2@DBTC', '10@ETH'] }) expect(typeof transactionHex).toStrictEqual('string') }) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index e2481ee8bf..09fa1567d0 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -37,7 +37,7 @@ export enum SelectionModeType { FORWARD = 'forward' } -type AccountRegexType = `${string}@${string}` +type AccountRegexType = `${number}@${string}` /** * Account RPCs for DeFi Blockchain diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index cd203d52ae..00fb4ae41e 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -274,7 +274,7 @@ interface Account { sendTokensToAddress (from: AddressBalances, to: AddressBalances, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise } -type AccountRegexType = `${string}@${string}` +type AccountRegexType = `${number}@${string}` enum SelectionModeType { PIE = 'pie', From 734aa244f975e6923a06db067b17b48aff7f736c Mon Sep 17 00:00:00 2001 From: Siradji Date: Fri, 4 Jun 2021 12:32:23 +0100 Subject: [PATCH 11/28] Implemented addition test from sendTokensToAddress --- .../account/sendTokensToAddress.test.ts | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index b2a08594f8..7180f94431 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -1,6 +1,6 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' -import { SelectionModeType } from '../../../src/category/account' +import { AddressBalances, SelectionModeType } from '../../../src/category/account' describe('SendTokenToAddress', () => { const container = new MasterNodeRegTestContainer() @@ -45,45 +45,72 @@ describe('SendTokenToAddress', () => { await container.generate(1) } + async function sendTokensToAddress (from: AddressBalances, to: AddressBalances, selectionMode?: SelectionModeType): Promise { + const transactionHex = await client.account.sendTokensToAddress(from, to, selectionMode) + await container.generate(1) + return transactionHex + } + it('should create a transaction with auto select (empty source address)', async () => { - const transactionHex = await client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['2@DFI'] }) + const address = await client.wallet.getNewAddress() + const transactionHex = await sendTokensToAddress({}, { [address]: ['2@DFI'] }) + const account = await client.account.getAccount(address) + expect(account).toStrictEqual(['2.00000000@DFI']) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with Pie selection Mode', async () => { - const transactionHex = await client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['2@DFI'] }, SelectionModeType.PIE) + const address = await client.wallet.getNewAddress() + const transactionHex = await sendTokensToAddress({}, { [address]: ['2@DFI'] }, SelectionModeType.PIE) + const account = await client.account.getAccount(address) + expect(account).toStrictEqual(['2.00000000@DFI']) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with Forward selection mode', async () => { - const transactionHex = await client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['2@DFI'] }, SelectionModeType.FORWARD) + const address = await client.wallet.getNewAddress() + const transactionHex = await sendTokensToAddress({}, { [address]: ['2@DFI'] }, SelectionModeType.FORWARD) + const account = await client.account.getAccount(address) + expect(account).toStrictEqual(['2.00000000@DFI']) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with Crumbs selection mode', async () => { - const transactionHex = await client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['2@DFI'] }, SelectionModeType.CRUMBS) + const address = await client.wallet.getNewAddress() + const transactionHex = await sendTokensToAddress({}, { [address]: ['2@DFI'] }, SelectionModeType.CRUMBS) + const account = await client.account.getAccount(address) + expect(account).toStrictEqual(['2.00000000@DFI']) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with multiple destination address tokens', async () => { - const transactionHex = await client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['2@ETH', '0.1@DBTC', '10@DFI'] }) + const address = await client.wallet.getNewAddress() + const transactionHex = await sendTokensToAddress({}, { [address]: ['2@ETH', '0.1@DBTC', '10@DFI'] }) + const account = await client.account.getAccount(address) + expect(account).toStrictEqual(['10.00000000@DFI', '0.10000000@DBTC', '2.00000000@ETH']) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with source address provided', async () => { - const transactionHex = await client.account.sendTokensToAddress({ [from]: ['10@ETH'] }, { [await client.wallet.getNewAddress()]: ['10@ETH'] }) + const address = await client.wallet.getNewAddress() + const transactionHex = await sendTokensToAddress({ [from]: ['10@ETH'] }, { [address]: ['10@ETH'] }) + const account = await client.account.getAccount(address) + expect(account).toStrictEqual(['10.00000000@ETH']) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with multiple source address tokens provided', async () => { - const transactionHex = await client.account.sendTokensToAddress({ [from]: ['2@DBTC', '10@ETH'] }, { [await client.wallet.getNewAddress()]: ['2@DBTC', '10@ETH'] }) + const address = await client.wallet.getNewAddress() + const transactionHex = await sendTokensToAddress({ [from]: ['2@DBTC', '10@ETH'] }, { [address]: ['2@DBTC', '10@ETH'] }) + const account = await client.account.getAccount(address) + expect(account).toStrictEqual(['2.00000000@DBTC', '10.00000000@ETH']) expect(typeof transactionHex).toStrictEqual('string') }) From 3f8455e5b969800e18c1ef9c8eaad3649051eb79 Mon Sep 17 00:00:00 2001 From: Siradji Date: Tue, 8 Jun 2021 16:30:56 +0100 Subject: [PATCH 12/28] Implemented requested changes --- .../account/sendTokensToAddress.test.ts | 40 ++++++++++++------- .../src/category/account.ts | 15 +++++-- website/docs/jellyfish/api/account.md | 10 ++++- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 7180f94431..5e4c37ee34 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -1,6 +1,6 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' -import { AddressBalances, SelectionModeType } from '../../../src/category/account' +import { SelectionModeType } from '../../../src/category/account' describe('SendTokenToAddress', () => { const container = new MasterNodeRegTestContainer() @@ -45,15 +45,11 @@ describe('SendTokenToAddress', () => { await container.generate(1) } - async function sendTokensToAddress (from: AddressBalances, to: AddressBalances, selectionMode?: SelectionModeType): Promise { - const transactionHex = await client.account.sendTokensToAddress(from, to, selectionMode) - await container.generate(1) - return transactionHex - } - it('should create a transaction with auto select (empty source address)', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await sendTokensToAddress({}, { [address]: ['2@DFI'] }) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }) + await container.generate(1) + const account = await client.account.getAccount(address) expect(account).toStrictEqual(['2.00000000@DFI']) @@ -62,7 +58,12 @@ describe('SendTokenToAddress', () => { it('should create a transaction with Pie selection Mode', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await sendTokensToAddress({}, { [address]: ['2@DFI'] }, SelectionModeType.PIE) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }, { + selectionMode: SelectionModeType + .PIE + }) + await container.generate(1) + const account = await client.account.getAccount(address) expect(account).toStrictEqual(['2.00000000@DFI']) @@ -71,7 +72,11 @@ describe('SendTokenToAddress', () => { it('should create a transaction with Forward selection mode', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await sendTokensToAddress({}, { [address]: ['2@DFI'] }, SelectionModeType.FORWARD) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }, { + selectionMode: SelectionModeType + .FORWARD + }) + await container.generate(1) const account = await client.account.getAccount(address) expect(account).toStrictEqual(['2.00000000@DFI']) @@ -80,7 +85,11 @@ describe('SendTokenToAddress', () => { it('should create a transaction with Crumbs selection mode', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await sendTokensToAddress({}, { [address]: ['2@DFI'] }, SelectionModeType.CRUMBS) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }, { + selectionMode: SelectionModeType + .CRUMBS + }) + await container.generate(1) const account = await client.account.getAccount(address) expect(account).toStrictEqual(['2.00000000@DFI']) @@ -89,7 +98,8 @@ describe('SendTokenToAddress', () => { it('should create a transaction with multiple destination address tokens', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await sendTokensToAddress({}, { [address]: ['2@ETH', '0.1@DBTC', '10@DFI'] }) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@ETH', '0.1@DBTC', '10@DFI'] }) + await container.generate(1) const account = await client.account.getAccount(address) expect(account).toStrictEqual(['10.00000000@DFI', '0.10000000@DBTC', '2.00000000@ETH']) @@ -98,7 +108,8 @@ describe('SendTokenToAddress', () => { it('should create a transaction with source address provided', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await sendTokensToAddress({ [from]: ['10@ETH'] }, { [address]: ['10@ETH'] }) + const transactionHex = await client.account.sendTokensToAddress({ [from]: ['10@ETH'] }, { [address]: ['10@ETH'] }) + await container.generate(1) const account = await client.account.getAccount(address) expect(account).toStrictEqual(['10.00000000@ETH']) @@ -107,7 +118,8 @@ describe('SendTokenToAddress', () => { it('should create a transaction with multiple source address tokens provided', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await sendTokensToAddress({ [from]: ['2@DBTC', '10@ETH'] }, { [address]: ['2@DBTC', '10@ETH'] }) + const transactionHex = await client.account.sendTokensToAddress({ [from]: ['2@DBTC', '10@ETH'] }, { [address]: ['2@DBTC', '10@ETH'] }) + await container.generate(1) const account = await client.account.getAccount(address) expect(account).toStrictEqual(['2.00000000@DBTC', '10.00000000@ETH']) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 09fa1567d0..8be91f5a58 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -323,11 +323,16 @@ export class Account { * * @param {AddressBalances} from The source defi address is the key, the value is amount in amount amount@token format * @param {AddressBalances} to The defi address is the key, the value is amount in amount amount@token format - * @param {SelectionModeType} [selectionMode=SelectionModeType.PIE] account selection mode. If "from" param is empty, it will auto select. + * @param {SendTokensOptions} [options] + * @param {SelectionModeType} [options.selectionMode] Account selection mode. If "from" param is empty, it will auto select. * @return {Promise} */ - async sendTokensToAddress (from: AddressBalances, to: AddressBalances, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise { - return await this.client.call('sendtokenstoaddress', [from, to, selectionMode], 'number') + async sendTokensToAddress ( + from: AddressBalances, + to: AddressBalances, + options: SendTokensOptions = { selectionMode: SelectionModeType.PIE } + ): Promise { + return await this.client.call('sendtokenstoaddress', [from, to, options.selectionMode], 'number') } } @@ -409,3 +414,7 @@ export interface AccountHistoryCountOptions { export interface AddressBalances { [key: string]: AccountRegexType[] } + +export interface SendTokensOptions { + selectionMode: SelectionModeType +} diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index 00fb4ae41e..0b671ac1c5 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -271,7 +271,11 @@ Creates a transfer transaction from your accounts balances. ```ts title="client.account.sendTokensToAddress()" interface Account { - sendTokensToAddress (from: AddressBalances, to: AddressBalances, selectionMode: SelectionModeType = SelectionModeType.PIE): Promise + sendTokensToAddress ( + from: AddressBalances, + to: AddressBalances, + options: SendTokensOptions ={selectionMode: SelectionModeType} + ): Promise } type AccountRegexType = `${number}@${string}` @@ -285,4 +289,8 @@ enum SelectionModeType { interface AddressBalances { [key: string]: AccountRegexType[] } + +interface SendTokensOptions { + selectionMode: SelectionModeType +} ``` \ No newline at end of file From f682bb7320ef6f066c88a66a350cfc85caad1105 Mon Sep 17 00:00:00 2001 From: Siradji Date: Wed, 9 Jun 2021 06:52:51 +0100 Subject: [PATCH 13/28] Additional Tests --- .../account/sendTokensToAddress.test.ts | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 5e4c37ee34..105c1ba226 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -1,6 +1,6 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' -import { SelectionModeType } from '../../../src/category/account' +import { SelectionModeType, SendTokensOptions } from '../../../src/category/account' describe('SendTokenToAddress', () => { const container = new MasterNodeRegTestContainer() @@ -17,12 +17,12 @@ describe('SendTokenToAddress', () => { await container.stop() }) - let from: string + let addA: string async function setup (): Promise { - from = await container.call('getnewaddress') - await createToken(from, 'DBTC', 200) - await createToken(from, 'ETH', 200) + addA = await container.call('getnewaddress') + await createToken(addA, 'DBTC', 200) + await createToken(addA, 'ETH', 200) } async function createToken (address: string, symbol: string, amount: number): Promise { @@ -57,26 +57,29 @@ describe('SendTokenToAddress', () => { }) it('should create a transaction with Pie selection Mode', async () => { + const options: SendTokensOptions = { + selectionMode: SelectionModeType.PIE + } const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }, { - selectionMode: SelectionModeType - .PIE - }) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['4@DFI'] }, options) await container.generate(1) const account = await client.account.getAccount(address) + const balanceDeducted = await client.account.getAccount(addA) - expect(account).toStrictEqual(['2.00000000@DFI']) + expect(balanceDeducted[0]).toStrictEqual('194.00000000@DFI') + expect(account).toStrictEqual(['4.00000000@DFI']) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with Forward selection mode', async () => { + const options: SendTokensOptions = { + selectionMode: SelectionModeType.FORWARD + } const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }, { - selectionMode: SelectionModeType - .FORWARD - }) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }, options) await container.generate(1) + const account = await client.account.getAccount(address) expect(account).toStrictEqual(['2.00000000@DFI']) @@ -84,12 +87,13 @@ describe('SendTokenToAddress', () => { }) it('should create a transaction with Crumbs selection mode', async () => { + const options: SendTokensOptions = { + selectionMode: SelectionModeType.CRUMBS + } const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }, { - selectionMode: SelectionModeType - .CRUMBS - }) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }, options) await container.generate(1) + const account = await client.account.getAccount(address) expect(account).toStrictEqual(['2.00000000@DFI']) @@ -108,8 +112,9 @@ describe('SendTokenToAddress', () => { it('should create a transaction with source address provided', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [from]: ['10@ETH'] }, { [address]: ['10@ETH'] }) + const transactionHex = await client.account.sendTokensToAddress({ [addA]: ['10@ETH'] }, { [address]: ['10@ETH'] }) await container.generate(1) + const account = await client.account.getAccount(address) expect(account).toStrictEqual(['10.00000000@ETH']) @@ -118,8 +123,9 @@ describe('SendTokenToAddress', () => { it('should create a transaction with multiple source address tokens provided', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [from]: ['2@DBTC', '10@ETH'] }, { [address]: ['2@DBTC', '10@ETH'] }) + const transactionHex = await client.account.sendTokensToAddress({ [addA]: ['2@DBTC', '10@ETH'] }, { [address]: ['2@DBTC', '10@ETH'] }) await container.generate(1) + const account = await client.account.getAccount(address) expect(account).toStrictEqual(['2.00000000@DBTC', '10.00000000@ETH']) @@ -127,8 +133,12 @@ describe('SendTokenToAddress', () => { }) it('should fail and throw an exception if destination address param is empty', async () => { - const promise = client.account.sendTokensToAddress({}, {}) + await expect(client.account.sendTokensToAddress({}, {})).rejects.toThrow('zero amounts in "to" param') + }) + + it('should throw an error with insufficient fund', async () => { + const promise = client.account.sendTokensToAddress({ [addA]: ['500@DBTC'] }, { [await client.wallet.getNewAddress()]: ['500@DBTC'] }) - await expect(promise).rejects.toThrow('zero amounts in "to" param') + await expect(promise).rejects.toThrow('amount 197.90000000 is less than 500.00000000') }) }) From a303da41dbb72f6a4c6702211e093a4829996cdb Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Thu, 10 Jun 2021 12:57:58 +0100 Subject: [PATCH 14/28] Suggestions from code review --- .../account/sendTokensToAddress.test.ts | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 105c1ba226..c63d8fc1a7 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -17,15 +17,21 @@ describe('SendTokenToAddress', () => { await container.stop() }) - let addA: string + let addressA: string + let addressB: string + let addressC: string async function setup (): Promise { - addA = await container.call('getnewaddress') - await createToken(addA, 'DBTC', 200) - await createToken(addA, 'ETH', 200) + addressB = await container.call('getnewaddress') + addressA = await container.call('getnewaddress') + addressC = await container.call('getnewaddress') + + await createToken(addressB, 'DBTC', 200, 100) + await createToken(addressC, 'DETH', 200, 50) + await createToken(addressA, 'ETH', 200, 200) } - async function createToken (address: string, symbol: string, amount: number): Promise { + async function createToken (address: string, symbol: string, amount: number, uxtoAmount: number = 100): Promise { const metadata = { symbol, name: symbol, @@ -34,11 +40,11 @@ describe('SendTokenToAddress', () => { tradeable: true, collateralAddress: address } - await container.waitForWalletBalanceGTE(101) + await container.waitForWalletBalanceGTE(200) await container.call('createtoken', [metadata]) await container.generate(1) - await container.call('utxostoaccount', [{ [address]: '100@0' }]) + await container.call('utxostoaccount', [{ [address]: `${uxtoAmount.toString()}@0` }]) await container.generate(1) await container.call('minttokens', [`${amount.toString()}@${symbol}`]) @@ -47,12 +53,12 @@ describe('SendTokenToAddress', () => { it('should create a transaction with auto select (empty source address)', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@ETH'] }) await container.generate(1) const account = await client.account.getAccount(address) - expect(account).toStrictEqual(['2.00000000@DFI']) + expect(account).toStrictEqual(['2.00000000@ETH']) expect(typeof transactionHex).toStrictEqual('string') }) @@ -64,11 +70,12 @@ describe('SendTokenToAddress', () => { const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['4@DFI'] }, options) await container.generate(1) - const account = await client.account.getAccount(address) - const balanceDeducted = await client.account.getAccount(addA) + const accountTo = await client.account.getAccount(address) + const accountFrom = await client.account.getAccount(addressA) + const accountFromAmount = Number(accountFrom[0].split('@')[0]) - expect(balanceDeducted[0]).toStrictEqual('194.00000000@DFI') - expect(account).toStrictEqual(['4.00000000@DFI']) + expect(accountFromAmount).toBeLessThan(200) + expect(accountTo).toStrictEqual(['4.00000000@DFI']) expect(typeof transactionHex).toStrictEqual('string') }) @@ -77,12 +84,12 @@ describe('SendTokenToAddress', () => { selectionMode: SelectionModeType.FORWARD } const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }, options) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['5@DFI'] }, options) await container.generate(1) - const account = await client.account.getAccount(address) + const accountTo = await client.account.getAccount(address) - expect(account).toStrictEqual(['2.00000000@DFI']) + expect(accountTo).toStrictEqual(['5.00000000@DFI']) expect(typeof transactionHex).toStrictEqual('string') }) @@ -91,12 +98,15 @@ describe('SendTokenToAddress', () => { selectionMode: SelectionModeType.CRUMBS } const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@DFI'] }, options) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['30@DFI'] }, options) await container.generate(1) - const account = await client.account.getAccount(address) + const accountToBalance = await client.account.getAccount(address) + const accountFrom = await client.account.getAccount(addressC) + const accountFromAmount = Number(accountFrom[0].split('@')[0]) - expect(account).toStrictEqual(['2.00000000@DFI']) + expect(accountFromAmount).toBeLessThan(50) + expect(accountToBalance).toStrictEqual(['30.00000000@DFI']) expect(typeof transactionHex).toStrictEqual('string') }) @@ -112,7 +122,7 @@ describe('SendTokenToAddress', () => { it('should create a transaction with source address provided', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [addA]: ['10@ETH'] }, { [address]: ['10@ETH'] }) + const transactionHex = await client.account.sendTokensToAddress({ [addressA]: ['10@ETH'] }, { [address]: ['10@ETH'] }) await container.generate(1) const account = await client.account.getAccount(address) @@ -123,12 +133,12 @@ describe('SendTokenToAddress', () => { it('should create a transaction with multiple source address tokens provided', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [addA]: ['2@DBTC', '10@ETH'] }, { [address]: ['2@DBTC', '10@ETH'] }) + const transactionHex = await client.account.sendTokensToAddress({ [addressA]: ['2@DFI', '10@ETH'] }, { [address]: ['2@DFI', '10@ETH'] }) await container.generate(1) const account = await client.account.getAccount(address) - expect(account).toStrictEqual(['2.00000000@DBTC', '10.00000000@ETH']) + expect(account).toStrictEqual(['2.00000000@DFI', '10.00000000@ETH']) expect(typeof transactionHex).toStrictEqual('string') }) @@ -137,8 +147,8 @@ describe('SendTokenToAddress', () => { }) it('should throw an error with insufficient fund', async () => { - const promise = client.account.sendTokensToAddress({ [addA]: ['500@DBTC'] }, { [await client.wallet.getNewAddress()]: ['500@DBTC'] }) + const promise = client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['500@ETH'] }) - await expect(promise).rejects.toThrow('amount 197.90000000 is less than 500.00000000') + await expect(promise).rejects.toThrow() }) }) From a85e47b24f255f77c82c9893cf0ce5f9338932e8 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Sun, 13 Jun 2021 23:03:29 +0100 Subject: [PATCH 15/28] Suggestions from code reviews --- .../account/sendTokensToAddress.test.ts | 98 ++++++++----------- website/docs/jellyfish/api/account.md | 2 +- 2 files changed, 40 insertions(+), 60 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index c63d8fc1a7..2ac2f5cda3 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -20,18 +20,31 @@ describe('SendTokenToAddress', () => { let addressA: string let addressB: string let addressC: string + let tokenAddress: string async function setup (): Promise { - addressB = await container.call('getnewaddress') + tokenAddress = await container.call('getnewaddress') addressA = await container.call('getnewaddress') + addressB = await container.call('getnewaddress') addressC = await container.call('getnewaddress') - await createToken(addressB, 'DBTC', 200, 100) - await createToken(addressC, 'DETH', 200, 50) - await createToken(addressA, 'ETH', 200, 200) + await createToken(tokenAddress, 'CAT', 100) + await createToken(tokenAddress, 'ETH', 100) + await createToken(tokenAddress, 'DETH', 100) + + await accountToAccount('CAT') + await accountToAccount('ETH') + await accountToAccount('DETH') } - async function createToken (address: string, symbol: string, amount: number, uxtoAmount: number = 100): Promise { + async function accountToAccount (symbol: string): Promise { + await container.call('accounttoaccount', [tokenAddress, { [addressA]: `${20}@${symbol}` }]) + await container.call('accounttoaccount', [tokenAddress, { [addressB]: `${30}@${symbol}` }]) + await container.call('accounttoaccount', [tokenAddress, { [addressC]: `${50}@${symbol}` }]) + await container.generate(1) + } + + async function createToken (address: string, symbol: string, amount: number): Promise { const metadata = { symbol, name: symbol, @@ -40,57 +53,29 @@ describe('SendTokenToAddress', () => { tradeable: true, collateralAddress: address } - await container.waitForWalletBalanceGTE(200) + + await container.waitForWalletBalanceGTE(100) await container.call('createtoken', [metadata]) await container.generate(1) - await container.call('utxostoaccount', [{ [address]: `${uxtoAmount.toString()}@0` }]) + await container.call('utxostoaccount', [{ [address]: '100@0' }]) await container.generate(1) await container.call('minttokens', [`${amount.toString()}@${symbol}`]) await container.generate(1) } - it('should create a transaction with auto select (empty source address)', async () => { + it('should create a transaction with auto select (empty source address) Pie selection mode', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@ETH'] }) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@CAT'] }) await container.generate(1) - const account = await client.account.getAccount(address) + const account = await client.account.getAccount(addressC) - expect(account).toStrictEqual(['2.00000000@ETH']) - expect(typeof transactionHex).toStrictEqual('string') - }) - - it('should create a transaction with Pie selection Mode', async () => { - const options: SendTokensOptions = { - selectionMode: SelectionModeType.PIE - } - const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['4@DFI'] }, options) - await container.generate(1) - - const accountTo = await client.account.getAccount(address) - const accountFrom = await client.account.getAccount(addressA) - const accountFromAmount = Number(accountFrom[0].split('@')[0]) - - expect(accountFromAmount).toBeLessThan(200) - expect(accountTo).toStrictEqual(['4.00000000@DFI']) - expect(typeof transactionHex).toStrictEqual('string') - }) - - it('should create a transaction with Forward selection mode', async () => { - const options: SendTokensOptions = { - selectionMode: SelectionModeType.FORWARD - } - const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['5@DFI'] }, options) - await container.generate(1) - - const accountTo = await client.account.getAccount(address) - - expect(accountTo).toStrictEqual(['5.00000000@DFI']) + expect(await client.account.getAccount(address)).toStrictEqual(['2.00000000@CAT']) expect(typeof transactionHex).toStrictEqual('string') + expect(transactionHex.length).toStrictEqual(64) + expect(account[0]).toStrictEqual('48.00000000@CAT') }) it('should create a transaction with Crumbs selection mode', async () => { @@ -98,26 +83,25 @@ describe('SendTokenToAddress', () => { selectionMode: SelectionModeType.CRUMBS } const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['30@DFI'] }, options) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['10@DETH'] }, options) await container.generate(1) - const accountToBalance = await client.account.getAccount(address) - const accountFrom = await client.account.getAccount(addressC) - const accountFromAmount = Number(accountFrom[0].split('@')[0]) + const account = await client.account.getAccount(addressA) - expect(accountFromAmount).toBeLessThan(50) - expect(accountToBalance).toStrictEqual(['30.00000000@DFI']) + expect(account[2]).toStrictEqual('10.00000000@DETH') + expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@DETH']) expect(typeof transactionHex).toStrictEqual('string') + expect(transactionHex.length).toStrictEqual(64) }) it('should create a transaction with multiple destination address tokens', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@ETH', '0.1@DBTC', '10@DFI'] }) + const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@ETH', '0.1@CAT', '10@DETH'] }) await container.generate(1) - const account = await client.account.getAccount(address) - expect(account).toStrictEqual(['10.00000000@DFI', '0.10000000@DBTC', '2.00000000@ETH']) + expect(await client.account.getAccount(address)).toStrictEqual(['0.10000000@CAT', '2.00000000@ETH', '10.00000000@DETH']) expect(typeof transactionHex).toStrictEqual('string') + expect(transactionHex.length).toStrictEqual(64) }) it('should create a transaction with source address provided', async () => { @@ -125,20 +109,16 @@ describe('SendTokenToAddress', () => { const transactionHex = await client.account.sendTokensToAddress({ [addressA]: ['10@ETH'] }, { [address]: ['10@ETH'] }) await container.generate(1) - const account = await client.account.getAccount(address) - - expect(account).toStrictEqual(['10.00000000@ETH']) + expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@ETH']) expect(typeof transactionHex).toStrictEqual('string') }) it('should create a transaction with multiple source address tokens provided', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [addressA]: ['2@DFI', '10@ETH'] }, { [address]: ['2@DFI', '10@ETH'] }) + const transactionHex = await client.account.sendTokensToAddress({ [addressA]: ['2@CAT', '10@ETH'] }, { [address]: ['2@CAT', '10@ETH'] }) await container.generate(1) - const account = await client.account.getAccount(address) - - expect(account).toStrictEqual(['2.00000000@DFI', '10.00000000@ETH']) + expect(await client.account.getAccount(address)).toStrictEqual(['2.00000000@CAT', '10.00000000@ETH']) expect(typeof transactionHex).toStrictEqual('string') }) @@ -149,6 +129,6 @@ describe('SendTokenToAddress', () => { it('should throw an error with insufficient fund', async () => { const promise = client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['500@ETH'] }) - await expect(promise).rejects.toThrow() + await expect(promise).rejects.toThrow('Not enough balance on wallet accounts, call utxostoaccount to increase it.') }) }) diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index 0b671ac1c5..279d129f8f 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -274,7 +274,7 @@ interface Account { sendTokensToAddress ( from: AddressBalances, to: AddressBalances, - options: SendTokensOptions ={selectionMode: SelectionModeType} + options: SendTokensOptions = {selectionMode: SelectionModeType} ): Promise } From bf4f71a0d8a215e00b93e3c5ef88d1f3aae11cd7 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Tue, 15 Jun 2021 05:28:41 +0100 Subject: [PATCH 16/28] Suggestions from code review --- .../__tests__/category/account/sendTokensToAddress.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 2ac2f5cda3..cd39f92b53 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -111,6 +111,7 @@ describe('SendTokenToAddress', () => { expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@ETH']) expect(typeof transactionHex).toStrictEqual('string') + expect(transactionHex.length).toStrictEqual(64) }) it('should create a transaction with multiple source address tokens provided', async () => { @@ -120,6 +121,7 @@ describe('SendTokenToAddress', () => { expect(await client.account.getAccount(address)).toStrictEqual(['2.00000000@CAT', '10.00000000@ETH']) expect(typeof transactionHex).toStrictEqual('string') + expect(transactionHex.length).toStrictEqual(64) }) it('should fail and throw an exception if destination address param is empty', async () => { From 2602078cb89be82480c6924dce7c9a6680d0487c Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Tue, 15 Jun 2021 09:31:01 +0100 Subject: [PATCH 17/28] Quick save --- .../account/sendTokensToAddress.test.ts | 100 ++++++++++-------- 1 file changed, 53 insertions(+), 47 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index cd39f92b53..ba7dcbf223 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -1,6 +1,6 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' -import { SelectionModeType, SendTokensOptions } from '../../../src/category/account' +import { SelectionModeType } from '../../../src/category/account' describe('SendTokenToAddress', () => { const container = new MasterNodeRegTestContainer() @@ -19,28 +19,31 @@ describe('SendTokenToAddress', () => { let addressA: string let addressB: string - let addressC: string - let tokenAddress: string + let addressC1: string + let addressC2: string + let addressC3: string async function setup (): Promise { - tokenAddress = await container.call('getnewaddress') addressA = await container.call('getnewaddress') addressB = await container.call('getnewaddress') - addressC = await container.call('getnewaddress') - - await createToken(tokenAddress, 'CAT', 100) - await createToken(tokenAddress, 'ETH', 100) - await createToken(tokenAddress, 'DETH', 100) - - await accountToAccount('CAT') - await accountToAccount('ETH') - await accountToAccount('DETH') - } - - async function accountToAccount (symbol: string): Promise { - await container.call('accounttoaccount', [tokenAddress, { [addressA]: `${20}@${symbol}` }]) - await container.call('accounttoaccount', [tokenAddress, { [addressB]: `${30}@${symbol}` }]) - await container.call('accounttoaccount', [tokenAddress, { [addressC]: `${50}@${symbol}` }]) + addressC1 = await container.call('getnewaddress') + addressC2 = await container.call('getnewaddress') + addressC3 = await container.call('getnewaddress') + + const tokenAddrA = await container.call('getnewaddress') + const tokenAddrB = await container.call('getnewaddress') + const tokenAddrC = await container.call('getnewaddress') + + await createToken(tokenAddrA, 'ANT', 200) + await createToken(tokenAddrB, 'DOG', 200) + await createToken(tokenAddrB, 'BAT', 200) + await createToken(tokenAddrC, 'CAT', 200) + + await container.call('accounttoaccount', [tokenAddrA, { [addressA]: `${3}@ANT` }]) + await container.call('accounttoaccount', [tokenAddrB, { [addressB]: `${68}@BAT` }]) + await container.call('accounttoaccount', [tokenAddrC, { [addressC1]: `${89}@CAT` }]) + await container.call('accounttoaccount', [tokenAddrC, { [addressC2]: `${15}@CAT` }]) + await container.call('accounttoaccount', [tokenAddrC, { [addressC3]: `${54}@CAT` }]) await container.generate(1) } @@ -65,63 +68,66 @@ describe('SendTokenToAddress', () => { await container.generate(1) } - it('should create a transaction with auto select (empty source address) Pie selection mode', async () => { + it('should create a transaction with Pie selection mode', async () => { + const accountBalanceBefore = await client.account.getAccount(addressC1) + const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@CAT'] }) + const hex = await client.account.sendTokensToAddress({}, { [address]: ['2@CAT'] }, { selectionMode: SelectionModeType.PIE }) await container.generate(1) - const account = await client.account.getAccount(addressC) + const accountBalanceAfter = await client.account.getAccount(addressC1) + expect(accountBalanceBefore).toStrictEqual(['89.00000000@CAT']) + expect(accountBalanceAfter).toStrictEqual(['87.00000000@CAT']) expect(await client.account.getAccount(address)).toStrictEqual(['2.00000000@CAT']) - expect(typeof transactionHex).toStrictEqual('string') - expect(transactionHex.length).toStrictEqual(64) - expect(account[0]).toStrictEqual('48.00000000@CAT') + expect(typeof hex).toStrictEqual('string') + expect(hex.length).toStrictEqual(64) }) it('should create a transaction with Crumbs selection mode', async () => { - const options: SendTokensOptions = { - selectionMode: SelectionModeType.CRUMBS - } + const accountBalanceBefore = await client.account.getAccount(addressC2) + const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['10@DETH'] }, options) + const hex = await client.account.sendTokensToAddress({}, { [address]: ['3@CAT'] }, { selectionMode: SelectionModeType.CRUMBS }) await container.generate(1) - const account = await client.account.getAccount(addressA) + const accountBalanceAfter = await client.account.getAccount(addressC2) - expect(account[2]).toStrictEqual('10.00000000@DETH') - expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@DETH']) - expect(typeof transactionHex).toStrictEqual('string') - expect(transactionHex.length).toStrictEqual(64) + expect(accountBalanceBefore).toStrictEqual(['15.00000000@CAT']) + expect(accountBalanceAfter).toStrictEqual(['12.00000000@CAT']) + expect(await client.account.getAccount(address)).toStrictEqual(['3.00000000@CAT']) + expect(typeof hex).toStrictEqual('string') + expect(hex.length).toStrictEqual(64) }) it('should create a transaction with multiple destination address tokens', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@ETH', '0.1@CAT', '10@DETH'] }) + const hex = await client.account.sendTokensToAddress({}, { [address]: ['10@CAT', '10@BAT'] }) await container.generate(1) - expect(await client.account.getAccount(address)).toStrictEqual(['0.10000000@CAT', '2.00000000@ETH', '10.00000000@DETH']) - expect(typeof transactionHex).toStrictEqual('string') - expect(transactionHex.length).toStrictEqual(64) + expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@BAT', '10.00000000@CAT']) + expect(typeof hex).toStrictEqual('string') + expect(hex.length).toStrictEqual(64) }) it('should create a transaction with source address provided', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [addressA]: ['10@ETH'] }, { [address]: ['10@ETH'] }) + const hex = await client.account.sendTokensToAddress({ [addressC1]: ['10@CAT'] }, { [address]: ['10@CAT'] }) await container.generate(1) - expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@ETH']) - expect(typeof transactionHex).toStrictEqual('string') - expect(transactionHex.length).toStrictEqual(64) + expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@CAT']) + expect(typeof hex).toStrictEqual('string') + expect(hex.length).toStrictEqual(64) }) it('should create a transaction with multiple source address tokens provided', async () => { const address = await client.wallet.getNewAddress() - const transactionHex = await client.account.sendTokensToAddress({ [addressA]: ['2@CAT', '10@ETH'] }, { [address]: ['2@CAT', '10@ETH'] }) + const hex = await client.account.sendTokensToAddress({ [addressB]: ['8@BAT'] }, { [address]: ['8@BAT'] }) await container.generate(1) - expect(await client.account.getAccount(address)).toStrictEqual(['2.00000000@CAT', '10.00000000@ETH']) - expect(typeof transactionHex).toStrictEqual('string') - expect(transactionHex.length).toStrictEqual(64) + expect(await client.account.getAccount(address)).toStrictEqual(['8.00000000@BAT']) + expect(typeof hex).toStrictEqual('string') + expect(hex.length).toStrictEqual(64) }) it('should fail and throw an exception if destination address param is empty', async () => { @@ -129,7 +135,7 @@ describe('SendTokenToAddress', () => { }) it('should throw an error with insufficient fund', async () => { - const promise = client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['500@ETH'] }) + const promise = client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['500@ANT'] }) await expect(promise).rejects.toThrow('Not enough balance on wallet accounts, call utxostoaccount to increase it.') }) From 1d7e752bb54a18350062a1e09fc8be66bf404158 Mon Sep 17 00:00:00 2001 From: websterlcl Date: Tue, 15 Jun 2021 17:24:51 +0800 Subject: [PATCH 18/28] rewrite setup fixture, added selection mode test --- .../account/sendTokensToAddress.test.ts | 198 +++++++++++------- 1 file changed, 127 insertions(+), 71 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index ba7dcbf223..b3fc76279c 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -1,6 +1,6 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' -import { SelectionModeType } from '../../../src/category/account' +import { SelectionModeType /* SendTokensOptions */ } from '../../../src/category/account' describe('SendTokenToAddress', () => { const container = new MasterNodeRegTestContainer() @@ -17,33 +17,34 @@ describe('SendTokenToAddress', () => { await container.stop() }) - let addressA: string - let addressB: string - let addressC1: string - let addressC2: string - let addressC3: string + let addrA: string + let addrB: string + let addrCForward: string + let addrCCrumbs: string + let addrCPie: string async function setup (): Promise { - addressA = await container.call('getnewaddress') - addressB = await container.call('getnewaddress') - addressC1 = await container.call('getnewaddress') - addressC2 = await container.call('getnewaddress') - addressC3 = await container.call('getnewaddress') - - const tokenAddrA = await container.call('getnewaddress') - const tokenAddrB = await container.call('getnewaddress') + addrA = await container.call('getnewaddress') + addrB = await container.call('getnewaddress') + addrCForward = await container.call('getnewaddress') + addrCCrumbs = await container.call('getnewaddress') + addrCPie = await container.call('getnewaddress') + console.log('addrCForward: ', addrCForward) + console.log('addrCCrumbs: ', addrCCrumbs) + console.log('addrCPie: ', addrCPie) + + const tokenaddrA = await container.call('getnewaddress') + const tokenaddrB = await container.call('getnewaddress') const tokenAddrC = await container.call('getnewaddress') - - await createToken(tokenAddrA, 'ANT', 200) - await createToken(tokenAddrB, 'DOG', 200) - await createToken(tokenAddrB, 'BAT', 200) + await createToken(tokenaddrA, 'ANT', 100) + await createToken(tokenaddrB, 'BAT', 100) await createToken(tokenAddrC, 'CAT', 200) - await container.call('accounttoaccount', [tokenAddrA, { [addressA]: `${3}@ANT` }]) - await container.call('accounttoaccount', [tokenAddrB, { [addressB]: `${68}@BAT` }]) - await container.call('accounttoaccount', [tokenAddrC, { [addressC1]: `${89}@CAT` }]) - await container.call('accounttoaccount', [tokenAddrC, { [addressC2]: `${15}@CAT` }]) - await container.call('accounttoaccount', [tokenAddrC, { [addressC3]: `${54}@CAT` }]) + await container.call('accounttoaccount', [tokenaddrA, { [addrA]: `${100}@ANT` }]) + await container.call('accounttoaccount', [tokenaddrB, { [addrB]: `${100}@BAT` }]) + await container.call('accounttoaccount', [tokenAddrC, { [addrCForward]: `${49}@CAT` }]) + await container.call('accounttoaccount', [tokenAddrC, { [addrCCrumbs]: `${3}@CAT` }]) + await container.call('accounttoaccount', [tokenAddrC, { [addrCPie]: `${104}@CAT` }]) await container.generate(1) } @@ -57,86 +58,141 @@ describe('SendTokenToAddress', () => { collateralAddress: address } - await container.waitForWalletBalanceGTE(100) + await container.waitForWalletBalanceGTE(amount) await container.call('createtoken', [metadata]) await container.generate(1) - await container.call('utxostoaccount', [{ [address]: '100@0' }]) + await container.call('utxostoaccount', [{ [address]: `${amount.toString()}@0` }]) await container.generate(1) await container.call('minttokens', [`${amount.toString()}@${symbol}`]) await container.generate(1) } - it('should create a transaction with Pie selection mode', async () => { - const accountBalanceBefore = await client.account.getAccount(addressC1) - - const address = await client.wallet.getNewAddress() - const hex = await client.account.sendTokensToAddress({}, { [address]: ['2@CAT'] }, { selectionMode: SelectionModeType.PIE }) - await container.generate(1) + function accToNum (acc: string): number { + return Number(acc.split('@')[0]) + } - const accountBalanceAfter = await client.account.getAccount(addressC1) + it('should sendTokensToAddress with selectionMode pie', async () => { + const accPieBefore = (await client.account.getAccount(addrCPie))[0] + console.log('accPieBefore: ', accPieBefore) - expect(accountBalanceBefore).toStrictEqual(['89.00000000@CAT']) - expect(accountBalanceAfter).toStrictEqual(['87.00000000@CAT']) - expect(await client.account.getAccount(address)).toStrictEqual(['2.00000000@CAT']) + const addrReceiver = await client.wallet.getNewAddress() + const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['4@CAT'] }, { + selectionMode: SelectionModeType.PIE + }) expect(typeof hex).toStrictEqual('string') expect(hex.length).toStrictEqual(64) - }) - it('should create a transaction with Crumbs selection mode', async () => { - const accountBalanceBefore = await client.account.getAccount(addressC2) - - const address = await client.wallet.getNewAddress() - const hex = await client.account.sendTokensToAddress({}, { [address]: ['3@CAT'] }, { selectionMode: SelectionModeType.CRUMBS }) await container.generate(1) - const accountBalanceAfter = await client.account.getAccount(addressC2) + const accPieAfter = (await client.account.getAccount(addrCPie))[0] + console.log('accPieAfter: ', accPieAfter) + expect(accToNum(accPieAfter)).toStrictEqual(accToNum(accPieBefore) - 4) // 100 - expect(accountBalanceBefore).toStrictEqual(['15.00000000@CAT']) - expect(accountBalanceAfter).toStrictEqual(['12.00000000@CAT']) - expect(await client.account.getAccount(address)).toStrictEqual(['3.00000000@CAT']) - expect(typeof hex).toStrictEqual('string') - expect(hex.length).toStrictEqual(64) + const accReceiver = await client.account.getAccount(addrReceiver) + console.log('accReceiver: ', accReceiver) + expect(accReceiver[0]).toStrictEqual('4.00000000@CAT') }) - it('should create a transaction with multiple destination address tokens', async () => { - const address = await client.wallet.getNewAddress() - const hex = await client.account.sendTokensToAddress({}, { [address]: ['10@CAT', '10@BAT'] }) - await container.generate(1) + it('should sendTokensToAddress with selectionMode crumbs', async () => { + const accCrumbsBefore = (await client.account.getAccount(addrCCrumbs))[0] + console.log('accCrumbsBefore: ', accCrumbsBefore) - expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@BAT', '10.00000000@CAT']) + const addrReceiver = await client.wallet.getNewAddress() + const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['2@CAT'] }, { + selectionMode: SelectionModeType.CRUMBS + }) expect(typeof hex).toStrictEqual('string') expect(hex.length).toStrictEqual(64) - }) - it('should create a transaction with source address provided', async () => { - const address = await client.wallet.getNewAddress() - const hex = await client.account.sendTokensToAddress({ [addressC1]: ['10@CAT'] }, { [address]: ['10@CAT'] }) await container.generate(1) - expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@CAT']) - expect(typeof hex).toStrictEqual('string') - expect(hex.length).toStrictEqual(64) + const accCrumbsAfter = (await client.account.getAccount(addrCCrumbs))[0] + console.log('accCrumbsAfter: ', accCrumbsAfter) + expect(accToNum(accCrumbsAfter)).toStrictEqual(accToNum(accCrumbsBefore) - 2) // 1 + + const accReceiver = await client.account.getAccount(addrReceiver) + console.log('accReceiver: ', accReceiver) + expect(accReceiver[0]).toStrictEqual('2.00000000@CAT') }) - it('should create a transaction with multiple source address tokens provided', async () => { - const address = await client.wallet.getNewAddress() - const hex = await client.account.sendTokensToAddress({ [addressB]: ['8@BAT'] }, { [address]: ['8@BAT'] }) - await container.generate(1) + // NOTE(canonrother): "selectionMode: forward" picks the address name order by ASC, its hard to test as address is random generated + it.skip('should sendTokensToAddress with selectionMode forward', async () => { + const accForwardBefore = (await client.account.getAccount(addrCCrumbs))[0] + console.log('accForwardBefore: ', accForwardBefore) - expect(await client.account.getAccount(address)).toStrictEqual(['8.00000000@BAT']) + const addrReceiver = await client.wallet.getNewAddress() + const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['6@CAT'] }, { + selectionMode: SelectionModeType.FORWARD + }) expect(typeof hex).toStrictEqual('string') expect(hex.length).toStrictEqual(64) - }) - it('should fail and throw an exception if destination address param is empty', async () => { - await expect(client.account.sendTokensToAddress({}, {})).rejects.toThrow('zero amounts in "to" param') - }) + await container.generate(1) - it('should throw an error with insufficient fund', async () => { - const promise = client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['500@ANT'] }) + const accForwardAfter = (await client.account.getAccount(addrCCrumbs))[0] + console.log('accForwardAfter: ', accForwardAfter) + expect(accToNum(accForwardAfter)).toStrictEqual(accToNum(accForwardBefore) - 6) // 43 - await expect(promise).rejects.toThrow('Not enough balance on wallet accounts, call utxostoaccount to increase it.') + const accReceiver = await client.account.getAccount(addrReceiver) + console.log('accReceiver: ', accReceiver) + expect(accReceiver[0]).toStrictEqual('6.00000000@CAT') }) + + // it('should create a transaction with Crumbs selection mode', async () => { + // const options: SendTokensOptions = { + // selectionMode: SelectionModeType.CRUMBS + // } + // const address = await client.wallet.getNewAddress() + // const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['10@DETH'] }, options) + // await container.generate(1) + + // const account = await client.account.getAccount(addrA) + + // expect(account[2]).toStrictEqual('10.00000000@DETH') + // expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@DETH']) + // expect(typeof transactionHex).toStrictEqual('string') + // expect(transactionHex.length).toStrictEqual(64) + // }) + + // it('should create a transaction with multiple destination address tokens', async () => { + // const address = await client.wallet.getNewAddress() + // const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@ETH', '0.1@CAT', '10@DETH'] }) + // await container.generate(1) + + // expect(await client.account.getAccount(address)).toStrictEqual(['0.10000000@CAT', '2.00000000@ETH', '10.00000000@DETH']) + // expect(typeof transactionHex).toStrictEqual('string') + // expect(transactionHex.length).toStrictEqual(64) + // }) + + // it('should create a transaction with source address provided', async () => { + // const address = await client.wallet.getNewAddress() + // const transactionHex = await client.account.sendTokensToAddress({ [addrA]: ['10@ETH'] }, { [address]: ['10@ETH'] }) + // await container.generate(1) + + // expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@ETH']) + // expect(typeof transactionHex).toStrictEqual('string') + // expect(transactionHex.length).toStrictEqual(64) + // }) + + // it('should create a transaction with multiple source address tokens provided', async () => { + // const address = await client.wallet.getNewAddress() + // const transactionHex = await client.account.sendTokensToAddress({ [addrA]: ['2@CAT', '10@ETH'] }, { [address]: ['2@CAT', '10@ETH'] }) + // await container.generate(1) + + // expect(await client.account.getAccount(address)).toStrictEqual(['2.00000000@CAT', '10.00000000@ETH']) + // expect(typeof transactionHex).toStrictEqual('string') + // expect(transactionHex.length).toStrictEqual(64) + // }) + + // it('should fail and throw an exception if destination address param is empty', async () => { + // await expect(client.account.sendTokensToAddress({}, {})).rejects.toThrow('zero amounts in "to" param') + // }) + + // it('should throw an error with insufficient fund', async () => { + // const promise = client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['500@ETH'] }) + + // await expect(promise).rejects.toThrow('Not enough balance on wallet accounts, call utxostoaccount to increase it.') + // }) }) From e75772ac1c0a7003020b71495e70f29030dffc2e Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Tue, 15 Jun 2021 11:37:06 +0100 Subject: [PATCH 19/28] Clean up --- .../account/sendTokensToAddress.test.ts | 105 ++++++------------ 1 file changed, 35 insertions(+), 70 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index b3fc76279c..67c2e774da 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -29,9 +29,6 @@ describe('SendTokenToAddress', () => { addrCForward = await container.call('getnewaddress') addrCCrumbs = await container.call('getnewaddress') addrCPie = await container.call('getnewaddress') - console.log('addrCForward: ', addrCForward) - console.log('addrCCrumbs: ', addrCCrumbs) - console.log('addrCPie: ', addrCPie) const tokenaddrA = await container.call('getnewaddress') const tokenaddrB = await container.call('getnewaddress') @@ -75,8 +72,6 @@ describe('SendTokenToAddress', () => { it('should sendTokensToAddress with selectionMode pie', async () => { const accPieBefore = (await client.account.getAccount(addrCPie))[0] - console.log('accPieBefore: ', accPieBefore) - const addrReceiver = await client.wallet.getNewAddress() const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['4@CAT'] }, { selectionMode: SelectionModeType.PIE @@ -87,17 +82,14 @@ describe('SendTokenToAddress', () => { await container.generate(1) const accPieAfter = (await client.account.getAccount(addrCPie))[0] - console.log('accPieAfter: ', accPieAfter) - expect(accToNum(accPieAfter)).toStrictEqual(accToNum(accPieBefore) - 4) // 100 + expect(accToNum(accPieAfter)).toStrictEqual(accToNum(accPieBefore) - 4) const accReceiver = await client.account.getAccount(addrReceiver) - console.log('accReceiver: ', accReceiver) expect(accReceiver[0]).toStrictEqual('4.00000000@CAT') }) it('should sendTokensToAddress with selectionMode crumbs', async () => { const accCrumbsBefore = (await client.account.getAccount(addrCCrumbs))[0] - console.log('accCrumbsBefore: ', accCrumbsBefore) const addrReceiver = await client.wallet.getNewAddress() const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['2@CAT'] }, { @@ -109,18 +101,15 @@ describe('SendTokenToAddress', () => { await container.generate(1) const accCrumbsAfter = (await client.account.getAccount(addrCCrumbs))[0] - console.log('accCrumbsAfter: ', accCrumbsAfter) - expect(accToNum(accCrumbsAfter)).toStrictEqual(accToNum(accCrumbsBefore) - 2) // 1 + expect(accToNum(accCrumbsAfter)).toStrictEqual(accToNum(accCrumbsBefore) - 2) const accReceiver = await client.account.getAccount(addrReceiver) - console.log('accReceiver: ', accReceiver) expect(accReceiver[0]).toStrictEqual('2.00000000@CAT') }) // NOTE(canonrother): "selectionMode: forward" picks the address name order by ASC, its hard to test as address is random generated it.skip('should sendTokensToAddress with selectionMode forward', async () => { const accForwardBefore = (await client.account.getAccount(addrCCrumbs))[0] - console.log('accForwardBefore: ', accForwardBefore) const addrReceiver = await client.wallet.getNewAddress() const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['6@CAT'] }, { @@ -132,67 +121,43 @@ describe('SendTokenToAddress', () => { await container.generate(1) const accForwardAfter = (await client.account.getAccount(addrCCrumbs))[0] - console.log('accForwardAfter: ', accForwardAfter) expect(accToNum(accForwardAfter)).toStrictEqual(accToNum(accForwardBefore) - 6) // 43 const accReceiver = await client.account.getAccount(addrReceiver) - console.log('accReceiver: ', accReceiver) expect(accReceiver[0]).toStrictEqual('6.00000000@CAT') }) - // it('should create a transaction with Crumbs selection mode', async () => { - // const options: SendTokensOptions = { - // selectionMode: SelectionModeType.CRUMBS - // } - // const address = await client.wallet.getNewAddress() - // const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['10@DETH'] }, options) - // await container.generate(1) - - // const account = await client.account.getAccount(addrA) - - // expect(account[2]).toStrictEqual('10.00000000@DETH') - // expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@DETH']) - // expect(typeof transactionHex).toStrictEqual('string') - // expect(transactionHex.length).toStrictEqual(64) - // }) - - // it('should create a transaction with multiple destination address tokens', async () => { - // const address = await client.wallet.getNewAddress() - // const transactionHex = await client.account.sendTokensToAddress({}, { [address]: ['2@ETH', '0.1@CAT', '10@DETH'] }) - // await container.generate(1) - - // expect(await client.account.getAccount(address)).toStrictEqual(['0.10000000@CAT', '2.00000000@ETH', '10.00000000@DETH']) - // expect(typeof transactionHex).toStrictEqual('string') - // expect(transactionHex.length).toStrictEqual(64) - // }) - - // it('should create a transaction with source address provided', async () => { - // const address = await client.wallet.getNewAddress() - // const transactionHex = await client.account.sendTokensToAddress({ [addrA]: ['10@ETH'] }, { [address]: ['10@ETH'] }) - // await container.generate(1) - - // expect(await client.account.getAccount(address)).toStrictEqual(['10.00000000@ETH']) - // expect(typeof transactionHex).toStrictEqual('string') - // expect(transactionHex.length).toStrictEqual(64) - // }) - - // it('should create a transaction with multiple source address tokens provided', async () => { - // const address = await client.wallet.getNewAddress() - // const transactionHex = await client.account.sendTokensToAddress({ [addrA]: ['2@CAT', '10@ETH'] }, { [address]: ['2@CAT', '10@ETH'] }) - // await container.generate(1) - - // expect(await client.account.getAccount(address)).toStrictEqual(['2.00000000@CAT', '10.00000000@ETH']) - // expect(typeof transactionHex).toStrictEqual('string') - // expect(transactionHex.length).toStrictEqual(64) - // }) - - // it('should fail and throw an exception if destination address param is empty', async () => { - // await expect(client.account.sendTokensToAddress({}, {})).rejects.toThrow('zero amounts in "to" param') - // }) - - // it('should throw an error with insufficient fund', async () => { - // const promise = client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['500@ETH'] }) - - // await expect(promise).rejects.toThrow('Not enough balance on wallet accounts, call utxostoaccount to increase it.') - // }) + it('should sendTokensToAddress with multiple receiver tokens', async () => { + const address = await client.wallet.getNewAddress() + const hex = await client.account.sendTokensToAddress({}, { [address]: ['2@ANT', '5@CAT', '10@BAT'] }) + + expect(typeof hex).toStrictEqual('string') + expect(hex.length).toStrictEqual(64) + + await container.generate(1) + + expect(await client.account.getAccount(address)).toStrictEqual(['2.00000000@ANT', '10.00000000@BAT', '5.00000000@CAT']) + }) + + it('should sendTokensToAddress with source address', async () => { + const addrReceiver = await client.wallet.getNewAddress() + const hex = await client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [addrReceiver]: ['10@ANT'] }) + + expect(typeof hex).toStrictEqual('string') + expect(hex.length).toStrictEqual(64) + + await container.generate(1) + + expect(await client.account.getAccount(addrReceiver)).toStrictEqual(['10.00000000@ANT']) + }) + + it('should fail and throw an exception when no receiver address', async () => { + await expect(client.account.sendTokensToAddress({}, {})).rejects.toThrow('zero amounts in "to" param') + }) + + it('should throw an error when insufficient fund', async () => { + const promise = client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['500@CAT'] }) + + await expect(promise).rejects.toThrow('Not enough balance on wallet accounts, call utxostoaccount to increase it.') + }) }) From dc27073f776eba194231a0c54ba412b24f7b1165 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Tue, 15 Jun 2021 12:36:16 +0100 Subject: [PATCH 20/28] Clean up --- .../category/account/sendTokensToAddress.test.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 67c2e774da..45031f88ec 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -84,8 +84,8 @@ describe('SendTokenToAddress', () => { const accPieAfter = (await client.account.getAccount(addrCPie))[0] expect(accToNum(accPieAfter)).toStrictEqual(accToNum(accPieBefore) - 4) - const accReceiver = await client.account.getAccount(addrReceiver) - expect(accReceiver[0]).toStrictEqual('4.00000000@CAT') + const accReceiver = (await client.account.getAccount(addrReceiver))[0] + expect(accReceiver).toStrictEqual('4.00000000@CAT') }) it('should sendTokensToAddress with selectionMode crumbs', async () => { @@ -103,8 +103,8 @@ describe('SendTokenToAddress', () => { const accCrumbsAfter = (await client.account.getAccount(addrCCrumbs))[0] expect(accToNum(accCrumbsAfter)).toStrictEqual(accToNum(accCrumbsBefore) - 2) - const accReceiver = await client.account.getAccount(addrReceiver) - expect(accReceiver[0]).toStrictEqual('2.00000000@CAT') + const accReceiver = (await client.account.getAccount(addrReceiver))[0] + expect(accReceiver).toStrictEqual('2.00000000@CAT') }) // NOTE(canonrother): "selectionMode: forward" picks the address name order by ASC, its hard to test as address is random generated @@ -121,10 +121,10 @@ describe('SendTokenToAddress', () => { await container.generate(1) const accForwardAfter = (await client.account.getAccount(addrCCrumbs))[0] - expect(accToNum(accForwardAfter)).toStrictEqual(accToNum(accForwardBefore) - 6) // 43 + expect(accToNum(accForwardAfter)).toStrictEqual(accToNum(accForwardBefore) - 6) - const accReceiver = await client.account.getAccount(addrReceiver) - expect(accReceiver[0]).toStrictEqual('6.00000000@CAT') + const accReceiver = (await client.account.getAccount(addrReceiver))[0] + expect(accReceiver).toStrictEqual('6.00000000@CAT') }) it('should sendTokensToAddress with multiple receiver tokens', async () => { From 029117e61d616d994a9285bb441017e44dfeb553 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Wed, 16 Jun 2021 08:49:51 +0100 Subject: [PATCH 21/28] Minor fixes --- .../account/sendTokensToAddress.test.ts | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 45031f88ec..b7ab5347ec 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -72,10 +72,11 @@ describe('SendTokenToAddress', () => { it('should sendTokensToAddress with selectionMode pie', async () => { const accPieBefore = (await client.account.getAccount(addrCPie))[0] - const addrReceiver = await client.wallet.getNewAddress() + const addrReceiver = await container.call('getnewaddress') const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['4@CAT'] }, { selectionMode: SelectionModeType.PIE }) + expect(typeof hex).toStrictEqual('string') expect(hex.length).toStrictEqual(64) @@ -91,7 +92,7 @@ describe('SendTokenToAddress', () => { it('should sendTokensToAddress with selectionMode crumbs', async () => { const accCrumbsBefore = (await client.account.getAccount(addrCCrumbs))[0] - const addrReceiver = await client.wallet.getNewAddress() + const addrReceiver = await container.call('getnewaddress') const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['2@CAT'] }, { selectionMode: SelectionModeType.CRUMBS }) @@ -128,7 +129,7 @@ describe('SendTokenToAddress', () => { }) it('should sendTokensToAddress with multiple receiver tokens', async () => { - const address = await client.wallet.getNewAddress() + const address = await container.call('getnewaddress') const hex = await client.account.sendTokensToAddress({}, { [address]: ['2@ANT', '5@CAT', '10@BAT'] }) expect(typeof hex).toStrictEqual('string') @@ -140,7 +141,7 @@ describe('SendTokenToAddress', () => { }) it('should sendTokensToAddress with source address', async () => { - const addrReceiver = await client.wallet.getNewAddress() + const addrReceiver = await container.call('getnewaddress') const hex = await client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [addrReceiver]: ['10@ANT'] }) expect(typeof hex).toStrictEqual('string') @@ -156,8 +157,22 @@ describe('SendTokenToAddress', () => { }) it('should throw an error when insufficient fund', async () => { - const promise = client.account.sendTokensToAddress({}, { [await client.wallet.getNewAddress()]: ['500@CAT'] }) + const promise = client.account.sendTokensToAddress({}, { [await container.call('getnewaddress')]: ['500@CAT'] }) await expect(promise).rejects.toThrow('Not enough balance on wallet accounts, call utxostoaccount to increase it.') }) + + it('should throw an error when sending different tokens', async () => { + const promise = client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [await container.call('getnewaddress')]: ['10@BAT'] }) + + await expect(promise).rejects.toThrow('\'Execution test failed:\n' + + 'ApplyAnyAccountsToAccountsTx: sum of inputs (from) != sum of outputs (to)\'') + }) + + it('should throw an error when sending different amount', async () => { + const promise = client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [await container.call('getnewaddress')]: ['20@ANT'] }) + + await expect(promise).rejects.toThrow('\'Execution test failed:\n' + + 'ApplyAnyAccountsToAccountsTx: sum of inputs (from) != sum of outputs (to)\'') + }) }) From fbc666ddf3a4461b1fb23ed78bf199f77f2194e0 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Wed, 16 Jun 2021 11:07:24 +0100 Subject: [PATCH 22/28] Fixed test failure --- .../__tests__/category/account/sendTokensToAddress.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index b7ab5347ec..21145e77e1 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -165,14 +165,12 @@ describe('SendTokenToAddress', () => { it('should throw an error when sending different tokens', async () => { const promise = client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [await container.call('getnewaddress')]: ['10@BAT'] }) - await expect(promise).rejects.toThrow('\'Execution test failed:\n' + - 'ApplyAnyAccountsToAccountsTx: sum of inputs (from) != sum of outputs (to)\'') + await expect(promise).rejects.toThrow('RpcApiError: \'Execution test failed:\nApplyAnyAccountsToAccountsTx: sum of inputs (from) != sum of outputs (to)\', code: -32600, method: sendtokenstoaddress') }) it('should throw an error when sending different amount', async () => { const promise = client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [await container.call('getnewaddress')]: ['20@ANT'] }) - await expect(promise).rejects.toThrow('\'Execution test failed:\n' + - 'ApplyAnyAccountsToAccountsTx: sum of inputs (from) != sum of outputs (to)\'') + await expect(promise).rejects.toThrow('RpcApiError: \'Execution test failed:\nApplyAnyAccountsToAccountsTx: sum of inputs (from) != sum of outputs (to)\', code: -32600, method: sendtokenstoaddress') }) }) From 4fa3108ad4b3a0a0c0dae23d4c058c6190cced01 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Wed, 16 Jun 2021 11:28:07 +0100 Subject: [PATCH 23/28] Fixed test failure --- .../__tests__/category/account/sendTokensToAddress.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 21145e77e1..7af5bada37 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -165,12 +165,12 @@ describe('SendTokenToAddress', () => { it('should throw an error when sending different tokens', async () => { const promise = client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [await container.call('getnewaddress')]: ['10@BAT'] }) - await expect(promise).rejects.toThrow('RpcApiError: \'Execution test failed:\nApplyAnyAccountsToAccountsTx: sum of inputs (from) != sum of outputs (to)\', code: -32600, method: sendtokenstoaddress') + await expect(promise).rejects.toThrow('RpcApiError: \'Test AnyAccountsToAccountsTx execution failed:\nsum of inputs (from) != sum of outputs (to)\', code: -32600, method: sendtokenstoaddress') }) it('should throw an error when sending different amount', async () => { const promise = client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [await container.call('getnewaddress')]: ['20@ANT'] }) - await expect(promise).rejects.toThrow('RpcApiError: \'Execution test failed:\nApplyAnyAccountsToAccountsTx: sum of inputs (from) != sum of outputs (to)\', code: -32600, method: sendtokenstoaddress') + await expect(promise).rejects.toThrow('RpcApiError: \'Test AnyAccountsToAccountsTx execution failed:\nsum of inputs (from) != sum of outputs (to)\', code: -32600, method: sendtokenstoaddress') }) }) From 0014c900fd5f1b965a772b75e79d541618dab6c5 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Thu, 17 Jun 2021 08:17:48 +0100 Subject: [PATCH 24/28] Minor fixes --- .../category/account/sendTokensToAddress.test.ts | 10 +++++----- packages/jellyfish-api-core/src/category/account.ts | 2 +- website/docs/jellyfish/api/account.md | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index 7af5bada37..c4bed8afa9 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -1,6 +1,6 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' -import { SelectionModeType /* SendTokensOptions */ } from '../../../src/category/account' +import { SelectionModeType } from '../../../src/category/account' describe('SendTokenToAddress', () => { const container = new MasterNodeRegTestContainer() @@ -112,7 +112,7 @@ describe('SendTokenToAddress', () => { it.skip('should sendTokensToAddress with selectionMode forward', async () => { const accForwardBefore = (await client.account.getAccount(addrCCrumbs))[0] - const addrReceiver = await client.wallet.getNewAddress() + const addrReceiver = await container.call('getnewaddress') const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['6@CAT'] }, { selectionMode: SelectionModeType.FORWARD }) @@ -129,15 +129,15 @@ describe('SendTokenToAddress', () => { }) it('should sendTokensToAddress with multiple receiver tokens', async () => { - const address = await container.call('getnewaddress') - const hex = await client.account.sendTokensToAddress({}, { [address]: ['2@ANT', '5@CAT', '10@BAT'] }) + const addrReceiver = await container.call('getnewaddress') + const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['2@ANT', '5@CAT', '10@BAT'] }) expect(typeof hex).toStrictEqual('string') expect(hex.length).toStrictEqual(64) await container.generate(1) - expect(await client.account.getAccount(address)).toStrictEqual(['2.00000000@ANT', '10.00000000@BAT', '5.00000000@CAT']) + expect(await client.account.getAccount(addrReceiver)).toStrictEqual(['2.00000000@ANT', '10.00000000@BAT', '5.00000000@CAT']) }) it('should sendTokensToAddress with source address', async () => { diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 8be91f5a58..7aa1bea8ca 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -323,7 +323,7 @@ export class Account { * * @param {AddressBalances} from The source defi address is the key, the value is amount in amount amount@token format * @param {AddressBalances} to The defi address is the key, the value is amount in amount amount@token format - * @param {SendTokensOptions} [options] + * @param {SendTokensOptions} [options = { selectionMode: SelectionModeType.PIE }] * @param {SelectionModeType} [options.selectionMode] Account selection mode. If "from" param is empty, it will auto select. * @return {Promise} */ diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index 279d129f8f..378254a022 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -274,7 +274,7 @@ interface Account { sendTokensToAddress ( from: AddressBalances, to: AddressBalances, - options: SendTokensOptions = {selectionMode: SelectionModeType} + options: SendTokensOptions = {selectionMode: SelectionModeType.PIE} ): Promise } From 8392652b4413203e34dbe9186d58d9867b5d8bbb Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Thu, 17 Jun 2021 08:36:35 +0100 Subject: [PATCH 25/28] Minor fixes --- website/docs/jellyfish/api/account.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index 378254a022..c540d40b3d 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -273,8 +273,8 @@ Creates a transfer transaction from your accounts balances. interface Account { sendTokensToAddress ( from: AddressBalances, - to: AddressBalances, - options: SendTokensOptions = {selectionMode: SelectionModeType.PIE} + to: AddressBalances, + options: SendTokensOptions = { selectionMode: SelectionModeType.PIE } ): Promise } From 383e106b550ef95d6690b845b818eab3e2359d08 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Mon, 21 Jun 2021 09:02:32 +0100 Subject: [PATCH 26/28] Update packages/jellyfish-api-core/src/category/account.ts Co-authored-by: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> --- packages/jellyfish-api-core/src/category/account.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 7aa1bea8ca..15da7b7a45 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -321,8 +321,8 @@ export class Account { /** * Creates a transfer transaction from your accounts balances. * - * @param {AddressBalances} from The source defi address is the key, the value is amount in amount amount@token format - * @param {AddressBalances} to The defi address is the key, the value is amount in amount amount@token format + * @param {AddressBalances} from source address as the key, the value is amount formatted as amount@token + * @param {AddressBalances} to address as the key, the value is amount formatted as amount@token * @param {SendTokensOptions} [options = { selectionMode: SelectionModeType.PIE }] * @param {SelectionModeType} [options.selectionMode] Account selection mode. If "from" param is empty, it will auto select. * @return {Promise} From 1d8121391c1241f9463dfc023b0c1344c7452778 Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Mon, 21 Jun 2021 09:03:24 +0100 Subject: [PATCH 27/28] Update website/docs/jellyfish/api/account.md Co-authored-by: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> --- website/docs/jellyfish/api/account.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index c540d40b3d..03e50eb350 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -269,12 +269,11 @@ interface AccountHistoryCountOptions { Creates a transfer transaction from your accounts balances. ```ts title="client.account.sendTokensToAddress()" - -interface Account { +interface account { sendTokensToAddress ( - from: AddressBalances, - to: AddressBalances, - options: SendTokensOptions = { selectionMode: SelectionModeType.PIE } + from: AddressBalances, + to: AddressBalances, + options: SendTokensOptions = { selectionMode: SelectionModeType.PIE } ): Promise } @@ -293,4 +292,4 @@ interface AddressBalances { interface SendTokensOptions { selectionMode: SelectionModeType } -``` \ No newline at end of file +``` From 65b56b098422ec0925485358e4ce40f903ffa24e Mon Sep 17 00:00:00 2001 From: Suraj Auwal Date: Mon, 21 Jun 2021 17:23:08 +0100 Subject: [PATCH 28/28] Test file refactor --- .../account/sendTokensToAddress.test.ts | 145 +++++++++--------- 1 file changed, 73 insertions(+), 72 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts index c4bed8afa9..7d4f449d52 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/sendTokensToAddress.test.ts @@ -17,32 +17,12 @@ describe('SendTokenToAddress', () => { await container.stop() }) - let addrA: string - let addrB: string - let addrCForward: string - let addrCCrumbs: string - let addrCPie: string + let address: string async function setup (): Promise { - addrA = await container.call('getnewaddress') - addrB = await container.call('getnewaddress') - addrCForward = await container.call('getnewaddress') - addrCCrumbs = await container.call('getnewaddress') - addrCPie = await container.call('getnewaddress') - - const tokenaddrA = await container.call('getnewaddress') - const tokenaddrB = await container.call('getnewaddress') - const tokenAddrC = await container.call('getnewaddress') - await createToken(tokenaddrA, 'ANT', 100) - await createToken(tokenaddrB, 'BAT', 100) - await createToken(tokenAddrC, 'CAT', 200) - - await container.call('accounttoaccount', [tokenaddrA, { [addrA]: `${100}@ANT` }]) - await container.call('accounttoaccount', [tokenaddrB, { [addrB]: `${100}@BAT` }]) - await container.call('accounttoaccount', [tokenAddrC, { [addrCForward]: `${49}@CAT` }]) - await container.call('accounttoaccount', [tokenAddrC, { [addrCCrumbs]: `${3}@CAT` }]) - await container.call('accounttoaccount', [tokenAddrC, { [addrCPie]: `${104}@CAT` }]) - await container.generate(1) + address = await container.call('getnewaddress') + await createToken(address, 'ANT', 100) + await createToken(address, 'BAT', 100) } async function createToken (address: string, symbol: string, amount: number): Promise { @@ -70,86 +50,107 @@ describe('SendTokenToAddress', () => { return Number(acc.split('@')[0]) } - it('should sendTokensToAddress with selectionMode pie', async () => { - const accPieBefore = (await client.account.getAccount(addrCPie))[0] - const addrReceiver = await container.call('getnewaddress') - const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['4@CAT'] }, { - selectionMode: SelectionModeType.PIE - }) + describe('sendTokensToAddress selectionModes', () => { + let tokenAddress: string + let addressForward: string + let addressPie: string + let addressCrumbs: string - expect(typeof hex).toStrictEqual('string') - expect(hex.length).toStrictEqual(64) + beforeAll(async () => { + tokenAddress = await container.call('getnewaddress') + addressPie = await container.call('getnewaddress') + addressForward = await container.call('getnewaddress') + addressCrumbs = await container.call('getnewaddress') - await container.generate(1) + await createToken(tokenAddress, 'CAT', 200) - const accPieAfter = (await client.account.getAccount(addrCPie))[0] - expect(accToNum(accPieAfter)).toStrictEqual(accToNum(accPieBefore) - 4) + await container.call('accounttoaccount', [tokenAddress, { [addressForward]: `${49}@CAT` }]) + await container.call('accounttoaccount', [tokenAddress, { [addressCrumbs]: `${3}@CAT` }]) + await container.call('accounttoaccount', [tokenAddress, { [addressPie]: `${104}@CAT` }]) + await container.generate(1) + }) - const accReceiver = (await client.account.getAccount(addrReceiver))[0] - expect(accReceiver).toStrictEqual('4.00000000@CAT') - }) + it('should sendTokensToAddress with selectionMode pie', async () => { + const balanceBefore = (await client.account.getAccount(addressPie))[0] + const addressReceiver = await container.call('getnewaddress') + const hex = await client.account.sendTokensToAddress({}, { [addressReceiver]: ['4@CAT'] }, { + selectionMode: SelectionModeType.PIE + }) + + expect(typeof hex).toStrictEqual('string') + expect(hex.length).toStrictEqual(64) - it('should sendTokensToAddress with selectionMode crumbs', async () => { - const accCrumbsBefore = (await client.account.getAccount(addrCCrumbs))[0] + await container.generate(1) - const addrReceiver = await container.call('getnewaddress') - const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['2@CAT'] }, { - selectionMode: SelectionModeType.CRUMBS + const balanceAfter = (await client.account.getAccount(addressPie))[0] + expect(accToNum(balanceAfter)).toStrictEqual(accToNum(balanceBefore) - 4) + + const accReceiver = (await client.account.getAccount(addressReceiver))[0] + expect(accReceiver).toStrictEqual('4.00000000@CAT') }) - expect(typeof hex).toStrictEqual('string') - expect(hex.length).toStrictEqual(64) - await container.generate(1) + it('should sendTokensToAddress with selectionMode crumbs', async () => { + const balanceBefore = (await client.account.getAccount(addressCrumbs))[0] + const addressReceiver = await container.call('getnewaddress') - const accCrumbsAfter = (await client.account.getAccount(addrCCrumbs))[0] - expect(accToNum(accCrumbsAfter)).toStrictEqual(accToNum(accCrumbsBefore) - 2) + const hex = await client.account.sendTokensToAddress({}, { [addressReceiver]: ['2@CAT'] }, { + selectionMode: SelectionModeType.CRUMBS + }) + expect(typeof hex).toStrictEqual('string') + expect(hex.length).toStrictEqual(64) - const accReceiver = (await client.account.getAccount(addrReceiver))[0] - expect(accReceiver).toStrictEqual('2.00000000@CAT') - }) + await container.generate(1) - // NOTE(canonrother): "selectionMode: forward" picks the address name order by ASC, its hard to test as address is random generated - it.skip('should sendTokensToAddress with selectionMode forward', async () => { - const accForwardBefore = (await client.account.getAccount(addrCCrumbs))[0] + const balanceAfter = (await client.account.getAccount(addressCrumbs))[0] + expect(accToNum(balanceAfter)).toStrictEqual(accToNum(balanceBefore) - 2) - const addrReceiver = await container.call('getnewaddress') - const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['6@CAT'] }, { - selectionMode: SelectionModeType.FORWARD + const accountReceiver = (await client.account.getAccount(addressReceiver))[0] + expect(accountReceiver).toStrictEqual('2.00000000@CAT') }) - expect(typeof hex).toStrictEqual('string') - expect(hex.length).toStrictEqual(64) - await container.generate(1) + // NOTE(canonrother): "selectionMode: forward" picks the address name order by ASC, its hard to test as address is random generated + it.skip('should sendTokensToAddress with selectionMode forward', async () => { + const balanceForwardBefore = (await client.account.getAccount(addressForward))[0] + const addressReceiver = await container.call('getnewaddress') + + const hex = await client.account.sendTokensToAddress({}, { [addressReceiver]: ['6@CAT'] }, { + selectionMode: SelectionModeType.FORWARD + }) + expect(typeof hex).toStrictEqual('string') + expect(hex.length).toStrictEqual(64) - const accForwardAfter = (await client.account.getAccount(addrCCrumbs))[0] - expect(accToNum(accForwardAfter)).toStrictEqual(accToNum(accForwardBefore) - 6) + await container.generate(1) - const accReceiver = (await client.account.getAccount(addrReceiver))[0] - expect(accReceiver).toStrictEqual('6.00000000@CAT') + const balanceAfter = (await client.account.getAccount(addressForward))[0] + expect(accToNum(balanceAfter)).toStrictEqual(accToNum(balanceForwardBefore) - 6) + + const accountReceiver = (await client.account.getAccount(balanceAfter))[0] + expect(accountReceiver).toStrictEqual('6.00000000@CAT') + }) }) it('should sendTokensToAddress with multiple receiver tokens', async () => { - const addrReceiver = await container.call('getnewaddress') - const hex = await client.account.sendTokensToAddress({}, { [addrReceiver]: ['2@ANT', '5@CAT', '10@BAT'] }) + const addressReceiver = await container.call('getnewaddress') + const hex = await client.account.sendTokensToAddress({}, { [addressReceiver]: ['2@ANT', '10@BAT'] }) expect(typeof hex).toStrictEqual('string') expect(hex.length).toStrictEqual(64) await container.generate(1) - expect(await client.account.getAccount(addrReceiver)).toStrictEqual(['2.00000000@ANT', '10.00000000@BAT', '5.00000000@CAT']) + expect(await client.account.getAccount(addressReceiver)).toStrictEqual(['2.00000000@ANT', '10.00000000@BAT']) }) it('should sendTokensToAddress with source address', async () => { - const addrReceiver = await container.call('getnewaddress') - const hex = await client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [addrReceiver]: ['10@ANT'] }) + const addressReceiver = await container.call('getnewaddress') + const hex = await client.account.sendTokensToAddress({ [address]: ['10@ANT'] }, { [addressReceiver]: ['10@ANT'] }) expect(typeof hex).toStrictEqual('string') expect(hex.length).toStrictEqual(64) await container.generate(1) - expect(await client.account.getAccount(addrReceiver)).toStrictEqual(['10.00000000@ANT']) + expect(await client.account.getAccount(addressReceiver)).toStrictEqual(['10.00000000@ANT']) }) it('should fail and throw an exception when no receiver address', async () => { @@ -163,13 +164,13 @@ describe('SendTokenToAddress', () => { }) it('should throw an error when sending different tokens', async () => { - const promise = client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [await container.call('getnewaddress')]: ['10@BAT'] }) + const promise = client.account.sendTokensToAddress({ [address]: ['10@ANT'] }, { [await container.call('getnewaddress')]: ['10@BAT'] }) await expect(promise).rejects.toThrow('RpcApiError: \'Test AnyAccountsToAccountsTx execution failed:\nsum of inputs (from) != sum of outputs (to)\', code: -32600, method: sendtokenstoaddress') }) it('should throw an error when sending different amount', async () => { - const promise = client.account.sendTokensToAddress({ [addrA]: ['10@ANT'] }, { [await container.call('getnewaddress')]: ['20@ANT'] }) + const promise = client.account.sendTokensToAddress({ [address]: ['10@ANT'] }, { [await container.call('getnewaddress')]: ['20@ANT'] }) await expect(promise).rejects.toThrow('RpcApiError: \'Test AnyAccountsToAccountsTx execution failed:\nsum of inputs (from) != sum of outputs (to)\', code: -32600, method: sendtokenstoaddress') })