vega-frontend-monorepo/libs/liquidity/src/lib/markets-liquidity-provider.ts
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

135 lines
3.2 KiB
TypeScript

import { useMemo } from 'react';
import { Schema } from '@vegaprotocol/types';
import {
makeDataProvider,
makeDerivedDataProvider,
useDataProvider,
useYesterday,
} from '@vegaprotocol/react-helpers';
import type {
MarketCandles,
MarketWithCandles,
MarketWithData,
} from '@vegaprotocol/market-list';
import {
marketsCandlesProvider,
marketListProvider,
} from '@vegaprotocol/market-list';
import type { LiquidityProvisionMarkets } from './__generated__/LiquidityProvisionMarkets';
import { LiquidityProvisionMarketsDocument } from './__generated___/MarketsLiquidity';
import {
calcDayVolume,
getCandle24hAgo,
getChange,
getLiquidityForMarket,
sumLiquidityCommitted,
getFeeLevels,
getTargetStake,
} from './utils/liquidity-utils';
import type { Provider, LiquidityProvisionMarket } from './utils';
interface FeeLevels {
commitmentAmount: number;
fee: string;
}
export type Market = MarketWithData &
MarketWithCandles & { feeLevels: FeeLevels[]; target: string };
export interface Markets {
markets: Market[];
}
const getData = (
responseData: LiquidityProvisionMarkets
): LiquidityProvisionMarket[] | null => {
return (
responseData?.marketsConnection?.edges.map((edge) => {
return edge.node;
}) || null
);
};
export const addData = (
markets: (MarketWithData & MarketWithCandles)[],
marketsCandles24hAgo: MarketCandles[],
marketsLiquidity: LiquidityProvisionMarket[]
) => {
return markets.map((market) => {
const dayVolume = calcDayVolume(market.candles);
const candle24hAgo = getCandle24hAgo(market.id, marketsCandles24hAgo);
const volumeChange = getChange(market.candles || [], candle24hAgo?.close);
const liquidityProviders = getLiquidityForMarket(
market.id,
marketsLiquidity
) as Provider[];
return {
...market,
dayVolume,
volumeChange,
liquidityCommitted: sumLiquidityCommitted(liquidityProviders),
feeLevels: getFeeLevels(liquidityProviders) || [],
target: getTargetStake(market.id, marketsLiquidity),
};
});
};
export const liquidityMarketsProvider = makeDataProvider<
LiquidityProvisionMarkets,
LiquidityProvisionMarket[],
never,
never
>({
query: LiquidityProvisionMarketsDocument,
getData,
});
const liquidityProvisionProvider = makeDerivedDataProvider<Markets, never>(
[
marketListProvider,
(callback, client, variables) =>
marketsCandlesProvider(callback, client, {
...variables,
interval: Schema.Interval.INTERVAL_I1D,
}),
liquidityMarketsProvider,
],
(parts) => {
const data = addData(
parts[0] as (MarketWithData & MarketWithCandles)[],
parts[1] as MarketCandles[],
parts[2] as LiquidityProvisionMarket[]
);
return { markets: data };
}
);
export const useMarketsLiquidity = () => {
const yesterday = useYesterday();
const variables = useMemo(() => {
return {
since: new Date(yesterday).toISOString(),
interval: Schema.Interval.INTERVAL_I1H,
};
}, [yesterday]);
const { data, loading, error } = useDataProvider({
dataProvider: liquidityProvisionProvider,
variables,
noUpdate: true,
});
return {
data,
loading,
error,
};
};