fix: dont use current value in update callback for estimate queries (#2721)
This commit is contained in:
parent
68446ccf65
commit
a016feab2a
@ -9,26 +9,23 @@ export const useMarketAccountBalance = (marketId: string) => {
|
|||||||
const { pubKey } = useVegaWallet();
|
const { pubKey } = useVegaWallet();
|
||||||
const [accountBalance, setAccountBalance] = useState<string>('');
|
const [accountBalance, setAccountBalance] = useState<string>('');
|
||||||
const [accountDecimals, setAccountDecimals] = useState<number | null>(null);
|
const [accountDecimals, setAccountDecimals] = useState<number | null>(null);
|
||||||
const variables = useMemo(() => {
|
|
||||||
return { partyId: pubKey || '' };
|
|
||||||
}, [pubKey]);
|
|
||||||
const update = useCallback(
|
const update = useCallback(
|
||||||
({ data }: { data: Account[] | null }) => {
|
({ data }: { data: Account[] | null }) => {
|
||||||
const account = getMarketAccount({ accounts: data, marketId });
|
const account = getMarketAccount({ accounts: data, marketId });
|
||||||
if (accountBalance !== account?.balance) {
|
if (account?.balance) {
|
||||||
setAccountBalance(account?.balance || '');
|
setAccountBalance(account?.balance || '');
|
||||||
}
|
}
|
||||||
if (accountDecimals !== account?.asset.decimals) {
|
if (account?.asset.decimals) {
|
||||||
setAccountDecimals(account?.asset.decimals || null);
|
setAccountDecimals(account?.asset.decimals || null);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
[accountBalance, accountDecimals, marketId]
|
[marketId]
|
||||||
);
|
);
|
||||||
|
|
||||||
useDataProvider({
|
useDataProvider({
|
||||||
dataProvider: accountsDataProvider,
|
dataProvider: accountsDataProvider,
|
||||||
variables,
|
variables: { partyId: pubKey || '' },
|
||||||
skip: !pubKey || !marketId,
|
skip: !pubKey || !marketId,
|
||||||
update,
|
update,
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useMemo, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
import { useVegaWallet } from '@vegaprotocol/wallet';
|
import { useVegaWallet } from '@vegaprotocol/wallet';
|
||||||
import { useDataProvider } from '@vegaprotocol/react-helpers';
|
import { useDataProvider } from '@vegaprotocol/react-helpers';
|
||||||
import { marginsDataProvider } from './margin-data-provider';
|
import { marginsDataProvider } from './margin-data-provider';
|
||||||
@ -22,19 +22,16 @@ const getMarketMarginPosition = ({
|
|||||||
export const useMarketMargin = (marketId: string) => {
|
export const useMarketMargin = (marketId: string) => {
|
||||||
const { pubKey } = useVegaWallet();
|
const { pubKey } = useVegaWallet();
|
||||||
const [marginLevel, setMarginLevel] = useState<string>('');
|
const [marginLevel, setMarginLevel] = useState<string>('');
|
||||||
const variables = useMemo(() => {
|
|
||||||
return { partyId: pubKey || '' };
|
|
||||||
}, [pubKey]);
|
|
||||||
|
|
||||||
const update = useCallback(
|
const update = useCallback(
|
||||||
({ data }: { data: MarginsQuery['party'] | null }) => {
|
({ data }: { data: MarginsQuery['party'] | null }) => {
|
||||||
const marginMarketPosition = getMarketMarginPosition({ data, marketId });
|
const marginMarketPosition = getMarketMarginPosition({ data, marketId });
|
||||||
if (marginLevel !== marginMarketPosition?.maintenanceLevel) {
|
if (marginMarketPosition?.maintenanceLevel) {
|
||||||
setMarginLevel(marginMarketPosition?.maintenanceLevel || '');
|
setMarginLevel(marginMarketPosition?.maintenanceLevel || '');
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
[marginLevel, setMarginLevel, marketId]
|
[setMarginLevel, marketId]
|
||||||
);
|
);
|
||||||
|
|
||||||
useDataProvider<
|
useDataProvider<
|
||||||
@ -42,7 +39,7 @@ export const useMarketMargin = (marketId: string) => {
|
|||||||
MarginsSubscriptionSubscription['margins']
|
MarginsSubscriptionSubscription['margins']
|
||||||
>({
|
>({
|
||||||
dataProvider: marginsDataProvider,
|
dataProvider: marginsDataProvider,
|
||||||
variables,
|
variables: { partyId: pubKey || '' },
|
||||||
skip: !pubKey || !marketId,
|
skip: !pubKey || !marketId,
|
||||||
update,
|
update,
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useMemo, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
import { useVegaWallet } from '@vegaprotocol/wallet';
|
import { useVegaWallet } from '@vegaprotocol/wallet';
|
||||||
import { positionsDataProvider } from './positions-data-providers';
|
import { positionsDataProvider } from './positions-data-providers';
|
||||||
import { useDataProvider } from '@vegaprotocol/react-helpers';
|
import { useDataProvider } from '@vegaprotocol/react-helpers';
|
||||||
@ -22,18 +22,15 @@ const getMarketPosition = ({
|
|||||||
export const useMarketPositionOpenVolume = (marketId: string) => {
|
export const useMarketPositionOpenVolume = (marketId: string) => {
|
||||||
const { pubKey } = useVegaWallet();
|
const { pubKey } = useVegaWallet();
|
||||||
const [openVolume, setOpenVolume] = useState<string>('');
|
const [openVolume, setOpenVolume] = useState<string>('');
|
||||||
const variables = useMemo(() => {
|
|
||||||
return { partyId: pubKey || '' };
|
|
||||||
}, [pubKey]);
|
|
||||||
const update = useCallback(
|
const update = useCallback(
|
||||||
({ data }: { data: PositionsQuery['party'] | undefined }) => {
|
({ data }: { data: PositionsQuery['party'] | undefined }) => {
|
||||||
const position = getMarketPosition({ data, marketId });
|
const position = getMarketPosition({ data, marketId });
|
||||||
if (openVolume !== position?.openVolume) {
|
if (position?.openVolume) {
|
||||||
setOpenVolume(position?.openVolume || '');
|
setOpenVolume(position?.openVolume || '');
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
[openVolume, setOpenVolume, marketId]
|
[setOpenVolume, marketId]
|
||||||
);
|
);
|
||||||
|
|
||||||
useDataProvider<
|
useDataProvider<
|
||||||
@ -41,7 +38,7 @@ export const useMarketPositionOpenVolume = (marketId: string) => {
|
|||||||
PositionsSubscriptionSubscription['positions']
|
PositionsSubscriptionSubscription['positions']
|
||||||
>({
|
>({
|
||||||
dataProvider: positionsDataProvider,
|
dataProvider: positionsDataProvider,
|
||||||
variables,
|
variables: { partyId: pubKey || '' },
|
||||||
skip: !pubKey || !marketId,
|
skip: !pubKey || !marketId,
|
||||||
update,
|
update,
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user