Skip to content

Commit

Permalink
perf: Fetch vaults by chainid instead of per gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
memoyil committed Nov 4, 2024
1 parent 6f45e7a commit fb3f96f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
25 changes: 23 additions & 2 deletions apps/web/src/views/GaugesVoting/hooks/useGaugesFilter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chainNames } from '@pancakeswap/chains'
import { ChainId, chainNames } from '@pancakeswap/chains'
import { GAUGES_SUPPORTED_CHAIN_IDS, GAUGE_TYPE_NAMES, Gauge, GaugeType } from '@pancakeswap/gauges'
import { FeeAmount } from '@pancakeswap/v3-sdk'
import {
Expand All @@ -11,6 +11,8 @@ import {
} from 'nuqs'
import { useCallback, useEffect, useState } from 'react'
import { useDebounce } from '@pancakeswap/hooks'
import { fetchPositionManager, PCSDuoTokenVaultConfig } from '@pancakeswap/position-managers'
import fromPairs from 'lodash/fromPairs'
import { Filter, FilterValue, Gauges, OptionsType, SortOptions } from '../components/GaugesFilter'
import { getPositionManagerName } from '../utils'

Expand Down Expand Up @@ -162,10 +164,29 @@ const useFilteredGauges = ({ filter, fullGauges, searchText, sort, setSort }) =>
// Asynchronous search based on searchText
if (searchText?.length > 0) {
try {
const positionManagerPairs: Partial<Record<ChainId, PCSDuoTokenVaultConfig[]>> = fromPairs(
await Promise.all(
results
.reduce((acc, gauge) => {
if (!acc.includes(gauge.chainId)) {
acc.push(gauge.chainId)
}
return acc
}, [])
.map(async (chainId) => {
const positionManagerName = await fetchPositionManager(chainId, signal)
return [chainId, positionManagerName]
}),
),
)
const updatedResults = await Promise.all(
results.map(async (gauge) => {
try {
const positionManagerName = await getPositionManagerName(gauge, signal)
const positionManagerName = await getPositionManagerName(
gauge,
positionManagerPairs?.[gauge.chainId] ?? undefined,
signal,
)
const isMatch = [
// search by pairName or tokenName
gauge.pairName.toLowerCase(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getPositionManagerName } from 'views/GaugesVoting/utils'
export const usePositionManagerName = (data: Gauge) => {
const { data: managerName } = useQuery({
queryKey: ['position-manager-name'],
queryFn: async ({ signal }) => getPositionManagerName(data, signal),
queryFn: async ({ signal }) => getPositionManagerName(data, undefined, signal),
enabled: Boolean(data),
refetchOnWindowFocus: false,
refetchOnReconnect: false,
Expand Down
13 changes: 10 additions & 3 deletions apps/web/src/views/GaugesVoting/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ export const getGaugeHash = (gaugeAddress: Address = zeroAddress, chainId: numbe
return keccak256(encodePacked(['address', 'uint256'], [gaugeAddress, BigInt(chainId || 0)]))
}

export const getPositionManagerName = async (gauge: Gauge, signal?: AbortSignal): Promise<string> => {
export const getPositionManagerName = async (
gauge: Gauge,
vaults?: PCSDuoTokenVaultConfig[],
signal?: AbortSignal,
): Promise<string> => {
if (gauge.type !== GaugeType.ALM) return ''

const vaults: PCSDuoTokenVaultConfig[] = await fetchPositionManager(gauge.chainId, signal)
const matchedVault = vaults.find((v) => v.vaultAddress === gauge.address)
let _vaults = vaults
if (!vaults) {
_vaults = await fetchPositionManager(gauge.chainId, signal)
}
const matchedVault = _vaults?.find((v) => v.vaultAddress === gauge.address)

if (!matchedVault) return gauge.managerName ?? ''

Expand Down

0 comments on commit fb3f96f

Please sign in to comment.