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
This commit is contained in:
m.ray 2022-09-12 14:28:07 +01:00 committed by GitHub
parent 549e0e004d
commit d8512afb2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 6 deletions

View File

@ -60,6 +60,10 @@ export const DealTicket = ({
price: price:
order.price && removeDecimal(order.price, market.decimalPlaces), order.price && removeDecimal(order.price, market.decimalPlaces),
size: removeDecimal(order.size, market.positionDecimalPlaces), size: removeDecimal(order.size, market.positionDecimalPlaces),
expiresAt:
order.timeInForce === OrderTimeInForce.TIME_IN_FORCE_GTT
? order.expiresAt
: undefined,
}); });
} }
}, },

View File

@ -3,6 +3,7 @@ import {
addDecimalsFormatNumber, addDecimalsFormatNumber,
toDecimal, toDecimal,
Size, Size,
getDateTimeFormat,
} from '@vegaprotocol/react-helpers'; } from '@vegaprotocol/react-helpers';
import { OrderType } from '@vegaprotocol/types'; import { OrderType } from '@vegaprotocol/types';
import { import {
@ -13,6 +14,7 @@ import {
Dialog, Dialog,
Icon, Icon,
} from '@vegaprotocol/ui-toolkit'; } from '@vegaprotocol/ui-toolkit';
import { OrderTimeInForce } from '@vegaprotocol/types';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import type { OrderFields } from '../order-data-provider'; import type { OrderFields } from '../order-data-provider';
@ -77,6 +79,13 @@ export const OrderEditDialog = ({
</p> </p>
</div> </div>
</div> </div>
{order.timeInForce === OrderTimeInForce.TIME_IN_FORCE_GTT &&
order.expiresAt && (
<div>
<p className={headerClassName}>{t(`Expires at`)}</p>
<p>{getDateTimeFormat().format(new Date(order.expiresAt))}</p>
</div>
)}
<div className="grid grid-cols-1 md:grid-cols-2 gap-8 py-4"> <div className="grid grid-cols-1 md:grid-cols-2 gap-8 py-4">
<form onSubmit={handleSubmit(onSubmit)} data-testid="edit-order"> <form onSubmit={handleSubmit(onSubmit)} data-testid="edit-order">

View File

@ -142,7 +142,7 @@ function setup(context?: Partial<VegaWalletContextShape>) {
} }
describe('useOrderSubmit', () => { 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 mockSendTx = jest.fn().mockReturnValue(Promise.resolve({}));
const keypair = { const keypair = {
pub: '0x123', pub: '0x123',
@ -175,7 +175,45 @@ describe('useOrderSubmit', () => {
side: Side.SIDE_BUY, side: Side.SIDE_BUY,
timeInForce: OrderTimeInForce.TIME_IN_FORCE_GTT, timeInForce: OrderTimeInForce.TIME_IN_FORCE_GTT,
price: '123456789', 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,
}, },
}); });
}); });

View File

@ -6,7 +6,8 @@ import { determineId, toNanoSeconds } from '@vegaprotocol/react-helpers';
import { useVegaTransaction } from '@vegaprotocol/wallet'; import { useVegaTransaction } from '@vegaprotocol/wallet';
import * as Sentry from '@sentry/react'; import * as Sentry from '@sentry/react';
import { useOrderEvent } from './use-order-event'; 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 { OrderType, OrderStatus } from '@vegaprotocol/types';
import { Icon, Intent } from '@vegaprotocol/ui-toolkit'; import { Icon, Intent } from '@vegaprotocol/ui-toolkit';
import { t } from '@vegaprotocol/react-helpers'; import { t } from '@vegaprotocol/react-helpers';
@ -131,7 +132,9 @@ export const useOrderSubmit = () => {
order.type === OrderType.TYPE_LIMIT && order.price order.type === OrderType.TYPE_LIMIT && order.price
? order.price ? order.price
: undefined, : undefined,
expiresAt: order.expiresAt expiresAt:
order.expiresAt &&
order.timeInForce === OrderTimeInForce.TIME_IN_FORCE_GTT
? toNanoSeconds(order.expiresAt) // Wallet expects timestamp in nanoseconds ? toNanoSeconds(order.expiresAt) // Wallet expects timestamp in nanoseconds
: undefined, : undefined,
}, },