From a016feab2ab472ba6143b9cba5c2c5c7c585b686 Mon Sep 17 00:00:00 2001 From: Matthew Russell Date: Tue, 24 Jan 2023 13:33:55 -0800 Subject: [PATCH] fix: dont use current value in update callback for estimate queries (#2721) --- libs/accounts/src/lib/use-market-account-balance.tsx | 11 ++++------- libs/positions/src/lib/use-market-margin.tsx | 11 ++++------- .../src/lib/use-market-position-open-volume.tsx | 11 ++++------- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/libs/accounts/src/lib/use-market-account-balance.tsx b/libs/accounts/src/lib/use-market-account-balance.tsx index 99b0e2cd2..97fef2a3d 100644 --- a/libs/accounts/src/lib/use-market-account-balance.tsx +++ b/libs/accounts/src/lib/use-market-account-balance.tsx @@ -9,26 +9,23 @@ export const useMarketAccountBalance = (marketId: string) => { const { pubKey } = useVegaWallet(); const [accountBalance, setAccountBalance] = useState(''); const [accountDecimals, setAccountDecimals] = useState(null); - const variables = useMemo(() => { - return { partyId: pubKey || '' }; - }, [pubKey]); const update = useCallback( ({ data }: { data: Account[] | null }) => { const account = getMarketAccount({ accounts: data, marketId }); - if (accountBalance !== account?.balance) { + if (account?.balance) { setAccountBalance(account?.balance || ''); } - if (accountDecimals !== account?.asset.decimals) { + if (account?.asset.decimals) { setAccountDecimals(account?.asset.decimals || null); } return true; }, - [accountBalance, accountDecimals, marketId] + [marketId] ); useDataProvider({ dataProvider: accountsDataProvider, - variables, + variables: { partyId: pubKey || '' }, skip: !pubKey || !marketId, update, }); diff --git a/libs/positions/src/lib/use-market-margin.tsx b/libs/positions/src/lib/use-market-margin.tsx index d2aeaf300..8452dab60 100644 --- a/libs/positions/src/lib/use-market-margin.tsx +++ b/libs/positions/src/lib/use-market-margin.tsx @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useState } from 'react'; +import { useCallback, useState } from 'react'; import { useVegaWallet } from '@vegaprotocol/wallet'; import { useDataProvider } from '@vegaprotocol/react-helpers'; import { marginsDataProvider } from './margin-data-provider'; @@ -22,19 +22,16 @@ const getMarketMarginPosition = ({ export const useMarketMargin = (marketId: string) => { const { pubKey } = useVegaWallet(); const [marginLevel, setMarginLevel] = useState(''); - const variables = useMemo(() => { - return { partyId: pubKey || '' }; - }, [pubKey]); const update = useCallback( ({ data }: { data: MarginsQuery['party'] | null }) => { const marginMarketPosition = getMarketMarginPosition({ data, marketId }); - if (marginLevel !== marginMarketPosition?.maintenanceLevel) { + if (marginMarketPosition?.maintenanceLevel) { setMarginLevel(marginMarketPosition?.maintenanceLevel || ''); } return true; }, - [marginLevel, setMarginLevel, marketId] + [setMarginLevel, marketId] ); useDataProvider< @@ -42,7 +39,7 @@ export const useMarketMargin = (marketId: string) => { MarginsSubscriptionSubscription['margins'] >({ dataProvider: marginsDataProvider, - variables, + variables: { partyId: pubKey || '' }, skip: !pubKey || !marketId, update, }); diff --git a/libs/positions/src/lib/use-market-position-open-volume.tsx b/libs/positions/src/lib/use-market-position-open-volume.tsx index 449625805..c2764d53a 100644 --- a/libs/positions/src/lib/use-market-position-open-volume.tsx +++ b/libs/positions/src/lib/use-market-position-open-volume.tsx @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useState } from 'react'; +import { useCallback, useState } from 'react'; import { useVegaWallet } from '@vegaprotocol/wallet'; import { positionsDataProvider } from './positions-data-providers'; import { useDataProvider } from '@vegaprotocol/react-helpers'; @@ -22,18 +22,15 @@ const getMarketPosition = ({ export const useMarketPositionOpenVolume = (marketId: string) => { const { pubKey } = useVegaWallet(); const [openVolume, setOpenVolume] = useState(''); - const variables = useMemo(() => { - return { partyId: pubKey || '' }; - }, [pubKey]); const update = useCallback( ({ data }: { data: PositionsQuery['party'] | undefined }) => { const position = getMarketPosition({ data, marketId }); - if (openVolume !== position?.openVolume) { + if (position?.openVolume) { setOpenVolume(position?.openVolume || ''); } return true; }, - [openVolume, setOpenVolume, marketId] + [setOpenVolume, marketId] ); useDataProvider< @@ -41,7 +38,7 @@ export const useMarketPositionOpenVolume = (marketId: string) => { PositionsSubscriptionSubscription['positions'] >({ dataProvider: positionsDataProvider, - variables, + variables: { partyId: pubKey || '' }, skip: !pubKey || !marketId, update, });