vega-frontend-monorepo/libs/accounts/src/lib/asset-balance.tsx
botond 7613a9fa9f
Feat/migrate libs (#1266)
* feat: add new generator with config

* feat: split off gql queries to separate files for the new generator

* fix: delete dummy schema

* feat: add generated queries with new codegen

* fix: regenerate from scratch and remove duplicates

* fix: libs and app gen folders

* fix: remove more duplicate queries

* fix: add generated files to be ignored by the formatter

* fix: format

* feat: migrate assets to use new code generator

* fix: generated imports

* fix: lint

* fix: accounts export

* fix: more imports

* fix: add type alias for new type system and regenerate

* fix: format

* fix: export

* fix: format

* fix: delete leftover file from merge

* chore: migrate candles-chart lib

* fix: remove redundant asset files

* chore: migrate accounts

* chore: update glob

* chore: add cleanup to fix-imports to support type migrations for now

* fix: imports

* fix: update market types

* fix: type import paths

* fix: format

* fix: import

* fix: types in e2e tests

* fix: format

* fix: lint
2022-09-09 16:14:48 +01:00

42 lines
1022 B
TypeScript

import { useMemo } from 'react';
import {
addDecimalsFormatNumber,
useDataProvider,
} from '@vegaprotocol/react-helpers';
import type { AccountFieldsFragment } from './__generated__/Accounts';
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<
AccountFieldsFragment[],
AccountFieldsFragment
>({
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;
};