2023-08-28 18:02:13 -07:00
|
|
|
import { PerpMarket } from '@blockworks-foundation/mango-v4'
|
2022-12-05 18:54:11 -08:00
|
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import useMangoGroup from 'hooks/useMangoGroup'
|
2022-11-30 07:46:20 -08:00
|
|
|
import useSelectedMarket from 'hooks/useSelectedMarket'
|
2022-12-05 18:54:11 -08:00
|
|
|
import { useMemo } from 'react'
|
2023-01-18 05:13:29 -08:00
|
|
|
import { MANGO_DATA_API_URL } from 'utils/constants'
|
2023-04-17 09:25:59 -07:00
|
|
|
import Tooltip from '@components/shared/Tooltip'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-05-11 20:46:59 -07:00
|
|
|
import Link from 'next/link'
|
2022-12-05 18:54:11 -08:00
|
|
|
|
|
|
|
const fetchFundingRate = async (groupPk: string | undefined) => {
|
|
|
|
const res = await fetch(
|
2023-07-21 11:47:53 -07:00
|
|
|
`${MANGO_DATA_API_URL}/one-hour-funding-rate?mango-group=${groupPk}`,
|
2022-12-05 18:54:11 -08:00
|
|
|
)
|
|
|
|
return await res.json()
|
|
|
|
}
|
|
|
|
|
|
|
|
export const usePerpFundingRate = () => {
|
|
|
|
const { group } = useMangoGroup()
|
|
|
|
|
2023-02-27 23:20:11 -08:00
|
|
|
const res = useQuery<
|
|
|
|
{ market_index: number; funding_rate_hourly: number }[],
|
|
|
|
Error
|
|
|
|
>(['funding-rate'], () => fetchFundingRate(group?.publicKey?.toString()), {
|
|
|
|
cacheTime: 1000 * 60 * 10,
|
|
|
|
staleTime: 1000 * 60,
|
|
|
|
retry: 3,
|
|
|
|
enabled: !!group,
|
|
|
|
})
|
2022-12-05 18:54:11 -08:00
|
|
|
|
2022-12-09 13:37:44 -08:00
|
|
|
return Array.isArray(res?.data) ? res : { isSuccess: false, data: null }
|
2022-12-05 18:54:11 -08:00
|
|
|
}
|
2022-11-30 07:46:20 -08:00
|
|
|
|
2023-03-27 04:25:23 -07:00
|
|
|
export const formatFunding = Intl.NumberFormat('en', {
|
2023-03-07 09:31:32 -08:00
|
|
|
minimumSignificantDigits: 1,
|
2023-04-17 09:25:59 -07:00
|
|
|
maximumSignificantDigits: 2,
|
2023-03-07 09:31:32 -08:00
|
|
|
style: 'percent',
|
|
|
|
})
|
|
|
|
|
2022-11-30 07:46:20 -08:00
|
|
|
const PerpFundingRate = () => {
|
|
|
|
const { selectedMarket } = useSelectedMarket()
|
2022-12-05 18:54:11 -08:00
|
|
|
const rate = usePerpFundingRate()
|
2023-04-17 09:25:59 -07:00
|
|
|
const { t } = useTranslation(['common', 'trade'])
|
2022-12-09 13:37:44 -08:00
|
|
|
|
2022-12-05 18:54:11 -08:00
|
|
|
const fundingRate = useMemo(() => {
|
|
|
|
if (rate.isSuccess && selectedMarket instanceof PerpMarket) {
|
2022-12-09 13:37:44 -08:00
|
|
|
const marketRate = rate?.data?.find(
|
2023-07-21 11:47:53 -07:00
|
|
|
(r) => r.market_index === selectedMarket.perpMarketIndex,
|
2022-12-05 18:54:11 -08:00
|
|
|
)
|
2023-03-07 08:27:46 -08:00
|
|
|
const funding = marketRate?.funding_rate_hourly
|
2023-04-17 09:25:59 -07:00
|
|
|
return typeof funding === 'number' ? funding : undefined
|
2022-12-05 18:54:11 -08:00
|
|
|
}
|
2022-12-09 13:37:44 -08:00
|
|
|
}, [rate, selectedMarket])
|
2022-12-05 18:54:11 -08:00
|
|
|
|
2022-11-30 07:46:20 -08:00
|
|
|
return (
|
2023-04-17 09:25:59 -07:00
|
|
|
<>
|
|
|
|
<div className="ml-6 flex-col whitespace-nowrap">
|
|
|
|
<Tooltip
|
|
|
|
content={
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
Funding is paid continuously. The 1hr rate displayed is a
|
|
|
|
rolling average of the past 60 mins.
|
|
|
|
</div>
|
|
|
|
<div className="mt-2">
|
|
|
|
When positive, longs will pay shorts and when negative shorts
|
|
|
|
pay longs.
|
|
|
|
</div>
|
|
|
|
{typeof fundingRate === 'number' ? (
|
|
|
|
<div className="mt-2">
|
2023-08-28 18:02:13 -07:00
|
|
|
The annualized funding rate is{' '}
|
2023-05-11 20:46:59 -07:00
|
|
|
<span className="font-mono text-th-fgd-2">
|
|
|
|
{formatFunding.format(fundingRate * 8760)}
|
|
|
|
</span>
|
2023-04-17 09:25:59 -07:00
|
|
|
</div>
|
|
|
|
) : null}
|
2023-05-11 20:46:59 -07:00
|
|
|
<Link
|
|
|
|
className="mt-2 block"
|
|
|
|
href={`/stats?market=${selectedMarket?.name}`}
|
|
|
|
shallow={true}
|
|
|
|
>
|
|
|
|
View Chart
|
|
|
|
</Link>
|
2023-04-17 09:25:59 -07:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div className="flex items-center">
|
2023-05-11 21:08:06 -07:00
|
|
|
<div className="tooltip-underline mb-0.5 text-xs text-th-fgd-4">
|
2023-04-17 09:25:59 -07:00
|
|
|
{t('trade:funding-rate')}
|
|
|
|
</div>
|
2023-05-11 21:08:06 -07:00
|
|
|
{/* <InformationCircleIcon className="ml-1 h-4 w-4 text-th-fgd-4" /> */}
|
2023-04-17 09:25:59 -07:00
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
<p className="font-mono text-xs text-th-fgd-2">
|
|
|
|
{selectedMarket instanceof PerpMarket &&
|
|
|
|
typeof fundingRate === 'number' ? (
|
|
|
|
<span>{formatFunding.format(fundingRate)}</span>
|
|
|
|
) : (
|
|
|
|
<span className="text-th-fgd-4">-</span>
|
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</>
|
2022-11-30 07:46:20 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PerpFundingRate
|