From 3dc77b0eff34122cc549e39e9b1a0ec4dd737818 Mon Sep 17 00:00:00 2001 From: "m.ray" <16125548+MadalinaRaicu@users.noreply.github.com> Date: Fri, 1 Dec 2023 18:38:32 +0200 Subject: [PATCH] chore(trading): add close position button back to console (#5407) --- apps/trading/.env.mainnet | 1 + libs/environment/src/hooks/use-environment.ts | 8 +- libs/environment/src/types.ts | 1 + .../src/utils/validate-environment.ts | 1 + .../lib/components/order-list/order-list.tsx | 4 +- .../src/lib/positions-manager.spec.tsx | 8 +- libs/positions/src/lib/positions-manager.tsx | 73 +++++++++---------- libs/utils/src/lib/constants.ts | 4 + .../src/lib/use-vega-transaction-toasts.tsx | 4 +- 9 files changed, 55 insertions(+), 49 deletions(-) diff --git a/apps/trading/.env.mainnet b/apps/trading/.env.mainnet index 177747d36..5bdd8db66 100644 --- a/apps/trading/.env.mainnet +++ b/apps/trading/.env.mainnet @@ -28,3 +28,4 @@ NX_REFERRALS=false NX_TENDERMINT_URL=https://be.vega.community NX_TENDERMINT_WEBSOCKET_URL=wss://be.vega.community/websocket +NX_DISABLE_CLOSE_POSITION=true diff --git a/libs/environment/src/hooks/use-environment.ts b/libs/environment/src/hooks/use-environment.ts index 85520ad54..7bed18ea9 100644 --- a/libs/environment/src/hooks/use-environment.ts +++ b/libs/environment/src/hooks/use-environment.ts @@ -150,7 +150,7 @@ export const useEnvironment = create()((set, get) => ({ * Initialize Vega app to dynamically select a node from the * VEGA_CONFIG_URL * - * This can be ommitted if you intend to only use a single node, + * This can be omitted if you intend to only use a single node, * in those cases be sure to set NX_VEGA_URL */ export const useInitializeEnv = () => { @@ -415,6 +415,12 @@ function compileFeatureFlags(): FeatureFlags { REFERRALS: TRUTHY.includes( windowOrDefault('NX_REFERRALS', process.env['NX_REFERRALS']) as string ), + DISABLE_CLOSE_POSITION: TRUTHY.includes( + windowOrDefault( + 'NX_DISABLE_CLOSE_POSITION', + process.env['NX_DISABLE_CLOSE_POSITION'] + ) as string + ), UPDATE_MARKET_STATE: TRUTHY.includes( windowOrDefault( 'NX_UPDATE_MARKET_STATE', diff --git a/libs/environment/src/types.ts b/libs/environment/src/types.ts index 296adfe22..b09d804eb 100644 --- a/libs/environment/src/types.ts +++ b/libs/environment/src/types.ts @@ -27,6 +27,7 @@ export type CosmicElevatorFlags = Pick< | 'UPDATE_MARKET_STATE' | 'GOVERNANCE_TRANSFERS' | 'VOLUME_DISCOUNTS' + | 'DISABLE_CLOSE_POSITION' >; export type Configuration = z.infer; export const CUSTOM_NODE_KEY = 'custom' as const; diff --git a/libs/environment/src/utils/validate-environment.ts b/libs/environment/src/utils/validate-environment.ts index 912a225a5..160a65ef7 100644 --- a/libs/environment/src/utils/validate-environment.ts +++ b/libs/environment/src/utils/validate-environment.ts @@ -81,6 +81,7 @@ const COSMIC_ELEVATOR_FLAGS = { UPDATE_MARKET_STATE: z.optional(z.boolean()), GOVERNANCE_TRANSFERS: z.optional(z.boolean()), VOLUME_DISCOUNTS: z.optional(z.boolean()), + DISABLE_CLOSE_POSITION: z.optional(z.boolean()), }; const EXPLORER_FLAGS = { diff --git a/libs/orders/src/lib/components/order-list/order-list.tsx b/libs/orders/src/lib/components/order-list/order-list.tsx index ffe893d74..1a1cbbf43 100644 --- a/libs/orders/src/lib/components/order-list/order-list.tsx +++ b/libs/orders/src/lib/components/order-list/order-list.tsx @@ -1,6 +1,6 @@ import { memo, forwardRef, useMemo, type ForwardedRef } from 'react'; import { - MAXGOINT64, + HALFMAXGOINT64, addDecimalsFormatNumber, getDateTimeFormat, isNumeric, @@ -151,7 +151,7 @@ export const OrderListTable = memo< : ''; if ( - data.size === MAXGOINT64 && + data.size >= HALFMAXGOINT64 && data.timeInForce === Schema.OrderTimeInForce.TIME_IN_FORCE_IOC && data.reduceOnly diff --git a/libs/positions/src/lib/positions-manager.spec.tsx b/libs/positions/src/lib/positions-manager.spec.tsx index 0f8260e86..a320eba42 100644 --- a/libs/positions/src/lib/positions-manager.spec.tsx +++ b/libs/positions/src/lib/positions-manager.spec.tsx @@ -3,7 +3,7 @@ import { PositionsManager } from './positions-manager'; import { positionsMarketsProvider } from './positions-data-providers'; import { singleRow } from './positions.mock'; import { MockedProvider } from '@apollo/client/testing'; -import { MAXGOINT64 } from '@vegaprotocol/utils'; +import { HALFMAXGOINT64 } from '@vegaprotocol/utils'; const mockCreate = jest.fn(); @@ -31,9 +31,7 @@ jest.mock('@vegaprotocol/data-provider', () => ({ })); describe('PositionsManager', () => { - // TODO: Close position temporarily disabled in https://github.com/vegaprotocol/frontend-monorepo/pull/5350 - // eslint-disable-next-line jest/no-disabled-tests - it.skip('should close position with max uint64', async () => { + it('should close position with half of max uint64', async () => { render(, { wrapper: MockedProvider, }); @@ -43,6 +41,6 @@ describe('PositionsManager', () => { expect( mockCreate.mock.lastCall[0].batchMarketInstructions.submissions[0].size - ).toEqual(MAXGOINT64); + ).toEqual(HALFMAXGOINT64); }); }); diff --git a/libs/positions/src/lib/positions-manager.tsx b/libs/positions/src/lib/positions-manager.tsx index 033776999..7e20d19b9 100644 --- a/libs/positions/src/lib/positions-manager.tsx +++ b/libs/positions/src/lib/positions-manager.tsx @@ -7,13 +7,11 @@ import { } from './positions-data-providers'; import type { useDataGridEvents } from '@vegaprotocol/datagrid'; import { useT } from '../use-t'; - -// TODO: Close position temporarily disabled in https://github.com/vegaprotocol/frontend-monorepo/pull/5350 -// -// import { useCallback } from 'react'; -// import * as Schema from '@vegaprotocol/types'; -// import { useVegaTransactionStore } from '@vegaprotocol/web3'; -// import { MAXGOINT64 } from '@vegaprotocol/utils'; +import { useCallback } from 'react'; +import * as Schema from '@vegaprotocol/types'; +import { useVegaTransactionStore } from '@vegaprotocol/web3'; +import { HALFMAXGOINT64 } from '@vegaprotocol/utils'; +import { FLAGS } from '@vegaprotocol/environment'; interface PositionsManagerProps { partyIds: string[]; @@ -32,37 +30,35 @@ export const PositionsManager = ({ }: PositionsManagerProps) => { const t = useT(); const { pubKeys, pubKey } = useVegaWallet(); + const create = useVegaTransactionStore((store) => store.create); + const disableClosePositionsButton = FLAGS.DISABLE_CLOSE_POSITION; - // TODO: Close position temporarily disabled in https://github.com/vegaprotocol/frontend-monorepo/pull/5350 - // - // const create = useVegaTransactionStore((store) => store.create); - // - // const onClose = useCallback( - // ({ marketId, openVolume }: { marketId: string; openVolume: string }) => - // create({ - // batchMarketInstructions: { - // cancellations: [ - // { - // marketId, - // orderId: '', // omit order id to cancel all active orders - // }, - // ], - // submissions: [ - // { - // marketId: marketId, - // type: Schema.OrderType.TYPE_MARKET as const, - // timeInForce: Schema.OrderTimeInForce.TIME_IN_FORCE_IOC as const, - // side: openVolume.startsWith('-') - // ? Schema.Side.SIDE_BUY - // : Schema.Side.SIDE_SELL, - // size: MAXGOINT64, // improvement for avoiding leftovers filled in the meantime when close request has been sent - // reduceOnly: true, - // }, - // ], - // }, - // }), - // [create] - // ); + const onClose = useCallback( + ({ marketId, openVolume }: { marketId: string; openVolume: string }) => + create({ + batchMarketInstructions: { + cancellations: [ + { + marketId, + orderId: '', // omit order id to cancel all active orders + }, + ], + submissions: [ + { + marketId: marketId, + type: Schema.OrderType.TYPE_MARKET as const, + timeInForce: Schema.OrderTimeInForce.TIME_IN_FORCE_IOC as const, + side: openVolume.startsWith('-') + ? Schema.Side.SIDE_BUY + : Schema.Side.SIDE_SELL, + size: HALFMAXGOINT64, // improvement for avoiding leftovers filled in the meantime when close request has been sent + reduceOnly: true, + }, + ], + }, + }), + [create] + ); const { data: marketIds } = useDataProvider({ dataProvider: positionsMarketsProvider, @@ -81,8 +77,7 @@ export const PositionsManager = ({ pubKeys={pubKeys} rowData={data} onMarketClick={onMarketClick} - // TODO: temporarily disable close position - // onClose={onClose} + onClose={disableClosePositionsButton ? undefined : onClose} isReadOnly={isReadOnly} multipleKeys={partyIds.length > 1} overlayNoRowsTemplate={error ? error.message : t('No positions')} diff --git a/libs/utils/src/lib/constants.ts b/libs/utils/src/lib/constants.ts index 718b3e46f..91df7088e 100644 --- a/libs/utils/src/lib/constants.ts +++ b/libs/utils/src/lib/constants.ts @@ -1 +1,5 @@ export const MAXGOINT64 = '9223372036854775807'; +// The fix for the close position functionality needs MaxInt64/2 for the size +// Issue: https://github.com/vegaprotocol/vega/issues/10177 +// Core PR: https://github.com/vegaprotocol/vega/pull/10178 +export const HALFMAXGOINT64 = '4611686018427387903'; diff --git a/libs/web3/src/lib/use-vega-transaction-toasts.tsx b/libs/web3/src/lib/use-vega-transaction-toasts.tsx index 168a351c6..ef73de2e4 100644 --- a/libs/web3/src/lib/use-vega-transaction-toasts.tsx +++ b/libs/web3/src/lib/use-vega-transaction-toasts.tsx @@ -41,7 +41,7 @@ import { toBigNum, truncateByChars, useFormatTrigger, - MAXGOINT64, + HALFMAXGOINT64, } from '@vegaprotocol/utils'; import { useAssetsMapProvider } from '@vegaprotocol/assets'; import { useEthWithdrawApprovalsStore } from './use-ethereum-withdraw-approvals-store'; @@ -1014,7 +1014,7 @@ export const getVegaTransactionContentIntent = (tx: VegaStoredTxState) => { tx.order && tx.order.status === Schema.OrderStatus.STATUS_STOPPED && tx.order.timeInForce === Schema.OrderTimeInForce.TIME_IN_FORCE_IOC && - tx.order.size === MAXGOINT64 && + tx.order.size >= HALFMAXGOINT64 && // isClosePositionTransaction(tx) && Intent.Success;