vega-frontend-monorepo/apps/trading/pages/liquidity/[marketId].page.tsx
m.ray 4d20151abd
feat: 1537 LP view data provider subscriptions after 0.58 release (#1630)
* fix: #1537 add lp provider sub

* fix: change LP title & update party.id

* fix: #1537 fix obligation and supplied

* fix: #1537 add min width on accounts

* fix: #1537 consistent null check

* fix: #1537 update lp data providers

* fix:  add acounts data provider but requires party id

* feat: #1537 LP data provider -  marketID & query or subscribe for accounts - partyId

* fix: #1537 remove log and fix build

* fix: #1537 fix build console lite and lp dashboard

* fix: #1537 fix tsconfig lp dahsboard

* fix: #1537 use interval to poll LP data - sub not working yet
2022-10-24 20:42:53 +01:00

231 lines
6.8 KiB
TypeScript

import {
liquidityProvisionsDataProvider,
LiquidityTable,
lpAggregatedDataProvider,
marketLiquidityDataProvider,
} from '@vegaprotocol/liquidity';
import { tooltipMapping } from '@vegaprotocol/market-info';
import {
addDecimalsFormatNumber,
NetworkParams,
t,
useDataProvider,
useNetworkParam,
} from '@vegaprotocol/react-helpers';
import { Schema } from '@vegaprotocol/types';
import {
AsyncRenderer,
Link as UiToolkitLink,
Tab,
Tabs,
} from '@vegaprotocol/ui-toolkit';
import { useVegaWallet } from '@vegaprotocol/wallet';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { Header, HeaderStat } from '../../components/header';
import type { AgGridReact } from 'ag-grid-react';
import type { LiquidityProvisionData } from '@vegaprotocol/liquidity';
const LiquidityPage = ({ id }: { id?: string }) => {
const { query } = useRouter();
const { pubKey } = useVegaWallet();
const gridRef = useRef<AgGridReact | null>(null);
// Default to first marketId query item if found
const marketId =
id || (Array.isArray(query.marketId) ? query.marketId[0] : query.marketId);
const { data: marketProvision } = useDataProvider({
dataProvider: marketLiquidityDataProvider,
noUpdate: true,
variables: useMemo(() => ({ marketId }), [marketId]),
});
const dataRef = useRef<LiquidityProvisionData[] | null>(null);
const { reload } = useDataProvider({
dataProvider: liquidityProvisionsDataProvider,
variables: useMemo(() => ({ marketId }), [marketId]),
});
const update = useCallback(
({ data }: { data: LiquidityProvisionData[] }) => {
if (!gridRef.current?.api) {
return false;
}
if (dataRef.current?.length) {
dataRef.current = data;
gridRef.current.api.refreshInfiniteCache();
return true;
}
return false;
},
[gridRef]
);
const {
data: liquidityProviders,
loading,
error,
} = useDataProvider({
dataProvider: lpAggregatedDataProvider,
update,
variables: useMemo(
() => ({ marketId, partyId: pubKey }),
[marketId, pubKey]
),
});
// To be removed when liquidityProvision subscriptions are working
useEffect(() => {
const interval = setInterval(reload, 10000);
return () => clearInterval(interval);
}, [reload]);
const targetStake = marketProvision?.market?.data?.targetStake;
const suppliedStake = marketProvision?.market?.data?.suppliedStake;
const assetDecimalPlaces =
marketProvision?.market?.tradableInstrument.instrument.product
.settlementAsset.decimals || 0;
const symbol =
marketProvision?.market?.tradableInstrument.instrument.product
.settlementAsset.symbol;
const { param: stakeToCcySiskas } = useNetworkParam(
NetworkParams.market_liquidity_stakeToCcySiskas
);
const stakeToCcySiska = stakeToCcySiskas && stakeToCcySiskas[0];
const myLpEdges = useMemo(
() => liquidityProviders?.filter((e) => e.party.id === pubKey),
[liquidityProviders, pubKey]
);
const activeEdges = useMemo(
() =>
liquidityProviders?.filter(
(e) => e.status === Schema.LiquidityProvisionStatus.STATUS_ACTIVE
),
[liquidityProviders]
);
const inactiveEdges = useMemo(
() =>
liquidityProviders?.filter(
(e) => e.status !== Schema.LiquidityProvisionStatus.STATUS_ACTIVE
),
[liquidityProviders]
);
const enum LiquidityTabs {
Active = 'active',
Inactive = 'inactive',
MyLiquidityProvision = 'myLP',
}
const getActiveDefaultId = () => {
if (myLpEdges && myLpEdges.length > 0) {
return LiquidityTabs.MyLiquidityProvision;
}
if (activeEdges?.length) return LiquidityTabs.Active;
else if (inactiveEdges && inactiveEdges.length > 0) {
return LiquidityTabs.Inactive;
}
return LiquidityTabs.Active;
};
return (
<AsyncRenderer loading={loading} error={error} data={liquidityProviders}>
<div className="h-full grid grid-rows-[min-content_1fr]">
<Header
title={
<Link href={`/markets/${marketId}`} passHref={true}>
<UiToolkitLink className="sm:text-lg md:text-xl lg:text-2xl flex items-center gap-2 whitespace-nowrap hover:text-neutral-500 dark:hover:text-neutral-300">
{`${
marketProvision?.market?.tradableInstrument.instrument.name
} ${t('liquidity provision')}`}
</UiToolkitLink>
</Link>
}
>
<HeaderStat
heading={t('Target stake')}
description={tooltipMapping['targetStake']}
>
<div>
{targetStake
? `${addDecimalsFormatNumber(
targetStake,
assetDecimalPlaces ?? 0
)} ${symbol}`
: '-'}
</div>
</HeaderStat>
<HeaderStat
heading={t('Supplied stake')}
description={tooltipMapping['suppliedStake']}
>
<div>
{suppliedStake
? `${addDecimalsFormatNumber(
suppliedStake,
assetDecimalPlaces ?? 0
)} ${symbol}`
: '-'}
</div>
</HeaderStat>
<HeaderStat heading={t('Market ID')}>
<div className="break-word">{marketId}</div>
</HeaderStat>
</Header>
<Tabs active={getActiveDefaultId()}>
<Tab
id={LiquidityTabs.MyLiquidityProvision}
name={t('My liquidity provision')}
hidden={!pubKey}
>
{myLpEdges && (
<LiquidityTable
ref={gridRef}
data={myLpEdges}
symbol={symbol}
stakeToCcySiskas={stakeToCcySiska}
assetDecimalPlaces={assetDecimalPlaces}
/>
)}
</Tab>
<Tab id={LiquidityTabs.Active} name={t('Active')}>
{activeEdges && (
<LiquidityTable
ref={gridRef}
data={activeEdges}
symbol={symbol}
assetDecimalPlaces={assetDecimalPlaces}
stakeToCcySiskas={stakeToCcySiska}
/>
)}
</Tab>
{
<Tab id={LiquidityTabs.Inactive} name={t('Inactive')}>
{inactiveEdges && (
<LiquidityTable
ref={gridRef}
data={inactiveEdges}
symbol={symbol}
assetDecimalPlaces={assetDecimalPlaces}
stakeToCcySiskas={stakeToCcySiska}
/>
)}
</Tab>
}
</Tabs>
</div>
</AsyncRenderer>
);
};
LiquidityPage.getInitialProps = () => ({
page: 'liquidity',
});
export default LiquidityPage;