vega-frontend-monorepo/libs/deal-ticket/src/hooks/use-order-closeout.ts
Sam Keen 7c8a54b35a
feat(1263): split validator page into separate tables (#2250)
* frontend-monorepo-1263 validator status of pending now named 'candidate' in the ui

* feat(1263): validator table updates, calculating penalties

* feat(1263): bit of cleanup

* feat(1263): tables built and values derived

* feat(1263): tweaks

* feat(1263): upping site max width to 1500px

* feat(1263): tweak to normalised voting power column width

* feat(1263): ensure validator rows when hovered have cursor:pointer

* feat(1263): consensus validators now have a ranking field by voting power shown in the table

* feat(1263): fixed linting errors

* feat(1263): a couple of extra tests and correct import of schema to use enums

* Update apps/token/src/routes/staking/home/validator-tables/standby-pending-validators-table.tsx

Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>

* Update apps/token/src/routes/staking/home/validator-tables/validator-tables.tsx

Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>

* Update apps/token/src/routes/staking/home/validator-tables/validator-tables.tsx

Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>

* Update apps/token/src/routes/staking/home/validator-tables/validator-tables.tsx

Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>

* feat(1263): tweaks from PR comments, initial push of util to remove 'node' from arrs

* feat(1263): updated new util to remove 'node' from 'edges'. Implemented across the monorepo

* feat(1263): cleaned up splitting validators into consensus, pending and ersatz to put into tables

* feat(1263): used decimals from app state in validator-tables.tsx

* feat(1263): removed redundant compact import

Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>
2022-11-30 12:55:30 +00:00

94 lines
2.9 KiB
TypeScript

import { BigNumber } from 'bignumber.js';
import type { OrderSubmissionBody } from '@vegaprotocol/wallet';
import { useVegaWallet } from '@vegaprotocol/wallet';
import {
addDecimal,
formatNumber,
removePaginationWrapper,
} from '@vegaprotocol/react-helpers';
import { useMarketPositions } from './use-market-positions';
import { useMarketDataMarkPrice } from './use-market-data-mark-price';
import { usePartyMarketDataQuery } from './__generated__/PartyMarketData';
import { Schema } from '@vegaprotocol/types';
import type { PartyBalanceQuery } from './__generated__/PartyBalance';
import { useSettlementAccount } from './use-settlement-account';
import type { MarketDealTicket } from '@vegaprotocol/market-list';
interface Props {
order: OrderSubmissionBody['orderSubmission'];
market: MarketDealTicket;
partyData?: PartyBalanceQuery;
}
export const useOrderCloseOut = ({
order,
market,
partyData,
}: Props): string | null => {
const { pubKey } = useVegaWallet();
const accounts = removePaginationWrapper(
partyData?.party?.accountsConnection?.edges
);
const account = useSettlementAccount(
market.tradableInstrument.instrument.product.settlementAsset.id,
accounts
);
const { data } = usePartyMarketDataQuery({
pollInterval: 5000,
variables: { partyId: pubKey || '' },
skip: !pubKey,
});
const markPriceData = useMarketDataMarkPrice(market.id);
const marketPositions = useMarketPositions({
marketId: market.id,
partyId: pubKey || '',
});
const marginMaintenanceLevel = new BigNumber(
addDecimal(
data?.party?.marginsConnection?.edges?.find(
(nodes) => nodes.node.market.id === market.id
)?.node.maintenanceLevel || 0,
market.decimalPlaces
)
);
const dataAccounts = removePaginationWrapper(
data?.party?.accountsConnection?.edges
);
const positionAccount = dataAccounts.find(
(account) => account.market?.id === market.id
);
const positionAccountBalance = new BigNumber(
addDecimal(
positionAccount?.balance || 0,
positionAccount?.asset?.decimals || 0
)
);
const generalAccountBalance = new BigNumber(
addDecimal(account?.balance || 0, account?.asset.decimals || 0)
);
const volume = new BigNumber(
addDecimal(
marketPositions?.openVolume.toString() || '0',
market.positionDecimalPlaces
)
)[order.side === Schema.Side.SIDE_BUY ? 'plus' : 'minus'](order.size);
const markPrice = new BigNumber(
addDecimal(
markPriceData?.market?.data?.markPrice || 0,
markPriceData?.market?.decimalPlaces || 0
)
);
// regarding formula (marginMaintenanceLevel - positionAccountBalance - generalAccountBalance) / volume + markPrice
const marginDifference = marginMaintenanceLevel
.minus(positionAccountBalance)
.minus(generalAccountBalance);
const closeOut = marginDifference.div(volume).plus(markPrice);
if (closeOut.isPositive()) {
return formatNumber(closeOut, market.decimalPlaces);
}
return null;
};