From fb3f96fe805096fda4e0611e0fabbba43e01db12 Mon Sep 17 00:00:00 2001 From: memoyil <2213635+memoyil@users.noreply.github.com> Date: Mon, 4 Nov 2024 09:55:54 +0100 Subject: [PATCH] perf: Fetch vaults by chainid instead of per gauge --- .../GaugesVoting/hooks/useGaugesFilter.ts | 25 +++++++++++++++++-- .../hooks/usePositionManagerName.ts | 2 +- apps/web/src/views/GaugesVoting/utils.ts | 13 +++++++--- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/apps/web/src/views/GaugesVoting/hooks/useGaugesFilter.ts b/apps/web/src/views/GaugesVoting/hooks/useGaugesFilter.ts index be457ebcbc359..8dbebd4b73461 100644 --- a/apps/web/src/views/GaugesVoting/hooks/useGaugesFilter.ts +++ b/apps/web/src/views/GaugesVoting/hooks/useGaugesFilter.ts @@ -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 { @@ -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' @@ -162,10 +164,29 @@ const useFilteredGauges = ({ filter, fullGauges, searchText, sort, setSort }) => // Asynchronous search based on searchText if (searchText?.length > 0) { try { + const positionManagerPairs: Partial> = 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(), diff --git a/apps/web/src/views/GaugesVoting/hooks/usePositionManagerName.ts b/apps/web/src/views/GaugesVoting/hooks/usePositionManagerName.ts index d5d4fbcd914e8..7687aac9080a4 100644 --- a/apps/web/src/views/GaugesVoting/hooks/usePositionManagerName.ts +++ b/apps/web/src/views/GaugesVoting/hooks/usePositionManagerName.ts @@ -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, diff --git a/apps/web/src/views/GaugesVoting/utils.ts b/apps/web/src/views/GaugesVoting/utils.ts index 583de84dfe905..586e48fc088f6 100644 --- a/apps/web/src/views/GaugesVoting/utils.ts +++ b/apps/web/src/views/GaugesVoting/utils.ts @@ -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 => { +export const getPositionManagerName = async ( + gauge: Gauge, + vaults?: PCSDuoTokenVaultConfig[], + signal?: AbortSignal, +): Promise => { 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 ?? ''