chore(trading): back merge hotfixes (#6099)
Co-authored-by: Art <artur@vegaprotocol.io>
This commit is contained in:
parent
ed9cd3e0dd
commit
cd4b0e508e
@ -1,42 +1,25 @@
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import { useVegaWallet } from '@vegaprotocol/wallet-react';
|
||||
import { useDataProvider } from '@vegaprotocol/data-provider';
|
||||
import { accountsDataProvider } from './accounts-data-provider';
|
||||
import type { Account } from './accounts-data-provider';
|
||||
import { getSettlementAccount } from './get-settlement-account';
|
||||
|
||||
export const useAccountBalance = (assetId?: string) => {
|
||||
const { pubKey } = useVegaWallet();
|
||||
const [accountBalance, setAccountBalance] = useState<string>('');
|
||||
const [accountDecimals, setAccountDecimals] = useState<number | null>(null);
|
||||
const variables = useMemo(() => {
|
||||
return { partyId: pubKey || '' };
|
||||
}, [pubKey]);
|
||||
const update = useCallback(
|
||||
({ data }: { data: Account[] | null }) => {
|
||||
|
||||
const { data, loading, error } = useDataProvider({
|
||||
dataProvider: accountsDataProvider,
|
||||
variables: { partyId: pubKey || '' },
|
||||
skip: !pubKey || !assetId,
|
||||
});
|
||||
|
||||
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,
|
||||
variables,
|
||||
skip: !pubKey || !assetId,
|
||||
update,
|
||||
});
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
accountBalance: pubKey ? accountBalance : '',
|
||||
accountDecimals: pubKey ? accountDecimals : null,
|
||||
return {
|
||||
accountBalance: account ? account.balance : '',
|
||||
accountDecimals: account ? account.asset.decimals : null,
|
||||
loading,
|
||||
error,
|
||||
}),
|
||||
[accountBalance, accountDecimals, pubKey, loading, error]
|
||||
);
|
||||
};
|
||||
};
|
||||
|
@ -1,68 +1,41 @@
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import { useVegaWallet } from '@vegaprotocol/wallet-react';
|
||||
import { useDataProvider } from '@vegaprotocol/data-provider';
|
||||
import { accountsDataProvider } from './accounts-data-provider';
|
||||
import type { Account } from './accounts-data-provider';
|
||||
import { AccountType } from '@vegaprotocol/types';
|
||||
|
||||
export const useMarginAccountBalance = (marketId: string) => {
|
||||
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 { data, loading, error } = useDataProvider({
|
||||
dataProvider: accountsDataProvider,
|
||||
variables: { partyId: pubKey || '' },
|
||||
skip: !pubKey || !marketId,
|
||||
});
|
||||
|
||||
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 =
|
||||
orderMarginAccount?.asset.decimals || marginAccount?.asset.decimals;
|
||||
if (decimals) {
|
||||
setAccountDecimals(decimals);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
[marketId]
|
||||
);
|
||||
const { loading, error } = useDataProvider({
|
||||
dataProvider: accountsDataProvider,
|
||||
variables: { partyId: pubKey || '' },
|
||||
skip: !pubKey || !marketId,
|
||||
update,
|
||||
});
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
marginAccountBalance: pubKey ? marginAccountBalance : '',
|
||||
orderMarginAccountBalance: pubKey ? orderMarginAccountBalance : '',
|
||||
accountDecimals: pubKey ? accountDecimals : null,
|
||||
return {
|
||||
marginAccountBalance: marginAccount ? marginAccount.balance : '',
|
||||
orderMarginAccountBalance: orderMarginAccount
|
||||
? orderMarginAccount.balance
|
||||
: '',
|
||||
accountDecimals: pubKey ? decimals : null,
|
||||
loading,
|
||||
error,
|
||||
}),
|
||||
[
|
||||
marginAccountBalance,
|
||||
orderMarginAccountBalance,
|
||||
accountDecimals,
|
||||
pubKey,
|
||||
loading,
|
||||
error,
|
||||
]
|
||||
);
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user