Skip to content

Commit

Permalink
feat(tokens): display mainnet tokens total USD (#2271)
Browse files Browse the repository at this point in the history
# Motivation

The goal is to display mainnet tokens total USD balances in
`NetworkSwitscher`.


<img width="324" alt="Screenshot 2024-09-06 at 17 00 56"
src="https://github.com/user-attachments/assets/d4546250-064a-4a7b-9cf6-4cf93d538e81">
  • Loading branch information
DenysKarmazynDFINITY authored Sep 6, 2024
1 parent f1c620a commit 9362690
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/frontend/src/lib/components/networks/MainnetNetwork.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts">
import NetworkComponent from '$lib/components/networks/Network.svelte';
import { enabledMainnetTokensUsdBalancesPerNetwork } from '$lib/derived/tokens.derived';
import type { Network } from '$lib/types/network';
export let network: Network;
let usdBalance: number;
$: usdBalance = $enabledMainnetTokensUsdBalancesPerNetwork[network.id] ?? 0;
</script>

<NetworkComponent {network} {usdBalance} on:icSelected />
3 changes: 2 additions & 1 deletion src/frontend/src/lib/components/networks/Network.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import type { Network, NetworkId } from '$lib/types/network';
export let network: Network;
export let usdBalance: number | undefined = undefined;
let id: NetworkId;
let name: string;
let icon: string | undefined;
$: ({ id, name, icon } = network);
</script>

<NetworkButton {id} {name} {icon} on:icSelected />
<NetworkButton {id} {name} {usdBalance} {icon} on:icSelected />
13 changes: 10 additions & 3 deletions src/frontend/src/lib/components/networks/NetworkButton.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<script lang="ts">
import { IconCheck } from '@dfinity/gix-components';
import { nonNullish } from '@dfinity/utils';
import { createEventDispatcher } from 'svelte';
import { fade } from 'svelte/transition';
import { page } from '$app/stores';
import TextWithLogo from '$lib/components/ui/TextWithLogo.svelte';
import { networkId } from '$lib/derived/network.derived';
import type { NetworkId } from '$lib/types/network';
import { formatUSD } from '$lib/utils/format.utils';
import { gotoReplaceRoot, isRouteTransactions, switchNetwork } from '$lib/utils/nav.utils';
export let id: NetworkId | undefined;
export let name: string;
export let icon: string | undefined;
export let description: string | undefined = undefined;
export let usdBalance: number | undefined = undefined;
const dispatch = createEventDispatcher();
Expand All @@ -27,8 +29,13 @@
};
</script>

<button class="w-full flex justify-between" on:click={onClick}>
<TextWithLogo {name} {description} {icon} logo="start" />
<button class="w-full flex justify-between items-start" on:click={onClick}>
<TextWithLogo
{name}
{icon}
logo="start"
description={nonNullish(usdBalance) ? formatUSD(usdBalance) : undefined}
/>

{#if id === $networkId}
<span in:fade><IconCheck size="20px" /></span>
Expand Down
11 changes: 10 additions & 1 deletion src/frontend/src/lib/components/networks/NetworksSwitcher.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import chainFusion from '$lib/assets/chain_fusion.svg';
import IconChevronDown from '$lib/components/icons/IconChevronDown.svelte';
import IconMorePlain from '$lib/components/icons/IconMorePlain.svelte';
import MainnetNetwork from '$lib/components/networks/MainnetNetwork.svelte';
import Network from '$lib/components/networks/Network.svelte';
import NetworkButton from '$lib/components/networks/NetworkButton.svelte';
import NetworksTestnetsToggle from '$lib/components/networks/NetworksTestnetsToggle.svelte';
import ButtonSwitcher from '$lib/components/ui/ButtonSwitcher.svelte';
import { selectedNetwork } from '$lib/derived/network.derived';
import { networksMainnets, networksTestnets } from '$lib/derived/networks.derived';
import { enabledMainnetTokensUsdBalancesPerNetwork } from '$lib/derived/tokens.derived';
import { i18n } from '$lib/stores/i18n.store';
import { testnetsStore } from '$lib/stores/settings.store';
Expand All @@ -23,6 +25,12 @@
let testnets: boolean;
$: testnets = $testnetsStore?.enabled ?? false;
let mainnetTokensUsdBalance: number;
$: mainnetTokensUsdBalance = $networksMainnets.reduce(
(acc, { id }) => acc + ($enabledMainnetTokensUsdBalancesPerNetwork[id] ?? 0),
0
);
</script>

<ButtonSwitcher
Expand All @@ -40,13 +48,14 @@
id={undefined}
name={$i18n.networks.chain_fusion}
icon={chainFusion}
usdBalance={mainnetTokensUsdBalance}
on:icSelected={close}
/>
</li>

{#each $networksMainnets as network}
<li>
<Network {network} on:icSelected={close} />
<MainnetNetwork {network} on:icSelected={close} />
</li>
{/each}
</ul>
Expand Down

0 comments on commit 9362690

Please sign in to comment.