Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bonsai-ui): migrate live trades table #1407

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions src/views/tables/LiveTrades.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useMemo } from 'react';

import { BonsaiHelpers, BonsaiHooks } from '@/abacus-ts/ontology';
import { LiveTrade } from '@/abacus-ts/summaryTypes';
import { IndexerOrderSide } from '@/types/indexer/indexerApiGen';
import { OrderSide } from '@dydxprotocol/v4-client-js';
import { shallowEqual } from 'react-redux';
import styled, { css, keyframes } from 'styled-components';

import { MarketTrade } from '@/constants/abacus';
import { STRING_KEYS } from '@/constants/localization';
import { TOKEN_DECIMALS } from '@/constants/numbers';
import { EMPTY_ARR } from '@/constants/objects';

import { useBreakpoints } from '@/hooks/useBreakpoints';
import { useLocaleSeparators } from '@/hooks/useLocaleSeparators';
Expand All @@ -18,14 +20,12 @@ import { LoadingSpace } from '@/components/Loading/LoadingSpinner';
import { Output, OutputType } from '@/components/Output';

import { useAppSelector } from '@/state/appTypes';
import { getCurrentMarketAssetData } from '@/state/assetsSelectors';
import { getSelectedLocale } from '@/state/localizationSelectors';
import { getCurrentMarketConfig, getCurrentMarketLiveTrades } from '@/state/perpetualsSelectors';

import { getConsistentAssetSizeString } from '@/lib/consistentAssetSize';
import { getSimpleStyledOutputType } from '@/lib/genericFunctionalComponentUtils';
import { isTruthy } from '@/lib/isTruthy';
import { getSelectedOrderSide } from '@/lib/tradeData';
import { MaybeBigNumber, MustBigNumber } from '@/lib/numbers';

import { OrderbookTradesOutput, OrderbookTradesTable } from './OrderbookTradesTable';

Expand All @@ -39,7 +39,7 @@ type StyleProps = {

// Current fix for styled-component not preserving generic row
type RowData = {
key: number;
key: string;
createdAtMilliseconds: number;
price: number;
side: OrderSide;
Expand All @@ -49,23 +49,32 @@ type RowData = {
export const LiveTrades = ({ className, histogramSide = 'left' }: StyleProps) => {
const stringGetter = useStringGetter();
const { isTablet } = useBreakpoints();
const currentMarketAssetData = useAppSelector(getCurrentMarketAssetData, shallowEqual);
const currentMarketConfig = useAppSelector(getCurrentMarketConfig, shallowEqual);
const currentMarketLiveTrades = useAppSelector(getCurrentMarketLiveTrades, shallowEqual);
const currentMarketConfig = useAppSelector(BonsaiHelpers.currentMarket.marketInfo);
const currentMarketLiveTrades =
BonsaiHooks.useCurrentMarketLiveTrades().data?.trades ?? EMPTY_ARR;

const { displayableAssetId } = currentMarketAssetData ?? {};
const { stepSizeDecimals, tickSizeDecimals, stepSize } = currentMarketConfig ?? {};
const {
stepSizeDecimals,
tickSizeDecimals,
stepSize,
displayableAsset: displayableAssetId,
} = currentMarketConfig ?? {};
const { decimal: decimalSeparator, group: groupSeparator } = useLocaleSeparators();
const selectedLocale = useAppSelector(getSelectedLocale);

const rows = currentMarketLiveTrades.map(
({ createdAtMilliseconds, price, size, side }: MarketTrade, idx) => ({
key: idx,
createdAtMilliseconds,
price,
side: getSelectedOrderSide(side),
size,
})
const rows = useMemo(
() =>
currentMarketLiveTrades.map(
({ createdAt, price, size, side, id }: LiveTrade): RowData => ({
key: id,
createdAtMilliseconds: new Date(createdAt).valueOf(),
price: MustBigNumber(price).toNumber(),
// todo use same helper as the horizontal panel files
side: side === IndexerOrderSide.BUY ? OrderSide.BUY : OrderSide.SELL,
size: MustBigNumber(size).toNumber(),
})
),
[currentMarketLiveTrades]
);

const columns = useMemo(() => {
Expand Down Expand Up @@ -109,7 +118,7 @@ export const LiveTrades = ({ className, histogramSide = 'left' }: StyleProps) =>
decimalSeparator,
groupSeparator,
selectedLocale,
stepSize: stepSize ?? 10 ** (-1 * TOKEN_DECIMALS),
stepSize: MaybeBigNumber(stepSize)?.toNumber() ?? 10 ** (-1 * TOKEN_DECIMALS),
stepSizeDecimals: stepSizeDecimals ?? TOKEN_DECIMALS,
})}
tw="text-[color:--accent-color] tablet:text-color-text-1"
Expand Down
Loading