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: move store variables to agent specific #603

Open
wants to merge 12 commits into
base: fix/meme-staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ const createMainWindow = async () => {
} catch (e) {
logger.electron('Store IPC failed:', JSON.stringify(e));
}

if (isDev) {
mainWindow.webContents.openDevTools();
}
Expand Down
59 changes: 43 additions & 16 deletions electron/store.js
Original file line number Diff line number Diff line change
@@ -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<void>} - 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const AvoidSuspensionAlert = () => {
if (!storeState) return false;

return (
storeState.firstRewardNotificationShown &&
!storeState.agentEvictionAlertShown
storeState?.firstRewardNotificationShown &&
!storeState?.agentEvictionAlertShown
);
}, [storeState]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const MainNeedsFunds = () => {
hasEnoughOlasForInitialFunding &&
!isInitialFunded
) {
electronApi.store?.set?.(`isInitialFunded_${selectedAgentType}`, true);
electronApi.store?.set?.(`${selectedAgentType}.isInitialFunded`, true);
}
}, [
electronApi.store,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
3 changes: 1 addition & 2 deletions frontend/hooks/useNeedsFunds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export const useNeedsFunds = (stakingProgramId: Maybe<StakingProgramId>) => {
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;
Expand Down
15 changes: 12 additions & 3 deletions frontend/types/ElectronApi.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
Loading