chore(trading): back merge hotfixes (#6099)

Co-authored-by: Art <artur@vegaprotocol.io>
This commit is contained in:
Matthew Russell 2024-03-26 09:59:13 +00:00 committed by GitHub
parent ed9cd3e0dd
commit cd4b0e508e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 84 deletions

View File

@ -1,42 +1,25 @@
import { useCallback, useMemo, useState } from 'react';
import { useVegaWallet } from '@vegaprotocol/wallet-react'; import { useVegaWallet } from '@vegaprotocol/wallet-react';
import { useDataProvider } from '@vegaprotocol/data-provider'; import { useDataProvider } from '@vegaprotocol/data-provider';
import { accountsDataProvider } from './accounts-data-provider'; import { accountsDataProvider } from './accounts-data-provider';
import type { Account } from './accounts-data-provider';
import { getSettlementAccount } from './get-settlement-account'; import { getSettlementAccount } from './get-settlement-account';
export const useAccountBalance = (assetId?: string) => { export const useAccountBalance = (assetId?: string) => {
const { pubKey } = useVegaWallet(); const { pubKey } = useVegaWallet();
const [accountBalance, setAccountBalance] = useState<string>('');
const [accountDecimals, setAccountDecimals] = useState<number | null>(null); const { data, loading, error } = useDataProvider({
const variables = useMemo(() => {
return { partyId: pubKey || '' };
}, [pubKey]);
const update = useCallback(
({ data }: { data: Account[] | null }) => {
const account = assetId
? getSettlementAccount({ accounts: data, assetId })
: undefined;
setAccountBalance(account?.balance || '');
setAccountDecimals(account?.asset.decimals || null);
return true;
},
[assetId]
);
const { loading, error } = useDataProvider({
dataProvider: accountsDataProvider, dataProvider: accountsDataProvider,
variables, variables: { partyId: pubKey || '' },
skip: !pubKey || !assetId, skip: !pubKey || !assetId,
update,
}); });
return useMemo( const account = assetId
() => ({ ? getSettlementAccount({ accounts: data, assetId })
accountBalance: pubKey ? accountBalance : '', : undefined;
accountDecimals: pubKey ? accountDecimals : null,
loading, return {
error, accountBalance: account ? account.balance : '',
}), accountDecimals: account ? account.asset.decimals : null,
[accountBalance, accountDecimals, pubKey, loading, error] loading,
); error,
};
}; };

View File

@ -1,68 +1,41 @@
import { useCallback, useMemo, useState } from 'react';
import { useVegaWallet } from '@vegaprotocol/wallet-react'; import { useVegaWallet } from '@vegaprotocol/wallet-react';
import { useDataProvider } from '@vegaprotocol/data-provider'; import { useDataProvider } from '@vegaprotocol/data-provider';
import { accountsDataProvider } from './accounts-data-provider'; import { accountsDataProvider } from './accounts-data-provider';
import type { Account } from './accounts-data-provider';
import { AccountType } from '@vegaprotocol/types'; import { AccountType } from '@vegaprotocol/types';
export const useMarginAccountBalance = (marketId: string) => { export const useMarginAccountBalance = (marketId: string) => {
const { pubKey } = useVegaWallet(); const { pubKey } = useVegaWallet();
const [marginAccountBalance, setMarginAccountBalance] = useState<string>('');
const [orderMarginAccountBalance, setOrderMarginAccountBalance] =
useState<string>('');
const [accountDecimals, setAccountDecimals] = useState<number | null>(null);
const update = useCallback(
({ data }: { data: Account[] | null }) => {
const marginAccount = data?.find((account) => {
return (
account.market?.id === marketId &&
account.type === AccountType.ACCOUNT_TYPE_MARGIN
);
});
const orderMarginAccount = data?.find((account) => {
return (
account.market?.id === marketId &&
account.type === AccountType.ACCOUNT_TYPE_ORDER_MARGIN
);
});
if (marginAccount?.balance) {
setMarginAccountBalance(marginAccount?.balance || '');
}
if (orderMarginAccount?.balance) {
setOrderMarginAccountBalance(orderMarginAccount?.balance || '');
}
const decimals = const { data, loading, error } = useDataProvider({
orderMarginAccount?.asset.decimals || marginAccount?.asset.decimals;
if (decimals) {
setAccountDecimals(decimals);
}
return true;
},
[marketId]
);
const { loading, error } = useDataProvider({
dataProvider: accountsDataProvider, dataProvider: accountsDataProvider,
variables: { partyId: pubKey || '' }, variables: { partyId: pubKey || '' },
skip: !pubKey || !marketId, skip: !pubKey || !marketId,
update,
}); });
return useMemo( const marginAccount = data?.find((account) => {
() => ({ return (
marginAccountBalance: pubKey ? marginAccountBalance : '', account.market?.id === marketId &&
orderMarginAccountBalance: pubKey ? orderMarginAccountBalance : '', account.type === AccountType.ACCOUNT_TYPE_MARGIN
accountDecimals: pubKey ? accountDecimals : null, );
loading, });
error,
}), const orderMarginAccount = data?.find((account) => {
[ return (
marginAccountBalance, account.market?.id === marketId &&
orderMarginAccountBalance, account.type === AccountType.ACCOUNT_TYPE_ORDER_MARGIN
accountDecimals, );
pubKey, });
loading,
error, const decimals =
] orderMarginAccount?.asset.decimals || marginAccount?.asset.decimals;
);
return {
marginAccountBalance: marginAccount ? marginAccount.balance : '',
orderMarginAccountBalance: orderMarginAccount
? orderMarginAccount.balance
: '',
accountDecimals: pubKey ? decimals : null,
loading,
error,
};
}; };