From f2ae5a861e754fdb449e1a68eed5c6de9c7defa8 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Tue, 1 Nov 2022 16:36:38 +0800 Subject: [PATCH 1/9] poolpair length test --- .../category/poolpair/createPoolPair.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/jellyfish-api-core/__tests__/category/poolpair/createPoolPair.test.ts b/packages/jellyfish-api-core/__tests__/category/poolpair/createPoolPair.test.ts index 50aa6ca81d..afc385a796 100644 --- a/packages/jellyfish-api-core/__tests__/category/poolpair/createPoolPair.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/poolpair/createPoolPair.test.ts @@ -1,6 +1,7 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' import BigNumber from 'bignumber.js' +import { RpcApiError } from '../../../src' describe('Poolpair', () => { const container = new MasterNodeRegTestContainer() @@ -78,4 +79,19 @@ describe('Poolpair', () => { } expect(assertions).toStrictEqual(1) }) + + it('should throw error if name is too long', async () => { + const address = await container.call('getnewaddress') + const metadata = { + tokenA: 'DBTC', + tokenB: 'DFI', + commission: 1, + status: true, + ownerAddress: address, + pairSymbol: '000000000' + } + const promise = client.poolpair.createPoolPair(metadata) + await expect(promise).rejects.toThrow(RpcApiError) + await expect(promise).rejects.toThrow('pairSymbol is larger than') + }) }) From 332771696316783c5efcd9343c1ab7a95b44d4ef Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Wed, 2 Nov 2022 16:38:47 +0800 Subject: [PATCH 2/9] add test to swap to sender if no recipient --- .../__tests__/category/poolpair/poolswap.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/jellyfish-api-core/__tests__/category/poolpair/poolswap.test.ts b/packages/jellyfish-api-core/__tests__/category/poolpair/poolswap.test.ts index 62fa9f1a42..e49f63d157 100644 --- a/packages/jellyfish-api-core/__tests__/category/poolpair/poolswap.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/poolpair/poolswap.test.ts @@ -710,4 +710,20 @@ describe('poolSwap asymmetric pool swap fee', () => { await checkCATtoDFI(poolPairBefore, receiverAddress, feeInPct, feeOutPct) }) + + it('should swap to sender if no recipient is specified', async () => { + await testing.fixture.createPoolPair({ + a: { amount: 1000, symbol: 'CAT' }, + b: { amount: 1000, symbol: 'DFI' } + }) + const swapper = await testing.generateAddress() + await utxosToAccount(container, 1, { address: swapper }) + + await container.call('compositeswap', [{ from: swapper, tokenFrom: 'DFI', amountFrom: 1, tokenTo: 'CAT' }]) + await container.generate(1) + + const swapperAccount = await client.account.getAccount(swapper) + expect(swapperAccount[0]).toContain('CAT') + expect(swapperAccount[0]).not.toContain('DFI') + }) }) From 61646b4124fff102ac6b1e07bcaf4ec155ec9654 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Wed, 2 Nov 2022 17:44:04 +0800 Subject: [PATCH 3/9] add commission fix test --- .../poolpair/addPoolLiquidity.test.ts | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts b/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts index 70248a8cfe..64600e5bcb 100644 --- a/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts @@ -1,10 +1,12 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' import { UTXO } from '@defichain/jellyfish-api-core/dist/category/poolpair' +import { Testing } from '@defichain/jellyfish-testing' describe('Poolpair', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) + const testing = Testing.create(container) beforeAll(async () => { await container.start() @@ -39,7 +41,7 @@ describe('Poolpair', () => { const defaultMetadata = { tokenA: 'DFI', tokenB, - commission: 0, + commission: 0.02, status: true, ownerAddress: address } @@ -85,6 +87,34 @@ describe('Poolpair', () => { expect(typeof data).toStrictEqual('string') }) + // runs on the HEAD-273e26b5f container instead of epic-grandcentral + it('should give rewards after activation', async () => { + const shareAddress = await container.call('getnewaddress') + + await client.poolpair.addPoolLiquidity({ + '*': ['5@DFI', '100@DDAI'] + }, shareAddress) + await container.generate(1) + + const address = await container.getNewAddress() + await container.call('sendtokenstoaddress', [{}, { [address]: ['20@DFI'] }]) + await testing.generate(1) + + await client.poolpair.poolSwap({ + from: address, + tokenFrom: 'DFI', + amountFrom: 10, + to: address, + tokenTo: 'DDAI' + }) + await container.generate(2) + + const accountHistory = (await client.account.listAccountHistory('all')).filter((item) => { + return item.type === 'Commission' + }) + expect(accountHistory).toHaveLength(1) + }) + it('should addPoolLiquidity with utxos', async () => { const shareAddress = await container.call('getnewaddress') const tokenAAddress = await container.call('getnewaddress') From 54125a35589536f4fd1e7f5bb06176efb8760ae9 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Wed, 2 Nov 2022 17:46:41 +0800 Subject: [PATCH 4/9] remove redundant Testing usage --- .../__tests__/category/poolpair/addPoolLiquidity.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts b/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts index 64600e5bcb..cbf8d81980 100644 --- a/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts @@ -1,12 +1,10 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' import { UTXO } from '@defichain/jellyfish-api-core/dist/category/poolpair' -import { Testing } from '@defichain/jellyfish-testing' describe('Poolpair', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) - const testing = Testing.create(container) beforeAll(async () => { await container.start() @@ -98,7 +96,7 @@ describe('Poolpair', () => { const address = await container.getNewAddress() await container.call('sendtokenstoaddress', [{}, { [address]: ['20@DFI'] }]) - await testing.generate(1) + await container.generate(1) await client.poolpair.poolSwap({ from: address, From 210806d48a75dfe98309e1faec2a3fad6fc0c932 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Tue, 8 Nov 2022 17:09:13 +0800 Subject: [PATCH 5/9] add testpoolswap RPC error tests --- apps/rich-list-api/docker-compose.yml | 2 +- apps/whale-api/docker-compose.yml | 2 +- .../category/poolpair/testpoolswap.test.ts | 38 +++++++++++++++++++ .../src/containers/DeFiDContainer.ts | 2 +- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/apps/rich-list-api/docker-compose.yml b/apps/rich-list-api/docker-compose.yml index d7e556c4f2..731510e274 100644 --- a/apps/rich-list-api/docker-compose.yml +++ b/apps/rich-list-api/docker-compose.yml @@ -13,7 +13,7 @@ services: POSTGRES_DB: defichainrichlist defi-blockchain: - image: defi/defichain:HEAD-49fba65ce + image: defi/defichain:master-1dff273bf ports: - "19554:19554" command: > diff --git a/apps/whale-api/docker-compose.yml b/apps/whale-api/docker-compose.yml index 57c6151e88..f8c1de2bb7 100644 --- a/apps/whale-api/docker-compose.yml +++ b/apps/whale-api/docker-compose.yml @@ -2,7 +2,7 @@ version: '3.7' services: defi-blockchain: - image: defi/defichain:HEAD-49fba65ce + image: defi/defichain:master-1dff273bf ports: - "19554:19554" command: > diff --git a/packages/jellyfish-api-core/__tests__/category/poolpair/testpoolswap.test.ts b/packages/jellyfish-api-core/__tests__/category/poolpair/testpoolswap.test.ts index c056707b5d..b8eb89341c 100644 --- a/packages/jellyfish-api-core/__tests__/category/poolpair/testpoolswap.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/poolpair/testpoolswap.test.ts @@ -189,4 +189,42 @@ describe('Poolpair', () => { } }) }) + + it('should throw error when no tokenFrom', async () => { + const promise = client.poolpair.testPoolSwap({ + from: await getNewAddress(container), + tokenFrom: '', + amountFrom: 13, + to: await getNewAddress(container), + tokenTo: 'DFI' + }) + + await expect(promise).rejects.toThrow(RpcApiError) + await expect(promise).rejects.toMatchObject({ + payload: { + code: -8, + message: 'tokenFrom is empty', + method: 'testpoolswap' + } + }) + }) + + it('should throw error when no tokenTo', async () => { + const promise = client.poolpair.testPoolSwap({ + from: await getNewAddress(container), + tokenFrom: 'DFI', + amountFrom: 13, + to: await getNewAddress(container), + tokenTo: '' + }) + + await expect(promise).rejects.toThrow(RpcApiError) + await expect(promise).rejects.toMatchObject({ + payload: { + code: -8, + message: 'tokenTo is empty', + method: 'testpoolswap' + } + }) + }) }) diff --git a/packages/testcontainers/src/containers/DeFiDContainer.ts b/packages/testcontainers/src/containers/DeFiDContainer.ts index a7eca2268c..f3ce857154 100644 --- a/packages/testcontainers/src/containers/DeFiDContainer.ts +++ b/packages/testcontainers/src/containers/DeFiDContainer.ts @@ -35,7 +35,7 @@ export abstract class DeFiDContainer extends DockerContainer { if (process?.env?.DEFICHAIN_DOCKER_IMAGE !== undefined) { return process.env.DEFICHAIN_DOCKER_IMAGE } - return 'defi/defichain:HEAD-49fba65ce' + return 'defi/defichain:master-1dff273bf' } public static readonly DefaultStartOptions = { From 471faee11fd659852fcc153c20b58e7d72528c0d Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Thu, 10 Nov 2022 12:05:09 +0800 Subject: [PATCH 6/9] WIP: compositeswap tests --- .../category/poolpair/compositeSwap.test.ts | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/packages/jellyfish-api-core/__tests__/category/poolpair/compositeSwap.test.ts b/packages/jellyfish-api-core/__tests__/category/poolpair/compositeSwap.test.ts index b1699ae444..63752c9412 100644 --- a/packages/jellyfish-api-core/__tests__/category/poolpair/compositeSwap.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/poolpair/compositeSwap.test.ts @@ -594,4 +594,60 @@ describe('compositeSwap', () => { await expect(promise).rejects.toThrow(RpcApiError) await expect(promise).rejects.toThrow('TokenTo was not found') }) + + it('should not compositeSwap for more than 3 pools', async () => { + await testing.generate(20) + const [toAddress, fromAddress] = await testing.generateAddress(2) + await testing.token.create({ symbol: 'T1' }) + await testing.token.create({ symbol: 'T2' }) + await testing.token.create({ symbol: 'T3' }) + await testing.token.create({ symbol: 'T4' }) + await testing.token.create({ symbol: 'T5' }) + await testing.generate(1) + + await testing.token.mint({ symbol: 'T1', amount: 20 }) + await testing.token.mint({ symbol: 'T2', amount: 20 }) + await testing.token.mint({ symbol: 'T3', amount: 20 }) + await testing.token.mint({ symbol: 'T4', amount: 20 }) + await testing.token.mint({ symbol: 'T5', amount: 10 }) + await testing.generate(1) + + await testing.poolpair.create({ tokenA: 'T1', tokenB: 'T2' }) + await testing.poolpair.create({ tokenA: 'T2', tokenB: 'T3' }) + await testing.poolpair.create({ tokenA: 'T3', tokenB: 'T4' }) + await testing.poolpair.create({ tokenA: 'T4', tokenB: 'T5' }) + await testing.generate(1) + + await testing.poolpair.add({ + a: { symbol: 'T1', amount: 10 }, + b: { symbol: 'T2', amount: 10 } + }) + await testing.poolpair.add({ + a: { symbol: 'T2', amount: 10 }, + b: { symbol: 'T3', amount: 10 } + }) + await testing.poolpair.add({ + a: { symbol: 'T3', amount: 10 }, + b: { symbol: 'T4', amount: 10 } + }) + await testing.poolpair.add({ + a: { symbol: 'T4', amount: 10 }, + b: { symbol: 'T5', amount: 10 } + }) + await testing.generate(1) + + await testing.token.send({ symbol: 'T1', amount: 1, address: fromAddress }) + await testing.generate(1) + + const metadata: poolpair.PoolSwapMetadata = { + from: fromAddress, + tokenFrom: 'T1', + amountFrom: 1, + to: toAddress, + tokenTo: 'T5', + maxPrice: 1 + } + + await client.poolpair.testPoolSwap(metadata) + }) }) From 431b967088121156824133464d7ebfc4170f91b6 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Mon, 21 Nov 2022 15:23:27 +0800 Subject: [PATCH 7/9] Revert "WIP: compositeswap tests" This reverts commit 471faee11fd659852fcc153c20b58e7d72528c0d. This test has been included in #1856 along with testpoolswap changes. --- .../category/poolpair/compositeSwap.test.ts | 56 ------------------- 1 file changed, 56 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/poolpair/compositeSwap.test.ts b/packages/jellyfish-api-core/__tests__/category/poolpair/compositeSwap.test.ts index 63752c9412..b1699ae444 100644 --- a/packages/jellyfish-api-core/__tests__/category/poolpair/compositeSwap.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/poolpair/compositeSwap.test.ts @@ -594,60 +594,4 @@ describe('compositeSwap', () => { await expect(promise).rejects.toThrow(RpcApiError) await expect(promise).rejects.toThrow('TokenTo was not found') }) - - it('should not compositeSwap for more than 3 pools', async () => { - await testing.generate(20) - const [toAddress, fromAddress] = await testing.generateAddress(2) - await testing.token.create({ symbol: 'T1' }) - await testing.token.create({ symbol: 'T2' }) - await testing.token.create({ symbol: 'T3' }) - await testing.token.create({ symbol: 'T4' }) - await testing.token.create({ symbol: 'T5' }) - await testing.generate(1) - - await testing.token.mint({ symbol: 'T1', amount: 20 }) - await testing.token.mint({ symbol: 'T2', amount: 20 }) - await testing.token.mint({ symbol: 'T3', amount: 20 }) - await testing.token.mint({ symbol: 'T4', amount: 20 }) - await testing.token.mint({ symbol: 'T5', amount: 10 }) - await testing.generate(1) - - await testing.poolpair.create({ tokenA: 'T1', tokenB: 'T2' }) - await testing.poolpair.create({ tokenA: 'T2', tokenB: 'T3' }) - await testing.poolpair.create({ tokenA: 'T3', tokenB: 'T4' }) - await testing.poolpair.create({ tokenA: 'T4', tokenB: 'T5' }) - await testing.generate(1) - - await testing.poolpair.add({ - a: { symbol: 'T1', amount: 10 }, - b: { symbol: 'T2', amount: 10 } - }) - await testing.poolpair.add({ - a: { symbol: 'T2', amount: 10 }, - b: { symbol: 'T3', amount: 10 } - }) - await testing.poolpair.add({ - a: { symbol: 'T3', amount: 10 }, - b: { symbol: 'T4', amount: 10 } - }) - await testing.poolpair.add({ - a: { symbol: 'T4', amount: 10 }, - b: { symbol: 'T5', amount: 10 } - }) - await testing.generate(1) - - await testing.token.send({ symbol: 'T1', amount: 1, address: fromAddress }) - await testing.generate(1) - - const metadata: poolpair.PoolSwapMetadata = { - from: fromAddress, - tokenFrom: 'T1', - amountFrom: 1, - to: toAddress, - tokenTo: 'T5', - maxPrice: 1 - } - - await client.poolpair.testPoolSwap(metadata) - }) }) From a8c0a48f25f0f298428cb3a404725f770a763df6 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Tue, 22 Nov 2022 15:13:42 +0800 Subject: [PATCH 8/9] Allow commission to pass independently or as part of the suite --- .../poolpair/addPoolLiquidity.test.ts | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts b/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts index cbf8d81980..1bc049ba25 100644 --- a/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/poolpair/addPoolLiquidity.test.ts @@ -60,32 +60,6 @@ describe('Poolpair', () => { await container.generate(1) } - it('should addPoolLiquidity', async () => { - const shareAddress = await container.call('getnewaddress') - const data = await client.poolpair.addPoolLiquidity({ - '*': ['10@DFI', '200@DDAI'] - }, shareAddress) - - expect(typeof data).toStrictEqual('string') - }) - - it('should addPoolLiquidity with specific input token address', async () => { - const tokenAAddress = await container.call('getnewaddress') - const tokenBAddress = await container.call('getnewaddress') - await container.call('sendtokenstoaddress', [{}, { [tokenAAddress]: ['10@DFI'] }]) - await container.call('sendtokenstoaddress', [{}, { [tokenBAddress]: ['200@DDAI'] }]) - await container.generate(1) - - const shareAddress = await container.call('getnewaddress') - const data = await client.poolpair.addPoolLiquidity({ - [tokenAAddress]: '5@DFI', - [tokenBAddress]: '100@DDAI' - }, shareAddress) - - expect(typeof data).toStrictEqual('string') - }) - - // runs on the HEAD-273e26b5f container instead of epic-grandcentral it('should give rewards after activation', async () => { const shareAddress = await container.call('getnewaddress') @@ -113,6 +87,31 @@ describe('Poolpair', () => { expect(accountHistory).toHaveLength(1) }) + it('should addPoolLiquidity', async () => { + const shareAddress = await container.call('getnewaddress') + const data = await client.poolpair.addPoolLiquidity({ + '*': ['10@DFI', '200@DDAI'] + }, shareAddress) + + expect(typeof data).toStrictEqual('string') + }) + + it('should addPoolLiquidity with specific input token address', async () => { + const tokenAAddress = await container.call('getnewaddress') + const tokenBAddress = await container.call('getnewaddress') + await container.call('sendtokenstoaddress', [{}, { [tokenAAddress]: ['10@DFI'] }]) + await container.call('sendtokenstoaddress', [{}, { [tokenBAddress]: ['200@DDAI'] }]) + await container.generate(1) + + const shareAddress = await container.call('getnewaddress') + const data = await client.poolpair.addPoolLiquidity({ + [tokenAAddress]: '5@DFI', + [tokenBAddress]: '100@DDAI' + }, shareAddress) + + expect(typeof data).toStrictEqual('string') + }) + it('should addPoolLiquidity with utxos', async () => { const shareAddress = await container.call('getnewaddress') const tokenAAddress = await container.call('getnewaddress') From 3bfaf9e166b419f8cb7a240ef9251b4c7995bacf Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Tue, 22 Nov 2022 15:31:35 +0800 Subject: [PATCH 9/9] Make pairSymbol length > 18 to fix test --- .../__tests__/category/poolpair/createPoolPair.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jellyfish-api-core/__tests__/category/poolpair/createPoolPair.test.ts b/packages/jellyfish-api-core/__tests__/category/poolpair/createPoolPair.test.ts index afc385a796..d139a36f07 100644 --- a/packages/jellyfish-api-core/__tests__/category/poolpair/createPoolPair.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/poolpair/createPoolPair.test.ts @@ -88,7 +88,7 @@ describe('Poolpair', () => { commission: 1, status: true, ownerAddress: address, - pairSymbol: '000000000' + pairSymbol: 'abcdefghijklmnopqrt' } const promise = client.poolpair.createPoolPair(metadata) await expect(promise).rejects.toThrow(RpcApiError)