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 9928a3de6a..5b9af0b953 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/closeOffer.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/closeOffer.test.ts @@ -81,6 +81,59 @@ describe('ICXOrderBook.closeOffer', () => { expect(accountBTCAfterOfferClose).toStrictEqual(accountBTCBeforeOffer) }) + // NOTE(surangap): This test is failing. + // it('should close an offer for sell BTC order from chain:BTC to chain:DFI', async () => { + // // create order - maker + // const order: ICXOrder = { + // chainFrom: 'BTC', + // tokenTo: idDFI, + // ownerAddress: accountDFI, + // amountFrom: new BigNumber(2), + // orderPrice: new BigNumber(100) + // } + // const createOrderResult: ICXGenericResult = await client.icxorderbook.createOrder(order, []) + // const createOrderTxId = createOrderResult.txid + // await container.generate(1) + + // // list ICX orders and check status + // const ordersAfterCreateOrder: Record = await client.call('icx_listorders', [], 'bignumber') + // expect((ordersAfterCreateOrder as Record)[createOrderTxId].status).toStrictEqual(ICXOrderStatus.OPEN) + + // const accountBTCBeforeOffer: Record = await client.call('getaccount', [accountBTC, {}, true], 'bignumber') + // // make offer to partial amount 10 DFI - taker + // const offer: ICXOffer = { + // orderTx: createOrderTxId, + // amount: new BigNumber(10), // + // ownerAddress: accountBTC, + // receivePubkey: '0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5' + // } + // const makeOfferResult = await client.icxorderbook.makeOffer(offer, []) + // const makeOfferTxId = makeOfferResult.txid + // await container.generate(1) + + // const accountBTCAfterOffer: Record = await client.call('getaccount', [accountBTC, {}, true], 'bignumber') + // // check fee of 0.01 DFI has been reduced from the accountBTCBefore[idDFI] + // // Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate + // expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI].minus(0.01)) + + // // List the ICX offers for orderTx = createOrderTxId and check + // const ordersAfterMakeOffer: Record = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber') + // expect(Object.keys(ordersAfterMakeOffer).length).toBe(2) // extra entry for the warning text returned by the RPC atm. + // expect((ordersAfterMakeOffer as Record)[makeOfferTxId].status).toStrictEqual(ICXOrderStatus.OPEN) + + // // close offer makeOfferTxId - taker + // await client.icxorderbook.closeOffer(makeOfferTxId) + // await container.generate(1) + + // // List the ICX offers for orderTx = createOrderTxId and check no more offers + // const ordersAfterCloseOffer: Record = await await container.call('icx_listorders', [{ orderTx: createOrderTxId }]) + // expect(Object.keys(ordersAfterCloseOffer).length).toBe(1) // extra entry for the warning text returned by the RPC atm. + + // // check accountBTC balance, should be the same as accountBTCBeforeOffer + // const accountBTCAfterOfferClose: Record = await client.call('getaccount', [accountBTC, {}, true], 'bignumber') + // expect(accountBTCAfterOfferClose).toStrictEqual(accountBTCBeforeOffer) + // }) + it('should close an offer with input utxos', async () => { // create order - maker const order: ICXOrder = { diff --git a/packages/jellyfish-api-core/__tests__/category/icxorderbook/icx_setup.ts b/packages/jellyfish-api-core/__tests__/category/icxorderbook/icx_setup.ts index 6edbc47394..e7d50b46a8 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/icx_setup.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/icx_setup.ts @@ -107,4 +107,17 @@ export class ICXSetup { expect(result.ICX_TAKERFEE_PER_BTC as number).toStrictEqual(fee) ICX_TAKERFEE_PER_BTC = result.ICX_TAKERFEE_PER_BTC as number } + + async closeAllOpenOffers (): Promise { + const orders = await this.container.call('icx_listorders', []) + for (const orderTx of Object.keys(orders).splice(1)) { + const offers = await this.container.call('icx_listorders', [{ orderTx: orderTx }]) + for (const offerTx of Object.keys(offers).splice(1)) { + if (offers[offerTx].status === 'OPEN') { + await this.container.call('icx_closeoffer', [offerTx]) + await this.container.generate(1) + } + } + } + } } diff --git a/packages/jellyfish-api-core/__tests__/category/icxorderbook/listOrders.test.ts b/packages/jellyfish-api-core/__tests__/category/icxorderbook/listOrders.test.ts index e89f30ab76..c87ad27fa0 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/listOrders.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/listOrders.test.ts @@ -30,6 +30,10 @@ describe('ICXOrderBook.listOrders', () => { await container.stop() }) + afterEach(async () => { + await icxSetup.closeAllOpenOffers() + }) + it('should list all the orders', async () => { // create first order - maker const order: ICXOrder = { @@ -128,11 +132,12 @@ describe('ICXOrderBook.listOrders', () => { // create second order - maker const order2: ICXOrder = { - chainFrom: 'BTC', - tokenTo: idDFI, + tokenFrom: idDFI, + chainTo: 'BTC', ownerAddress: accountDFI, - amountFrom: new BigNumber(2), - orderPrice: new BigNumber(100) + receivePubkey: '037f9563f30c609b19fd435a19b8bde7d6db703012ba1aba72e9f42a87366d1941', + amountFrom: new BigNumber(20), + orderPrice: new BigNumber(0.01) } const createOrder2Result = await client.icxorderbook.createOrder(order2, []) const createOrder2TxId = createOrder2Result.txid @@ -148,12 +153,11 @@ describe('ICXOrderBook.listOrders', () => { const makeOfferTxId = makeOfferResult.txid await container.generate(1) - // create offer to order createOrderTxId + // create offer to order createOrder2TxId const offer2: ICXOffer = { orderTx: createOrder2TxId, - amount: new BigNumber(1), // - ownerAddress: accountBTC, - receivePubkey: '0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5' + amount: new BigNumber(0.2), // 20 DFI = 0.2 BTC + ownerAddress: accountBTC } const makeOffer2Result = await client.icxorderbook.makeOffer(offer2, []) const makeOffer2TxId = makeOffer2Result.txid @@ -168,9 +172,8 @@ describe('ICXOrderBook.listOrders', () => { amount: offer2.amount, amountInFromAsset: offer2.amount.dividedBy(order2.orderPrice), ownerAddress: offer2.ownerAddress, - takerFee: offer2.amount.multipliedBy(ICX_TAKERFEE_PER_BTC), - expireHeight: expect.any(BigNumber), - receivePubkey: offer2.receivePubkey + takerFee: offer2.amount.multipliedBy(ICX_TAKERFEE_PER_BTC).multipliedBy(DEX_DFI_PER_BTC_RATE), + expireHeight: expect.any(BigNumber) } ) @@ -420,7 +423,7 @@ describe('ICXOrderBook.listOrders', () => { } ) - // const accountBTCBeforeOffer: Record = await client.call('getaccount', [accountBTC, {}, true], 'bignumber') + const accountBTCBeforeOffer: Record = await client.call('getaccount', [accountBTC, {}, true], 'bignumber') // make offer to partial amount 10 DFI - taker const offer: ICXOffer = { orderTx: createOrderTxId, @@ -441,12 +444,10 @@ describe('ICXOrderBook.listOrders', () => { const makeOffer2TxId = makeOffer2Result.txid await container.generate(1) - // const accountBTCAfterOffer: Record = await client.call('getaccount', [accountBTC, {}, true], 'bignumber') - - // NOTE(surangap): why below check is failing? + const accountBTCAfterOffer: Record = await client.call('getaccount', [accountBTC, {}, true], 'bignumber') // check fee of 0.02 DFI has been reduced from the accountBTCBeforeOffer[idDFI] // Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate - // expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI].minus(0.02)) + expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI].minus(0.02)) // List the ICX offers for orderTx = createOrderTxId and check const offersForOrder1: Record = await client.icxorderbook.listOrders({ orderTx: createOrderTxId }) diff --git a/packages/jellyfish-api-core/src/category/icxorderbook.ts b/packages/jellyfish-api-core/src/category/icxorderbook.ts index 0d5c3d2644..0f75a2d6f3 100644 --- a/packages/jellyfish-api-core/src/category/icxorderbook.ts +++ b/packages/jellyfish-api-core/src/category/icxorderbook.ts @@ -97,6 +97,28 @@ export class ICXOrderBook { ) } + /** + * Returns information about offers for passed order + * + * @param {} options + * @param {string} [options.orderTx] Order txid to list all offers for this order + * @return {Promise>} Object indluding details of offers. + */ + async listOrders (options: { orderTx: string }): Promise> + + /** + * Returns information about orders or fillorders based on ICXListOrderOptions passed + * + * @param {ICXListOrderOptions} options + * @param {string} [options.token] Token asset + * @param {string} [options.chain] Chain asset + * @param {string} [options.orderTx] Order txid to list all offers for this order + * @param {number} [options.limit = 50] Maximum number of orders to return (default: 50) + * @param {boolean} [options.closed] Display closed orders (default: false) + * @return {Promise>} Object indluding details of orders and offers. + */ + async listOrders (options?: ICXListOrderOptions): Promise> + /** * Returns information about orders or fillorders based on ICXListOrderOptions passed * diff --git a/website/docs/jellyfish/api/icxorderbook.md b/website/docs/jellyfish/api/icxorderbook.md index 00abe0cca4..a91c49cec8 100644 --- a/website/docs/jellyfish/api/icxorderbook.md +++ b/website/docs/jellyfish/api/icxorderbook.md @@ -136,6 +136,76 @@ interface ICXOfferInfo { ## listOrders +Returns information about offers for passed order + +```ts title="client.icxorderbook.listOrders()" +interface icxorderbook { + listOrders (options: { orderTx: string }): Promise> +} + +interface ICXOfferInfo { + orderTx: string + status: ICXOrderStatus + amount: BigNumber + amountInFromAsset: BigNumber + ownerAddress: string + receivePubkey?: string + takerFee: BigNumber + expireHeight: BigNumber +} +``` + +## listOrders + +Returns information about orders or fillorders based on ICXListOrderOptions passed + +```ts title="client.icxorderbook.listOrders()" +interface icxorderbook { + listOrders (options?: ICXListOrderOptions): Promise> +} + +interface ICXListOrderOptions { + token?: string + chain?: string + orderTx?: string + limit?: number + closed?: boolean +} + +interface ICXOrderInfo { + status: ICXOrderStatus + type: ICXOrderType + tokenFrom: string + chainTo?: string + receivePubkey?: string + chainFrom?: string + tokenTo?: string + ownerAddress: string + amountFrom: BigNumber + amountToFill: BigNumber + orderPrice: BigNumber + amountToFillInToAsset: BigNumber + height: BigNumber + expireHeight: BigNumber + closeHeight?: BigNumber + closeTx?: string + expired?: boolean +} + +interface ICXOfferInfo { + orderTx: string + status: ICXOrderStatus + amount: BigNumber + amountInFromAsset: BigNumber + ownerAddress: string + receivePubkey?: string + takerFee: BigNumber + expireHeight: BigNumber +} +``` + +## listOrders + Returns information about orders or fillorders based on ICXListOrderOptions passed ```ts title="client.icxorderbook.listOrders()"