diff --git a/packages/jellyfish-api-core/__tests__/category/icxorderbook/listHTLCs.test.ts b/packages/jellyfish-api-core/__tests__/category/icxorderbook/listHTLCs.test.ts new file mode 100644 index 0000000000..541ab14c4e --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/listHTLCs.test.ts @@ -0,0 +1,523 @@ +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, ICXOrderStatus +} from '../../../src/category/icxorderbook' +import BigNumber from 'bignumber.js' +import { accountBTC, accountDFI, ICXSetup, idDFI, symbolDFI } from './icx_setup' + +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 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 () => { + await container.stop() + }) + + afterEach(async () => { + // NOTE(surangap): enable this after #ain/583 + // await icxSetup.closeAllOpenOffers() + }) + + it('should list HTLCs for particular offer', async () => { + // create order - maker + const order: ICXOrder = { + tokenFrom: idDFI, + chainTo: 'BTC', + ownerAddress: accountDFI, + receivePubkey: '037f9563f30c609b19fd435a19b8bde7d6db703012ba1aba72e9f42a87366d1941', + amountFrom: new BigNumber(15), + orderPrice: new BigNumber(0.01) + } + const createOrderResult: ICXGenericResult = await client.icxorderbook.createOrder(order, []) + const createOrderTxId = createOrderResult.txid + await container.generate(1) + + // list ICX orders anc check + const ordersAfterCreateOrder: Record = await client.icxorderbook.listOrders() + 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(0.10), // 0.10 BTC = 10 DFI + ownerAddress: accountBTC + } + 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 accountBTCBeforeOffer[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 offersForOrder1: Record = await client.icxorderbook.listOrders({ orderTx: createOrderTxId }) + expect(Object.keys(offersForOrder1).length).toBe(2) // extra entry for the warning text returned by the RPC atm. + expect((offersForOrder1 as Record)[makeOfferTxId].status).toStrictEqual(ICXOrderStatus.OPEN) + + const accountDFIBeforeDFCHTLC: Record = await client.call('getaccount', [accountDFI, {}, true], 'bignumber') + // create DFCHTLC - maker + const DFCHTLC: HTLC = { + offerTx: makeOfferTxId, + amount: new BigNumber(10), // in DFC + hash: '957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220', + timeout: 500 + } + const DFCHTLCTxId = (await client.icxorderbook.submitDFCHTLC(DFCHTLC)).txid + await container.generate(1) + + const accountDFIAfterDFCHTLC: Record = await client.call('getaccount', [accountDFI, {}, true], 'bignumber') + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI].minus(0.01)) + + // List htlc anc check + const listHTLCOptions: ICXListHTLCOptions = { + offerTx: makeOfferTxId + } + const 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. + 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 = { + offerTx: makeOfferTxId, + amount: new BigNumber(0.10), + hash: '957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220', + htlcScriptAddress: '13sJQ9wBWh8ssihHUgAaCmNWJbBAG5Hr9N', + ownerPubkey: '036494e7c9467c8c7ff3bf29e841907fb0fa24241866569944ea422479ec0e6252', + timeout: 15 + } + const ExtHTLCTxId = (await client.icxorderbook.submitExtHTLC(ExtHTLC)).txid + await container.generate(1) + + // List htlc and check + const listHTLCOptionsAfterExtHTLC = { + offerTx: makeOfferTxId + } + const HTLCsAfterExtHTLC: Record = await client.icxorderbook.listHTLCs(listHTLCOptionsAfterExtHTLC) + expect(Object.keys(HTLCsAfterExtHTLC).length).toBe(3) // extra entry for the warning text returned by the RPC atm. + expect(HTLCsAfterExtHTLC[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(HTLCsAfterExtHTLC[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) + } + ) + }) + + it('should test ICXListHTLCOptions.limit parameter functionality', async () => { + const { order, createOrderTxId } = await icxSetup.createDFISellOrder('BTC', accountDFI, '037f9563f30c609b19fd435a19b8bde7d6db703012ba1aba72e9f42a87366d1941', new BigNumber(15), new BigNumber(0.01)) + const { makeOfferTxId } = await icxSetup.createDFIBuyOffer(createOrderTxId, new BigNumber(0.10), accountBTC) + + const accountDFIBeforeDFCHTLC: Record = await client.call('getaccount', [accountDFI, {}, true], 'bignumber') + // create DFCHTLC - maker + const DFCHTLC: HTLC = { + offerTx: makeOfferTxId, + amount: new BigNumber(10), // in DFC + hash: '957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220', + timeout: 500 + } + const DFCHTLCTxId = (await client.icxorderbook.submitDFCHTLC(DFCHTLC)).txid + await container.generate(1) + + const accountDFIAfterDFCHTLC: Record = await client.call('getaccount', [accountDFI, {}, true], 'bignumber') + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI].minus(0.01)) + + // List htlc anc check + const listHTLCOptions: ICXListHTLCOptions = { + offerTx: makeOfferTxId + } + const 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. + 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 = { + offerTx: makeOfferTxId, + amount: new BigNumber(0.10), + hash: '957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220', + htlcScriptAddress: '13sJQ9wBWh8ssihHUgAaCmNWJbBAG5Hr9N', + ownerPubkey: '036494e7c9467c8c7ff3bf29e841907fb0fa24241866569944ea422479ec0e6252', + timeout: 15 + } + const ExtHTLCTxId = (await client.icxorderbook.submitExtHTLC(ExtHTLC)).txid + await container.generate(1) + + // List htlc without limit param and check + const listHTLCOptionsAfterExtHTLC = { + offerTx: makeOfferTxId + } + const HTLCsAfterExtHTLC: Record = await client.icxorderbook.listHTLCs(listHTLCOptionsAfterExtHTLC) + expect(Object.keys(HTLCsAfterExtHTLC).length).toBe(3) // extra entry for the warning text returned by the RPC atm. + expect(HTLCsAfterExtHTLC[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(HTLCsAfterExtHTLC[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): Enable this after AIN#599 + // List htlc with limit of 1 and check + // const listHTLCOptionsWithLimit1 = { + // offerTx: makeOfferTxId, + // limit: 1 + // } + // const HTLCsWithLimit1: Record = await client.icxorderbook.listHTLCs(listHTLCOptionsWithLimit1) + // expect(Object.keys(HTLCsWithLimit1).length).toBe(2) + }) + + it('should list closed HTLCs with ICXListHTLCOptions.closed parameter after HTLCs are expired', async () => { + const startBlockHeight = (await container.call('getblockchaininfo', [])).blocks + const { order, createOrderTxId } = await icxSetup.createDFISellOrder('BTC', accountDFI, '037f9563f30c609b19fd435a19b8bde7d6db703012ba1aba72e9f42a87366d1941', new BigNumber(15), new BigNumber(0.01)) + const { makeOfferTxId } = await icxSetup.createDFIBuyOffer(createOrderTxId, new BigNumber(0.10), accountBTC) + + const accountDFIBeforeDFCHTLC: Record = await client.call('getaccount', [accountDFI, {}, true], 'bignumber') + // create DFCHTLC - maker + const DFCHTLC: HTLC = { + offerTx: makeOfferTxId, + amount: new BigNumber(10), // in DFC + hash: '957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220', + timeout: 500 + } + const DFCHTLCTxId = (await client.icxorderbook.submitDFCHTLC(DFCHTLC)).txid + await container.generate(1) + + const accountDFIAfterDFCHTLC: Record = await client.call('getaccount', [accountDFI, {}, true], 'bignumber') + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI].minus(0.01)) + + // submit EXT HTLC - taker + const ExtHTLC: ExtHTLC = { + offerTx: makeOfferTxId, + amount: new BigNumber(0.10), + hash: '957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220', + htlcScriptAddress: '13sJQ9wBWh8ssihHUgAaCmNWJbBAG5Hr9N', + ownerPubkey: '036494e7c9467c8c7ff3bf29e841907fb0fa24241866569944ea422479ec0e6252', + timeout: 15 + } + const ExtHTLCTxId = (await client.icxorderbook.submitExtHTLC(ExtHTLC)).txid + await container.generate(1) + + // List htlc without limit param and check + const listHTLCOptions: ICXListHTLCOptions = { + offerTx: makeOfferTxId + } + const 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. + 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) + } + ) + + // expire HTLCs + await container.generate(550) + const endBlockHeight = (await container.call('getblockchaininfo', [])).blocks + expect(endBlockHeight).toBeGreaterThan(Number(startBlockHeight) + Number(550)) + + // List htlc anc check + const listHTLCOptionsAfterExpiry = { + offerTx: makeOfferTxId + } + const HTLCsAfterExpiry: Record = await client.icxorderbook.listHTLCs(listHTLCOptionsAfterExpiry) + expect(Object.keys(HTLCsAfterExpiry).length).toBe(2) // NOTE(surangap): EXT HTLC will still be in OPEN state + + // List refended htlcs also and check + const listHTLCOptionsWithRefunded = { + offerTx: makeOfferTxId, + closed: true + } + const HTLCsWithRefunded: Record = await client.icxorderbook.listHTLCs(listHTLCOptionsWithRefunded) + expect(Object.keys(HTLCsWithRefunded).length).toBe(3) + }) + + it('should list closed HTLCs with ICXListHTLCOptions.closed parameter after DFC HTLC claim', async () => { + const { order, createOrderTxId } = await icxSetup.createDFISellOrder('BTC', accountDFI, '037f9563f30c609b19fd435a19b8bde7d6db703012ba1aba72e9f42a87366d1941', new BigNumber(15), new BigNumber(0.01)) + const { makeOfferTxId } = await icxSetup.createDFIBuyOffer(createOrderTxId, new BigNumber(0.10), accountBTC) + + const accountDFIBeforeDFCHTLC: Record = await client.call('getaccount', [accountDFI, {}, true], 'bignumber') + // create DFCHTLC - maker + const DFCHTLC: HTLC = { + offerTx: makeOfferTxId, + amount: new BigNumber(10), // in DFC + hash: '957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220', + timeout: 500 + } + const DFCHTLCTxId = (await client.icxorderbook.submitDFCHTLC(DFCHTLC)).txid + await container.generate(1) + + const accountDFIAfterDFCHTLC: Record = await client.call('getaccount', [accountDFI, {}, true], 'bignumber') + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI].minus(0.01)) + + // List htlc anc check + const listHTLCOptions: ICXListHTLCOptions = { + offerTx: makeOfferTxId + } + const 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. + 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 = { + offerTx: makeOfferTxId, + amount: new BigNumber(0.10), + hash: '957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220', + htlcScriptAddress: '13sJQ9wBWh8ssihHUgAaCmNWJbBAG5Hr9N', + ownerPubkey: '036494e7c9467c8c7ff3bf29e841907fb0fa24241866569944ea422479ec0e6252', + timeout: 15 + } + const ExtHTLCTxId = (await client.icxorderbook.submitExtHTLC(ExtHTLC)).txid + await container.generate(1) + + // List htlc without limit param and check + const listHTLCOptionsAfterExtHTLC = { + offerTx: makeOfferTxId + } + const HTLCsAfterExtHTLC: Record = await client.icxorderbook.listHTLCs(listHTLCOptionsAfterExtHTLC) + expect(Object.keys(HTLCsAfterExtHTLC).length).toBe(3) // extra entry for the warning text returned by the RPC atm. + expect(HTLCsAfterExtHTLC[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(HTLCsAfterExtHTLC[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 + await container.generate(1) + + // List HTLCs for offer + const listHTLCOptionsAfterClaim = { + offerTx: makeOfferTxId + } + const HTLCsAfterClaim: Record = await client.icxorderbook.listHTLCs(listHTLCOptionsAfterClaim) + expect(Object.keys(HTLCsAfterClaim).length).toBe(2) // extra entry for the warning text returned by the RPC atm. + // we have a common field "type", use that to narrow down the record + if (HTLCsAfterClaim[claimTxId].type === ICXHTLCType.CLAIM_DFC) { + // ICXClaimDFCHTLCInfo cast + const ClaimHTLCInfo: ICXClaimDFCHTLCInfo = HTLCsAfterClaim[claimTxId] as ICXClaimDFCHTLCInfo + expect(ClaimHTLCInfo.dfchtlcTx).toStrictEqual(DFCHTLCTxId) + expect(ClaimHTLCInfo.seed).toStrictEqual('f75a61ad8f7a6e0ab701d5be1f5d4523a9b534571e4e92e0c4610c6a6784ccef') + } + + // List htlc with closed=true and check + const listHTLCOptionsWithClosed = { + offerTx: makeOfferTxId, + closed: true + } + const HTLCsWithClosed: Record = await client.icxorderbook.listHTLCs(listHTLCOptionsWithClosed) + expect(Object.keys(HTLCsWithClosed).length).toBe(4) // extra entry for the warning text returned by the RPC atm. + // we have a common field "type", use that to narrow down the record + if (HTLCsWithClosed[claimTxId].type === ICXHTLCType.CLAIM_DFC) { + // ICXClaimDFCHTLCInfo cast + const ClaimHTLCInfo: ICXClaimDFCHTLCInfo = HTLCsWithClosed[claimTxId] as ICXClaimDFCHTLCInfo + expect(ClaimHTLCInfo.dfchtlcTx).toStrictEqual(DFCHTLCTxId) + expect(ClaimHTLCInfo.seed).toStrictEqual('f75a61ad8f7a6e0ab701d5be1f5d4523a9b534571e4e92e0c4610c6a6784ccef') + } + + if (HTLCsWithClosed[DFCHTLCTxId].type === ICXHTLCType.DFC) { + // ICXDFCHTLCInfo cast + const DFCHTLCInfo: ICXDFCHTLCInfo = HTLCsWithClosed[DFCHTLCTxId] as ICXDFCHTLCInfo + expect(DFCHTLCInfo.offerTx).toStrictEqual(makeOfferTxId) + expect(DFCHTLCInfo.status).toStrictEqual(ICXHTLCStatus.CLAIMED) + } + + if (HTLCsWithClosed[ExtHTLCTxId].type === ICXHTLCType.EXTERNAL) { + // ICXEXTHTLCInfo cast + const ExtHTLCInfo: ICXEXTHTLCInfo = HTLCsWithClosed[ExtHTLCTxId] as ICXEXTHTLCInfo + expect(ExtHTLCInfo.offerTx).toStrictEqual(makeOfferTxId) + expect(ExtHTLCInfo.status).toStrictEqual(ICXHTLCStatus.CLOSED) + } + }) + + it('should return an empty result set when invalid ICXListHTLCOptions.offerTx is passed', async () => { + const { order, createOrderTxId } = await icxSetup.createDFISellOrder('BTC', accountDFI, '037f9563f30c609b19fd435a19b8bde7d6db703012ba1aba72e9f42a87366d1941', new BigNumber(15), new BigNumber(0.01)) + const { makeOfferTxId } = await icxSetup.createDFIBuyOffer(createOrderTxId, new BigNumber(0.10), accountBTC) + + const accountDFIBeforeDFCHTLC: Record = await client.call('getaccount', [accountDFI, {}, true], 'bignumber') + // create DFCHTLC - maker + const DFCHTLC: HTLC = { + offerTx: makeOfferTxId, + amount: new BigNumber(10), // in DFC + hash: '957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220', + timeout: 500 + } + const DFCHTLCTxId = (await client.icxorderbook.submitDFCHTLC(DFCHTLC)).txid + await container.generate(1) + + const accountDFIAfterDFCHTLC: Record = await client.call('getaccount', [accountDFI, {}, true], 'bignumber') + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI].minus(0.01)) + + // List htlc and check + const listHTLCOptions: ICXListHTLCOptions = { + offerTx: makeOfferTxId + } + const 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. + 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 + const listHTLCOptionsIncorrectOfferTx = { + offerTx: '123' + } + const HTLCsForIncorrectOfferTx: Record = await client.icxorderbook.listHTLCs(listHTLCOptionsIncorrectOfferTx) + expect(Object.keys(HTLCsForIncorrectOfferTx).length).toBe(1) // extra entry for the warning text returned by the RPC atm. + + // List htlcs with invalid offer tx "INVALID_OFFER_TX" and check + const listHTLCOptionsIncorrectOfferTx2 = { + offerTx: 'INVALID_OFFER_TX' + } + const HTLCsForIncorrectOfferTx2: Record = await client.icxorderbook.listHTLCs(listHTLCOptionsIncorrectOfferTx2) + expect(Object.keys(HTLCsForIncorrectOfferTx2).length).toBe(1) // 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 8290cb4e83..e66abef5c4 100644 --- a/packages/jellyfish-api-core/src/category/icxorderbook.ts +++ b/packages/jellyfish-api-core/src/category/icxorderbook.ts @@ -179,7 +179,7 @@ export class ICXOrderBook { * @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) + * @param {boolean} [options.closed = false] Display closed orders (default: false) * @return {Promise>} Object including details of offers. */ async listOrders (options: { orderTx: string } & ICXListOrderOptions): Promise> @@ -192,7 +192,7 @@ export class ICXOrderBook { * @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) + * @param {boolean} [options.closed = false] Display closed orders (default: false) * @return {Promise>} Object including details of orders and offers. */ async listOrders (options?: ICXListOrderOptions): Promise> @@ -205,7 +205,7 @@ export class ICXOrderBook { * @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) + * @param {boolean} [options.closed = false] Display closed orders (default: false) * @return {Promise>} Object including details of the transaction. */ async listOrders (options: ICXListOrderOptions = {}): Promise> { @@ -217,6 +217,26 @@ export class ICXOrderBook { 'bignumber' ) } + + /** + * Returns information about HTLCs based on ICXListHTLCOptions passed + * + * @param {ICXListHTLCOptions} options + * @param {string} options.offerTx Offer txid for which to list all HTLCS + * @param {number} [options.limit = 20] Maximum number of orders to return (default: 20) + * @param {boolean} [options.refunded = false] Display refunded HTLC (default: false) + * @param {boolean} [options.closed = false] Display claimed HTLCs (default: false) + * @return {Promise>} Object indluding details of the HTLCS. + */ + async listHTLCs (options: ICXListHTLCOptions): Promise> { + return await this.client.call( + 'icx_listhtlcs', + [ + options + ], + 'bignumber' + ) + } } /** ICX order */ export interface ICXOrder { @@ -320,8 +340,10 @@ export enum ICXHTLCStatus { OPEN = 'OPEN', CLAIMED = 'CLAIMED', REFUNDED = 'REFUNDED', - EXPIRED = 'EXPIRED' + EXPIRED = 'EXPIRED', + CLOSED = 'CLOSED' } + /** ICX order info */ export interface ICXOrderInfo { /** Order status */ @@ -397,12 +419,10 @@ export interface ICXListOrderOptions { /** ICX listHTLC options */ export interface ICXListHTLCOptions { /** Offer txid for which to list all HTLCS */ - offerTx?: string + offerTx: string /** Maximum number of orders to return (default: 20) */ limit?: number - /** Display refunded HTLC (default: false) */ - refunded?: boolean - /** Display claimed HTLCs (default: false) NOTE(surangap): in c++ side desciption this is mentioned as "claimed". should be corrected */ + /** Display also claimed, expired and refunded HTLCs (default: false) */ closed?: boolean } diff --git a/website/docs/jellyfish/api/icxorderbook.md b/website/docs/jellyfish/api/icxorderbook.md index 22b4867ad5..092f36c067 100644 --- a/website/docs/jellyfish/api/icxorderbook.md +++ b/website/docs/jellyfish/api/icxorderbook.md @@ -178,6 +178,18 @@ interface icxorderbook { getOrder (orderTx: string): Promise> } +enum ICXOrderStatus { + OPEN = 'OPEN', + CLOSED = 'CLOSED', + FILLED = 'FILLED', + EXPIRED = 'EXPIRED' +} + +enum ICXOrderType { + INTERNAL = 'INTERNAL', + EXTERNAL = 'EXTERNAL', +} + interface ICXOrderInfo { status: ICXOrderStatus type: ICXOrderType @@ -221,6 +233,18 @@ interface icxorderbook { listOrders (options: ICXListOrderOptions = {}): Promise> } +enum ICXOrderStatus { + OPEN = 'OPEN', + CLOSED = 'CLOSED', + FILLED = 'FILLED', + EXPIRED = 'EXPIRED' +} + +enum ICXOrderType { + INTERNAL = 'INTERNAL', + EXTERNAL = 'EXTERNAL', +} + interface ICXListOrderOptions { token?: string chain?: string @@ -260,3 +284,68 @@ interface ICXOfferInfo { expireHeight: BigNumber } ``` + +## listHTLCs + +Returns information about HTLCs based on ICXListHTLCOptions passed + +```ts title="client.icxorderbook.listHTLCs()" +interface icxorderbook { + listHTLCs (options: ICXListHTLCOptions): Promise> +} + +enum ICXHTLCType { + CLAIM_DFC = 'CLAIM DFC', + DFC = 'DFC', + EXTERNAL = 'EXTERNAL' +} + +enum ICXHTLCStatus { + OPEN = 'OPEN', + CLAIMED = 'CLAIMED', + REFUNDED = 'REFUNDED', + EXPIRED = 'EXPIRED', + CLOSED = 'CLOSED' +} + +interface ICXListHTLCOptions { + offerTx: string + limit?: number + closed?: boolean +} + +interface ICXClaimDFCHTLCInfo { + type: ICXHTLCType + dfchtlcTx: string + seed: string + height: BigNumber +} + +interface ICXDFCHTLCInfo { + type: ICXHTLCType + status: ICXHTLCStatus + offerTx: string + amount: BigNumber + amountInEXTAsset: BigNumber + hash: string + timeout: BigNumber + height: BigNumber + refundHeight: BigNumber +} + +interface ICXEXTHTLCInfo { + type: ICXHTLCType + status: ICXHTLCStatus + offerTx: string + amount: BigNumber + amountInDFCAsset: BigNumber + hash: string + htlcScriptAddress: string + ownerPubkey: string + timeout: BigNumber + height: BigNumber +} +``` + + +