diff --git a/packages/shared/lib/participation/staking.ts b/packages/shared/lib/participation/staking.ts index a1f70eae6cc..17e2f2a2083 100644 --- a/packages/shared/lib/participation/staking.ts +++ b/packages/shared/lib/participation/staking.ts @@ -2,7 +2,7 @@ import { localize } from '@core/i18n' import { convertBech32AddressToEd25519Address } from '@lib/ed25519' import { showAppNotification } from '@lib/notifications' import { addError } from 'shared/lib/errors' -import { get } from 'svelte/store' +import { get, writable } from 'svelte/store' import { getDecimalSeparator } from '../currency' import { networkStatus } from '../networkStatus' import { activeProfile, updateProfile } from '../profile' @@ -36,6 +36,8 @@ import { } from './types' import { Platform } from '@lib/platform' +export const haveStakingResultsCached = writable(null) + /** * Determines whether an account is currently being staked or not. * diff --git a/packages/shared/lib/wallet.ts b/packages/shared/lib/wallet.ts index 4cbb53d797d..bf206365db7 100644 --- a/packages/shared/lib/wallet.ts +++ b/packages/shared/lib/wallet.ts @@ -20,6 +20,7 @@ import { didInitialiseMigrationListeners } from './migration' import { buildClientOptions, getDefaultClientOptions } from './network' import { showAppNotification } from './notifications' // PARTICIPATION +import { haveStakingResultsCached } from './participation' import { Platform } from './platform' import { activeProfile, updateProfile } from './profile' import { WALLET, WalletApi } from './shell/walletApi' @@ -104,6 +105,7 @@ export const resetWallet = (): void => { isFirstManualSync.set(true) isBackgroundSyncing.set(false) walletSetupType.set(null) + haveStakingResultsCached.set(null) } // Created to help selectedAccount reactivity. diff --git a/packages/shared/lib/walletApiListeners.ts b/packages/shared/lib/walletApiListeners.ts index 9046285770c..2a1db78097e 100644 --- a/packages/shared/lib/walletApiListeners.ts +++ b/packages/shared/lib/walletApiListeners.ts @@ -250,49 +250,44 @@ export const initialiseListeners = (): void => { onSuccess(response) { const { accounts } = get(wallet) - let completeCount = 0 const totalBalance = { balance: 0, incoming: 0, outgoing: 0, } - const latestAccounts = [] + const updatedAccounts = [] - // 1. Iterate on all accounts; - // 2. Get latest metadata for all accounts (to compute the latest balance overview); - // 3. Only update the account for which the balance change event emitted; - // 4. Update balance overview & accounts - for (const _account of response.payload) { - getAccountMetadataWithCallback(_account.id, (metaErr, meta) => { - if (!metaErr) { - // Compute balance overview for each account - totalBalance.balance += meta.balance - totalBalance.incoming += meta.incoming - totalBalance.outgoing += meta.outgoing - - aggregateAccountActivity(_account) + get(accounts).forEach((account, idx) => { + getAccountMetadataWithCallback(account?.id, (metaErr, meta) => { + if (metaErr) { + return + } - const updatedAccountInfo = formatAccountWithMetadata(_account, meta) + totalBalance.balance += meta.balance + totalBalance.incoming += meta.incoming + totalBalance.outgoing += meta.outgoing - // Keep the messages as is because they get updated through a different event - // Also, we create pairs for internal messages, so best to keep those rather than reimplementing the logic here - latestAccounts.push(updatedAccountInfo) + aggregateAccountActivity(account) + const updatedAccountInfo = formatAccountWithMetadata(account, meta) - completeCount++ + updatedAccounts.push( + account?.index === updatedAccountInfo?.index + ? { ...updatedAccountInfo, alias: account?.alias } + : account + ) - if (completeCount === response.payload.length) { - accounts.update((_accounts) => latestAccounts.sort((a, b) => a.index - b.index)) + if (idx === get(accounts).length - 1) { + accounts.update((_accounts) => updatedAccounts.sort((a, b) => a.index - b.index)) - updateBalanceOverview( - totalBalance.balance, - totalBalance.incoming, - totalBalance.outgoing - ) - } + updateBalanceOverview( + totalBalance.balance, + totalBalance.incoming, + totalBalance.outgoing + ) } }) - } + }) }, onError(response) {}, }) diff --git a/packages/shared/routes/dashboard/Dashboard.svelte b/packages/shared/routes/dashboard/Dashboard.svelte index 11f8e727778..735a1196f6c 100644 --- a/packages/shared/routes/dashboard/Dashboard.svelte +++ b/packages/shared/routes/dashboard/Dashboard.svelte @@ -31,7 +31,7 @@ showAppNotification, } from 'shared/lib/notifications' import { stopParticipationPoll, startParticipationPoll, StakingAirdrop } from 'shared/lib/participation' - import { cacheAllStakingPeriods } from 'shared/lib/participation/staking' + import { cacheAllStakingPeriods, haveStakingResultsCached } from 'shared/lib/participation/staking' import { pendingParticipations, resetPerformingParticipation } from 'shared/lib/participation/stores' import { Platform } from 'shared/lib/platform' import { closePopup, openPopup, popupState } from 'shared/lib/popup' @@ -41,6 +41,7 @@ asyncCreateAccount, asyncSyncAccount, isSyncing, + isFirstSessionSync, setSelectedAccount, STRONGHOLD_PASSWORD_CLEAR_INTERVAL_SECS, wallet, @@ -90,9 +91,13 @@ previousPendingParticipationsLength = participations?.length ?? 0 }) - $: if (!$isSyncing && $accountsLoaded) { - cacheAllStakingPeriods(StakingAirdrop.Shimmer) - cacheAllStakingPeriods(StakingAirdrop.Assembly) + $: if (!$isSyncing && !$isFirstSessionSync && $accountsLoaded) { + Promise.all([ + cacheAllStakingPeriods(StakingAirdrop.Shimmer), + cacheAllStakingPeriods(StakingAirdrop.Assembly), + ]).then(() => { + haveStakingResultsCached.set(true) + }) } const viewableAccounts: Readable = derived( diff --git a/packages/shared/routes/dashboard/wallet/Wallet.svelte b/packages/shared/routes/dashboard/wallet/Wallet.svelte index 8c4da8caebe..eaa5a723d2c 100644 --- a/packages/shared/routes/dashboard/wallet/Wallet.svelte +++ b/packages/shared/routes/dashboard/wallet/Wallet.svelte @@ -10,6 +10,7 @@ import { displayNotificationForLedgerProfile, promptUserToConnectLedger } from 'shared/lib/ledger' import { addProfileCurrencyPriceData } from 'shared/lib/market' import { showAppNotification } from 'shared/lib/notifications' + import { haveStakingResultsCached } from 'shared/lib/participation/staking' import { closePopup, openPopup } from 'shared/lib/popup' import { activeProfile, @@ -388,7 +389,9 @@ {/if} {#if $accountRoute === AccountRoute.Init} - + {#key $haveStakingResultsCached} + + {/key} {:else if $accountRoute === AccountRoute.Send} {:else if $accountRoute === AccountRoute.Receive}