diff --git a/electron/main.js b/electron/main.js index 03852b3c..c5bdd373 100644 --- a/electron/main.js +++ b/electron/main.js @@ -344,7 +344,6 @@ const createMainWindow = async () => { } catch (e) { logger.electron('Store IPC failed:', JSON.stringify(e)); } - if (isDev) { mainWindow.webContents.openDevTools(); } diff --git a/electron/store.js b/electron/store.js index 215b6c09..77305815 100644 --- a/electron/store.js +++ b/electron/store.js @@ -1,43 +1,70 @@ const Store = require('electron-store'); -// set schema to validate store data +const defaultInitialAgentSettings = { + isInitialFunded: false, +}; + +// Schema for validating store data const schema = { + // Global settings + environmentName: { type: 'string', default: '' }, + lastSelectedAgentType: { type: 'string', default: 'trader' }, + + // First time user settings firstStakingRewardAchieved: { type: 'boolean', default: false }, firstRewardNotificationShown: { type: 'boolean', default: false }, agentEvictionAlertShown: { type: 'boolean', default: false }, - environmentName: { type: 'string', default: '' }, - currentStakingProgram: { type: 'string', default: '' }, - - // agent settings - lastSelectedAgentType: { type: 'string', default: 'trader' }, - isInitialFunded_trader: { type: 'boolean', default: false }, - isInitialFunded_memeooorr: { type: 'boolean', default: false }, + // Each agent has its own settings + trader: { type: 'object', default: defaultInitialAgentSettings }, + memeooorr: { type: 'object', default: defaultInitialAgentSettings }, }; /** * Sets up the IPC communication and initializes the Electron store with default values and schema. * @param {Electron.IpcMain} ipcMain - The IPC channel for communication. * @param {Electron.BrowserWindow} mainWindow - The main Electron browser window. - * @returns {Promise} - A promise that resolves once the store is set up. */ const setupStoreIpc = (ipcMain, mainWindow) => { const store = new Store({ schema }); /** - * isInitialFunded Migration + * agent: trader Migration * - * Writes the old isInitialFunded value to the new isInitialFunded_trader - * And removes it from the store afterward + * Initially the store was setup with only trader agent settings. + * The following code migrates the old store to the new store schema. + */ + const traderAgent = { + ...(store.get('trader') || {}), + isInitialFunded: + store.get('isInitialFunded_trader') || + store.get('isInitialFunded') || + false, + }; + + // Set the trader agent and delete old keys + store.set('trader', traderAgent); + ['isInitialFunded', 'isInitialFunded_trader'].forEach((key) => + store.delete(key), + ); + + /** + * agent: memeooorr Migration */ - if (store.has('isInitialFunded')) { - store.set('isInitialFunded_trader', store.get('isInitialFunded')); - store.delete('isInitialFunded'); + if (store.has('isInitialFunded_memeooorr')) { + const memeooorrAgent = store.get('memeooorr') || {}; + store.set('memeooorr', { + ...memeooorrAgent, + isInitialFunded: store.get('isInitialFunded_memeooorr') || false, + }); + store.delete('isInitialFunded_memeooorr'); } + // Notify renderer process when store changes store.onDidAnyChange((data) => { - if (mainWindow?.webContents) + if (mainWindow?.webContents) { mainWindow.webContents.send('store-changed', data); + } }); // exposed to electron browser window diff --git a/frontend/components/MainPage/header/AgentButton/AgentNotRunningButton.tsx b/frontend/components/MainPage/header/AgentButton/AgentNotRunningButton.tsx index 86c2d4c9..7ee20b02 100644 --- a/frontend/components/MainPage/header/AgentButton/AgentNotRunningButton.tsx +++ b/frontend/components/MainPage/header/AgentButton/AgentNotRunningButton.tsx @@ -127,11 +127,8 @@ export const AgentNotRunningButton = () => { walletBalanceResult.evmChainId === selectedAgentConfig.evmHomeChainId, )?.balance; - if ( - service && - storeState?.[`isInitialFunded_${selectedAgentType}`] && - isServiceStaked - ) { + const isInitialFunded = storeState?.[selectedAgentType]?.isInitialFunded; + if (service && isInitialFunded && isServiceStaked) { return (serviceTotalStakedOlas ?? 0) >= requiredStakedOlas; } diff --git a/frontend/components/MainPage/sections/AlertSections/AvoidSuspensionAlert.tsx b/frontend/components/MainPage/sections/AlertSections/AvoidSuspensionAlert.tsx index c15aa546..5ebc3073 100644 --- a/frontend/components/MainPage/sections/AlertSections/AvoidSuspensionAlert.tsx +++ b/frontend/components/MainPage/sections/AlertSections/AvoidSuspensionAlert.tsx @@ -17,8 +17,8 @@ export const AvoidSuspensionAlert = () => { if (!storeState) return false; return ( - storeState.firstRewardNotificationShown && - !storeState.agentEvictionAlertShown + storeState?.firstRewardNotificationShown && + !storeState?.agentEvictionAlertShown ); }, [storeState]); diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx index ed8f350e..57546fc9 100644 --- a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx +++ b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx @@ -36,7 +36,7 @@ export const LowOperatingBalanceAlert = () => { if (!isBalanceLoaded) return null; if (!masterThresholds) return null; - if (!storeState?.[`isInitialFunded_${selectedAgentType}`]) return; + if (!storeState?.[selectedAgentType]?.isInitialFunded) return; if (!isLowBalance) return null; return ( diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/MainNeedsFunds.tsx b/frontend/components/MainPage/sections/AlertSections/LowFunds/MainNeedsFunds.tsx index 60a7169e..b7179c7e 100644 --- a/frontend/components/MainPage/sections/AlertSections/LowFunds/MainNeedsFunds.tsx +++ b/frontend/components/MainPage/sections/AlertSections/LowFunds/MainNeedsFunds.tsx @@ -30,7 +30,7 @@ export const MainNeedsFunds = () => { hasEnoughOlasForInitialFunding && !isInitialFunded ) { - electronApi.store?.set?.(`isInitialFunded_${selectedAgentType}`, true); + electronApi.store?.set?.(`${selectedAgentType}.isInitialFunded`, true); } }, [ electronApi.store, diff --git a/frontend/components/MainPage/sections/GasBalanceSection.tsx b/frontend/components/MainPage/sections/GasBalanceSection.tsx index d739d71a..c3270571 100644 --- a/frontend/components/MainPage/sections/GasBalanceSection.tsx +++ b/frontend/components/MainPage/sections/GasBalanceSection.tsx @@ -55,7 +55,7 @@ const BalanceStatus = () => { useEffect(() => { if (!isBalanceLoaded || !isServicesLoaded) return; if (!showNotification) return; - if (!storeState?.[`isInitialFunded_${selectedAgentType}`]) return; + if (!storeState?.[selectedAgentType]?.isInitialFunded) return; if (isMasterSafeLowOnNativeGas && !isLowBalanceNotificationShown) { showNotification('Operating balance is too low.'); diff --git a/frontend/hooks/useNeedsFunds.ts b/frontend/hooks/useNeedsFunds.ts index 52e3deae..3fd7787b 100644 --- a/frontend/hooks/useNeedsFunds.ts +++ b/frontend/hooks/useNeedsFunds.ts @@ -27,8 +27,7 @@ export const useNeedsFunds = (stakingProgramId: Maybe) => { const { isLoaded: isBalanceLoaded } = useBalanceContext(); const { masterSafeBalances } = useMasterBalances(); - const isInitialFunded = storeState?.[`isInitialFunded_${selectedAgentType}`]; - + const isInitialFunded = storeState?.[selectedAgentType]?.isInitialFunded; const serviceFundRequirements = useMemo<{ [chainId: number]: { [tokenSymbol: string]: number; diff --git a/frontend/types/ElectronApi.ts b/frontend/types/ElectronApi.ts index a15c18c3..dcb6b01f 100644 --- a/frontend/types/ElectronApi.ts +++ b/frontend/types/ElectronApi.ts @@ -1,13 +1,22 @@ import { AgentType } from '@/enums/Agent'; +type AgentSettings = { + isInitialFunded: boolean; +}; + export type ElectronStore = { + // Global settings environmentName?: string; - isInitialFunded_trader?: boolean; - isInitialFunded_memeooorr?: boolean; + lastSelectedAgentType?: AgentType; + + // First time user settings firstStakingRewardAchieved?: boolean; firstRewardNotificationShown?: boolean; agentEvictionAlertShown?: boolean; - lastSelectedAgentType?: AgentType; + + // Each agent has its own settings + trader: AgentSettings; + memeooorr: AgentSettings; }; export type ElectronTrayIconStatus =