Skip to content

Commit

Permalink
feat: succesfully move all thorchain LP queries to centralized querie…
Browse files Browse the repository at this point in the history
…s file
  • Loading branch information
gomesalexandre committed Jan 19, 2024
1 parent 5d8a3a7 commit 89f4b25
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
9 changes: 2 additions & 7 deletions src/pages/ThorChainLP/Pool/Pool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import { Main } from 'components/Layout/Main'
import {
calculateTVL,
get24hSwapChangePercentage,
get24hTvlChangePercentage,
getAllTimeVolume,
getFees,
getVolume,
} from 'lib/utils/thorchain/lp'
Expand Down Expand Up @@ -157,14 +155,11 @@ export const Pool = () => {
}, [swapData24h, swapDataPrevious24h, runeMarketData.price, assetMarketData.price])

const { data: tvl24hChange } = useQuery({
queryKey: ['get24hTvlChangePercentage', foundPool?.assetId ?? ''],
queryFn: () =>
foundPool ? get24hTvlChangePercentage(foundPool.assetId) : Promise.resolve(null),
...reactQueries.thorchainLp.tvl24hChange(foundPool?.assetId),
})

const { data: allTimeVolume } = useQuery({
queryKey: ['thorchainPoolVolumeAllTime', foundPool?.assetId ?? ''],
queryFn: () => (foundPool ? getAllTimeVolume(foundPool.assetId, runeMarketData.price) : ''),
...reactQueries.thorchainLp.allTimeVolume(foundPool?.assetId, runeMarketData.price),
})

const tvl = useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/ThorChainLP/queries/hooks/useAllUserLpData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export const useAllUserLpData = ({
assetIds.map(assetId => {
const poolAssetId = assetIdToPoolAssetId({ assetId })
return {
...reactQueries.thorchainLp.userLpData(assetId),
enabled: Boolean(
isThornodePoolsDataLoaded &&
isMidgardPoolsDataLoaded &&
allThornodePools?.find(pool => pool.asset === poolAssetId) &&
allMidgardPools?.find(pool => pool.asset === poolAssetId),
),
queryKey: ['thorchainUserLpData', { assetId }],
// We may or may not want to revisit this, but this will prevent overfetching for now
staleTime: Infinity,
queryFn: async () => {
Expand Down
12 changes: 2 additions & 10 deletions src/pages/ThorChainLP/queries/hooks/useUserLpData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { AccountId } from '@shapeshiftoss/caip'
import { type AssetId, thorchainAssetId } from '@shapeshiftoss/caip'
import type { UseQueryResult } from '@tanstack/react-query'
import { useQuery } from '@tanstack/react-query'
import { useMemo } from 'react'
import { reactQueries } from 'react-queries'
import { queryClient } from 'context/QueryClientProvider/queryClient'
import { bn } from 'lib/bignumber/bignumber'
Expand All @@ -29,11 +28,6 @@ export const useUserLpData = ({
...thorchainAccountIds,
]

const lpPositionQueryKey: [string, { assetId: AssetId }] = useMemo(
() => ['thorchainUserLpData', { assetId }],
[assetId],
)

const poolAssetMarketData = useAppSelector(state => selectMarketDataById(state, assetId))
const runeMarketData = useAppSelector(state => selectMarketDataById(state, thorchainAssetId))

Expand Down Expand Up @@ -108,13 +102,11 @@ export const useUserLpData = ({
return parsedPositions
}

// TODO(gomes): this is dumb and not really the way to use react-query - this is really just mapping over positions as queries and running a selector.
// We may want to run the queries themselves with `useQueries` and then map over the results with a `useMemo`
const liquidityPoolPositionData = useQuery({
queryKey: lpPositionQueryKey,
...reactQueries.thorchainLp.userLpData(assetId),
staleTime: Infinity,
queryFn: async ({ queryKey }) => {
const [, { assetId }] = queryKey
const [, , assetId] = queryKey

const allPositions = (
await Promise.all(
Expand Down
3 changes: 3 additions & 0 deletions src/pages/ThorChainLP/queries/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,7 @@ export const thorchainLp = createQueryKeys('thorchainLp', {
return positions
},
}),
userLpData: (assetId: AssetId) => ({
queryKey: ['thorchainUserLpData', { assetId }],
}),
})

0 comments on commit 89f4b25

Please sign in to comment.