feat(trading): improve funding rate formatting (#4847)

This commit is contained in:
Bartłomiej Głownia 2023-09-22 10:39:56 +02:00 committed by GitHub
parent bf8480fed4
commit 4c0d05844e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 30 deletions

View File

@ -126,32 +126,20 @@ export const FundingRate = ({ marketId }: { marketId: string }) => {
); );
}; };
const padStart = (n: number) => n.toString().padStart(2, '0'); const useNow = () => {
export const FundingCountdown = ({ marketId }: { marketId: string }) => {
const { data: fundingPeriods } = useFundingPeriodsQuery({
variables: {
marketId: marketId,
pagination: { first: 1 },
},
});
const { data: marketInfo } = useDataProvider({
dataProvider: marketInfoProvider,
variables: { marketId },
});
const [now, setNow] = useState(Date.now()); const [now, setNow] = useState(Date.now());
useEffect(() => { useEffect(() => {
const interval = setInterval(() => setNow(Date.now()), 1000); const interval = setInterval(() => setNow(Date.now()), 1000);
return () => clearInterval(interval); return () => clearInterval(interval);
}, []); }, []);
return now;
};
const node = fundingPeriods?.fundingPeriods.edges?.[0]?.node; const useEvery = (marketId: string) => {
let startTime: number | undefined = undefined; const { data: marketInfo } = useDataProvider({
if (node && node.startTime && !node.endTime) { dataProvider: marketInfoProvider,
startTime = fromNanoSeconds(node.startTime).getTime(); variables: { marketId },
} });
let diffFormatted = t('Unknown');
let every: number | undefined = undefined; let every: number | undefined = undefined;
const sourceType = const sourceType =
marketInfo && marketInfo &&
@ -165,14 +153,51 @@ export const FundingCountdown = ({ marketId }: { marketId: string }) => {
every *= 1000; every *= 1000;
} }
} }
return every;
};
const useStartTime = (marketId: string) => {
const { data: fundingPeriods } = useFundingPeriodsQuery({
variables: {
marketId: marketId,
pagination: { first: 1 },
},
});
const node = fundingPeriods?.fundingPeriods.edges?.[0]?.node;
let startTime: number | undefined = undefined;
if (node && node.startTime && !node.endTime) {
startTime = fromNanoSeconds(node.startTime).getTime();
}
return startTime;
};
const padStart = (n: number) => n.toString().padStart(2, '0');
const useFormatCountdown = (
now: number,
startTime?: number,
every?: number
) => {
if (startTime && every) { if (startTime && every) {
const diff = every - ((now - startTime) % every); const diff = every - ((now - startTime) % every);
const hours = (diff / 3.6e6) | 0; const hours = (diff / 3.6e6) | 0;
const mins = ((diff % 3.6e6) / 6e4) | 0; const mins = ((diff % 3.6e6) / 6e4) | 0;
const secs = Math.round((diff % 6e4) / 1e3); const secs = Math.round((diff % 6e4) / 1e3);
diffFormatted = `${padStart(hours)}:${padStart(mins)}:${padStart(secs)}`; return `${padStart(hours)}:${padStart(mins)}:${padStart(secs)}`;
} }
return <div data-testid="funding-countdown">{diffFormatted}</div>; return t('Unknown');
};
export const FundingCountdown = ({ marketId }: { marketId: string }) => {
const now = useNow();
const startTime = useStartTime(marketId);
const every = useEvery(marketId);
return (
<div data-testid="funding-countdown">
{useFormatCountdown(now, startTime, every)}
</div>
);
}; };
const ExpiryLabel = ({ market }: ExpiryLabelProps) => { const ExpiryLabel = ({ market }: ExpiryLabelProps) => {

View File

@ -38,7 +38,7 @@ export const FundingContainer = ({ marketId }: { marketId: string }) => {
if (edge.node.endTime) { if (edge.node.endTime) {
acc?.push({ acc?.push({
endTime: fromNanoSeconds(edge.node.endTime), endTime: fromNanoSeconds(edge.node.endTime),
fundingRate: Number(edge.node.fundingRate), fundingRate: Number(edge.node.fundingRate) * 100,
}); });
} }
return acc; return acc;
@ -51,5 +51,11 @@ export const FundingContainer = ({ marketId }: { marketId: string }) => {
if (!data || !values?.rows.length) { if (!data || !values?.rows.length) {
return <Splash> {t('No funding history data')}</Splash>; return <Splash> {t('No funding history data')}</Splash>;
} }
return <LineChart data={values} theme={theme} />; return (
<LineChart
data={values}
theme={theme}
priceFormat={(fundingRate) => `${fundingRate.toFixed(4)}%`}
/>
);
}; };

View File

@ -5,7 +5,7 @@ import { MarketSelector } from '../../components/market-selector/market-selector
import { MarketHeaderStats } from '../../client-pages/market/market-header-stats'; import { MarketHeaderStats } from '../../client-pages/market/market-header-stats';
import { useMarket, useMarketList } from '@vegaprotocol/markets'; import { useMarket, useMarketList } from '@vegaprotocol/markets';
import { useState } from 'react'; import { useState } from 'react';
import { MarketProductPill } from '@vegaprotocol/datagrid'; import { ProductTypeShortName } from '@vegaprotocol/types';
export const MarketHeader = () => { export const MarketHeader = () => {
const { marketId } = useParams(); const { marketId } = useParams();
@ -28,11 +28,13 @@ export const MarketHeader = () => {
<HeaderTitle> <HeaderTitle>
<span> <span>
{data.tradableInstrument.instrument.code} {data.tradableInstrument.instrument.code}
<MarketProductPill <span className="ml-2 text-xs uppercase text-muted">
productType={ {' '}
data.tradableInstrument.instrument.product.__typename {data.tradableInstrument.instrument.product.__typename &&
} ProductTypeShortName[
/> data.tradableInstrument.instrument.product.__typename
]}{' '}
</span>
</span> </span>
<VegaIcon name={VegaIconNames.CHEVRON_DOWN} size={14} /> <VegaIcon name={VegaIconNames.CHEVRON_DOWN} size={14} />
</HeaderTitle> </HeaderTitle>