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 5f98246550..cc174a27d4 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/closeOffer.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/closeOffer.test.ts @@ -8,7 +8,7 @@ import { RpcApiError } from '../../../src' describe('ICXOrderBook.closeOffer', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) - const icxSetup = new ICXSetup(container) + const icxSetup = new ICXSetup(container, client) beforeAll(async () => { await container.start() diff --git a/packages/jellyfish-api-core/__tests__/category/icxorderbook/createOrder.test.ts b/packages/jellyfish-api-core/__tests__/category/icxorderbook/createOrder.test.ts index 6208e58b0c..a2b39f0631 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/createOrder.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/createOrder.test.ts @@ -10,7 +10,7 @@ import { RpcApiError } from '../../../src' describe('ICXOrderBook.createOrder', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) - const icxSetup = new ICXSetup(container) + const icxSetup = new ICXSetup(container, client) beforeAll(async () => { await container.start() diff --git a/packages/jellyfish-api-core/__tests__/category/icxorderbook/getOrder.test.ts b/packages/jellyfish-api-core/__tests__/category/icxorderbook/getOrder.test.ts index 1ebdc9e0fa..3c520b01c8 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/getOrder.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/getOrder.test.ts @@ -10,7 +10,7 @@ import { RpcApiError } from '../../../src' describe('ICXOrderBook.getOrder', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) - const icxSetup = new ICXSetup(container) + const icxSetup = new ICXSetup(container, client) beforeAll(async () => { await container.start() 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..d9c0abece0 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/icx_setup.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/icx_setup.ts @@ -1,5 +1,8 @@ +import { BigNumber } from '@defichain/jellyfish-api-core' +import { ICXOrder, ICXGenericResult, ICXOrderInfo, ICXOfferInfo, ICXOffer } from '@defichain/jellyfish-api-core/category/icxorderbook' import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { createToken, mintTokens, accountToAccount } from '@defichain/testing' +import { ContainerAdapterClient } from 'jellyfish-api-core/__tests__/container_adapter_client' // globals export let symbolDFI: string @@ -14,8 +17,10 @@ export let DEX_DFI_PER_BTC_RATE: number export class ICXSetup { private readonly container: MasterNodeRegTestContainer - constructor (container: MasterNodeRegTestContainer) { + private readonly client: ContainerAdapterClient + constructor (container: MasterNodeRegTestContainer, client: ContainerAdapterClient) { this.container = container + this.client = client // reset global variables symbolDFI = '' symbolBTC = '' @@ -107,4 +112,82 @@ export class ICXSetup { expect(result.ICX_TAKERFEE_PER_BTC as number).toStrictEqual(fee) ICX_TAKERFEE_PER_BTC = result.ICX_TAKERFEE_PER_BTC as number } + + // setup the flow until ICX offer for DFI sell order + async setupUntilDFIBuyOffer (): Promise<{order: ICXOrder, createOrderTxId: string, offer: ICXOffer, makeOfferTxId: string}> { + // 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 this.client.icxorderbook.createOrder(order) + const createOrderTxId = result.txid + + await this.container.generate(1) + + // list ICX orders + let orders: Record = await this.client.icxorderbook.listOrders() + expect((orders as Record)[createOrderTxId]).toStrictEqual( + { + status: 'OPEN', + type: '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 = { + orderTx: createOrderTxId, + amount: new BigNumber(0.10), // 0.10 BTC = 10 DFI + ownerAddress: accountBTC + } + const accountBTCBeforeOffer = await this.container.call('getaccount', [accountBTC, {}, true]) + + result = await this.client.icxorderbook.makeOffer(offer, []) + const makeOfferTxId = result.txid + await this.container.generate(1) + + const accountBTCAfterOffer = await this.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(accountBTCAfterOffer[idDFI].toPrecision(8)).toStrictEqual((accountBTCBeforeOffer[idDFI] - 0.01).toPrecision(8)) + + // List the ICX offers for orderTx = createOrderTxId and check + orders = await this.client.icxorderbook.listOrders({ orderTx: createOrderTxId }) + expect(Object.keys(orders).length).toBe(2) // extra entry for the warning text returned by the RPC atm. + expect((orders as Record)[makeOfferTxId]).toStrictEqual( + { + orderTx: createOrderTxId, + status: '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) + } + ) + + return { + order: order, + createOrderTxId: createOrderTxId, + offer: offer, + makeOfferTxId: makeOfferTxId + } + } } 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 039d11daf1..774d86de75 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/listOrders.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/listOrders.test.ts @@ -9,7 +9,7 @@ import { accountBTC, accountDFI, DEX_DFI_PER_BTC_RATE, ICXSetup, ICX_TAKERFEE_PE describe('ICXOrderBook.listOrders', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) - const icxSetup = new ICXSetup(container) + const icxSetup = new ICXSetup(container, client) beforeAll(async () => { await container.start() diff --git a/packages/jellyfish-api-core/__tests__/category/icxorderbook/makeOffer.test.ts b/packages/jellyfish-api-core/__tests__/category/icxorderbook/makeOffer.test.ts index 8b7b693bc6..01b87f0cb5 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/makeOffer.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/makeOffer.test.ts @@ -8,7 +8,7 @@ import { RpcApiError } from '@defichain/jellyfish-api-core' describe('ICXOrderBook.makeOffer', () => { const container = new MasterNodeRegTestContainer() const client = new ContainerAdapterClient(container) - const icxSetup = new ICXSetup(container) + const icxSetup = new ICXSetup(container, client) beforeAll(async () => { await container.start() diff --git a/packages/jellyfish-api-core/__tests__/category/icxorderbook/submitDFCHTLC.test.ts b/packages/jellyfish-api-core/__tests__/category/icxorderbook/submitDFCHTLC.test.ts index 326c79c2f4..825f030f0f 100644 --- a/packages/jellyfish-api-core/__tests__/category/icxorderbook/submitDFCHTLC.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/icxorderbook/submitDFCHTLC.test.ts @@ -1,25 +1,33 @@ import { ContainerAdapterClient } from '../../container_adapter_client' import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { - ExtHTLC, HTLC, ICXClaimDFCHTLCInfo, ICXDFCHTLCInfo, ICXEXTHTLCInfo, ICXGenericResult, - ICXListHTLCOptions, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, InputUTXO + HTLC, ICXClaimDFCHTLCInfo, ICXDFCHTLCInfo, ICXEXTHTLCInfo, ICXGenericResult, ExtHTLC, + ICXListHTLCOptions, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, ICXOrderStatus, ICXOrderType, ICXHTLCType, UTXO } from '../../../src/category/icxorderbook' import BigNumber from 'bignumber.js' import { - setup, accountDFI, idDFI, accountBTC, checkBTCBuyOfferDetails, checkBTCSellOrderDetails, - checkDFIBuyOfferDetails, checkDFISellOrderDetails, checkDFCHTLCDetails, checkEXTHTLCDetails -} from './common.test' -import { RpcApiError } from '../../../src' + accountDFI, idDFI, accountBTC, ICXSetup, DEX_DFI_PER_BTC_RATE, ICX_TAKERFEE_PER_BTC, symbolBTC, symbolDFI +} from './icx_setup' +import { RpcApiError } from '@defichain/jellyfish-api-core' -describe('Test ICXOrderBook.submitDFCHTLC', () => { +describe('ICXOrderBook.submitDFCHTLC', () => { 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 () => { @@ -30,8 +38,7 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { // cleanup code here }) - // common code for some tests until ICX offer - const setupUntilDFIBuyOffer = async (): Promise => { + it('should submit dfc htlc for a dfc buy offer', async () => { // create order - maker const order: ICXOrder = { tokenFrom: idDFI, @@ -49,54 +56,22 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { // 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 submit DFCHTLC for a DFC buy 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) - } - - 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) + 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 = { @@ -114,12 +89,22 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { // 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]) @@ -135,18 +120,30 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { const accountDFIAfterDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // maker fee should be reduced from accountDFIBeforeDFCHTLC - expect(Number(accountDFIAfterDFCHTLC[idDFI])).toStrictEqual(Number(accountDFIBeforeDFCHTLC[idDFI]) - Number(0.01)) + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI] - 0.01) // List htlc const listHTLCOptions: ICXListHTLCOptions = { offerTx: makeOfferTxId } - const HTLCs: Record = await container.call('icx_listhtlcs', [listHTLCOptions]) + const HTLCs: Record = await client.call('icx_listhtlcs', [listHTLCOptions], 'bignumber') 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) + } + ) }) - it('Should submit DFCHTLC for a BTC buy offer', async () => { + it('should submit dfc htlc for a btc buy offer', async () => { const accountDFIStartBalance = await container.call('getaccount', [accountDFI, {}, true]) const order: ICXOrder = { chainFrom: 'BTC', @@ -162,7 +159,21 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { // list ICX orders let orders: Record = await client.icxorderbook.listOrders() - await checkBTCSellOrderDetails(container, order, createOrderTxId, orders as Record) + expect((orders as Record)[createOrderTxId]).toStrictEqual( + { + status: ICXOrderStatus.OPEN, + type: ICXOrderType.EXTERNAL, + tokenTo: order.tokenTo === '0' ? symbolDFI : symbolBTC, + chainFrom: order.chainFrom, + 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) + } + ) // create Offer to partial amout 1 BTC - taker const offer: ICXOffer = { @@ -180,12 +191,23 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { // check fee of 0.1 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.1000000)) + expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI] - 0.1) // 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 checkBTCBuyOfferDetails(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), + expireHeight: expect.any(BigNumber), + receivePubkey: offer.receivePubkey + } + ) // submit EXT HTLC - maker const ExtHTLC: ExtHTLC = { @@ -203,13 +225,26 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { let listHTLCOptions = { offerTx: makeOfferTxId } - let HTLCs: Record = await await container.call('icx_listhtlcs', [listHTLCOptions]) + let HTLCs: Record = await client.call('icx_listhtlcs', [listHTLCOptions], 'bignumber') expect(Object.keys(HTLCs).length).toBe(2) // extra entry for the warning text returned by the RPC atm. - await checkEXTHTLCDetails(ExtHTLC, ExtHTLCTxId, 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) + } + ) const accountDFIAfterEXTHTLC = await container.call('getaccount', [accountDFI, {}, true]) // maker deposit should be reduced from accountDFI - expect(Number(accountDFIAfterEXTHTLC[idDFI])).toStrictEqual(accountDFIStartBalance[idDFI] - Number(0.1000000)) + expect(accountDFIAfterEXTHTLC[idDFI]).toStrictEqual(accountDFIStartBalance[idDFI] - 0.1) const accountBTCBeforeDFCHTLC = await container.call('getaccount', [accountBTC, {}, true]) @@ -229,13 +264,26 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { listHTLCOptions = { offerTx: makeOfferTxId } - HTLCs = await container.call('icx_listhtlcs', [listHTLCOptions]) + HTLCs = await client.call('icx_listhtlcs', [listHTLCOptions], 'bignumber') expect(Object.keys(HTLCs).length).toBe(3) // 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.dividedBy(order.orderPrice), + hash: DFCHTLC.hash, + timeout: new BigNumber(500), + height: expect.any(BigNumber), + refundHeight: expect.any(BigNumber) + } + ) }) - it('Should submit DFCHTLC for a DFC buy offer with input UTXOs', async () => { - const makeOfferTxId = await setupUntilDFIBuyOffer() + it('should submit dfc htlc for a dfc buy offer with input utxos', async () => { + // icxSetup.setupUntilDFIBuyOffer() will setup until ICX offer for a DFI sell order + const { order, makeOfferTxId } = await icxSetup.setupUntilDFIBuyOffer() const accountDFIBeforeDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // create DFCHTLC - maker @@ -248,7 +296,7 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { // input utxos const utxos = await container.call('listunspent', [1, 9999999, [accountDFI], true]) - const inputUTXOs: InputUTXO[] = utxos.map((utxo: InputUTXO) => { + const inputUTXOs: UTXO[] = utxos.map((utxo: UTXO) => { return { txid: utxo.txid, vout: utxo.vout @@ -259,19 +307,32 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { const accountDFIAfterDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // maker fee should be reduced from accountDFIBeforeDFCHTLC - expect(Number(accountDFIAfterDFCHTLC[idDFI])).toStrictEqual(Number(accountDFIBeforeDFCHTLC[idDFI]) - Number(0.01)) + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI] - 0.01) // List htlc const listHTLCOptions: ICXListHTLCOptions = { offerTx: makeOfferTxId } - const HTLCs: Record = await container.call('icx_listhtlcs', [listHTLCOptions]) + const HTLCs: Record = await client.call('icx_listhtlcs', [listHTLCOptions], 'bignumber') 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) + } + ) }) - it('Should return an error when submiting DFCHTLC with invalid HTLC.offerTx', async () => { - const makeOfferTxId = await setupUntilDFIBuyOffer() + it('should return an error when submiting dfc htlc with invalid HTLC.offerTx', async () => { + // icxSetup.setupUntilDFIBuyOffer() will setup until ICX offer for a DFI sell order + const { makeOfferTxId } = await icxSetup.setupUntilDFIBuyOffer() const accountDFIBeforeDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // create DFCHTLC with invalid offerTx "123" - maker @@ -288,18 +349,19 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { const accountDFIAfterDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // should be the same balance as accountDFIBeforeDFCHTLC - expect(Number(accountDFIAfterDFCHTLC[idDFI])).toStrictEqual(Number(accountDFIBeforeDFCHTLC[idDFI])) + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI]) // List htlc const listHTLCOptions: ICXListHTLCOptions = { offerTx: makeOfferTxId } - const HTLCs: Record = await container.call('icx_listhtlcs', [listHTLCOptions]) + const HTLCs: Record = await client.call('icx_listhtlcs', [listHTLCOptions], 'bignumber') expect(Object.keys(HTLCs).length).toBe(1) // extra entry for the warning text returned by the RPC atm. }) - it('Should return an error when submiting DFCHTLC with incorrect HTLC.amount than amount in the offer', async () => { - const makeOfferTxId = await setupUntilDFIBuyOffer() + it('should return an error when submiting dfc htlc with incorrect HTLC.amount than amount in the offer', async () => { + // icxSetup.setupUntilDFIBuyOffer() will setup until ICX offer for a DFI sell order + const { makeOfferTxId } = await icxSetup.setupUntilDFIBuyOffer() const accountDFIBeforeDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // create DFCHTLC with invalid offerTx "123" - maker @@ -316,13 +378,13 @@ describe('Test ICXOrderBook.submitDFCHTLC', () => { const accountDFIAfterDFCHTLC = await container.call('getaccount', [accountDFI, {}, true]) // should be the same balance as accountDFIBeforeDFCHTLC - expect(Number(accountDFIAfterDFCHTLC[idDFI])).toStrictEqual(Number(accountDFIBeforeDFCHTLC[idDFI])) + expect(accountDFIAfterDFCHTLC[idDFI]).toStrictEqual(accountDFIBeforeDFCHTLC[idDFI]) // List htlc const listHTLCOptions: ICXListHTLCOptions = { offerTx: makeOfferTxId } - const HTLCs: Record = await container.call('icx_listhtlcs', [listHTLCOptions]) + const HTLCs: Record = await client.call('icx_listhtlcs', [listHTLCOptions], 'bignumber') expect(Object.keys(HTLCs).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 ffeacd1747..a597508063 100644 --- a/packages/jellyfish-api-core/src/category/icxorderbook.ts +++ b/packages/jellyfish-api-core/src/category/icxorderbook.ts @@ -89,12 +89,12 @@ export class ICXOrderBook { * @param {BigNumber} [htlc.amount] Amount in HTLC * @param {string} [htlc.hash] Hash of seed used for the hash lock part * @param {number} [htlc.timeout] Timeout (absolute in blocks) for expiration of HTLC in DFI blocks - * @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 submitDFCHTLC (htlc: HTLC, inputUTXOs: InputUTXO[] = []): Promise { + async submitDFCHTLC (htlc: HTLC, inputUTXOs: UTXO[] = []): Promise { return await this.client.call( 'icx_submitdfchtlc', [ @@ -338,7 +338,7 @@ export interface ICXClaimDFCHTLCInfo { /** HTLC claim secret */ seed: string /** HTLC creation height */ - height: number + height: BigNumber } /** ICX DFCHTLC info */ @@ -356,11 +356,11 @@ export interface ICXDFCHTLCInfo { /** Hash of DFCHTLC */ hash: string /** Timeout in blocks */ - timeout: number + timeout: BigNumber /** HTLC creation height */ - height: number + height: BigNumber /** HTLC refund height */ - refundHeight: number + refundHeight: BigNumber } /** ICX EXTHTLC info */ @@ -382,7 +382,7 @@ export interface ICXEXTHTLCInfo { /** Pubkey of the owner to which the funds are refunded if HTLC timeouts */ ownerPubkey: string /** Timeout in blocks */ - timeout: number + timeout: BigNumber /** HTLC creation height */ - height: number + height: BigNumber } diff --git a/website/docs/jellyfish/api/icxorderbook.md b/website/docs/jellyfish/api/icxorderbook.md index 51f57ceb33..994778c484 100644 --- a/website/docs/jellyfish/api/icxorderbook.md +++ b/website/docs/jellyfish/api/icxorderbook.md @@ -99,7 +99,7 @@ Create and submits a DFC HTLC transaction ```ts title="client.icxorderbook.submitDFCHTLC()" interface icxorderbook { - submitDFCHTLC (htlc: HTLC, inputUTXOs: InputUTXO[] = []): Promise + submitDFCHTLC (htlc: HTLC, inputUTXOs: UTXO[] = []): Promise } interface HTLC { @@ -109,7 +109,7 @@ interface HTLC { timeout?: number } -interface InputUTXO { +interface UTXO { txid: string vout: number }