lev-stake-sol/hooks/useStakeRates.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-09-14 20:48:49 -07:00
import { useQuery } from '@tanstack/react-query'
import {
fetchAndParsePricesCsv,
getPriceRangeFromPeriod,
calcYield,
DATA_SOURCE,
PERIOD,
} from '@glitchful-dev/sol-apy-sdk'
2023-09-15 11:53:40 -07:00
const fetchRates = async () => {
2023-09-14 20:48:49 -07:00
const [msolPrices, jitoPrices, bsolPrices, lidoPrices] = await Promise.all([
fetchAndParsePricesCsv(DATA_SOURCE.MARINADE_CSV),
fetchAndParsePricesCsv(DATA_SOURCE.JITO_CSV),
fetchAndParsePricesCsv(DATA_SOURCE.SOLBLAZE_CSV),
fetchAndParsePricesCsv(DATA_SOURCE.LIDO_CSV),
])
// may be null if the price range cannot be calculated
const msolRange = getPriceRangeFromPeriod(msolPrices, PERIOD.DAYS_30)
const jitoRange = getPriceRangeFromPeriod(jitoPrices, PERIOD.DAYS_30)
const bsolRange = getPriceRangeFromPeriod(bsolPrices, PERIOD.DAYS_30)
const lidoRange = getPriceRangeFromPeriod(lidoPrices, PERIOD.DAYS_30)
2023-09-14 20:48:49 -07:00
2023-09-15 11:53:40 -07:00
const rateData: Record<string, number> = {}
2023-09-14 20:48:49 -07:00
if (msolRange) {
2023-09-15 11:53:40 -07:00
rateData.msol = calcYield(msolRange)?.apy
2023-09-14 20:48:49 -07:00
}
if (jitoRange) {
2023-09-15 11:53:40 -07:00
rateData.jitosol = calcYield(jitoRange)?.apy
2023-09-14 20:48:49 -07:00
}
if (bsolRange) {
2023-09-15 11:53:40 -07:00
rateData.bsol = calcYield(bsolRange)?.apy
2023-09-14 20:48:49 -07:00
}
if (lidoRange) {
2023-09-15 11:53:40 -07:00
rateData.stsol = calcYield(lidoRange)?.apy
2023-09-14 20:48:49 -07:00
}
2023-09-15 11:53:40 -07:00
return rateData
2023-09-14 20:48:49 -07:00
}
2023-09-15 11:53:40 -07:00
export default function useStakeRates() {
const response = useQuery(['stake-rates'], () => fetchRates(), {
2023-09-14 20:48:49 -07:00
cacheTime: 1000 * 60 * 5,
staleTime: 1000 * 60,
retry: 3,
refetchOnWindowFocus: true,
})
return {
data: response.data,
isLoading: response.isFetching || response.isLoading,
}
2023-09-14 20:48:49 -07:00
}