From d8512afb2b03762c4a24e28870d6a5006886545b Mon Sep 17 00:00:00 2001 From: "m.ray" <16125548+MadalinaRaicu@users.noreply.github.com> Date: Mon, 12 Sep 2022 14:28:07 +0100 Subject: [PATCH] fix: #1297 Order submission expiresAt is only available when the time in force is of type GTT (#1308) * fix: #1297 Order submission expires at is only available when the time in force is of type GTT * fix: add expires at visible in edit dialog * fix:#1297 add GTC and GTT tests for expires at --- .../components/deal-ticket/deal-ticket.tsx | 4 ++ .../order-list/order-edit-dialog.tsx | 9 ++++ .../lib/order-hooks/use-order-submit.spec.tsx | 42 ++++++++++++++++++- .../src/lib/order-hooks/use-order-submit.tsx | 11 +++-- 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/libs/deal-ticket/src/components/deal-ticket/deal-ticket.tsx b/libs/deal-ticket/src/components/deal-ticket/deal-ticket.tsx index e0cb6ab4c..c1ed43607 100644 --- a/libs/deal-ticket/src/components/deal-ticket/deal-ticket.tsx +++ b/libs/deal-ticket/src/components/deal-ticket/deal-ticket.tsx @@ -60,6 +60,10 @@ export const DealTicket = ({ price: order.price && removeDecimal(order.price, market.decimalPlaces), size: removeDecimal(order.size, market.positionDecimalPlaces), + expiresAt: + order.timeInForce === OrderTimeInForce.TIME_IN_FORCE_GTT + ? order.expiresAt + : undefined, }); } }, diff --git a/libs/orders/src/lib/components/order-list/order-edit-dialog.tsx b/libs/orders/src/lib/components/order-list/order-edit-dialog.tsx index 16eb1f998..0fb03e705 100644 --- a/libs/orders/src/lib/components/order-list/order-edit-dialog.tsx +++ b/libs/orders/src/lib/components/order-list/order-edit-dialog.tsx @@ -3,6 +3,7 @@ import { addDecimalsFormatNumber, toDecimal, Size, + getDateTimeFormat, } from '@vegaprotocol/react-helpers'; import { OrderType } from '@vegaprotocol/types'; import { @@ -13,6 +14,7 @@ import { Dialog, Icon, } from '@vegaprotocol/ui-toolkit'; +import { OrderTimeInForce } from '@vegaprotocol/types'; import { useForm } from 'react-hook-form'; import type { OrderFields } from '../order-data-provider'; @@ -77,6 +79,13 @@ export const OrderEditDialog = ({

+ {order.timeInForce === OrderTimeInForce.TIME_IN_FORCE_GTT && + order.expiresAt && ( +
+

{t(`Expires at`)}

+

{getDateTimeFormat().format(new Date(order.expiresAt))}

+
+ )}
diff --git a/libs/orders/src/lib/order-hooks/use-order-submit.spec.tsx b/libs/orders/src/lib/order-hooks/use-order-submit.spec.tsx index 1410a7a6f..04fb24207 100644 --- a/libs/orders/src/lib/order-hooks/use-order-submit.spec.tsx +++ b/libs/orders/src/lib/order-hooks/use-order-submit.spec.tsx @@ -142,7 +142,7 @@ function setup(context?: Partial) { } describe('useOrderSubmit', () => { - it('should submit a correctly formatted order', async () => { + it('should submit a correctly formatted order on GTT', async () => { const mockSendTx = jest.fn().mockReturnValue(Promise.resolve({})); const keypair = { pub: '0x123', @@ -175,7 +175,45 @@ describe('useOrderSubmit', () => { side: Side.SIDE_BUY, timeInForce: OrderTimeInForce.TIME_IN_FORCE_GTT, price: '123456789', - expiresAt: order.expiresAt ? toNanoSeconds(order.expiresAt) : undefined, + expiresAt: toNanoSeconds(order.expiresAt), + }, + }); + }); + + it('should submit a correctly formatted order on GTC', async () => { + const mockSendTx = jest.fn().mockReturnValue(Promise.resolve({})); + const keypair = { + pub: '0x123', + } as VegaKeyExtended; + const { result } = setup({ + sendTx: mockSendTx, + keypairs: [keypair], + keypair, + }); + + const order = { + type: OrderType.TYPE_LIMIT, + size: '10', + timeInForce: OrderTimeInForce.TIME_IN_FORCE_GTC, + side: Side.SIDE_BUY, + price: '123456789', + expiresAt: new Date('2022-01-01'), + }; + await act(async () => { + result.current.submit({ ...order, marketId: defaultMarket.id }); + }); + + expect(mockSendTx).toHaveBeenCalledWith({ + pubKey: keypair.pub, + propagate: true, + orderSubmission: { + type: OrderType.TYPE_LIMIT, + marketId: defaultMarket.id, + size: '10', + side: Side.SIDE_BUY, + timeInForce: OrderTimeInForce.TIME_IN_FORCE_GTC, + price: '123456789', + expiresAt: undefined, }, }); }); diff --git a/libs/orders/src/lib/order-hooks/use-order-submit.tsx b/libs/orders/src/lib/order-hooks/use-order-submit.tsx index b36f0beee..47507131c 100644 --- a/libs/orders/src/lib/order-hooks/use-order-submit.tsx +++ b/libs/orders/src/lib/order-hooks/use-order-submit.tsx @@ -6,7 +6,8 @@ import { determineId, toNanoSeconds } from '@vegaprotocol/react-helpers'; import { useVegaTransaction } from '@vegaprotocol/wallet'; import * as Sentry from '@sentry/react'; import { useOrderEvent } from './use-order-event'; -import type { OrderTimeInForce, Side } from '@vegaprotocol/types'; +import type { Side } from '@vegaprotocol/types'; +import { OrderTimeInForce } from '@vegaprotocol/types'; import { OrderType, OrderStatus } from '@vegaprotocol/types'; import { Icon, Intent } from '@vegaprotocol/ui-toolkit'; import { t } from '@vegaprotocol/react-helpers'; @@ -131,9 +132,11 @@ export const useOrderSubmit = () => { order.type === OrderType.TYPE_LIMIT && order.price ? order.price : undefined, - expiresAt: order.expiresAt - ? toNanoSeconds(order.expiresAt) // Wallet expects timestamp in nanoseconds - : undefined, + expiresAt: + order.expiresAt && + order.timeInForce === OrderTimeInForce.TIME_IN_FORCE_GTT + ? toNanoSeconds(order.expiresAt) // Wallet expects timestamp in nanoseconds + : undefined, }, });