diff --git a/apps/console-lite/src/app/components/deal-ticket/use-order-validation.spec.tsx b/apps/console-lite/src/app/components/deal-ticket/use-order-validation.spec.tsx index 93810b4c2..5de1d0cd9 100644 --- a/apps/console-lite/src/app/components/deal-ticket/use-order-validation.spec.tsx +++ b/apps/console-lite/src/app/components/deal-ticket/use-order-validation.spec.tsx @@ -198,7 +198,11 @@ describe('useOrderValidation', () => { `( 'Returns an error message for market state when not accepting orders', ({ state }) => { - const { result } = setup({ market: { ...defaultOrder.market, state } }); + const market = { + ...defaultOrder.market, + data: { ...defaultOrder.market.data, marketState: state }, + }; + const { result } = setup({ market }); expect(result.current).toStrictEqual({ isDisabled: true, message: `This market is ${marketTranslations( @@ -220,14 +224,19 @@ describe('useOrderValidation', () => { balance: '0', margin: '100', balanceError: false, - // asset, }); - const { result } = setup({ - market: { - ...defaultOrder.market, - state, - tradingMode: Schema.MarketTradingMode.TRADING_MODE_BATCH_AUCTION, + + const market = { + ...defaultOrder.market, + data: { + ...defaultOrder.market.data, + marketState: state, + marketTradingMode: + Schema.MarketTradingMode.TRADING_MODE_BATCH_AUCTION, }, + }; + const { result } = setup({ + market, order: { ...order, type: Schema.OrderType.TYPE_LIMIT, @@ -252,8 +261,12 @@ describe('useOrderValidation', () => { `( `Returns an error message when trying to submit a non-limit order for a "$tradingMode" market`, ({ tradingMode, errorMessage }) => { + const market = { + ...defaultOrder.market, + data: { ...defaultOrder.market.data, marketTradingMode: tradingMode }, + }; const { result } = setup({ - market: { ...defaultOrder.market, tradingMode }, + market, order: { ...order, type: Schema.OrderType.TYPE_MARKET, @@ -278,8 +291,12 @@ describe('useOrderValidation', () => { `( `Returns an error message when submitting a limit order with a "$orderTimeInForce" value to a "$tradingMode" market`, ({ tradingMode, orderTimeInForce, errorMessage }) => { + const market = { + ...defaultOrder.market, + data: { ...defaultOrder.market.data, marketTradingMode: tradingMode }, + }; const { result } = setup({ - market: { ...defaultOrder.market, tradingMode }, + market, order: { ...order, type: Schema.OrderType.TYPE_LIMIT, @@ -382,14 +399,15 @@ describe('useOrderValidation', () => { `( 'Returns error when market state is pending and size is wrong', ({ state }) => { + const market = { + ...defaultOrder.market, + data: { ...defaultOrder.market.data, marketState: state }, + }; const { result } = setup({ fieldErrors: { size: { type: `validate`, message: DealTicket.ERROR_SIZE_DECIMAL }, }, - market: { - ...market, - state, - }, + market, }); expect(result.current).toStrictEqual({ isDisabled: true, diff --git a/apps/console-lite/src/app/components/deal-ticket/use-order-validation.tsx b/apps/console-lite/src/app/components/deal-ticket/use-order-validation.tsx index 31f559161..45bf064a4 100644 --- a/apps/console-lite/src/app/components/deal-ticket/use-order-validation.tsx +++ b/apps/console-lite/src/app/components/deal-ticket/use-order-validation.tsx @@ -169,13 +169,13 @@ export const useOrderValidation = ({ Schema.MarketState.STATE_TRADING_TERMINATED, Schema.MarketState.STATE_CANCELLED, Schema.MarketState.STATE_CLOSED, - ].includes(market.state) + ].includes(market.data.marketState) ) { return { isDisabled: true, message: t( `This market is ${marketTranslations( - market.state + market.data.marketState )} and not accepting orders` ), section: DEAL_TICKET_SECTION.SUMMARY, @@ -186,7 +186,7 @@ export const useOrderValidation = ({ [ Schema.MarketState.STATE_PROPOSED, Schema.MarketState.STATE_PENDING, - ].includes(market.state) + ].includes(market.data.marketState) ) { if (fieldErrorChecking) { return fieldErrorChecking; @@ -195,7 +195,7 @@ export const useOrderValidation = ({ isDisabled: false, message: t( `This market is ${marketTranslations( - market.state + market.data.marketState )} and only accepting liquidity commitment orders` ), section: DEAL_TICKET_SECTION.SUMMARY, @@ -205,9 +205,9 @@ export const useOrderValidation = ({ if (isMarketInAuction(market)) { if (order.type === Schema.OrderType.TYPE_MARKET) { if ( - market.tradingMode === + market.data.marketTradingMode === Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION && - market.data?.trigger === + market.data.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_LIQUIDITY ) { return { @@ -230,9 +230,9 @@ export const useOrderValidation = ({ }; } if ( - market.tradingMode === + market.data.marketTradingMode === Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION && - market.data?.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_PRICE + market.data.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_PRICE ) { return { isDisabled: true, @@ -270,9 +270,9 @@ export const useOrderValidation = ({ ].includes(order.timeInForce) ) { if ( - market.tradingMode === + market.data.marketTradingMode === Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION && - market.data?.trigger === + market.data.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_LIQUIDITY ) { return { @@ -297,9 +297,9 @@ export const useOrderValidation = ({ }; } if ( - market.tradingMode === + market.data.marketTradingMode === Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION && - market.data?.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_PRICE + market.data.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_PRICE ) { return { isDisabled: true, @@ -355,7 +355,7 @@ export const useOrderValidation = ({ Schema.MarketTradingMode.TRADING_MODE_BATCH_AUCTION, Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION, Schema.MarketTradingMode.TRADING_MODE_OPENING_AUCTION, - ].includes(market.tradingMode) + ].includes(market.data.marketTradingMode) ) { return { isDisabled: false, diff --git a/apps/trading-e2e/src/support/trading.ts b/apps/trading-e2e/src/support/trading.ts index 435a26d51..2866a3740 100644 --- a/apps/trading-e2e/src/support/trading.ts +++ b/apps/trading-e2e/src/support/trading.ts @@ -59,6 +59,7 @@ const mockTradingPage = ( generateMarketData({ trigger: trigger, marketTradingMode: tradingMode, + marketState: state, }) ); aliasQuery(req, 'MarketsData', generateMarketsData()); 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 22d7a2415..98051c73e 100644 --- a/libs/deal-ticket/src/components/deal-ticket/deal-ticket.tsx +++ b/libs/deal-ticket/src/components/deal-ticket/deal-ticket.tsx @@ -75,7 +75,7 @@ export const DealTicket = ({ return; } - const marketStateError = validateMarketState(market.state); + const marketStateError = validateMarketState(market.data.marketState); if (marketStateError !== true) { setError('summary', { message: marketStateError }); return; @@ -87,7 +87,7 @@ export const DealTicket = ({ } const marketTradingModeError = validateMarketTradingMode( - market.tradingMode + market.data.marketTradingMode ); if (marketTradingModeError !== true) { setError('summary', { message: marketTradingModeError }); @@ -110,8 +110,8 @@ export const DealTicket = ({ hasNoBalance, market.positionDecimalPlaces, market.decimalPlaces, - market.state, - market.tradingMode, + market.data.marketState, + market.data.marketTradingMode, setError, ] ); @@ -242,7 +242,7 @@ const SummaryMessage = memo( Schema.MarketTradingMode.TRADING_MODE_BATCH_AUCTION, Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION, Schema.MarketTradingMode.TRADING_MODE_OPENING_AUCTION, - ].includes(market.tradingMode) + ].includes(market.data.marketTradingMode) ) { return (
{ const grid: MarketDataGridProps['grid'] = []; const isLiquidityMonitoringAuction = - market.tradingMode === + market.data.marketTradingMode === Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION && - market.data?.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_LIQUIDITY; + market.data.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_LIQUIDITY; const formatStake = (value: string) => { const formattedValue = addDecimalsFormatNumber( @@ -32,14 +32,14 @@ export const compileGridData = ( if (!market.data) return grid; - if (market.data?.auctionStart) { + if (market.data.auctionStart) { grid.push({ label: t('Auction start'), value: getDateTimeFormat().format(new Date(market.data.auctionStart)), }); } - if (market.data?.auctionEnd) { + if (market.data.auctionEnd) { const endDate = getDateTimeFormat().format( new Date(market.data.auctionEnd) ); @@ -51,14 +51,14 @@ export const compileGridData = ( }); } - if (isLiquidityMonitoringAuction && market.data?.targetStake) { + if (isLiquidityMonitoringAuction && market.data.targetStake) { grid.push({ label: t('Target liquidity'), value: formatStake(market.data.targetStake), }); } - if (isLiquidityMonitoringAuction && market.data?.suppliedStake) { + if (isLiquidityMonitoringAuction && market.data.suppliedStake) { grid.push({ label: ( { // 0 can never be a valid uncrossing price // as it would require there being orders on the book at that price. if ( - market.data?.indicativePrice && + market.data.indicativePrice && market.data.indicativePrice !== '0' && - BigInt(market.data?.indicativePrice) !== BigInt(0) + BigInt(market.data.indicativePrice) !== BigInt(0) ) { return market.data.indicativePrice; } diff --git a/libs/deal-ticket/src/utils/is-market-in-auction.ts b/libs/deal-ticket/src/utils/is-market-in-auction.ts index 535d0a916..9c29c7c30 100644 --- a/libs/deal-ticket/src/utils/is-market-in-auction.ts +++ b/libs/deal-ticket/src/utils/is-market-in-auction.ts @@ -1,11 +1,13 @@ import { Schema } from '@vegaprotocol/types'; export const isMarketInAuction = (market: { - tradingMode: Schema.MarketTradingMode; + data: { + marketTradingMode: Schema.MarketTradingMode; + }; }) => { return [ Schema.MarketTradingMode.TRADING_MODE_BATCH_AUCTION, Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION, Schema.MarketTradingMode.TRADING_MODE_OPENING_AUCTION, - ].includes(market.tradingMode); + ].includes(market.data.marketTradingMode); }; diff --git a/libs/deal-ticket/src/utils/validate-time-in-force.ts b/libs/deal-ticket/src/utils/validate-time-in-force.ts index 4352930c2..2d89629ea 100644 --- a/libs/deal-ticket/src/utils/validate-time-in-force.ts +++ b/libs/deal-ticket/src/utils/validate-time-in-force.ts @@ -6,12 +6,12 @@ import type { MarketDealTicket } from '@vegaprotocol/market-list'; export const validateTimeInForce = (market: MarketDealTicket) => { return (value: Schema.OrderTimeInForce) => { const isMonitoringAuction = - market.tradingMode === + market.data.marketTradingMode === Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION; const isPriceTrigger = - market.data?.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_PRICE; + market.data.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_PRICE; const isLiquidityTrigger = - market.data?.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_LIQUIDITY; + market.data.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_LIQUIDITY; if (isMarketInAuction(market)) { if ( diff --git a/libs/deal-ticket/src/utils/validate-type.ts b/libs/deal-ticket/src/utils/validate-type.ts index 34f85d77c..e86602b85 100644 --- a/libs/deal-ticket/src/utils/validate-type.ts +++ b/libs/deal-ticket/src/utils/validate-type.ts @@ -7,13 +7,12 @@ export const validateType = (market: MarketDealTicket) => { return (value: Schema.OrderType) => { if (isMarketInAuction(market) && value === Schema.OrderType.TYPE_MARKET) { const isMonitoringAuction = - market.tradingMode === + market.data.marketTradingMode === Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION; const isPriceTrigger = - market.data?.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_PRICE; + market.data.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_PRICE; const isLiquidityTrigger = - market.data?.trigger === - Schema.AuctionTrigger.AUCTION_TRIGGER_LIQUIDITY; + market.data.trigger === Schema.AuctionTrigger.AUCTION_TRIGGER_LIQUIDITY; if (isMonitoringAuction && isPriceTrigger) { return MarketModeValidationType.PriceMonitoringAuction; diff --git a/libs/react-helpers/src/lib/generic-data-provider.ts b/libs/react-helpers/src/lib/generic-data-provider.ts index 37d34cf82..df5f23624 100644 --- a/libs/react-helpers/src/lib/generic-data-provider.ts +++ b/libs/react-helpers/src/lib/generic-data-provider.ts @@ -4,6 +4,7 @@ import type { FetchPolicy, OperationVariables, TypedDocumentNode, + FetchResult, } from '@apollo/client'; import type { Subscription } from 'zen-observable-ts'; import isEqual from 'lodash/isEqual'; @@ -362,6 +363,25 @@ function makeDataProviderInternal< } }; + const onNext = ({ + data: subscriptionData, + }: FetchResult) => { + if (!subscriptionData || !getDelta || !update) { + return; + } + const delta = getDelta(subscriptionData, variables); + if (loading || !data) { + updateQueue.push(delta); + } else { + const updatedData = update(data, delta, reload, variables); + if (updatedData === data) { + return; + } + data = updatedData; + notifyAll({ delta, isUpdate: true }); + } + }; + const initialize = async () => { if (subscription) { if (resetTimer) { @@ -375,6 +395,7 @@ function makeDataProviderInternal< if (!client) { return; } + if (subscriptionQuery && getDelta && update) { subscription = client .subscribe({ @@ -382,32 +403,14 @@ function makeDataProviderInternal< variables, fetchPolicy, }) - .subscribe( - ({ data: subscriptionData }) => { - if (!subscriptionData) { - return; - } - const delta = getDelta(subscriptionData, variables); - if (loading || !data) { - updateQueue.push(delta); - } else { - const updatedData = update(data, delta, reload, variables); - if (updatedData === data) { - return; - } - data = updatedData; - notifyAll({ delta, isUpdate: true }); - } - }, - (e) => { - error = e as Error; - if (subscription) { - subscription.unsubscribe(); - subscription = undefined; - } - notifyAll(); + .subscribe(onNext, (e) => { + error = e as Error; + if (subscription) { + subscription.unsubscribe(); + subscription = undefined; } - ); + notifyAll(); + }); } await initialFetch(); };