lev-stake-sol/hooks/useAccountHistory.ts

66 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-09-15 13:33:29 -07:00
import { useQuery } from '@tanstack/react-query'
import { MANGO_DATA_API_URL } from 'utils/constants'
import { ActivityFeed, EmptyObject } from 'types'
import dayjs from 'dayjs'
2023-09-20 15:01:47 -07:00
import useStakeAccounts from './useStakeAccounts'
2023-09-15 13:33:29 -07:00
const fetchHistory = async (
mangoAccountPk: string,
): Promise<Array<ActivityFeed> | EmptyObject | null> => {
const response = await fetch(
`${MANGO_DATA_API_URL}/stats/activity-feed?mango-account=${mangoAccountPk}&offset=0&limit=1000`,
)
const parsedResponse: Array<ActivityFeed> = await response.json()
if (Array.isArray(parsedResponse)) {
const activity = parsedResponse
.map((i) => {
return {
...i,
}
})
.sort(
(a, b) =>
dayjs(b.block_datetime).unix() - dayjs(a.block_datetime).unix(),
)
// only add to current feed if data request is offset and the mango account hasn't changed
// const combinedFeed =
// offset !== 0 ? loadedFeed.concat(latestFeed) : latestFeed
return activity
} else return null
}
export default function useAccountHistory() {
2023-09-20 15:01:47 -07:00
const { stakeAccounts } = useStakeAccounts()
2023-09-15 13:33:29 -07:00
2023-09-20 15:01:47 -07:00
const response = useQuery<Array<ActivityFeed[]> | EmptyObject | null>(
2023-09-15 13:33:29 -07:00
['history'],
() =>
2023-09-20 15:01:47 -07:00
stakeAccounts?.length
? Promise.all(
stakeAccounts.map((acc) => fetchHistory(acc.publicKey.toString())),
)
: // ? fetchHistory(mangoAccount.publicKey.toString())
null,
2023-09-15 13:33:29 -07:00
{
cacheTime: 1000 * 60 * 5,
staleTime: 1000 * 60,
retry: 3,
refetchOnWindowFocus: true,
2023-09-20 15:01:47 -07:00
enabled: !!stakeAccounts,
2023-09-15 13:33:29 -07:00
},
)
2023-09-20 15:01:47 -07:00
console.log('tx his reponse', response)
return {
history:
response?.data && Array.isArray(response.data)
? response.data.flat()
: [],
isLoading: response.isLoading || response.isFetching,
}
2023-09-15 13:33:29 -07:00
}