Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet/wallet-dashboard): show fiat balance #4747

Open
wants to merge 19 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/core/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export * from './useTransactionData';
export * from './useGetStakingValidatorDetails';
export * from './useCursorPagination';
export * from './useTheme';
export * from './useTotalFiatBalance';
export * from './useNFTBasicData';
export * from './useOwnedNFT';
export * from './useNftDetails';
Expand Down
27 changes: 27 additions & 0 deletions apps/core/src/hooks/useTotalFiatBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { useCurrentAccount } from '@iota/dapp-kit';
import { useBalance } from './useBalance';
import { useTokenPrice } from './useTokenPrice';
import { CoinFormat, formatBalance, useCoinMetadata } from './useFormatCoin';
import { IOTA_TYPE_ARG } from '@iota/iota-sdk/utils';

export function useTotalFiatBalance() {
const { data: { price } = {} } = useTokenPrice('iota');
cpl121 marked this conversation as resolved.
Show resolved Hide resolved
const address = useCurrentAccount()?.address;
const { data: coinBalance } = useBalance(address!);
evavirseda marked this conversation as resolved.
Show resolved Hide resolved
const totalBalance = Number(coinBalance?.totalBalance);
evavirseda marked this conversation as resolved.
Show resolved Hide resolved
const queryResult = useCoinMetadata(IOTA_TYPE_ARG);
const iotaToFiat = totalBalance && price ? Number(totalBalance) * Number(price) : 0;
evavirseda marked this conversation as resolved.
Show resolved Hide resolved
const formatted = formatBalance(iotaToFiat, queryResult.data?.decimals ?? 0, CoinFormat.FULL);
return price ? `${coinToFiat(formatted, price)}` : null;
}

function coinToFiat(coinBalance: string, coinPrice: string): string {
const totalBalanceInUsd = Number(coinBalance) * Number(coinPrice);
return Number(totalBalanceInUsd).toLocaleString('en', {
style: 'currency',
currency: 'USD',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could also be a constant, right?

Copy link
Member Author

@VmMad VmMad Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so could the locale, and the style, but maybe a format function is better, no?

});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { useCurrentAccount, useIotaClientContext } from '@iota/dapp-kit';
import { formatAddress, IOTA_TYPE_ARG } from '@iota/iota-sdk/utils';
import { useBalance, useFormatCoin } from '@iota/core';
import { useBalance, useFormatCoin, useTotalFiatBalance } from '@iota/core';
import { Address, Button, ButtonSize, ButtonType, Panel } from '@iota/apps-ui-kit';
import { getNetwork } from '@iota/iota-sdk/client';
import { ReceiveFundsDialog, SendTokenDialog } from '../dialogs';
Expand All @@ -14,6 +14,7 @@ export function AccountBalance() {
const account = useCurrentAccount();
const address = account?.address;
const [isReceiveDialogOpen, setIsReceiveDialogOpen] = useState(false);
const fiatBalance = useTotalFiatBalance();
const { network } = useIotaClientContext();
const { explorer } = getNetwork(network);
const { data: coinBalance, isPending } = useBalance(address!);
Expand Down Expand Up @@ -41,19 +42,26 @@ export function AccountBalance() {
<p>Loading...</p>
) : (
<div className="flex h-full flex-col items-center justify-center gap-y-lg p-lg">
{address && (
<Address
text={formattedAddress}
isCopyable
copyText={address}
isExternal
externalLink={explorerLink}
onCopySuccess={handleOnCopySuccess}
/>
)}
<span className="text-headline-lg text-neutral-10 dark:text-neutral-92">
{formatted} {symbol}
</span>
<div className="flex flex-col items-center gap-y-xs">
{address && (
<div className="-mr-lg">
<Address
text={formattedAddress}
isCopyable
copyText={address}
isExternal
externalLink={explorerLink}
onCopySuccess={handleOnCopySuccess}
/>
</div>
)}
<span className="text-headline-lg text-neutral-10 dark:text-neutral-92">
{formatted} {symbol}
</span>
<span className="text-body-md text-neutral-10 dark:text-neutral-92">
{fiatBalance}
</span>
</div>
<div className="flex w-full max-w-56 gap-xs">
<Button
onClick={openSendTokenDialog}
Expand Down
Loading