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

fix: force refresh assets on first login #5252

Merged
Expand Up @@ -24,6 +24,7 @@ import {
activeProfile,
incrementLoginProgress,
resetLoginProgress,
saveProfile,
setTimeStrongholdLastUnlocked,
} from '../../stores'
import { isLedgerProfile } from '../../utils'
Expand Down Expand Up @@ -82,7 +83,12 @@ export async function login(loginOptions?: ILoginOptions): Promise<void> {

// Step 5: load assets
incrementLoginProgress()
await refreshAccountAssetsForActiveProfile()
await refreshAccountAssetsForActiveProfile(
_activeProfile.forceAssetRefresh,
_activeProfile.forceAssetRefresh
)
_activeProfile.forceAssetRefresh = false
saveProfile(_activeProfile)
await loadNftsForActiveProfile()

// Step 6: generate and store activities for all accounts
Expand Down
Expand Up @@ -16,6 +16,7 @@ export function saveActiveProfile(): void {
settings: _activeProfile?.settings,
isDeveloperProfile: _activeProfile?.isDeveloperProfile,
clientOptions: _activeProfile?.clientOptions,
forceAssetRefresh: _activeProfile?.forceAssetRefresh,
...(_activeProfile?.hasVisitedDashboard && { hasVisitedDashboard: _activeProfile?.hasVisitedDashboard }),
...(_activeProfile?.lastUsedAccountIndex && { lastUsedAccountIndex: _activeProfile?.lastUsedAccountIndex }),
...(_activeProfile?.accountMetadata && { accountMetadata: _activeProfile?.accountMetadata }),
Expand Down
Expand Up @@ -9,40 +9,38 @@ import { currentProfileVersion, profiles, saveProfile } from '../../stores'
*/

export function checkAndMigrateProfiles(): void {
const shouldMigratePersistedProfiles = (get(currentProfileVersion) ?? -1) < PROFILE_VERSION
const shouldMigratePersistedProfiles = (get(currentProfileVersion) ?? 3) < PROFILE_VERSION
if (shouldMigratePersistedProfiles) {
migratePersistedProfiles()
currentProfileVersion.set(PROFILE_VERSION)
migrateEachVersion()
}
}

function migratePersistedProfiles(): void {
const _profiles = get(profiles)
for (const profile of _profiles) {
migratePersistedProfile(profile)
function migrateEachVersion(): void {
let migrationVersion = get(currentProfileVersion)
for (migrationVersion; migrationVersion < PROFILE_VERSION; migrationVersion++) {
migratePersistedProfile(migrationVersion)
currentProfileVersion.set(migrationVersion + 1)
}
}

function migratePersistedProfile(persistedProfile: IPersistedProfile): void {
let migratedPersistedProfile = persistedProfile
let migrationVersion = get(currentProfileVersion)
for (migrationVersion; migrationVersion < PROFILE_VERSION; migrationVersion++) {
migratedPersistedProfile =
persistedProfileMigrationsMap?.[migrationVersion]?.(migratedPersistedProfile) ?? migratedPersistedProfile
function migratePersistedProfile(migrationVersion): void {
const _profiles = get(profiles)
for (const profile of _profiles) {
persistedProfileMigrationsMap?.[migrationVersion]?.(profile)
}
saveProfile(migratedPersistedProfile)
}

const persistedProfileMigrationsMap: Record<number, (existingProfile: unknown) => IPersistedProfile> = {
const persistedProfileMigrationsMap: Record<number, (existingProfile: unknown) => void> = {
/**
* NOTE: 0-2 are missing here because we wrote this functionality,
* when the profile version was already 3.
*/
3: persistedProfileMigrationToV4,
4: persistedProfileMigrationToV5,
5: persistedProfileMigrationToV6,
}

function persistedProfileMigrationToV4(existingProfile: unknown): IPersistedProfile {
function persistedProfileMigrationToV4(existingProfile: unknown): void {
const newProfile = {}

const keysToKeep = [
Expand All @@ -65,10 +63,10 @@ function persistedProfileMigrationToV4(existingProfile: unknown): IPersistedProf
newProfile[key] = existingValue
})

return newProfile as IPersistedProfile
saveProfile(newProfile as IPersistedProfile)
}

function persistedProfileMigrationToV5(existingProfile: unknown): IPersistedProfile {
function persistedProfileMigrationToV5(existingProfile: unknown): void {
interface IOldPersistedProfile {
settings: {
currency: unknown
Expand All @@ -81,5 +79,10 @@ function persistedProfileMigrationToV5(existingProfile: unknown): IPersistedProf
const newProfile = oldProfile as unknown as IPersistedProfile
newProfile.settings.marketCurrency = DEFAULT_PERSISTED_PROFILE_OBJECT.settings?.marketCurrency

return newProfile
saveProfile(newProfile)
}

function persistedProfileMigrationToV6(existingProfile: IPersistedProfile): void {
existingProfile.forceAssetRefresh = true
saveProfile(existingProfile)
}
Expand Up @@ -17,5 +17,6 @@ export const DEFAULT_PERSISTED_PROFILE_OBJECT: IPersistedProfile = {
},
accountMetadata: [],
isDeveloperProfile: false,
forceAssetRefresh: false,
clientOptions: {},
}
@@ -1 +1 @@
export const PROFILE_VERSION = 5
export const PROFILE_VERSION = 6
Expand Up @@ -17,4 +17,5 @@ export interface IPersistedProfile {
hasVisitedDashboard?: boolean
lastUsedAccountIndex?: number
clientOptions: IClientOptions
forceAssetRefresh: boolean
}
@@ -1,12 +1,26 @@
import { BASE_TOKEN, COIN_TYPE } from '@core/network'
import { activeAccounts, activeProfile } from '@core/profile'
import { get } from 'svelte/store'
import { clearPersistedAssetForActiveProfile, addPersistedAsset } from '../stores/persisted-assets.store'
import {
clearPersistedAssetForActiveProfile,
addPersistedAsset,
persistedAssets,
} from '../stores/persisted-assets.store'
import { getOrRequestAssetFromPersistedAssets } from '../actions'
import { VerifiedStatus } from '../enums'
import { IPersistedAsset } from '../interfaces'

export async function refreshAccountAssetsForActiveProfile(clearPersistedAssets = false): Promise<void> {
export async function refreshAccountAssetsForActiveProfile(
clearPersistedAssets = false,
keepVerificationStatus = false
): Promise<void> {
const storedVerificationStates = {}
if (keepVerificationStatus) {
const assets = get(persistedAssets)?.[get(activeProfile)?.id] ?? {}
for (const [id, asset] of Object.entries(assets)) {
storedVerificationStates[id] = asset.verification
}
}
clearPersistedAssets && clearPersistedAssetForActiveProfile()

const networkProtocol = get(activeProfile)?.networkProtocol
Expand All @@ -22,20 +36,24 @@ export async function refreshAccountAssetsForActiveProfile(clearPersistedAssets
verification: { verified: true, status: VerifiedStatus.Official },
}

const persistedAssets: IPersistedAsset[] = []
const assets: IPersistedAsset[] = []
const accounts = get(activeAccounts)
for (const account of accounts) {
const tokens = account?.balances?.nativeTokens ?? []
for (const token of tokens) {
try {
const persistedAsset = await getOrRequestAssetFromPersistedAssets(token.tokenId)
if (persistedAsset) {
persistedAssets.push(persistedAsset)
if (keepVerificationStatus) {
const verificationStatus = storedVerificationStates[persistedAsset.id]
persistedAsset.verification = verificationStatus
}
assets.push(persistedAsset)
}
} catch (err) {
console.error(err)
}
}
}
addPersistedAsset(persistedBaseCoin, ...persistedAssets)
addPersistedAsset(persistedBaseCoin, ...assets)
}