From 71fc4a5326aaf2b33ef93b4db94b3c6117cd7f02 Mon Sep 17 00:00:00 2001 From: ChefMomota <98292246+ChefMomota@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:40:01 +0800 Subject: [PATCH] fix: Farm API (#10883) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ## PR-Codex overview This PR focuses on refactoring the fetching logic for `bCakeWrapperAddresses` and improving the usage of the `useV3FarmAPI` hook by utilizing `useMemo` for better performance. ### Detailed summary - In `fetcher.ts`, changed the mapping from `validReserveTokens` to `validLpTokens`. - Directly accessed `tokens.address` instead of using `getV2LiquidityToken`. - In `useV3FarmAPI.ts`, replaced `farms: farms ?? []` with `farms` for cleaner code. - Introduced `useMemo` to memoize `farms` based on `data`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- apps/web/src/hooks/useV3FarmAPI.ts | 7 +++++-- .../src/state/farmsV4/state/accountPositions/fetcher.ts | 5 ++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/apps/web/src/hooks/useV3FarmAPI.ts b/apps/web/src/hooks/useV3FarmAPI.ts index 63119abce3b1d..b4103ab34df2f 100644 --- a/apps/web/src/hooks/useV3FarmAPI.ts +++ b/apps/web/src/hooks/useV3FarmAPI.ts @@ -6,9 +6,10 @@ import { UniversalFarmConfigV3, } from '@pancakeswap/farms' import { useQuery } from '@tanstack/react-query' +import { useMemo } from 'react' export const useV3FarmAPI = (chainId: ChainId) => { - const { data: farms } = useQuery({ + const { data } = useQuery({ queryKey: ['fetch-v3-farm-api'], queryFn: async () => { if (chainId) { @@ -23,7 +24,9 @@ export const useV3FarmAPI = (chainId: ChainId) => { refetchOnMount: false, }) + const farms = useMemo(() => data ?? [], [data]) + return { - farms: farms ?? [], + farms, } } diff --git a/apps/web/src/state/farmsV4/state/accountPositions/fetcher.ts b/apps/web/src/state/farmsV4/state/accountPositions/fetcher.ts index 46d74191d3603..394bc57a80b47 100644 --- a/apps/web/src/state/farmsV4/state/accountPositions/fetcher.ts +++ b/apps/web/src/state/farmsV4/state/accountPositions/fetcher.ts @@ -139,9 +139,8 @@ export const getAccountV2LpDetails = async ( const validLpTokens = lpTokens.filter((token) => token.chainId === chainId) const bCakeWrapperAddresses = await Promise.all( - validReserveTokens.map(async (tokens) => { - const lpAddress = getV2LiquidityToken(tokens).address - const bCakeWrapperAddress = await getBCakeWrapperAddress(lpAddress, chainId) + validLpTokens.map(async (tokens) => { + const bCakeWrapperAddress = await getBCakeWrapperAddress(tokens.address, chainId) return bCakeWrapperAddress }), )