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: improve balance finder #7582

Merged
merged 10 commits into from
Oct 19, 2023
66 changes: 52 additions & 14 deletions packages/desktop/components/popups/BalanceFinderPopup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Icon as IconEnum } from '@auxiliary/icon'
import { showAppNotification } from '@auxiliary/notification'
import { PopupId, closePopup, openPopup } from '@auxiliary/popup'
import { findBalances, sumBalanceForAccounts, SearchAlgorithmType } from '@core/account'
import { findBalances, sumBalanceForAccounts, SearchAlgorithmType, selectedAccountIndex } from '@core/account'
import { localize } from '@core/i18n'
import { displayNotificationForLedgerProfile, ledgerNanoStatus } from '@core/ledger'
import { NetworkId } from '@core/network'
Expand All @@ -21,7 +21,18 @@
generateAndStoreActivitiesForAllAccounts,
refreshAccountAssetsForActiveProfile,
} from '@core/wallet'
import { Button, FontWeight, KeyValueBox, Text, TextHint, TextType, Icon, Tile, Spinner } from 'shared/components'
import {
Button,
FontWeight,
KeyValueBox,
Text,
TextHint,
TextType,
Icon,
Tile,
Spinner,
Checkbox,
} from 'shared/components'
import { TextHintVariant } from 'shared/components/enums'
import { onDestroy } from 'svelte'
import VirtualList from '@sveltejs/svelte-virtual-list'
Expand All @@ -33,20 +44,29 @@
export let consolidateAccountsOnLoad = false
export let title: string
export let body: string

const { isStrongholdLocked, network } = $activeProfile
const searchAlgorithm: SearchAlgorithmType =
network.id === NetworkId.Iota || NetworkId.IotaAlphanet ? SearchAlgorithmType.IDS : SearchAlgorithmType.BFS
export let searchInCurrentWallet: boolean = false
export let shouldInitSearch: boolean = false

let error = ''
let isBusy = false

const { isStrongholdLocked, network } = $activeProfile

$: isTransferring = $visibleActiveAccounts.some(
(account) => account.hasConsolidatingOutputsTransactionInProgress || account.isTransferring
)
$: visibleWalletList = searchInCurrentWallet
? $visibleActiveAccounts?.filter((account) => account.index === $selectedAccountIndex)
: $visibleActiveAccounts
$: searchForBalancesOnLoad && !$isStrongholdLocked && onFindBalancesClick()
$: consolidateAccountsOnLoad && !$isStrongholdLocked && onConsolidateAccountsClick()
$: totalBalance = sumBalanceForAccounts($visibleActiveAccounts)
$: searchInCurrentWallet, (shouldInitSearch = true)
$: searchAlgorithm = searchInCurrentWallet
? SearchAlgorithmType.DFS
: network.id === NetworkId.Iota || NetworkId.IotaAlphanet
? SearchAlgorithmType.IDS
: SearchAlgorithmType.BFS

// Button click handlers
async function onFindBalancesClick(): Promise<void> {
Expand All @@ -73,9 +93,10 @@

// Actions
async function findProfileBalances(): Promise<void> {
await findBalances(searchAlgorithm, !hasUsedBalanceFinder)
await findBalances(searchAlgorithm, !hasUsedBalanceFinder || shouldInitSearch)
await loadAccounts()
hasUsedBalanceFinder = true
shouldInitSearch = false
}

async function consolidateProfileAccounts(): Promise<void> {
Expand Down Expand Up @@ -127,13 +148,22 @@
hasUsedBalanceFinder,
title,
body,
searchInCurrentWallet,
shouldInitSearch,
},
})
},
onCancelled: function () {
openPopup({
id: PopupId.BalanceFinder,
props: { hasUsedBalanceFinder, title, body, showConsolidation },
props: {
hasUsedBalanceFinder,
title,
body,
showConsolidation,
searchInCurrentWallet,
shouldInitSearch,
},
})
},
},
Expand All @@ -156,7 +186,7 @@
<Text type={TextType.p} color="gray-600" fontSize="15" lineHeight="5">{body}</Text>

<div class="w-full flex-col space-y-2 balance-overview-wrapper">
<VirtualList items={$visibleActiveAccounts} let:item>
<VirtualList items={visibleWalletList} let:item>
<div class="mb-2">
<Tile classes="flex justify-between items-center hover:bg-gray-100 dark:hover:bg-gray-1000">
<div class="flex items-center space-x-2">
Expand All @@ -172,11 +202,19 @@
</Tile>
</div>
</VirtualList>
<KeyValueBox
keyText={localize('popups.balanceFinder.totalAddressBalance')}
valueText={formatTokenAmountBestMatch(totalBalance, getBaseToken())}
/>
{#if !searchInCurrentWallet}
<KeyValueBox
keyText={localize('popups.balanceFinder.totalAddressBalance')}
valueText={formatTokenAmountBestMatch(totalBalance, getBaseToken())}
/>
{/if}
</div>
<Checkbox
label={localize('popups.balanceFinder.currentWallet')}
checked={searchInCurrentWallet}
onClick={() => (searchInCurrentWallet = !searchInCurrentWallet)}
disabled={isBusy}
/>

{#if hasUsedBalanceFinder}
<TextHint
Expand Down Expand Up @@ -231,7 +269,7 @@
overflow-y: scroll;
padding-right: 1.5rem !important;
min-height: 100px;
max-height: 300px;
max-height: 250px;
}
.balance-overview-wrapper :global(svelte-virtual-list-contents) {
margin-right: -1rem !important;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { Button, Text, ButtonSize } from 'shared/components'
import { Button, Text, ButtonSize, TextType } from 'shared/components'
import { localize } from '@core/i18n'
import { openPopup, PopupId } from '@auxiliary/popup'

Expand All @@ -14,8 +14,8 @@
}
</script>

<Text type="h4" classes="mb-3">{localize('views.settings.balanceFinder.title')}</Text>
<Text type="p" secondary classes="mb-5">{localize('views.settings.balanceFinder.description')}</Text>
<Text type={TextType.h4} classes="mb-3">{localize('views.settings.balanceFinder.title')}</Text>
<Text type={TextType.p} secondary classes="mb-5">{localize('views.settings.balanceFinder.description')}</Text>
<div class="flex flex-row items-center">
<Button size={ButtonSize.Medium} inlineStyle="min-width: 156px;" onClick={onBalanceFinderClick}>
{localize('actions.findBalances')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export const BALANCE_FINDER_ACCOUNT_RECOVERY_CONFIGURATION: AccountRecoveryConfi
initialAccountRange: 3,
accountGapLimit: 1,
numberOfRoundsBetweenBreadthSearch: 1,
addressGapLimit: 20,
addressGapLimit: 30,
},
}
3 changes: 2 additions & 1 deletion packages/shared/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,8 @@
"addressesSearched": "Addresses searched",
"addressesFound": "Addresses found",
"totalAddressBalance": "Total balance",
"searchAgainHint": "Is your balance or number of wallets incorrect? Search again until your full balance is shown."
"searchAgainHint": "Is your balance or number of wallets incorrect? Search again until your full balance is shown.",
"currentWallet": "Search only in the current wallet"
},
"balanceOverview": {
"title": "Balance overview",
Expand Down
Loading