2022-06-30 07:52:25 +00:00
|
|
|
import type { AgGridReact } from 'ag-grid-react';
|
|
|
|
import {
|
|
|
|
addDecimal,
|
|
|
|
addDecimalsFormatNumber,
|
|
|
|
formatNumber,
|
|
|
|
getDateTimeFormat,
|
|
|
|
t,
|
|
|
|
} from '@vegaprotocol/react-helpers';
|
2022-07-20 09:37:28 +00:00
|
|
|
import { Side } from '@vegaprotocol/types';
|
2022-06-30 07:52:25 +00:00
|
|
|
import { AgGridColumn } from 'ag-grid-react';
|
|
|
|
import { AgGridDynamic as AgGrid } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import { forwardRef } from 'react';
|
2022-07-20 09:37:28 +00:00
|
|
|
import type { ValueFormatterParams } from 'ag-grid-community';
|
2022-06-30 07:52:25 +00:00
|
|
|
import BigNumber from 'bignumber.js';
|
2022-07-20 09:37:28 +00:00
|
|
|
import type { AgGridReactProps, AgReactUiProps } from 'ag-grid-react';
|
|
|
|
import type {
|
|
|
|
FillFields,
|
|
|
|
FillFields_market_tradableInstrument_instrument_product,
|
|
|
|
} from './__generated__/FillFields';
|
|
|
|
import type { Fills_party_tradesConnection_edges_node } from './__generated__/Fills';
|
2022-06-30 07:52:25 +00:00
|
|
|
|
2022-07-20 09:37:28 +00:00
|
|
|
export type Props = (AgGridReactProps | AgReactUiProps) & {
|
2022-06-30 07:52:25 +00:00
|
|
|
partyId: string;
|
2022-07-20 09:37:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type AccountsTableValueFormatterParams = Omit<
|
|
|
|
ValueFormatterParams,
|
|
|
|
'data' | 'value'
|
|
|
|
> & {
|
|
|
|
data: Fills_party_tradesConnection_edges_node | null;
|
|
|
|
};
|
2022-06-30 07:52:25 +00:00
|
|
|
|
2022-07-20 09:37:28 +00:00
|
|
|
export const FillsTable = forwardRef<AgGridReact, Props>(
|
|
|
|
({ partyId, ...props }, ref) => {
|
2022-06-30 07:52:25 +00:00
|
|
|
return (
|
|
|
|
<AgGrid
|
|
|
|
ref={ref}
|
|
|
|
overlayNoRowsTemplate={t('No fills')}
|
|
|
|
defaultColDef={{ flex: 1, resizable: true }}
|
|
|
|
style={{ width: '100%', height: '100%' }}
|
2022-07-05 13:33:50 +00:00
|
|
|
getRowId={({ data }) => data?.id}
|
2022-07-20 09:37:28 +00:00
|
|
|
{...props}
|
2022-06-30 07:52:25 +00:00
|
|
|
>
|
|
|
|
<AgGridColumn headerName={t('Market')} field="market.name" />
|
|
|
|
<AgGridColumn
|
2022-08-11 08:10:20 +00:00
|
|
|
headerName={t('Size')}
|
2022-06-30 07:52:25 +00:00
|
|
|
field="size"
|
|
|
|
cellClass={({ data }: { data: FillFields }) => {
|
|
|
|
let className = '';
|
2022-07-05 13:33:50 +00:00
|
|
|
if (data?.buyer.id === partyId) {
|
2022-08-11 08:10:20 +00:00
|
|
|
className = 'text-vega-green-dark dark:text-vega-green';
|
2022-07-05 13:33:50 +00:00
|
|
|
} else if (data?.seller.id) {
|
2022-08-11 08:10:20 +00:00
|
|
|
className = 'text-vega-red-dark dark:text-vega-red';
|
2022-06-30 07:52:25 +00:00
|
|
|
}
|
|
|
|
return className;
|
|
|
|
}}
|
|
|
|
valueFormatter={formatSize(partyId)}
|
|
|
|
/>
|
|
|
|
<AgGridColumn
|
|
|
|
headerName={t('Value')}
|
|
|
|
field="price"
|
|
|
|
valueFormatter={formatPrice}
|
|
|
|
/>
|
|
|
|
<AgGridColumn
|
|
|
|
headerName={t('Filled value')}
|
|
|
|
field="price"
|
|
|
|
valueFormatter={formatTotal}
|
|
|
|
/>
|
|
|
|
<AgGridColumn
|
|
|
|
headerName={t('Role')}
|
|
|
|
field="aggressor"
|
|
|
|
valueFormatter={formatRole(partyId)}
|
|
|
|
/>
|
|
|
|
<AgGridColumn
|
|
|
|
headerName={t('Fee')}
|
|
|
|
field="market.tradableInstrument.instrument.product"
|
|
|
|
valueFormatter={formatFee(partyId)}
|
|
|
|
/>
|
|
|
|
<AgGridColumn
|
|
|
|
headerName={t('Date')}
|
|
|
|
field="createdAt"
|
2022-07-20 09:37:28 +00:00
|
|
|
valueFormatter={({
|
|
|
|
value,
|
|
|
|
}: AccountsTableValueFormatterParams & {
|
|
|
|
value: Fills_party_tradesConnection_edges_node['createdAt'];
|
|
|
|
}) => {
|
2022-07-05 13:33:50 +00:00
|
|
|
if (value === undefined) {
|
|
|
|
return value;
|
|
|
|
}
|
2022-06-30 07:52:25 +00:00
|
|
|
return getDateTimeFormat().format(new Date(value));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</AgGrid>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-07-20 09:37:28 +00:00
|
|
|
const formatPrice = ({
|
|
|
|
value,
|
|
|
|
data,
|
|
|
|
}: AccountsTableValueFormatterParams & {
|
|
|
|
value?: Fills_party_tradesConnection_edges_node['price'];
|
|
|
|
}) => {
|
|
|
|
if (value === undefined || !data) {
|
|
|
|
return undefined;
|
2022-07-05 13:33:50 +00:00
|
|
|
}
|
2022-06-30 07:52:25 +00:00
|
|
|
const asset =
|
2022-07-05 13:33:50 +00:00
|
|
|
data?.market.tradableInstrument.instrument.product.settlementAsset.symbol;
|
2022-06-30 07:52:25 +00:00
|
|
|
const valueFormatted = addDecimalsFormatNumber(
|
|
|
|
value,
|
2022-07-05 13:33:50 +00:00
|
|
|
data?.market.decimalPlaces
|
2022-06-30 07:52:25 +00:00
|
|
|
);
|
|
|
|
return `${valueFormatted} ${asset}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const formatSize = (partyId: string) => {
|
2022-07-20 09:37:28 +00:00
|
|
|
return ({
|
|
|
|
value,
|
|
|
|
data,
|
|
|
|
}: AccountsTableValueFormatterParams & {
|
|
|
|
value?: Fills_party_tradesConnection_edges_node['size'];
|
|
|
|
}) => {
|
|
|
|
if (value === undefined || !data) {
|
|
|
|
return undefined;
|
2022-07-05 13:33:50 +00:00
|
|
|
}
|
2022-06-30 07:52:25 +00:00
|
|
|
let prefix;
|
2022-07-05 13:33:50 +00:00
|
|
|
if (data?.buyer.id === partyId) {
|
2022-06-30 07:52:25 +00:00
|
|
|
prefix = '+';
|
2022-07-05 13:33:50 +00:00
|
|
|
} else if (data?.seller.id) {
|
2022-06-30 07:52:25 +00:00
|
|
|
prefix = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
const size = addDecimalsFormatNumber(
|
|
|
|
value,
|
2022-07-05 13:33:50 +00:00
|
|
|
data?.market.positionDecimalPlaces
|
2022-06-30 07:52:25 +00:00
|
|
|
);
|
|
|
|
return `${prefix}${size}`;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-07-20 09:37:28 +00:00
|
|
|
const formatTotal = ({
|
|
|
|
value,
|
|
|
|
data,
|
|
|
|
}: AccountsTableValueFormatterParams & {
|
|
|
|
value?: Fills_party_tradesConnection_edges_node['price'];
|
|
|
|
}) => {
|
|
|
|
if (value === undefined || !data) {
|
|
|
|
return undefined;
|
2022-07-05 13:33:50 +00:00
|
|
|
}
|
2022-06-30 07:52:25 +00:00
|
|
|
const asset =
|
2022-07-05 13:33:50 +00:00
|
|
|
data?.market.tradableInstrument.instrument.product.settlementAsset.symbol;
|
2022-06-30 07:52:25 +00:00
|
|
|
const size = new BigNumber(
|
2022-07-05 13:33:50 +00:00
|
|
|
addDecimal(data?.size, data?.market.positionDecimalPlaces)
|
2022-06-30 07:52:25 +00:00
|
|
|
);
|
2022-07-05 13:33:50 +00:00
|
|
|
const price = new BigNumber(addDecimal(value, data?.market.decimalPlaces));
|
2022-06-30 07:52:25 +00:00
|
|
|
|
|
|
|
const total = size.times(price).toString();
|
2022-07-05 13:33:50 +00:00
|
|
|
const valueFormatted = formatNumber(total, data?.market.decimalPlaces);
|
2022-06-30 07:52:25 +00:00
|
|
|
return `${valueFormatted} ${asset}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const formatRole = (partyId: string) => {
|
2022-07-20 09:37:28 +00:00
|
|
|
return ({
|
|
|
|
value,
|
|
|
|
data,
|
|
|
|
}: AccountsTableValueFormatterParams & {
|
|
|
|
value?: Fills_party_tradesConnection_edges_node['aggressor'];
|
|
|
|
}) => {
|
2022-07-05 13:33:50 +00:00
|
|
|
if (value === undefined) {
|
|
|
|
return value;
|
|
|
|
}
|
2022-06-30 07:52:25 +00:00
|
|
|
const taker = t('Taker');
|
|
|
|
const maker = t('Maker');
|
2022-07-05 13:33:50 +00:00
|
|
|
if (data?.buyer.id === partyId) {
|
2022-06-30 07:52:25 +00:00
|
|
|
if (value === Side.Buy) {
|
|
|
|
return taker;
|
|
|
|
} else {
|
|
|
|
return maker;
|
|
|
|
}
|
2022-07-05 13:33:50 +00:00
|
|
|
} else if (data?.seller.id === partyId) {
|
2022-06-30 07:52:25 +00:00
|
|
|
if (value === Side.Sell) {
|
|
|
|
return taker;
|
|
|
|
} else {
|
|
|
|
return maker;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return '-';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const formatFee = (partyId: string) => {
|
2022-07-20 09:37:28 +00:00
|
|
|
return ({
|
|
|
|
value,
|
|
|
|
data,
|
|
|
|
}: AccountsTableValueFormatterParams & {
|
|
|
|
value?: FillFields_market_tradableInstrument_instrument_product;
|
|
|
|
}) => {
|
2022-07-05 13:33:50 +00:00
|
|
|
if (value === undefined) {
|
|
|
|
return value;
|
|
|
|
}
|
2022-06-30 07:52:25 +00:00
|
|
|
const asset = value.settlementAsset;
|
|
|
|
let feesObj;
|
2022-07-05 13:33:50 +00:00
|
|
|
if (data?.buyer.id === partyId) {
|
|
|
|
feesObj = data?.buyerFee;
|
|
|
|
} else if (data?.seller.id === partyId) {
|
|
|
|
feesObj = data?.sellerFee;
|
2022-06-30 07:52:25 +00:00
|
|
|
} else {
|
|
|
|
return '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
const fee = new BigNumber(feesObj.makerFee)
|
|
|
|
.plus(feesObj.infrastructureFee)
|
|
|
|
.plus(feesObj.liquidityFee);
|
|
|
|
const totalFees = addDecimalsFormatNumber(fee.toString(), asset.decimals);
|
|
|
|
return `${totalFees} ${asset.symbol}`;
|
|
|
|
};
|
|
|
|
};
|