diff --git a/packages/jellyfish-api-core/__tests__/category/icxorderbook/listHTLCs.test.ts b/packages/jellyfish-api-core/__tests__/category/icxorderbook/listHTLCs.test.ts index fd5b3e0e4e..2f1d210c64 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/listHTLCs.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/listHTLCs.test.ts @@ -2,20 +2,29 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ExtHTLC, HTLC, ICXClaimDFCHTLCInfo, ICXDFCHTLCInfo, ICXEXTHTLCInfo, ICXGenericResult, ICXListHTLCOptions, - ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, ICXHTLCType, ICXHTLCStatus + ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, ICXHTLCType, ICXHTLCStatus, ICXOrderStatus, ICXOrderType } from '../../../src/category/icxorderbook' import BigNumber from 'bignumber.js' -import { setup, accountDFI, idDFI, checkDFISellOrderDetails, accountBTC, checkDFIBuyOfferDetails, checkDFCHTLCDetails, checkEXTHTLCDetails } from './common.test' +import { accountBTC, accountDFI, DEX_DFI_PER_BTC_RATE, ICXSetup, ICX_TAKERFEE_PER_BTC, idDFI, symbolBTC, symbolDFI } from './icx_setup' -describe('Should test ICXOrderBook.listHTLCs', () => { +describe('ICXOrderBook.listHTLCs', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) + const icxSetup = new ICXSetup(container, client) 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 () => { @@ -26,55 +35,7 @@ describe('Should test ICXOrderBook.listHTLCs', () => { // cleanup code here }) - // common code for some tests until ICX offer - const setupUntilDFIBuyOffer = async (): Promise => { - // create order - maker - const order: ICXOrder = { - tokenFrom: idDFI, - chainTo: 'BTC', - ownerAddress: accountDFI, - receivePubkey: '037f9563f30c609b19fd435a19b8bde7d6db703012ba1aba72e9f42a87366d1941', - amountFrom: new BigNumber(15), - orderPrice: new BigNumber(0.01) - } - - let result: ICXGenericResult = await client.icxorderbook.createOrder(order, []) - const createOrderTxId = result.txid - - await container.generate(1) - - // list ICX orders - let orders: Record = await client.icxorderbook.listOrders() - await checkDFISellOrderDetails(container, order, createOrderTxId, orders as Record) - - // make Offer to partial amout 10 DFI - taker - const offer: ICXOffer = { - orderTx: createOrderTxId, - amount: new BigNumber(0.10), // 0.10 BTC = 10 DFI - ownerAddress: accountBTC - } - const accountBTCBeforeOffer = await container.call('getaccount', [accountBTC, {}, true]) - - result = await client.icxorderbook.makeOffer(offer, []) - const makeOfferTxId = result.txid - await container.generate(1) - - const accountBTCAfterOffer = await container.call('getaccount', [accountBTC, {}, true]) - - // check fee of 0.01 DFI has been reduced from the accountBTCBeforeOffer[idDFI] - // Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate - // NOTE(surangap): why sometimes garbage values are in expected, floting point representation problems? - expect(Number(accountBTCAfterOffer[idDFI]).toPrecision(8)).toStrictEqual((Number(accountBTCBeforeOffer[idDFI]) - Number(0.01)).toPrecision(8)) - - // List the ICX offers for orderTx = createOrderTxId and check - orders = await client.icxorderbook.listOrders({ orderTx: createOrderTxId }) - 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) - - return makeOfferTxId - } - - it('Should list htlcs for particular offer', async () => { + it('should list htlcs for particular offer', async () => { // create order - maker const order: ICXOrder = { tokenFrom: idDFI, @@ -90,7 +51,22 @@ describe('Should test ICXOrderBook.listHTLCs', () => { // list ICX orders anc check let orders: Record = await client.icxorderbook.listOrders() - await checkDFISellOrderDetails(container, order, createOrderTxId, orders as Record) + 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 = { @@ -107,12 +83,22 @@ describe('Should test ICXOrderBook.listHTLCs', () => { // check fee of 0.01 DFI has been reduced from the accountBTCBeforeOffer[idDFI] // Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate - expect(Number(accountBTCAfterOffer[idDFI])).toStrictEqual(Number(accountBTCBeforeOffer[idDFI]) - Number(0.01)) + expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI] - 0.01) // List the ICX offers for orderTx = createOrderTxId and check orders = await client.icxorderbook.listOrders({ orderTx: createOrderTxId }) 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) + } + ) const accountDFIBeforeDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) @@ -127,7 +113,7 @@ describe('Should test ICXOrderBook.listHTLCs', () => { await container.generate(1) const accountDFIAfterDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) - expect(Number(accountDFIAfterDFCHTLC[idDFI])).toStrictEqual(Number(accountDFIBeforeDFCHTLC[idDFI]) - Number(0.01)) + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI] - 0.01) // List htlc anc check let listHTLCOptions: ICXListHTLCOptions = { @@ -135,7 +121,19 @@ describe('Should test ICXOrderBook.listHTLCs', () => { } let HTLCs: Record = await client.icxorderbook.listHTLCs(listHTLCOptions) expect(Object.keys(HTLCs).length).toBe(2) // extra entry for the warning text returned by the RPC atm. - await checkDFCHTLCDetails(DFCHTLC, DFCHTLCTxId, HTLCs) + expect(HTLCs[DFCHTLCTxId] as ICXDFCHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.DFC, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: DFCHTLC.amount, + amountInEXTAsset: DFCHTLC.amount.multipliedBy(order.orderPrice), + hash: DFCHTLC.hash, + timeout: new BigNumber(DFCHTLC.timeout as number), + height: expect.any(BigNumber), + refundHeight: expect.any(BigNumber) + } + ) // submit EXT HTLC - taker const ExtHTLC: ExtHTLC = { @@ -155,14 +153,39 @@ describe('Should test ICXOrderBook.listHTLCs', () => { } HTLCs = await client.icxorderbook.listHTLCs(listHTLCOptions) expect(Object.keys(HTLCs).length).toBe(3) // extra entry for the warning text returned by the RPC atm. - await checkEXTHTLCDetails(ExtHTLC, ExtHTLCTxId, HTLCs) - await checkDFCHTLCDetails(DFCHTLC, DFCHTLCTxId, HTLCs) + expect(HTLCs[ExtHTLCTxId] as ICXEXTHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.EXTERNAL, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: ExtHTLC.amount, + amountInDFCAsset: ExtHTLC.amount.dividedBy(order.orderPrice), + hash: ExtHTLC.hash, + htlcScriptAddress: ExtHTLC.htlcScriptAddress, + ownerPubkey: ExtHTLC.ownerPubkey, + timeout: new BigNumber(ExtHTLC.timeout), + height: expect.any(BigNumber) + } + ) + expect(HTLCs[DFCHTLCTxId] as ICXDFCHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.DFC, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: DFCHTLC.amount, + amountInEXTAsset: DFCHTLC.amount.multipliedBy(order.orderPrice), + hash: DFCHTLC.hash, + timeout: new BigNumber(DFCHTLC.timeout as number), + height: expect.any(BigNumber), + refundHeight: expect.any(BigNumber) + } + ) }) // NOTE(surangap):check why this is failing /* - it('Should test ICXListHTLCOptions.limit parameter functionality', async () => { - const makeOfferTxId = await setupUntilDFIBuyOffer() + it('should test ICXListHTLCOptions.limit parameter functionality', async () => { + const {order, makeOfferTxId} = await icxSetup.setupUntilDFIBuyOffer() const accountDFIBeforeDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // create DFCHTLC - maker @@ -176,7 +199,7 @@ describe('Should test ICXOrderBook.listHTLCs', () => { await container.generate(1) const accountDFIAfterDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) - expect(Number(accountDFIAfterDFCHTLC[idDFI])).toStrictEqual(Number(accountDFIBeforeDFCHTLC[idDFI]) - Number(0.01)) + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI] - 0.01) // List htlc anc check let listHTLCOptions: ICXListHTLCOptions = { @@ -184,7 +207,19 @@ describe('Should test ICXOrderBook.listHTLCs', () => { } let HTLCs: Record = await client.icxorderbook.listHTLCs(listHTLCOptions) expect(Object.keys(HTLCs).length).toBe(2) // extra entry for the warning text returned by the RPC atm. - await checkDFCHTLCDetails(DFCHTLC, DFCHTLCTxId, HTLCs) + expect(HTLCs[DFCHTLCTxId] as ICXDFCHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.DFC, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: DFCHTLC.amount, + amountInEXTAsset: DFCHTLC.amount.multipliedBy(order.orderPrice), + hash: DFCHTLC.hash, + timeout: new BigNumber(DFCHTLC.timeout as number), + height: expect.any(BigNumber), + refundHeight: expect.any(BigNumber) + } + ) // submit EXT HTLC - taker const ExtHTLC: ExtHTLC = { @@ -204,8 +239,33 @@ describe('Should test ICXOrderBook.listHTLCs', () => { } HTLCs = await client.icxorderbook.listHTLCs(listHTLCOptions) expect(Object.keys(HTLCs).length).toBe(3) // extra entry for the warning text returned by the RPC atm. - await checkEXTHTLCDetails(ExtHTLC, ExtHTLCTxId, HTLCs) - await checkDFCHTLCDetails(DFCHTLC, DFCHTLCTxId, HTLCs) + expect(HTLCs[ExtHTLCTxId] as ICXEXTHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.EXTERNAL, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: ExtHTLC.amount, + amountInDFCAsset: ExtHTLC.amount.multipliedBy(order.orderPrice), + hash: ExtHTLC.hash, + htlcScriptAddress: ExtHTLC.htlcScriptAddress, + ownerPubkey: ExtHTLC.ownerPubkey, + timeout: new BigNumber(ExtHTLC.timeout), + height: expect.any(BigNumber) + } + ) + expect(HTLCs[DFCHTLCTxId] as ICXDFCHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.DFC, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: DFCHTLC.amount, + amountInEXTAsset: DFCHTLC.amount.multipliedBy(order.orderPrice), + hash: DFCHTLC.hash, + timeout: new BigNumber(DFCHTLC.timeout as number), + height: expect.any(BigNumber), + refundHeight: expect.any(BigNumber) + } + ) // List htlc with limit of 1 and check listHTLCOptions = { @@ -219,8 +279,8 @@ describe('Should test ICXOrderBook.listHTLCs', () => { // NOTE(surangap): check why this is failing /* - it('Should test ICXListHTLCOptions.refunded parameter functionality', async () => { - const makeOfferTxId = await setupUntilDFIBuyOffer() + it('should test ICXListHTLCOptions.refunded parameter functionality', async () => { + const {order, makeOfferTxId} = await icxSetup.setupUntilDFIBuyOffer() const accountDFIBeforeDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // create DFCHTLC - maker @@ -234,7 +294,7 @@ describe('Should test ICXOrderBook.listHTLCs', () => { await container.generate(1) const accountDFIAfterDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) - expect(Number(accountDFIAfterDFCHTLC[idDFI])).toStrictEqual(Number(accountDFIBeforeDFCHTLC[idDFI]) - Number(0.01)) + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI] - 0.01) // submit EXT HTLC - taker const ExtHTLC: ExtHTLC = { @@ -254,8 +314,33 @@ describe('Should test ICXOrderBook.listHTLCs', () => { } let HTLCs: Record = await client.icxorderbook.listHTLCs(listHTLCOptions) expect(Object.keys(HTLCs).length).toBe(3) // extra entry for the warning text returned by the RPC atm. - await checkEXTHTLCDetails(ExtHTLC, ExtHTLCTxId, HTLCs) - await checkDFCHTLCDetails(DFCHTLC, DFCHTLCTxId, HTLCs) + expect(HTLCs[ExtHTLCTxId] as ICXEXTHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.EXTERNAL, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: ExtHTLC.amount, + amountInDFCAsset: ExtHTLC.amount.multipliedBy(order.orderPrice), + hash: ExtHTLC.hash, + htlcScriptAddress: ExtHTLC.htlcScriptAddress, + ownerPubkey: ExtHTLC.ownerPubkey, + timeout: new BigNumber(ExtHTLC.timeout), + height: expect.any(BigNumber) + } + ) + expect(HTLCs[DFCHTLCTxId] as ICXDFCHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.DFC, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: DFCHTLC.amount, + amountInEXTAsset: DFCHTLC.amount.multipliedBy(order.orderPrice), + hash: DFCHTLC.hash, + timeout: new BigNumber(DFCHTLC.timeout as number), + height: expect.any(BigNumber), + refundHeight: expect.any(BigNumber) + } + ) // expire HTLCs await container.generate(550) @@ -279,8 +364,8 @@ describe('Should test ICXOrderBook.listHTLCs', () => { }) */ - it('Should test ICXListHTLCOptions.closed parameter functionality', async () => { - const makeOfferTxId = await setupUntilDFIBuyOffer() + it('should test ICXListHTLCOptions.closed parameter functionality', async () => { + const { order, makeOfferTxId } = await icxSetup.setupUntilDFIBuyOffer() const accountDFIBeforeDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // create DFCHTLC - maker @@ -294,7 +379,7 @@ describe('Should test ICXOrderBook.listHTLCs', () => { await container.generate(1) const accountDFIAfterDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) - expect(Number(accountDFIAfterDFCHTLC[idDFI])).toStrictEqual(Number(accountDFIBeforeDFCHTLC[idDFI]) - Number(0.01)) + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI] - 0.01) // List htlc anc check let listHTLCOptions: ICXListHTLCOptions = { @@ -302,7 +387,19 @@ describe('Should test ICXOrderBook.listHTLCs', () => { } let HTLCs: Record = await client.icxorderbook.listHTLCs(listHTLCOptions) expect(Object.keys(HTLCs).length).toBe(2) // extra entry for the warning text returned by the RPC atm. - await checkDFCHTLCDetails(DFCHTLC, DFCHTLCTxId, HTLCs) + expect(HTLCs[DFCHTLCTxId] as ICXDFCHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.DFC, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: DFCHTLC.amount, + amountInEXTAsset: DFCHTLC.amount.multipliedBy(order.orderPrice), + hash: DFCHTLC.hash, + timeout: new BigNumber(DFCHTLC.timeout as number), + height: expect.any(BigNumber), + refundHeight: expect.any(BigNumber) + } + ) // submit EXT HTLC - taker const ExtHTLC: ExtHTLC = { @@ -322,8 +419,33 @@ describe('Should test ICXOrderBook.listHTLCs', () => { } HTLCs = await client.icxorderbook.listHTLCs(listHTLCOptions) expect(Object.keys(HTLCs).length).toBe(3) // extra entry for the warning text returned by the RPC atm. - await checkEXTHTLCDetails(ExtHTLC, ExtHTLCTxId, HTLCs) - await checkDFCHTLCDetails(DFCHTLC, DFCHTLCTxId, HTLCs) + expect(HTLCs[ExtHTLCTxId] as ICXEXTHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.EXTERNAL, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: ExtHTLC.amount, + amountInDFCAsset: ExtHTLC.amount.dividedBy(order.orderPrice), + hash: ExtHTLC.hash, + htlcScriptAddress: ExtHTLC.htlcScriptAddress, + ownerPubkey: ExtHTLC.ownerPubkey, + timeout: new BigNumber(ExtHTLC.timeout), + height: expect.any(BigNumber) + } + ) + expect(HTLCs[DFCHTLCTxId] as ICXDFCHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.DFC, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: DFCHTLC.amount, + amountInEXTAsset: DFCHTLC.amount.multipliedBy(order.orderPrice), + hash: DFCHTLC.hash, + timeout: new BigNumber(DFCHTLC.timeout as number), + height: expect.any(BigNumber), + refundHeight: expect.any(BigNumber) + } + ) // claim - taker const claimTxId = (await client.icxorderbook.claimDFCHTLC(DFCHTLCTxId, 'f75a61ad8f7a6e0ab701d5be1f5d4523a9b534571e4e92e0c4610c6a6784ccef')).txid @@ -373,8 +495,8 @@ describe('Should test ICXOrderBook.listHTLCs', () => { } }) - it('Should return an empty result set when invalid ICXListHTLCOptions.offerTx is passed', async () => { - const makeOfferTxId = await setupUntilDFIBuyOffer() + it('should return an empty result set when invalid ICXListHTLCOptions.offerTx is passed', async () => { + const { order, makeOfferTxId } = await icxSetup.setupUntilDFIBuyOffer() const accountDFIBeforeDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // create DFCHTLC - maker @@ -388,7 +510,7 @@ describe('Should test ICXOrderBook.listHTLCs', () => { await container.generate(1) const accountDFIAfterDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) - expect(Number(accountDFIAfterDFCHTLC[idDFI])).toStrictEqual(Number(accountDFIBeforeDFCHTLC[idDFI]) - Number(0.01)) + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI] - 0.01) // List htlc and check let listHTLCOptions: ICXListHTLCOptions = { @@ -396,7 +518,19 @@ describe('Should test ICXOrderBook.listHTLCs', () => { } let HTLCs: Record = await client.icxorderbook.listHTLCs(listHTLCOptions) expect(Object.keys(HTLCs).length).toBe(2) // extra entry for the warning text returned by the RPC atm. - await checkDFCHTLCDetails(DFCHTLC, DFCHTLCTxId, HTLCs) + expect(HTLCs[DFCHTLCTxId] as ICXDFCHTLCInfo).toStrictEqual( + { + type: ICXHTLCType.DFC, + status: ICXOrderStatus.OPEN, + offerTx: makeOfferTxId, + amount: DFCHTLC.amount, + amountInEXTAsset: DFCHTLC.amount.multipliedBy(order.orderPrice), + hash: DFCHTLC.hash, + timeout: new BigNumber(DFCHTLC.timeout as number), + height: expect.any(BigNumber), + refundHeight: expect.any(BigNumber) + } + ) // List htlcs with invalid offer tx "123" and check // List htlc without limit param and check diff --git a/website/docs/jellyfish/api/icxorderbook.md b/website/docs/jellyfish/api/icxorderbook.md index c95c507bd6..6bc5dfbb6d 100644 --- a/website/docs/jellyfish/api/icxorderbook.md +++ b/website/docs/jellyfish/api/icxorderbook.md @@ -281,7 +281,7 @@ interface ICXClaimDFCHTLCInfo { type: ICXHTLCType dfchtlcTx: string seed: string - height: number + height: BigNumber } interface ICXDFCHTLCInfo { @@ -291,9 +291,9 @@ interface ICXDFCHTLCInfo { amount: BigNumber amountInEXTAsset: BigNumber hash: string - timeout: number - height: number - refundHeight: number + timeout: BigNumber + height: BigNumber + refundHeight: BigNumber } interface ICXEXTHTLCInfo { @@ -305,8 +305,8 @@ interface ICXEXTHTLCInfo { hash: string htlcScriptAddress: string ownerPubkey: string - timeout: number - height: number + timeout: BigNumber + height: BigNumber } ```