2022-09-02 20:53:00 +00:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
import {
|
|
|
|
addDecimalsFormatNumber,
|
|
|
|
useDataProvider,
|
|
|
|
} from '@vegaprotocol/react-helpers';
|
2022-09-09 15:14:48 +00:00
|
|
|
import type { AccountFieldsFragment } from './__generated__/Accounts';
|
2022-09-02 20:53:00 +00:00
|
|
|
|
|
|
|
import { accountsDataProvider } from './accounts-data-provider';
|
|
|
|
|
|
|
|
interface AssetBalanceProps {
|
|
|
|
partyId: string;
|
|
|
|
assetSymbol: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const AssetBalance = ({ partyId, assetSymbol }: AssetBalanceProps) => {
|
|
|
|
const variables = useMemo(() => ({ partyId }), [partyId]);
|
|
|
|
const { data } = useDataProvider<
|
2022-09-09 15:14:48 +00:00
|
|
|
AccountFieldsFragment[],
|
|
|
|
AccountFieldsFragment
|
2022-09-02 20:53:00 +00:00
|
|
|
>({
|
|
|
|
dataProvider: accountsDataProvider,
|
|
|
|
variables,
|
|
|
|
});
|
|
|
|
if (data && data.length) {
|
|
|
|
const totalBalance = data.reduce((a, c) => {
|
|
|
|
if (c.asset.symbol === assetSymbol) {
|
|
|
|
return a + BigInt(c.balance);
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}, BigInt(0));
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
{addDecimalsFormatNumber(
|
|
|
|
totalBalance.toString(),
|
|
|
|
data[0].asset.decimals
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|