feat(positions): close position with max possible size (#4940)

This commit is contained in:
Maciek 2023-10-02 16:38:22 +02:00 committed by GitHub
parent 13817e4d57
commit 0872a14f44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 30 deletions

View File

@ -0,0 +1,39 @@
import { render, screen, fireEvent } from '@testing-library/react';
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';
const mockCreate = jest.fn();
jest.mock('@vegaprotocol/wallet', () => ({
...jest.requireActual('@vegaprotocol/wallet'),
useVegaWallet: jest.fn(() => ({ pubKey: 'partyId' })),
useVegaTransactionStore: jest.fn(() => mockCreate),
}));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockUseDataProvider = (args: any) => {
if (args.dataProvider === positionsMarketsProvider) {
return { data: ['market-1', 'market-2'] };
}
return { data: [singleRow] };
};
jest.mock('@vegaprotocol/data-provider', () => ({
...jest.requireActual('@vegaprotocol/data-provider'),
useDataProvider: jest.fn((args) => mockUseDataProvider(args)),
}));
describe('PositionsManager', () => {
it('should close position with max uint64', async () => {
render(<PositionsManager partyIds={['partyId']} isReadOnly={false} />, {
wrapper: MockedProvider,
});
expect(await screen.getByTestId('close-position')).toBeInTheDocument();
fireEvent.click(screen.getByTestId('close-position'));
expect(
mockCreate.mock.lastCall[0].batchMarketInstructions.submissions[0].size
).toEqual(MAXGOINT64);
});
});

View File

@ -1,7 +1,7 @@
import { useCallback } from 'react'; import { useCallback } from 'react';
import { PositionsTable } from './positions-table'; import { PositionsTable } from './positions-table';
import * as Schema from '@vegaprotocol/types'; import * as Schema from '@vegaprotocol/types';
import { useVegaTransactionStore } from '@vegaprotocol/wallet'; import { useVegaTransactionStore, useVegaWallet } from '@vegaprotocol/wallet';
import { t } from '@vegaprotocol/i18n'; import { t } from '@vegaprotocol/i18n';
import { useDataProvider } from '@vegaprotocol/data-provider'; import { useDataProvider } from '@vegaprotocol/data-provider';
import { import {
@ -9,7 +9,7 @@ import {
positionsMarketsProvider, positionsMarketsProvider,
} from './positions-data-providers'; } from './positions-data-providers';
import type { useDataGridEvents } from '@vegaprotocol/datagrid'; import type { useDataGridEvents } from '@vegaprotocol/datagrid';
import { useVegaWallet } from '@vegaprotocol/wallet'; import { MAXGOINT64 } from '@vegaprotocol/utils';
interface PositionsManagerProps { interface PositionsManagerProps {
partyIds: string[]; partyIds: string[];
@ -46,7 +46,7 @@ export const PositionsManager = ({
side: openVolume.startsWith('-') side: openVolume.startsWith('-')
? Schema.Side.SIDE_BUY ? Schema.Side.SIDE_BUY
: Schema.Side.SIDE_SELL, : Schema.Side.SIDE_SELL,
size: openVolume.replace('-', ''), size: MAXGOINT64, // improvement for avoiding leftovers filled in the meantime when close request has been sent
reduceOnly: true, reduceOnly: true,
}, },
], ],

View File

@ -6,6 +6,7 @@ import * as Schema from '@vegaprotocol/types';
import { PositionStatus } from '@vegaprotocol/types'; import { PositionStatus } from '@vegaprotocol/types';
import type { ICellRendererParams } from 'ag-grid-community'; import type { ICellRendererParams } from 'ag-grid-community';
import { addDecimalsFormatNumber } from '@vegaprotocol/utils'; import { addDecimalsFormatNumber } from '@vegaprotocol/utils';
import { singleRow } from './positions.mock';
jest.mock('./liquidation-price', () => ({ jest.mock('./liquidation-price', () => ({
LiquidationPrice: () => ( LiquidationPrice: () => (
@ -13,33 +14,6 @@ jest.mock('./liquidation-price', () => ({
), ),
})); }));
const singleRow: Position = {
partyId: 'partyId',
assetId: 'asset-id',
assetSymbol: 'BTC',
averageEntryPrice: '133',
currentLeverage: 1.1,
assetDecimals: 2, // this is settlementAsset.decimals
quantum: '0.1',
lossSocializationAmount: '0',
marginAccountBalance: '12345600',
marketDecimalPlaces: 1,
marketId: 'string',
marketCode: 'ETHBTC.QM21',
marketTradingMode: Schema.MarketTradingMode.TRADING_MODE_CONTINUOUS,
marketState: Schema.MarketState.STATE_ACTIVE,
markPrice: '123',
notional: '12300',
openVolume: '100',
positionDecimalPlaces: 0,
realisedPNL: '123',
status: PositionStatus.POSITION_STATUS_UNSPECIFIED,
totalBalance: '123456',
unrealisedPNL: '456',
updatedAt: '2022-07-27T15:02:58.400Z',
productType: 'Future',
};
describe('Positions', () => { describe('Positions', () => {
const renderComponent = async (rowData: Position) => { const renderComponent = async (rowData: Position) => {
await act(async () => { await act(async () => {

View File

@ -10,6 +10,8 @@ import type {
MarginsQuery, MarginsQuery,
MarginFieldsFragment, MarginFieldsFragment,
} from '@vegaprotocol/accounts'; } from '@vegaprotocol/accounts';
import type { Position } from './positions-data-providers';
import * as Schema from '@vegaprotocol/types';
export const positionsQuery = ( export const positionsQuery = (
override?: PartialDeep<PositionsQuery> override?: PartialDeep<PositionsQuery>
@ -168,3 +170,30 @@ const marginsFields: MarginFieldsFragment[] = [
}, },
}, },
]; ];
export const singleRow: Position = {
partyId: 'partyId',
assetId: 'asset-id',
assetSymbol: 'BTC',
averageEntryPrice: '133',
currentLeverage: 1.1,
assetDecimals: 2, // this is settlementAsset.decimals
quantum: '0.1',
lossSocializationAmount: '0',
marginAccountBalance: '12345600',
marketDecimalPlaces: 1,
marketId: 'string',
marketCode: 'ETHBTC.QM21',
marketTradingMode: Schema.MarketTradingMode.TRADING_MODE_CONTINUOUS,
marketState: Schema.MarketState.STATE_ACTIVE,
markPrice: '123',
notional: '12300',
openVolume: '100',
positionDecimalPlaces: 0,
realisedPNL: '123',
status: PositionStatus.POSITION_STATUS_UNSPECIFIED,
totalBalance: '123456',
unrealisedPNL: '456',
updatedAt: '2022-07-27T15:02:58.400Z',
productType: 'Future',
};

View File

@ -15,3 +15,4 @@ export * from './lib/time';
export * from './lib/validate'; export * from './lib/validate';
export * from './lib/resolve-network-name'; export * from './lib/resolve-network-name';
export * from './lib/is-test-env'; export * from './lib/is-test-env';
export * from './lib/constants';

View File

@ -0,0 +1 @@
export const MAXGOINT64 = '9223372036854775807';