fix: #656 modify order validation to trade when suspended

This commit is contained in:
madalinaraicu 2022-07-14 12:58:55 +03:00
parent febd998ec9
commit f87735a45c
2 changed files with 123 additions and 27 deletions

View File

@ -43,22 +43,22 @@ export const DealTicket = ({
const step = toDecimal(market.positionDecimalPlaces); const step = toDecimal(market.positionDecimalPlaces);
const orderType = watch('type'); const orderType = watch('type');
const orderTimeInForce = watch('timeInForce'); const orderTimeInForce = watch('timeInForce');
const invalidText = useOrderValidation({ const { message, isDisabled: disabled } = useOrderValidation({
step, step,
market, market,
orderType, orderType,
orderTimeInForce, orderTimeInForce,
fieldErrors: errors, fieldErrors: errors,
}); });
const isDisabled = transactionStatus === 'pending' || Boolean(invalidText); const isDisabled = transactionStatus === 'pending' || disabled;
const onSubmit = useCallback( const onSubmit = useCallback(
(order: Order) => { (order: Order) => {
if (!isDisabled && !invalidText) { if (!isDisabled) {
submit(order); submit(order);
} }
}, },
[isDisabled, invalidText, submit] [isDisabled, submit]
); );
return ( return (
@ -118,9 +118,12 @@ export const DealTicket = ({
> >
{transactionStatus === 'pending' ? t('Pending...') : t('Place order')} {transactionStatus === 'pending' ? t('Pending...') : t('Place order')}
</Button> </Button>
{invalidText && ( {message && (
<InputError className="mb-8" data-testid="dealticket-error-message"> <InputError
{invalidText} className="mt-12 mb-12"
data-testid="dealticket-error-message"
>
{message}
</InputError> </InputError>
)} )}
</form> </form>

View File

@ -28,33 +28,105 @@ export const useOrderValidation = ({
}: ValidationProps) => { }: ValidationProps) => {
const { keypair } = useVegaWallet(); const { keypair } = useVegaWallet();
const invalidText = useMemo(() => { const { message, isDisabled } = useMemo(() => {
if (!keypair) { if (!keypair) {
return t('No public key selected'); return { message: t('No public key selected'), isDisabled: true };
} }
if (keypair.tainted) { if (keypair.tainted) {
return t('Selected public key has been tainted'); return {
isDisabled: true,
message: t('Selected public key has been tainted'),
};
}
if ([MarketState.Cancelled, MarketState.Rejected].includes(market.state)) {
return {
isDisabled: true,
message: t(
`This market is ${market.state.toLowerCase()} and not accepting orders`
),
};
}
if (
[
MarketState.Cancelled,
MarketState.Closed,
MarketState.Proposed,
MarketState.Settled,
].includes(market.state)
) {
return {
isDisabled: true,
message: t(
`This market is ${market.state.toLowerCase()} and only accepting liquidity orders`
),
};
} }
if (market.state !== MarketState.Active) { if (market.state !== MarketState.Active) {
if (market.state === MarketState.Suspended) { if (market.state === MarketState.Suspended) {
return t('Market is currently suspended'); if (market.tradingMode === MarketTradingMode.Continuous) {
if (orderType !== OrderType.Limit) {
return {
isDisabled: true,
message: t(
'Only limit orders are permitted when market is in auction'
),
};
}
if (
[
OrderTimeInForce.FOK,
OrderTimeInForce.IOC,
OrderTimeInForce.GFN,
].includes(orderTimeInForce)
) {
return {
isDisabled: true,
message: t(
'Only GTT, GTC and GFA are permitted when market is in auction'
),
};
}
}
return {
isDisabled: false,
message: t(
'This market is currently suspended and only accepting liquidity orders'
),
};
} }
if ( if (
market.state === MarketState.Proposed || market.state === MarketState.Proposed ||
market.state === MarketState.Pending market.state === MarketState.Pending
) { ) {
return t('Market is not active yet'); return {
isDisabled: false,
message: t(
'This market is not active yet and can accept only liquidity orders'
),
};
} }
return t('Market is no longer active'); return {
isDisabled: true,
message: t('This market is no longer active.'),
};
} }
if (market.tradingMode !== MarketTradingMode.Continuous) { if (market.tradingMode !== MarketTradingMode.Continuous) {
if (orderType !== OrderType.Limit) { if (orderType !== OrderType.Limit) {
return t('Only limit orders are permitted when market is in auction'); return {
isDisabled: true,
message: t(
'Only limit orders are permitted when market is in auction'
),
};
} }
if ( if (
@ -64,26 +136,41 @@ export const useOrderValidation = ({
OrderTimeInForce.GFN, OrderTimeInForce.GFN,
].includes(orderTimeInForce) ].includes(orderTimeInForce)
) { ) {
return t( return {
'Only GTT, GTC and GFA are permitted when market is in auction' isDisabled: true,
); message: t(
'Only GTT, GTC and GFA are permitted when market is in auction'
),
};
} }
} }
if (fieldErrors?.size?.type === 'required') { if (fieldErrors?.size?.type === 'required') {
return t('An amount needs to be provided'); return {
isDisabled: true,
message: t('An amount needs to be provided'),
};
} }
if (fieldErrors?.size?.type === 'min') { if (fieldErrors?.size?.type === 'min') {
return t(`The amount cannot be lower than "${step}"`); return {
isDisabled: true,
message: t(`The amount cannot be lower than "${step}"`),
};
} }
if (fieldErrors?.price?.type === 'required') { if (fieldErrors?.price?.type === 'required') {
return t('A price needs to be provided'); return {
isDisabled: true,
message: t('A price needs to be provided'),
};
} }
if (fieldErrors?.price?.type === 'min') { if (fieldErrors?.price?.type === 'min') {
return t(`The price cannot be negative`); return {
isDisabled: true,
message: t(`The price cannot be negative`),
};
} }
if ( if (
@ -91,14 +178,20 @@ export const useOrderValidation = ({
fieldErrors?.size?.message === ERROR_SIZE_DECIMAL fieldErrors?.size?.message === ERROR_SIZE_DECIMAL
) { ) {
if (market.positionDecimalPlaces === 0) { if (market.positionDecimalPlaces === 0) {
return t('No decimal amounts allowed for this order'); return {
isDisabled: true,
message: t('No decimal amounts allowed for this order'),
};
} }
return t( return {
`The amount field only takes up to ${market.positionDecimalPlaces} decimals` isDisabled: true,
); message: t(
`The amount field only takes up to ${market.positionDecimalPlaces} decimals`
),
};
} }
return ''; return { isDisabled: false, message: '' };
}, [ }, [
keypair, keypair,
step, step,
@ -110,5 +203,5 @@ export const useOrderValidation = ({
orderTimeInForce, orderTimeInForce,
]); ]);
return invalidText; return { message, isDisabled };
}; };