From f712ce8b3dc78453a9aa84d5279d9b61f7b4c3cf Mon Sep 17 00:00:00 2001 From: surangap Date: Wed, 23 Jun 2021 17:36:24 +0800 Subject: [PATCH] Refactored code. --- .../category/icxorderbook/closeOffer.test.ts | 134 ++++++++++++++---- .../src/category/icxorderbook.ts | 4 +- website/docs/jellyfish/api/icxorderbook.md | 4 +- 3 files changed, 113 insertions(+), 29 deletions(-) diff --git a/packages/jellyfish-api-core/__tests__/category/icxorderbook/closeOffer.test.ts b/packages/jellyfish-api-core/__tests__/category/icxorderbook/closeOffer.test.ts index 1571bcc320..5f98246550 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/closeOffer.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/closeOffer.test.ts @@ -1,19 +1,28 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import { MasterNodeRegTestContainer } from '@defichain/testcontainers' -import { ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, InputUTXO } from '../../../src/category/icxorderbook' +import { ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, UTXO, ICXOrderStatus, ICXOrderType } from '../../../src/category/icxorderbook' import BigNumber from 'bignumber.js' -import { setup, accountDFI, idDFI, checkDFISellOrderDetails, accountBTC, checkDFIBuyOfferDetails } from './common.test' +import { accountDFI, idDFI, accountBTC, ICXSetup, symbolDFI, symbolBTC, ICX_TAKERFEE_PER_BTC, DEX_DFI_PER_BTC_RATE } from './icx_setup' import { RpcApiError } from '../../../src' -describe('Should test ICXOrderBook.closeOffer', () => { +describe('ICXOrderBook.closeOffer', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) + const icxSetup = new ICXSetup(container) beforeAll(async () => { await container.start() await container.waitForReady() await container.waitForWalletCoinbaseMaturity() - await setup(container) + await icxSetup.createAccounts() + await icxSetup.createBTCToken() + await icxSetup.initializeTokensIds() + await icxSetup.mintBTCtoken(100) + await icxSetup.fundAccount(accountDFI, symbolDFI, 500) + await icxSetup.fundAccount(accountBTC, symbolDFI, 10) // for fee + await icxSetup.createBTCDFIPool() + await icxSetup.addLiquidityToBTCDFIPool(1, 100) + await icxSetup.setTakerFee(0.001) }) afterAll(async () => { @@ -24,7 +33,7 @@ describe('Should test ICXOrderBook.closeOffer', () => { // cleanup code here }) - it('Should close an offer', async () => { + it('should close an offer', async () => { // create order - maker const order: ICXOrder = { tokenFrom: idDFI, @@ -39,8 +48,23 @@ describe('Should test ICXOrderBook.closeOffer', () => { await container.generate(1) // list ICX orders - let orders: Record = await container.call('icx_listorders', []) - await checkDFISellOrderDetails(container, order, createOrderTxId, orders as Record) + let orders: Record = await client.call('icx_listorders', [], 'bignumber') + expect((orders as Record)[createOrderTxId]).toStrictEqual( + { + status: ICXOrderStatus.OPEN, + type: ICXOrderType.INTERNAL, + tokenFrom: order.tokenFrom === '0' ? symbolDFI : symbolBTC, + chainTo: order.chainTo, + receivePubkey: order.receivePubkey, + ownerAddress: order.ownerAddress, + amountFrom: order.amountFrom, + amountToFill: order.amountFrom, + orderPrice: order.orderPrice, + amountToFillInToAsset: order.amountFrom.multipliedBy(order.orderPrice), + height: expect.any(BigNumber), + expireHeight: expect.any(BigNumber) + } + ) // make offer to partial amout 10 DFI - taker const offer: ICXOffer = { @@ -56,12 +80,22 @@ describe('Should test ICXOrderBook.closeOffer', () => { const accountBTCAfterOffer = await container.call('getaccount', [accountBTC, {}, true]) // check fee of 0.01 DFI has been reduced from the accountBTCBefore[idDFI] // Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate - expect(Number(accountBTCAfterOffer[idDFI])).toStrictEqual(Number(accountBTCBeforeOffer[idDFI]) - Number(0.01000000)) + expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI] - 0.01) // List the ICX offers for orderTx = createOrderTxId and check - orders = await container.call('icx_listorders', [{ orderTx: createOrderTxId }]) + orders = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber') expect(Object.keys(orders).length).toBe(2) // extra entry for the warning text returned by the RPC atm. - await checkDFIBuyOfferDetails(container, offer, makeOfferTxId, orders as Record) + expect((orders as Record)[makeOfferTxId]).toStrictEqual( + { + orderTx: createOrderTxId, + status: ICXOrderStatus.OPEN, + amount: offer.amount, + amountInFromAsset: offer.amount.dividedBy(order.orderPrice), + ownerAddress: offer.ownerAddress, + takerFee: offer.amount.multipliedBy(ICX_TAKERFEE_PER_BTC).multipliedBy(DEX_DFI_PER_BTC_RATE), + expireHeight: expect.any(BigNumber) + } + ) // close offer makeOfferTxId - taker await client.icxorderbook.closeOffer(makeOfferTxId) @@ -76,7 +110,7 @@ describe('Should test ICXOrderBook.closeOffer', () => { expect(accountBTCAfterOfferClose).toStrictEqual(accountBTCBeforeOffer) }) - it('Should close an offer with input utxos', async () => { + it('should close an offer with input utxos', async () => { // create order - maker const order: ICXOrder = { tokenFrom: idDFI, @@ -91,8 +125,23 @@ describe('Should test ICXOrderBook.closeOffer', () => { await container.generate(1) // list ICX orders - let orders: Record = await container.call('icx_listorders', []) - await checkDFISellOrderDetails(container, order, createOrderTxId, orders as Record) + let orders: Record = await client.call('icx_listorders', [], 'bignumber') + expect((orders as Record)[createOrderTxId]).toStrictEqual( + { + status: ICXOrderStatus.OPEN, + type: ICXOrderType.INTERNAL, + tokenFrom: order.tokenFrom === '0' ? symbolDFI : symbolBTC, + chainTo: order.chainTo, + receivePubkey: order.receivePubkey, + ownerAddress: order.ownerAddress, + amountFrom: order.amountFrom, + amountToFill: order.amountFrom, + orderPrice: order.orderPrice, + amountToFillInToAsset: order.amountFrom.multipliedBy(order.orderPrice), + height: expect.any(BigNumber), + expireHeight: expect.any(BigNumber) + } + ) // make offer to partial amout 10 DFI - taker const offer: ICXOffer = { @@ -108,16 +157,26 @@ describe('Should test ICXOrderBook.closeOffer', () => { const accountBTCAfterOffer = await container.call('getaccount', [accountBTC, {}, true]) // check fee of 0.01 DFI has been reduced from the accountBTCBefore[idDFI] // Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate - expect(Number(accountBTCAfterOffer[idDFI])).toStrictEqual(Number(accountBTCBeforeOffer[idDFI]) - Number(0.01000000)) + expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI] - 0.01) // List the ICX offers for orderTx = createOrderTxId and check - orders = await container.call('icx_listorders', [{ orderTx: createOrderTxId }]) + orders = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber') expect(Object.keys(orders).length).toBe(2) // extra entry for the warning text returned by the RPC atm. - await checkDFIBuyOfferDetails(container, offer, makeOfferTxId, orders as Record) + expect((orders as Record)[makeOfferTxId]).toStrictEqual( + { + orderTx: createOrderTxId, + status: ICXOrderStatus.OPEN, + amount: offer.amount, + amountInFromAsset: offer.amount.dividedBy(order.orderPrice), + ownerAddress: offer.ownerAddress, + takerFee: offer.amount.multipliedBy(ICX_TAKERFEE_PER_BTC).multipliedBy(DEX_DFI_PER_BTC_RATE), + expireHeight: expect.any(BigNumber) + } + ) // input utxos const utxos = await container.call('listunspent', [1, 9999999, [accountBTC], true]) - const inputUTXOs: InputUTXO[] = utxos.map((utxo: InputUTXO) => { + const inputUTXOs: UTXO[] = utxos.map((utxo: UTXO) => { return { txid: utxo.txid, vout: utxo.vout @@ -128,7 +187,7 @@ describe('Should test ICXOrderBook.closeOffer', () => { await container.generate(1) // List the ICX offers for orderTx = createOrderTxId and check no more offers - orders = await container.call('icx_listorders', [{ orderTx: createOrderTxId }]) + orders = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber') expect(Object.keys(orders).length).toBe(1) // extra entry for the warning text returned by the RPC atm. // check accountBTC balance, should be the same as accountBTCBeforeOffer @@ -136,7 +195,7 @@ describe('Should test ICXOrderBook.closeOffer', () => { expect(accountBTCAfterOfferClose).toStrictEqual(accountBTCBeforeOffer) }) - it('Should return an error when incorrect offer transaction is passed', async () => { + it('should return an error when incorrect offer transaction is passed', async () => { // create order - maker const order: ICXOrder = { tokenFrom: idDFI, @@ -151,8 +210,23 @@ describe('Should test ICXOrderBook.closeOffer', () => { await container.generate(1) // list ICX orders - let orders: Record = await container.call('icx_listorders', []) - await checkDFISellOrderDetails(container, order, createOrderTxId, orders as Record) + let orders: Record = await client.call('icx_listorders', [], 'bignumber') + expect((orders as Record)[createOrderTxId]).toStrictEqual( + { + status: ICXOrderStatus.OPEN, + type: ICXOrderType.INTERNAL, + tokenFrom: order.tokenFrom === '0' ? symbolDFI : symbolBTC, + chainTo: order.chainTo, + receivePubkey: order.receivePubkey, + ownerAddress: order.ownerAddress, + amountFrom: order.amountFrom, + amountToFill: order.amountFrom, + orderPrice: order.orderPrice, + amountToFillInToAsset: order.amountFrom.multipliedBy(order.orderPrice), + height: expect.any(BigNumber), + expireHeight: expect.any(BigNumber) + } + ) // make offer to partial amout 10 DFI - taker const offer: ICXOffer = { @@ -168,12 +242,22 @@ describe('Should test ICXOrderBook.closeOffer', () => { const accountBTCAfterOffer = await container.call('getaccount', [accountBTC, {}, true]) // check fee of 0.01 DFI has been reduced from the accountBTCBefore[idDFI] // Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate - expect(Number(accountBTCAfterOffer[idDFI])).toStrictEqual(Number(accountBTCBeforeOffer[idDFI]) - Number(0.01000000)) + expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI] - 0.01) // List the ICX offers for orderTx = createOrderTxId and check - orders = await container.call('icx_listorders', [{ orderTx: createOrderTxId }]) + orders = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber') expect(Object.keys(orders).length).toBe(2) // extra entry for the warning text returned by the RPC atm. - await checkDFIBuyOfferDetails(container, offer, makeOfferTxId, orders as Record) + expect((orders as Record)[makeOfferTxId]).toStrictEqual( + { + orderTx: createOrderTxId, + status: ICXOrderStatus.OPEN, + amount: offer.amount, + amountInFromAsset: offer.amount.dividedBy(order.orderPrice), + ownerAddress: offer.ownerAddress, + takerFee: offer.amount.multipliedBy(ICX_TAKERFEE_PER_BTC).multipliedBy(DEX_DFI_PER_BTC_RATE), + expireHeight: expect.any(BigNumber) + } + ) // close offer "123" - taker const promise = client.icxorderbook.closeOffer('123') @@ -182,7 +266,7 @@ describe('Should test ICXOrderBook.closeOffer', () => { await expect(promise).rejects.toThrow('RpcApiError: \'OfferTx (0000000000000000000000000000000000000000000000000000000000000123) does not exist\', code: -8, method: icx_closeoffer') // List the ICX offers for orderTx = createOrderTxId and check - orders = await container.call('icx_listorders', [{ orderTx: createOrderTxId }]) + orders = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber') expect(Object.keys(orders).length).toBe(2) // extra entry for the warning text returned by the RPC atm. }) }) diff --git a/packages/jellyfish-api-core/src/category/icxorderbook.ts b/packages/jellyfish-api-core/src/category/icxorderbook.ts index f1e2bd4ed9..d68e7a9a59 100644 --- a/packages/jellyfish-api-core/src/category/icxorderbook.ts +++ b/packages/jellyfish-api-core/src/category/icxorderbook.ts @@ -66,12 +66,12 @@ export class ICXOrderBook { * Closes offer transaction. * * @param {string} offerTx Transaction Id of maker offer - * @param {InputUTXO[]} inputUTXOs Specific utxos to spend + * @param {UTXO[]} inputUTXOs Specific utxos to spend * @param {string} [inputUTXOs.txid] transaction Id * @param {number} [inputUTXOs.vout] The output number * @return {Promise} Object indluding transaction id of the the transaction */ - async closeOffer (offerTx: string, inputUTXOs: InputUTXO[] = []): Promise { + async closeOffer (offerTx: string, inputUTXOs: UTXO[] = []): Promise { return await this.client.call( 'icx_closeoffer', [ diff --git a/website/docs/jellyfish/api/icxorderbook.md b/website/docs/jellyfish/api/icxorderbook.md index eab1007128..ffed6791cd 100644 --- a/website/docs/jellyfish/api/icxorderbook.md +++ b/website/docs/jellyfish/api/icxorderbook.md @@ -79,10 +79,10 @@ Closes offer transaction. ```ts title="client.icxorderbook.closeOffer()" interface icxorderbook { - closeOffer (offerTx: string, inputUTXOs: InputUTXO[] = []): Promise + closeOffer (offerTx: string, inputUTXOs: UTXO[] = []): Promise } -interface InputUTXO { +interface UTXO { txid: string vout: number }