Skip to content

fix: lend market data refresh #878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions apps/main/src/lend/entities/chain/chain-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ import { useMemo } from 'react'
import networks from '@/lend/networks'
import { ChainId } from '@/lend/types/lend.types'
import { OneWayMarketTemplate } from '@curvefi/lending-api/lib/markets'
import { ChainParams } from '@ui-kit/lib/model/query'
import { ChainParams } from '@ui-kit/lib/model'
import { useApiStore } from '@ui-kit/shared/useApiStore'
import { useOneWayMarketNames } from './chain-query'

export const useOneWayMarketMapping = (params: ChainParams<ChainId>) => {
const { chainId } = params
const { data: marketNames, ...rest } = useOneWayMarketNames(params)
export const useOneWayMarketMapping = ({ chainId }: ChainParams<ChainId>) => {
const { data: marketData, ...rest } = useOneWayMarketNames({ chainId })
const api = useApiStore((state) => state.lending)
const isLoadingApi = useApiStore((state) => state.isLoadingLending)
const apiChainId = api?.chainId
const data: Record<string, OneWayMarketTemplate> | undefined = useMemo(
() =>
marketNames && api && chainId == apiChainId && !isLoadingApi
marketData && api && chainId == apiChainId && !isLoadingApi
? Object.fromEntries(
marketNames
marketData.markets
.filter((marketName) => !networks[chainId!].hideMarketsInUI[marketName])
.map((name) => [name, api.getOneWayMarket(name)] as const)
.flatMap(([name, market]) => [
Expand All @@ -25,7 +24,7 @@ export const useOneWayMarketMapping = (params: ChainParams<ChainId>) => {
]),
)
: undefined,
[api, apiChainId, chainId, marketNames, isLoadingApi],
[api, apiChainId, chainId, isLoadingApi, marketData],
)
return { data, ...rest }
}
Expand Down
7 changes: 5 additions & 2 deletions apps/main/src/lend/entities/chain/chain-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { apiValidationGroup, chainValidationGroup } from './validation'

export const { useQuery: useOneWayMarketNames, prefetchQuery: prefetchMarkets } = queryFactory({
queryKey: ({ chainId }: ChainParams) => ['chain', { chainId }, 'markets'] as const,
queryFn: async (chainId: ChainQuery<ChainId>): Promise<string[]> => {
queryFn: async (chainId: ChainQuery<ChainId>) => {
const useAPI = chainId.chainId !== 146 // disable API for sonic
const api = useApiStore.getState().lending!
await api.oneWayfactory.fetchMarkets(useAPI)
return api.oneWayfactory.getMarketList()
const updatedAt = new Date() //
// Add date to the result so it is not cached by tanstack. Names may not have changed, but individual markets might
// The right thing to do is to return the markets data instead of the names, but that leaves curveJS in invalid state
return { updatedAt, markets: api.oneWayfactory.getMarketList() }
},
staleTime: '5m',
refetchInterval: '1m',
Expand Down