diff --git a/background/main.ts b/background/main.ts index aedec6078..c7573e855 100644 --- a/background/main.ts +++ b/background/main.ts @@ -153,7 +153,6 @@ import { selectActivitesHashesForEnrichment } from "./redux-slices/selectors" import { getActivityDetails } from "./redux-slices/utils/activities-utils" import { getRelevantTransactionAddresses } from "./services/enrichment/utils" import { AccountSignerWithId } from "./signing" -import { AnalyticsPreferences } from "./services/preferences/types" import { AnyAssetMetadata, assetAmountToDesiredDecimals, @@ -197,7 +196,7 @@ import { } from "./services/internal-signer" import { getPricePoint, getTokenPrices } from "./lib/prices" import { makeFlashbotsProviderCreator } from "./services/chain/serial-fallback-provider" -import { DismissableItem } from "./services/preferences" +import { AnalyticsPreferences, DismissableItem } from "./services/preferences" import { newPricePoints } from "./redux-slices/prices" // This sanitizer runs on store and action data before serializing for remote diff --git a/background/redux-slices/ui.ts b/background/redux-slices/ui.ts index 2d56c0e17..394a67e8f 100644 --- a/background/redux-slices/ui.ts +++ b/background/redux-slices/ui.ts @@ -4,8 +4,7 @@ import { AddressOnNetwork } from "../accounts" import { ETHEREUM } from "../constants" import { AnalyticsEvent, OneTimeAnalyticsEvent } from "../lib/posthog" import { EVMNetwork } from "../networks" -import { DismissableItem } from "../services/preferences" -import { AnalyticsPreferences } from "../services/preferences/types" +import { AnalyticsPreferences, DismissableItem } from "../services/preferences" import { AccountSignerWithId } from "../signing" import { AccountSignerSettings } from "../ui" import { AccountState, addAddressNetwork } from "./accounts" diff --git a/background/services/preferences/db.ts b/background/services/preferences/db.ts index d0687ffa5..ce12f3877 100644 --- a/background/services/preferences/db.ts +++ b/background/services/preferences/db.ts @@ -6,7 +6,6 @@ import { AddressOnNetwork } from "../../accounts" import DEFAULT_PREFERENCES, { DEFAULT_AUTOLOCK_INTERVAL } from "./defaults" import { AccountSignerSettings } from "../../ui" import { AccountSignerWithId } from "../../signing" -import { AnalyticsPreferences } from "./types" import { NETWORK_BY_CHAIN_ID } from "../../constants" import { UNIXTime } from "../../types" @@ -45,21 +44,25 @@ const getNewUrlsForTokenList = ( return newURLs } -// The idea is to use this interface to describe the data structure stored in indexedDb -// In the future this might also have a runtime type check capability, but it's good enough for now. -// NOTE: Check if can be merged with preferences/types.ts +export type TokenListPreferences = { + autoUpdate: boolean + urls: string[] +} + +export type AnalyticsPreferences = { + isEnabled: boolean + hasDefaultOnBeenTurnedOn: boolean +} + export type Preferences = { id?: number savedAt: number - tokenLists: { autoUpdate: boolean; urls: string[] } + tokenLists: TokenListPreferences currency: FiatCurrency defaultWallet: boolean - currentAddress?: string selectedAccount: AddressOnNetwork - analytics: { - isEnabled: boolean - hasDefaultOnBeenTurnedOn: boolean - } + accountSignersSettings: AccountSignerSettings[] + analytics: AnalyticsPreferences autoLockInterval: UNIXTime } @@ -143,19 +146,21 @@ export class PreferenceDatabase extends Dexie { tx .table("preferences") .toCollection() - .modify((storedPreferences: Preferences) => { - if (storedPreferences.currentAddress) { - // eslint-disable-next-line no-param-reassign - storedPreferences.selectedAccount = { - network: DEFAULT_PREFERENCES.selectedAccount.network, - address: storedPreferences.currentAddress, + .modify( + (storedPreferences: Preferences & { currentAddress?: string }) => { + if (storedPreferences.currentAddress) { + // eslint-disable-next-line no-param-reassign + storedPreferences.selectedAccount = { + network: DEFAULT_PREFERENCES.selectedAccount.network, + address: storedPreferences.currentAddress, + } + } else { + // eslint-disable-next-line no-param-reassign + storedPreferences.selectedAccount = + DEFAULT_PREFERENCES.selectedAccount } - } else { - // eslint-disable-next-line no-param-reassign - storedPreferences.selectedAccount = - DEFAULT_PREFERENCES.selectedAccount - } - }), + }, + ), ) // Add the new default token list @@ -424,7 +429,10 @@ export class PreferenceDatabase extends Dexie { this.on("populate", (tx: Transaction) => { // This could be tx.preferences but the typing for populate // is not generic so it does not know about the preferences table - tx.table("preferences").add(DEFAULT_PREFERENCES) + tx.table("preferences").add({ + savedAt: Date.now(), + ...DEFAULT_PREFERENCES, + }) }) } diff --git a/background/services/preferences/defaults.ts b/background/services/preferences/defaults.ts index 0a5681ad1..69e32d342 100644 --- a/background/services/preferences/defaults.ts +++ b/background/services/preferences/defaults.ts @@ -1,10 +1,9 @@ import { ETHEREUM, MINUTE, USD } from "../../constants" import { storageGatewayURL } from "../../lib/storage-gateway" -import { Preferences } from "./types" export const DEFAULT_AUTOLOCK_INTERVAL = 60 * MINUTE -const defaultPreferences: Preferences = { +const defaultPreferences = { tokenLists: { autoUpdate: false, urls: [ diff --git a/background/services/preferences/index.ts b/background/services/preferences/index.ts index 438802f47..774af1102 100644 --- a/background/services/preferences/index.ts +++ b/background/services/preferences/index.ts @@ -4,10 +4,12 @@ import { ServiceLifecycleEvents, ServiceCreatorFunction } from "../types" import { AnalyticsPreferences, - Preferences, + Preferences as DbPreferences, TokenListPreferences, -} from "./types" -import { DismissableItem, getOrCreateDB, PreferenceDatabase } from "./db" + DismissableItem, + getOrCreateDB, + PreferenceDatabase, +} from "./db" import BaseService from "../base" import { normalizeEVMAddress } from "../../lib/utils" import { ETHEREUM, OPTIMISM, ARBITRUM_ONE } from "../../constants" @@ -16,7 +18,14 @@ import { HexString, UNIXTime } from "../../types" import { AccountSignerSettings } from "../../ui" import { AccountSignerWithId } from "../../signing" -export { ManuallyDismissableItem, SingleShotItem, DismissableItem } from "./db" +export { + AnalyticsPreferences, + ManuallyDismissableItem, + SingleShotItem, + DismissableItem, +} from "./db" + +export type Preferences = Omit type AddressBookEntry = { network: EVMNetwork diff --git a/background/services/preferences/types.ts b/background/services/preferences/types.ts deleted file mode 100644 index b188be4a6..000000000 --- a/background/services/preferences/types.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { FiatCurrency } from "../../assets" -import { AddressOnNetwork } from "../../accounts" -import { AccountSignerSettings } from "../../ui" -import { UNIXTime } from "../../types" - -export interface TokenListPreferences { - autoUpdate: boolean - urls: string[] -} - -export interface Preferences { - tokenLists: TokenListPreferences - currency: FiatCurrency - defaultWallet: boolean - selectedAccount: AddressOnNetwork - accountSignersSettings: AccountSignerSettings[] - analytics: { - isEnabled: boolean - hasDefaultOnBeenTurnedOn: boolean - } - autoLockInterval: UNIXTime -} - -export type AnalyticsPreferences = Preferences["analytics"]